codec.spec.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { describe, expect, it } from 'vitest'
  2. import { parseHookOutput } from '@deepseek-ai/dsh-hook-protocol'
  3. describe('parseHookOutput — exit code semantics', () => {
  4. it('exit 0 with no stdout is a neutral success', () => {
  5. const out = parseHookOutput(0, '', '')
  6. expect(out.exitCode).toBe(0)
  7. expect(out.decision).toBeUndefined()
  8. expect(out.continue).toBeUndefined()
  9. })
  10. it('exit 2 is a blocking error: stderr becomes the block decision + reason', () => {
  11. const out = parseHookOutput(2, '', 'this command is not allowed')
  12. expect(out.decision).toBe('block')
  13. expect(out.reason).toBe('this command is not allowed')
  14. expect(out.stderr).toBe('this command is not allowed')
  15. })
  16. it('exit 2 with empty stderr still blocks, with no reason', () => {
  17. const out = parseHookOutput(2, '', ' ')
  18. expect(out.decision).toBe('block')
  19. expect(out.reason).toBeUndefined()
  20. })
  21. it('other non-zero exit is a non-blocking error (no decision, stderr recorded)', () => {
  22. const out = parseHookOutput(1, '', 'some warning')
  23. expect(out.decision).toBeUndefined()
  24. expect(out.exitCode).toBe(1)
  25. expect(out.stderr).toBe('some warning')
  26. })
  27. it('undefined exit (could not run) carries no decision', () => {
  28. const out = parseHookOutput(undefined, '', 'spawn failed: ENOENT')
  29. expect(out.exitCode).toBeUndefined()
  30. expect(out.decision).toBeUndefined()
  31. expect(out.stderr).toBe('spawn failed: ENOENT')
  32. })
  33. })
  34. describe('parseHookOutput — structured stdout (exit 0 only)', () => {
  35. it('parses top-level continue/stopReason/suppressOutput/systemMessage', () => {
  36. const out = parseHookOutput(0, JSON.stringify({
  37. continue: false, stopReason: 'budget exceeded', suppressOutput: true, systemMessage: 'heads up',
  38. }), '')
  39. expect(out.continue).toBe(false)
  40. expect(out.stopReason).toBe('budget exceeded')
  41. expect(out.suppressOutput).toBe(true)
  42. expect(out.systemMessage).toBe('heads up')
  43. })
  44. it('parses legacy top-level decision + reason (approve/block ONLY)', () => {
  45. expect(parseHookOutput(0, JSON.stringify({ decision: 'block', reason: 'nope' }), '').decision).toBe('block')
  46. expect(parseHookOutput(0, JSON.stringify({ decision: 'approve' }), '').decision).toBe('approve')
  47. })
  48. it('a top-level decision of allow/deny/ask is INVALID and ignored (reserved for permissionDecision)', () => {
  49. // Both reference schemas restrict the legacy top-level `decision` to
  50. // approve/block; allow/deny/ask must come from hookSpecificOutput.permissionDecision.
  51. expect(parseHookOutput(0, JSON.stringify({ decision: 'deny' }), '').decision).toBeUndefined()
  52. expect(parseHookOutput(0, JSON.stringify({ decision: 'allow' }), '').decision).toBeUndefined()
  53. expect(parseHookOutput(0, JSON.stringify({ decision: 'ask' }), '').decision).toBeUndefined()
  54. })
  55. it('captures hookEventName from hookSpecificOutput (the discriminator a bridge validates)', () => {
  56. const out = parseHookOutput(0, JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny' } }), '')
  57. expect(out.hookEventName).toBe('PreToolUse')
  58. expect(out.decision).toBe('deny')
  59. })
  60. it('hookSpecificOutput.permissionDecision OVERRIDES the legacy top-level decision', () => {
  61. const out = parseHookOutput(0, JSON.stringify({
  62. decision: 'approve',
  63. hookSpecificOutput: { permissionDecision: 'deny', permissionDecisionReason: 'denied by policy' },
  64. }), '')
  65. expect(out.decision).toBe('deny')
  66. expect(out.reason).toBe('denied by policy')
  67. })
  68. it('parses allow/ask permissionDecision (the bridge decides whether to honor)', () => {
  69. expect(parseHookOutput(0, JSON.stringify({ hookSpecificOutput: { permissionDecision: 'allow' } }), '').decision).toBe('allow')
  70. expect(parseHookOutput(0, JSON.stringify({ hookSpecificOutput: { permissionDecision: 'ask' } }), '').decision).toBe('ask')
  71. })
  72. it('parses additionalContext and updatedInput from hookSpecificOutput', () => {
  73. const out = parseHookOutput(0, JSON.stringify({
  74. hookSpecificOutput: { additionalContext: 'remember X', updatedInput: { command: 'safe' } },
  75. }), '')
  76. expect(out.additionalContext).toBe('remember X')
  77. expect(out.updatedInput).toEqual({ command: 'safe' })
  78. })
  79. it('an unknown decision string is ignored (not coerced)', () => {
  80. expect(parseHookOutput(0, JSON.stringify({ decision: 'maybe' }), '').decision).toBeUndefined()
  81. })
  82. it('DISCARDS a hookSpecificOutput block whose hookEventName mismatches the firing event', () => {
  83. // A PreToolUse block emitted on a Stop hook is malformed — its event-scoped
  84. // fields must not take effect (a stray PreToolUse deny must not deny the Stop).
  85. const out = parseHookOutput(0, JSON.stringify({
  86. hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: 'no', additionalContext: 'x', updatedInput: { command: 'y' } },
  87. }), '', 'Stop')
  88. expect(out.hookEventName).toBe('PreToolUse') // still recorded for the log
  89. expect(out.decision).toBeUndefined() // event-scoped fields discarded
  90. expect(out.reason).toBeUndefined()
  91. expect(out.additionalContext).toBeUndefined()
  92. expect(out.updatedInput).toBeUndefined()
  93. })
  94. it('APPLIES a hookSpecificOutput block whose hookEventName matches the firing event', () => {
  95. const out = parseHookOutput(0, JSON.stringify({
  96. hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', additionalContext: 'x' },
  97. }), '', 'PreToolUse')
  98. expect(out.decision).toBe('deny')
  99. expect(out.additionalContext).toBe('x')
  100. })
  101. it('applies the block when expectedEventName is omitted (opt-out) even if it names an event', () => {
  102. const out = parseHookOutput(0, JSON.stringify({
  103. hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny' },
  104. }), '')
  105. expect(out.decision).toBe('deny')
  106. })
  107. it('DISCARDS a block with NO hookEventName when a firing event is expected', () => {
  108. // Under the keyed schema a missing discriminator is as malformed as a
  109. // mismatched one: a discriminator-less block must not apply its event-scoped
  110. // permission fields to whatever event happens to be firing.
  111. const out = parseHookOutput(0, JSON.stringify({
  112. hookSpecificOutput: { permissionDecision: 'deny', additionalContext: 'x' },
  113. }), '', 'Stop')
  114. expect(out.hookEventName).toBeUndefined() // none to record
  115. expect(out.decision).toBeUndefined() // event-scoped fields discarded
  116. expect(out.additionalContext).toBeUndefined()
  117. })
  118. it('applies a discriminator-less block when expectedEventName is omitted (opt-out)', () => {
  119. // With no firing event to validate against, the block applies as-is.
  120. const out = parseHookOutput(0, JSON.stringify({
  121. hookSpecificOutput: { permissionDecision: 'deny' },
  122. }), '')
  123. expect(out.decision).toBe('deny')
  124. })
  125. it('a mismatched block does NOT discard the event-agnostic top-level decision/continue', () => {
  126. // Only the per-event block is scoped; top-level fields are event-agnostic.
  127. const out = parseHookOutput(0, JSON.stringify({
  128. decision: 'block', reason: 'top', continue: false, stopReason: 'halt',
  129. hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'allow' },
  130. }), '', 'Stop')
  131. expect(out.decision).toBe('block') // top-level survives; the allow block was discarded
  132. expect(out.reason).toBe('top')
  133. expect(out.continue).toBe(false)
  134. expect(out.stopReason).toBe('halt')
  135. })
  136. it('malformed JSON on a clean exit is lenient (no structured output, no throw)', () => {
  137. const out = parseHookOutput(0, '{ not valid json', '')
  138. expect(out.decision).toBeUndefined()
  139. expect(out.continue).toBeUndefined()
  140. })
  141. it('non-object stdout (plain text) on exit 0 is left for the bridge (no JSON attempt)', () => {
  142. const out = parseHookOutput(0, 'just some text output', '')
  143. expect(out.decision).toBeUndefined()
  144. expect(out.continue).toBeUndefined()
  145. // The raw stdout is preserved verbatim so the bridge can render/use it
  146. // (CC output; Codex additionalContext) — trimmed.
  147. expect(out.stdout).toBe('just some text output')
  148. })
  149. it('preserves raw stdout (trimmed) alongside parsed structured fields', () => {
  150. const json = JSON.stringify({ decision: 'block' })
  151. const out = parseHookOutput(0, ` ${json} \n`, '')
  152. expect(out.stdout).toBe(json)
  153. expect(out.decision).toBe('block')
  154. })
  155. it('stdout is empty string when the hook emits none', () => {
  156. expect(parseHookOutput(0, '', '').stdout).toBe('')
  157. })
  158. it('a JSON array stdout parses but yields no fields (not an object)', () => {
  159. // Starts with '{'? No — '[' — so it is not even attempted. Neutral.
  160. const out = parseHookOutput(0, '[1,2,3]', '')
  161. expect(out.decision).toBeUndefined()
  162. })
  163. it('structured stdout is IGNORED on a blocking (exit 2) run — stderr is authoritative', () => {
  164. const out = parseHookOutput(2, JSON.stringify({ decision: 'approve' }), 'blocked')
  165. // exit 2 forces block regardless of what stdout claims
  166. expect(out.decision).toBe('block')
  167. expect(out.reason).toBe('blocked')
  168. })
  169. })