runner.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { describe, expect, it } from 'vitest'
  2. import type { BashExecRequest, BashExecSpec, BashExecutor, BashRunResult } from '@deepseek-ai/dsh-bash'
  3. import { DEFAULT_HOOK_TIMEOUT_MS, runHook } from '@deepseek-ai/dsh-hook-protocol'
  4. /**
  5. * A minimal stand-in for the bits of {@link BashExecutor} that {@link runHook}
  6. * actually calls (`resolve` then `run`). `runHook` is pure plumbing over those
  7. * two methods, so a duck-typed recorder is the right test seam — the REAL
  8. * executor (dsh-bash-local) is exercised end-to-end by the hook-bridge plugins
  9. * that consume this library, not here.
  10. */
  11. function recordingBash(run: (spec: BashExecSpec) => Promise<BashRunResult>): {
  12. bash: BashExecutor
  13. specs: BashExecSpec[]
  14. } {
  15. const specs: BashExecSpec[] = []
  16. const bash = {
  17. resolve(request: BashExecRequest): BashExecSpec {
  18. // Carry the request through verbatim, defaulting the required spec fields —
  19. // exactly what dsh-bash-local's resolve does for the fields runHook sets.
  20. return {
  21. command: request.command,
  22. workdir: request.workdir ?? '/stub',
  23. timeoutMs: request.timeoutMs ?? 0,
  24. ...request.signal ? { signal: request.signal } : {},
  25. ...request.stdin !== undefined ? { stdin: request.stdin } : {},
  26. ...request.env !== undefined ? { env: request.env } : {},
  27. sandboxMode: request.sandboxMode,
  28. }
  29. },
  30. async run(spec: BashExecSpec): Promise<BashRunResult> {
  31. specs.push(spec)
  32. return run(spec)
  33. },
  34. } as unknown as BashExecutor
  35. return { bash, specs }
  36. }
  37. function result(over: Partial<BashRunResult> = {}): BashRunResult {
  38. return {
  39. exitCode: 0,
  40. signal: null,
  41. timedOut: false,
  42. aborted: false,
  43. timeoutMs: 1000,
  44. stdout: { text: '', truncated: false },
  45. stderr: { text: '', truncated: false },
  46. ...over,
  47. }
  48. }
  49. const clock = () => { let t = 0; return () => (t += 5) } // +5ms per call → duration 5
  50. describe('runHook — payload + env + stdin plumbing', () => {
  51. it('serializes the payload to stdin (with trailing newline when requested)', async () => {
  52. const { bash, specs } = recordingBash(async () => result({ stdout: { text: '', truncated: false } }))
  53. await runHook(bash, { command: 'my-hook.sh' }, {
  54. payload: { hook_event_name: 'PreToolUse', tool_name: 'Bash' },
  55. defaultTimeoutMs: 60000,
  56. trailingNewline: true,
  57. }, clock())
  58. expect(specs[0]!.stdin).toBe(JSON.stringify({ hook_event_name: 'PreToolUse', tool_name: 'Bash' }) + '\n')
  59. expect(specs[0]!.command).toBe('my-hook.sh')
  60. })
  61. it('omits the trailing newline when trailingNewline is false (Codex)', async () => {
  62. const { bash, specs } = recordingBash(async () => result())
  63. await runHook(bash, { command: 'h' }, { payload: { a: 1 }, defaultTimeoutMs: 1000, trailingNewline: false }, clock())
  64. expect(specs[0]!.stdin).toBe('{"a":1}')
  65. })
  66. it('threads env and cwd into the request', async () => {
  67. const { bash, specs } = recordingBash(async () => result())
  68. await runHook(bash, { command: 'h' }, {
  69. payload: {}, env: { CLAUDE_PROJECT_DIR: '/proj' }, cwd: '/work',
  70. defaultTimeoutMs: 1000, trailingNewline: true,
  71. }, clock())
  72. expect(specs[0]!.env).toEqual({ CLAUDE_PROJECT_DIR: '/proj' })
  73. expect(specs[0]!.workdir).toBe('/work')
  74. })
  75. it('a per-hook timeoutSec (seconds) overrides the default (ms)', async () => {
  76. const { bash, specs } = recordingBash(async () => result())
  77. await runHook(bash, { command: 'h', timeoutSec: 3 }, { payload: {}, defaultTimeoutMs: 60000, trailingNewline: true }, clock())
  78. expect(specs[0]!.timeoutMs).toBe(3000)
  79. })
  80. it('falls back to the default timeout when the hook sets none', async () => {
  81. const { bash, specs } = recordingBash(async () => result())
  82. await runHook(bash, { command: 'h' }, { payload: {}, defaultTimeoutMs: 60000, trailingNewline: true }, clock())
  83. expect(specs[0]!.timeoutMs).toBe(60000)
  84. expect(DEFAULT_HOOK_TIMEOUT_MS).toBe(600_000) // the CC/Codex reference default (10 minutes)
  85. })
  86. it('passes the abort signal through', async () => {
  87. const controller = new AbortController()
  88. const { bash, specs } = recordingBash(async () => result())
  89. await runHook(bash, { command: 'h' }, { payload: {}, signal: controller.signal, defaultTimeoutMs: 1000, trailingNewline: true }, clock())
  90. expect(specs[0]!.signal).toBe(controller.signal)
  91. })
  92. })
  93. describe('runHook — outcome decoding + duration', () => {
  94. it('decodes a clean exit with structured stdout and reports a duration', async () => {
  95. const { bash } = recordingBash(async () => result({
  96. exitCode: 0, stdout: { text: JSON.stringify({ decision: 'block', reason: 'no' }), truncated: false },
  97. }))
  98. const { output, durationMs } = await runHook(bash, { command: 'h' }, { payload: {}, defaultTimeoutMs: 1000, trailingNewline: true }, clock())
  99. expect(output.decision).toBe('block')
  100. expect(output.reason).toBe('no')
  101. expect(durationMs).toBe(5)
  102. })
  103. it('a signal death (exitCode null) decodes as undefined exit (non-blocking error)', async () => {
  104. const { bash } = recordingBash(async () => result({ exitCode: null, signal: 'SIGKILL', stderr: { text: 'killed', truncated: false } }))
  105. const { output } = await runHook(bash, { command: 'h' }, { payload: {}, defaultTimeoutMs: 1000, trailingNewline: true }, clock())
  106. expect(output.exitCode).toBeUndefined()
  107. expect(output.decision).toBeUndefined()
  108. expect(output.stderr).toBe('killed')
  109. })
  110. it('an executor rejection (infra fault) becomes a non-blocking error, never throws', async () => {
  111. const { bash } = recordingBash(async () => { throw new Error('bad workdir: ENOENT') })
  112. const { output } = await runHook(bash, { command: 'h' }, { payload: {}, defaultTimeoutMs: 1000, trailingNewline: true }, clock())
  113. expect(output.exitCode).toBeUndefined()
  114. expect(output.stderr).toBe('bad workdir: ENOENT')
  115. expect(output.decision).toBeUndefined()
  116. })
  117. it('a non-Error rejection is stringified onto stderr', async () => {
  118. const { bash } = recordingBash(async () => { throw 'plain string fault' })
  119. const { output } = await runHook(bash, { command: 'h' }, { payload: {}, defaultTimeoutMs: 1000, trailingNewline: true }, clock())
  120. expect(output.stderr).toBe('plain string fault')
  121. })
  122. it('threads expectedEventName so a mismatched hookSpecificOutput block is discarded', async () => {
  123. const { bash } = recordingBash(async () => result({
  124. exitCode: 0,
  125. stdout: { text: JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny' } }), truncated: false },
  126. }))
  127. const { output } = await runHook(bash, { command: 'h' }, {
  128. payload: {}, defaultTimeoutMs: 1000, trailingNewline: true, expectedEventName: 'Stop',
  129. }, clock())
  130. // A PreToolUse block on a Stop hook is malformed → its decision is discarded.
  131. expect(output.hookEventName).toBe('PreToolUse')
  132. expect(output.decision).toBeUndefined()
  133. })
  134. })