events.spec.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, it } from 'vitest'
  2. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  3. import { appendHookInvoked, appendHookResult } from '@deepseek-ai/dsh-hook-protocol'
  4. describe('hook/* session events', () => {
  5. it('appendHookInvoked records a log-only hook/invoked (with matcher when present)', () => {
  6. const session = new Session(SessionId('s'))
  7. appendHookInvoked(session, { turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'h1', matcher: 'Bash' })
  8. const ev = [...session.events].find(e => e.type === 'hook/invoked')
  9. expect(ev?.type).toBe('hook/invoked')
  10. if (ev?.type === 'hook/invoked') {
  11. expect(ev.data).toMatchObject({ turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'h1', matcher: 'Bash' })
  12. }
  13. // Log-only: no surfaceOp on the event.
  14. expect((ev as unknown as { surfaceOp?: unknown }).surfaceOp).toBeUndefined()
  15. })
  16. it('omits matcher when absent (match-all hook)', () => {
  17. const session = new Session(SessionId('s'))
  18. appendHookInvoked(session, { turn: 2, point: 'Stop', dialect: 'native', handlerId: 'h2' })
  19. const ev = [...session.events].find(e => e.type === 'hook/invoked')
  20. if (ev?.type === 'hook/invoked') {
  21. expect('matcher' in ev.data).toBe(false)
  22. }
  23. })
  24. it('appendHookResult records the decided outcome, omitting absent optionals', () => {
  25. const session = new Session(SessionId('s'))
  26. appendHookResult(session, {
  27. turn: 1, point: 'PreToolUse', handlerId: 'h1', decision: 'deny',
  28. exitCode: 2, stderrSummary: 'blocked', durationMs: 12,
  29. })
  30. const full = [...session.events].find(e => e.type === 'hook/result')
  31. if (full?.type === 'hook/result') {
  32. expect(full.data).toMatchObject({ turn: 1, point: 'PreToolUse', handlerId: 'h1', decision: 'deny', exitCode: 2, stderrSummary: 'blocked', durationMs: 12 })
  33. }
  34. // A result with no exit code / no stderr (e.g. a hook that could not run) omits both keys.
  35. const session2 = new Session(SessionId('s2'))
  36. appendHookResult(session2, { turn: 1, point: 'Stop', handlerId: 'h3', decision: 'allow', durationMs: 3 })
  37. const sparse = [...session2.events].find(e => e.type === 'hook/result')
  38. if (sparse?.type === 'hook/result') {
  39. expect('exitCode' in sparse.data).toBe(false)
  40. expect('stderrSummary' in sparse.data).toBe(false)
  41. expect(sparse.data.durationMs).toBe(3)
  42. }
  43. })
  44. it('an invoked/result pair correlates by handlerId', () => {
  45. const session = new Session(SessionId('s'))
  46. appendHookInvoked(session, { turn: 1, point: 'PreToolUse', dialect: 'claude', handlerId: 'pair-1' })
  47. appendHookResult(session, { turn: 1, point: 'PreToolUse', handlerId: 'pair-1', decision: 'allow', exitCode: 0, durationMs: 7 })
  48. const invoked = [...session.events].find(e => e.type === 'hook/invoked')
  49. const result = [...session.events].find(e => e.type === 'hook/result')
  50. expect(invoked?.type === 'hook/invoked' && invoked.data.handlerId).toBe('pair-1')
  51. expect(result?.type === 'hook/result' && result.data.handlerId).toBe('pair-1')
  52. })
  53. })