invariant.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
  4. import * as HookInvariant from '@deepseek-ai/dsh-hook-protocol/invariant'
  5. import InvariantService from '@deepseek-ai/dsh-invariants'
  6. async function setup(): Promise<Context> {
  7. const ctx = new Context()
  8. await ctx.plugin(SessionStore)
  9. await ctx.plugin(InvariantService)
  10. await ctx.plugin(HookInvariant)
  11. return ctx
  12. }
  13. const invoked = (overrides: Record<string, unknown> = {}) => ({
  14. turn: 1,
  15. point: 'PreToolUse',
  16. dialect: 'claude' as const,
  17. handlerId: 'hook-1',
  18. ...overrides,
  19. })
  20. const result = (overrides: Record<string, unknown> = {}) => ({
  21. turn: 1,
  22. point: 'PreToolUse',
  23. handlerId: 'hook-1',
  24. decision: 'pass',
  25. durationMs: 3,
  26. ...overrides,
  27. })
  28. function startTurn(session: Session, turn = 1): void {
  29. session.append('turn/start', { turn })
  30. }
  31. describe('hook-protocol invariants', () => {
  32. it('pairs serial and repeated handler invocations', async () => {
  33. const ctx = await setup()
  34. const session = ctx.sessions.create()
  35. startTurn(session)
  36. session.append('hook/invoked', invoked())
  37. session.append('hook/invoked', invoked())
  38. session.append('step/start', { turn: 1, step: 1 })
  39. session.append('hook/result', result())
  40. session.append('hook/result', result())
  41. })
  42. it('rebuilds pending hook provenance from an existing session', async () => {
  43. const ctx = new Context()
  44. await ctx.plugin(SessionStore)
  45. const session = ctx.sessions.create()
  46. session.append('turn/start', { turn: 1 })
  47. session.append('hook/invoked', invoked())
  48. await ctx.plugin(InvariantService)
  49. await ctx.plugin(HookInvariant)
  50. expect(() => session.append('hook/result', result())).not.toThrow()
  51. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  52. })
  53. it('adopts a bare session first observed through publication', async () => {
  54. const ctx = await setup()
  55. const session = Session.create(SessionId('bare-hook-session'))
  56. expect(() => {
  57. ctx.emit('session/event', session, {
  58. type: 'turn/start', seq: 0, time: 0,
  59. data: { turn: 1 },
  60. })
  61. ctx.emit('session/event', session, {
  62. type: 'hook/invoked', seq: 1, time: 1, data: invoked(),
  63. })
  64. ctx.emit('session/event', session, {
  65. type: 'hook/result', seq: 2, time: 2, data: result(),
  66. })
  67. }).not.toThrow()
  68. })
  69. it('rejects hook events outside or for a different open turn', async () => {
  70. const ctx = await setup()
  71. const session = ctx.sessions.create()
  72. expect(() => session.append('hook/invoked', invoked())).toThrow(/outside any open turn/)
  73. startTurn(session)
  74. expect(() => session.append('hook/invoked', invoked({ turn: 2 }))).toThrow(/but open turn is 1/)
  75. })
  76. it('rejects an unenclosed hook event when replaying an existing session', async () => {
  77. const ctx = new Context()
  78. await ctx.plugin(SessionStore)
  79. const session = ctx.sessions.create()
  80. startTurn(session)
  81. session.append('turn/end', { turn: 1, reason: { kind: 'completed' } })
  82. session.append('hook/invoked', invoked())
  83. await ctx.plugin(InvariantService)
  84. await expect(ctx.plugin(HookInvariant).then(() => undefined)).rejects.toThrow(/outside any open turn/)
  85. })
  86. it.each([
  87. [invoked({ point: '' }), /point and handlerId must be non-empty/],
  88. [invoked({ handlerId: '' }), /point and handlerId must be non-empty/],
  89. [invoked({ dialect: 'other' }), /unknown dialect/],
  90. ])('rejects malformed hook invocation %#', async (data, message) => {
  91. const ctx = await setup()
  92. const session = ctx.sessions.create()
  93. startTurn(session)
  94. expect(() => session.append('hook/invoked', data as never)).toThrow(message)
  95. })
  96. it('rejects unmatched and malformed results', async () => {
  97. const ctx = await setup()
  98. const session = ctx.sessions.create()
  99. startTurn(session)
  100. expect(() => session.append('hook/result', result())).toThrow(/no matching hook\/invoked/)
  101. session.append('hook/invoked', invoked())
  102. expect(() => session.append('hook/result', result({ durationMs: -1 })))
  103. .toThrow(/durationMs must be a non-negative finite number/)
  104. expect(() => session.append('hook/result', result({ point: 'Stop' })))
  105. .toThrow(/no matching hook\/invoked/)
  106. })
  107. })