invariant.spec.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { AgentMessageId, type Agent } from '@deepseek-ai/dsh-agent'
  4. import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant'
  5. import { scopeTarget } from '@deepseek-ai/dsh-scope'
  6. import InvariantService from '@deepseek-ai/dsh-invariants'
  7. async function setup(): Promise<Context> {
  8. const ctx = new Context()
  9. await ctx.plugin(InvariantService)
  10. await ctx.plugin(AgentInvariant)
  11. return ctx
  12. }
  13. function mockAgent(id: string): Agent {
  14. return { id } as unknown as Agent
  15. }
  16. describe('agent status invariants', () => {
  17. it('accepts lifecycle transitions between idle and running', async () => {
  18. const ctx = await setup()
  19. const agent = mockAgent('a1')
  20. expect(() => {
  21. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle')
  22. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running')
  23. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'idle')
  24. }).not.toThrow()
  25. })
  26. it('rejects a no-op transition', async () => {
  27. const ctx = await setup()
  28. const agent = mockAgent('a3')
  29. ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running')
  30. expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', agent, 'running') })
  31. .toThrow(/no-op transition/)
  32. })
  33. it('tracks agents independently', async () => {
  34. const ctx = await setup()
  35. const a = mockAgent('a5')
  36. const b = mockAgent('b5')
  37. ctx.emit(scopeTarget(a, a), 'agent/status', a, 'running')
  38. expect(() => { ctx.emit(scopeTarget(b, b), 'agent/status', b, 'running') }).not.toThrow()
  39. })
  40. })
  41. describe('agent inbox invariants', () => {
  42. const info = () => ({ id: AgentMessageId('m'), content: [], source: { kind: 'user' as const } })
  43. it('accepts a dequeue and a discard covered by prior enqueues', async () => {
  44. const ctx = await setup()
  45. const agent = mockAgent('i1')
  46. const at = scopeTarget(agent, agent)
  47. expect(() => {
  48. ctx.emit(at, 'agent/inbox/enqueue', agent, info())
  49. ctx.emit(at, 'agent/inbox/enqueue', agent, info())
  50. ctx.emit(at, 'agent/inbox/dequeue', agent, info())
  51. ctx.emit(at, 'agent/inbox/discard', agent, [info()])
  52. }).not.toThrow()
  53. })
  54. it('rejects a dequeue with no outstanding item', async () => {
  55. const ctx = await setup()
  56. const agent = mockAgent('i2')
  57. expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/inbox/dequeue', agent, info()) })
  58. .toThrow(/without a matching prior enqueue/)
  59. })
  60. it('rejects a discard larger than the outstanding count', async () => {
  61. const ctx = await setup()
  62. const agent = mockAgent('i3')
  63. const at = scopeTarget(agent, agent)
  64. ctx.emit(at, 'agent/inbox/enqueue', agent, info())
  65. expect(() => { ctx.emit(at, 'agent/inbox/discard', agent, [info(), info()]) })
  66. .toThrow(/dropped 2 items but only 1 were outstanding/)
  67. })
  68. it('accepts an empty discard against a fresh agent', async () => {
  69. const ctx = await setup()
  70. const agent = mockAgent('i4')
  71. expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/inbox/discard', agent, []) }).not.toThrow()
  72. })
  73. })