invariant.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import 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. })