invariant.spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import SessionStore, { type Session, type SessionEvent } from '@deepseek-ai/dsh-session'
  4. import * as PlanModeInvariant from '@deepseek-ai/dsh-plan-mode/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, { enabled: true })
  10. await ctx.plugin(PlanModeInvariant)
  11. return ctx
  12. }
  13. function event(active: unknown): SessionEvent {
  14. return { type: 'plan/mode', seq: 0, time: 0, data: { active } } as SessionEvent
  15. }
  16. describe('plan-mode stream invariants', () => {
  17. it('accepts either boolean state', async () => {
  18. const ctx = await setup()
  19. expect(() => { ctx.emit('session/event', {} as Session, event(true)) }).not.toThrow()
  20. expect(() => { ctx.emit('session/event', {} as Session, event(false)) }).not.toThrow()
  21. })
  22. it.each([42, 'plan', undefined])('rejects invalid durable plan state %j', async (active) => {
  23. const ctx = await setup()
  24. expect(() => { ctx.emit('session/event', {} as Session, event(active)) })
  25. .toThrow(/expected a boolean/)
  26. })
  27. it('ignores unrelated dispatches and session events', async () => {
  28. const ctx = await setup()
  29. expect(() => {
  30. ctx.emit('tools/change')
  31. ctx.emit('session/event', {} as Session, {
  32. type: 'turn/start', seq: 0, time: 0, data: { turn: 1, trigger: { kind: 'message', source: { kind: 'user' } } },
  33. })
  34. }).not.toThrow()
  35. })
  36. it('rejects invalid existing state on late registration', async () => {
  37. const ctx = new Context()
  38. await ctx.plugin(SessionStore)
  39. ctx.sessions.create().append('plan/mode', { active: 'plan' as unknown as boolean })
  40. await ctx.plugin(InvariantService, { enabled: true })
  41. await expect(ctx.plugin(PlanModeInvariant).then(() => undefined)).rejects.toThrow(/expected a boolean/)
  42. })
  43. })