events.spec.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { describe, expect, it } from 'vitest'
  2. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  3. import { appendHookInvoked, appendHookResult, summarizeStderr, type HookOutput } from '@deepseek-ai/dsh-hook-protocol'
  4. /** A {@link HookOutput} with the required stream fields defaulted. */
  5. function output(over: Partial<HookOutput> = {}): HookOutput {
  6. return { exitCode: 0, stderr: '', stdout: '', ...over }
  7. }
  8. describe('hook/* session events', () => {
  9. it('appendHookInvoked records a log-only hook/invoked (with matcher when present)', () => {
  10. const session = Session.create(SessionId('s'))
  11. appendHookInvoked(session, { turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'h1', matcher: 'Bash' })
  12. const ev = [...session.events].find(e => e.type === 'hook/invoked')
  13. expect(ev?.type).toBe('hook/invoked')
  14. if (ev?.type === 'hook/invoked') {
  15. expect(ev.data).toMatchObject({ turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'h1', matcher: 'Bash' })
  16. }
  17. // Log-only: no surfaceOp on the event.
  18. expect((ev as unknown as { surfaceOp?: unknown }).surfaceOp).toBeUndefined()
  19. })
  20. it('omits matcher when absent (match-all hook)', () => {
  21. const session = Session.create(SessionId('s'))
  22. appendHookInvoked(session, { turn: 2, point: 'Stop', dialect: 'codex', handlerId: 'h2' })
  23. const ev = [...session.events].find(e => e.type === 'hook/invoked')
  24. if (ev?.type === 'hook/invoked') {
  25. expect('matcher' in ev.data).toBe(false)
  26. }
  27. })
  28. it('appendHookResult derives decision/exitCode/stderrSummary from the output', () => {
  29. const session = Session.create(SessionId('s'))
  30. appendHookResult(session, {
  31. turn: 1, point: 'PreToolUse', handlerId: 'h1',
  32. stderrSummaryMaxChars: 500, durationMs: 5, output: output({ exitCode: 2, stderr: 'blocked', decision: 'deny' }),
  33. })
  34. const full = [...session.events].find(e => e.type === 'hook/result')
  35. if (full?.type === 'hook/result') {
  36. expect(full.data).toEqual({ turn: 1, point: 'PreToolUse', handlerId: 'h1', decision: 'deny', exitCode: 2, stderrSummary: 'blocked', durationMs: 5 })
  37. }
  38. // A result with no exit code / no stderr (e.g. a hook that could not run) omits both keys.
  39. const session2 = Session.create(SessionId('s2'))
  40. appendHookResult(session2, {
  41. turn: 1, point: 'Stop', handlerId: 'h3',
  42. stderrSummaryMaxChars: 500, durationMs: 5, output: output({ exitCode: undefined, decision: 'allow' }),
  43. })
  44. const sparse = [...session2.events].find(e => e.type === 'hook/result')
  45. if (sparse?.type === 'hook/result') {
  46. expect('exitCode' in sparse.data).toBe(false)
  47. expect('stderrSummary' in sparse.data).toBe(false)
  48. expect(sparse.data.decision).toBe('allow')
  49. }
  50. })
  51. it('the decision falls back to stop on continue:false, else pass', () => {
  52. const session = Session.create(SessionId('s'))
  53. appendHookResult(session, { turn: 1, point: 'Stop', handlerId: 'halt', stderrSummaryMaxChars: 500, durationMs: 5, output: output({ continue: false }) })
  54. appendHookResult(session, { turn: 1, point: 'Stop', handlerId: 'noop', stderrSummaryMaxChars: 500, durationMs: 5, output: output() })
  55. // An explicit decision wins over the continue:false fallback.
  56. appendHookResult(session, { turn: 1, point: 'Stop', handlerId: 'both', stderrSummaryMaxChars: 500, durationMs: 5, output: output({ continue: false, decision: 'block' }) })
  57. const decisions = [...session.events]
  58. .filter(e => e.type === 'hook/result')
  59. .map(e => e.type === 'hook/result' ? [e.data.handlerId, e.data.decision] : [])
  60. expect(decisions).toEqual([['halt', 'stop'], ['noop', 'pass'], ['both', 'block']])
  61. })
  62. it('stderrSummary is trimmed and truncated to 500 characters with an ellipsis', () => {
  63. const session = Session.create(SessionId('s'))
  64. appendHookResult(session, {
  65. turn: 1, point: 'PreToolUse', handlerId: 'long',
  66. stderrSummaryMaxChars: 500, durationMs: 5, output: output({ exitCode: 2, stderr: ` ${'x'.repeat(600)} ` }),
  67. })
  68. const ev = [...session.events].find(e => e.type === 'hook/result')
  69. if (ev?.type === 'hook/result') {
  70. expect(ev.data.stderrSummary).toBe('x'.repeat(500) + '…')
  71. }
  72. })
  73. it('a 500-character stderr is kept verbatim (the cap is exclusive)', () => {
  74. const session = Session.create(SessionId('s'))
  75. appendHookResult(session, {
  76. turn: 1, point: 'PreToolUse', handlerId: 'edge',
  77. stderrSummaryMaxChars: 500, durationMs: 5, output: output({ exitCode: 2, stderr: 'y'.repeat(500) }),
  78. })
  79. const ev = [...session.events].find(e => e.type === 'hook/result')
  80. if (ev?.type === 'hook/result') {
  81. expect(ev.data.stderrSummary).toBe('y'.repeat(500))
  82. }
  83. })
  84. it('an invoked/result pair correlates by handlerId', () => {
  85. const session = Session.create(SessionId('s'))
  86. appendHookInvoked(session, { turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'pair-1' })
  87. appendHookResult(session, { turn: 1, point: 'PreToolUse', handlerId: 'pair-1', stderrSummaryMaxChars: 500, durationMs: 5, output: output({ decision: 'allow' }) })
  88. const invoked = [...session.events].find(e => e.type === 'hook/invoked')
  89. const result = [...session.events].find(e => e.type === 'hook/result')
  90. expect(invoked?.type === 'hook/invoked' && invoked.data.handlerId).toBe('pair-1')
  91. expect(result?.type === 'hook/result' && result.data.handlerId).toBe('pair-1')
  92. })
  93. })
  94. describe('summarizeStderr', () => {
  95. it('returns undefined for empty/whitespace stderr', () => {
  96. expect(summarizeStderr('', 500)).toBeUndefined()
  97. expect(summarizeStderr(' \n\t ', 500)).toBeUndefined()
  98. })
  99. it('passes through a summary at or under the cap, trimmed', () => {
  100. expect(summarizeStderr(' blocked: bad tool ', 500)).toBe('blocked: bad tool')
  101. expect(summarizeStderr('abc', 3)).toBe('abc')
  102. })
  103. it('truncates past the cap with an ellipsis', () => {
  104. expect(summarizeStderr('abcdef', 4)).toBe('abcd…')
  105. expect(summarizeStderr('x'.repeat(600), 500)).toBe('x'.repeat(500) + '…')
  106. })
  107. })