2026-07-19 19:19:57 +08:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2026-08-10 22:04:06 +08:00
|
|
|
import { Context } from '@deepseek-ai/cordis'
|
2026-07-30 15:35:18 +08:00
|
|
|
import type { Agent } from '@deepseek-ai/dsh-agent'
|
2026-07-19 19:19:57 +08:00
|
|
|
import * as AgentInvariant from '@deepseek-ai/dsh-agent/invariant'
|
|
|
|
|
import { scopeTarget } from '@deepseek-ai/dsh-scope'
|
2026-08-13 00:36:22 +08:00
|
|
|
import InvariantRegistry from '@deepseek-ai/dsh-invariants'
|
2026-07-19 19:19:57 +08:00
|
|
|
|
|
|
|
|
async function setup(): Promise<Context> {
|
|
|
|
|
const ctx = new Context()
|
2026-08-13 00:36:22 +08:00
|
|
|
await ctx.plugin(InvariantRegistry)
|
2026-07-19 19:19:57 +08:00
|
|
|
await ctx.plugin(AgentInvariant)
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mockAgent(id: string): Agent {
|
|
|
|
|
return { id } as unknown as Agent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('agent status invariants', () => {
|
2026-07-24 21:18:48 +08:00
|
|
|
it('accepts lifecycle transitions between idle and running', async () => {
|
2026-07-19 19:19:57 +08:00
|
|
|
const ctx = await setup()
|
|
|
|
|
const agent = mockAgent('a1')
|
|
|
|
|
expect(() => {
|
2026-08-06 12:13:14 +08:00
|
|
|
ctx.emit(scopeTarget(agent, agent), 'agent/status', { agent, status: 'idle' })
|
|
|
|
|
ctx.emit(scopeTarget(agent, agent), 'agent/status', { agent, status: 'running' })
|
|
|
|
|
ctx.emit(scopeTarget(agent, agent), 'agent/status', { agent, status: 'idle' })
|
2026-07-19 19:19:57 +08:00
|
|
|
}).not.toThrow()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('rejects a no-op transition', async () => {
|
|
|
|
|
const ctx = await setup()
|
|
|
|
|
const agent = mockAgent('a3')
|
2026-08-06 12:13:14 +08:00
|
|
|
ctx.emit(scopeTarget(agent, agent), 'agent/status', { agent, status: 'running' })
|
|
|
|
|
expect(() => { ctx.emit(scopeTarget(agent, agent), 'agent/status', { agent, status: 'running' }) })
|
2026-07-19 19:19:57 +08:00
|
|
|
.toThrow(/no-op transition/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('tracks agents independently', async () => {
|
|
|
|
|
const ctx = await setup()
|
|
|
|
|
const a = mockAgent('a5')
|
|
|
|
|
const b = mockAgent('b5')
|
2026-08-06 12:13:14 +08:00
|
|
|
ctx.emit(scopeTarget(a, a), 'agent/status', { agent: a, status: 'running' })
|
|
|
|
|
expect(() => { ctx.emit(scopeTarget(b, b), 'agent/status', { agent: b, status: 'running' }) }).not.toThrow()
|
2026-07-19 19:19:57 +08:00
|
|
|
})
|
|
|
|
|
})
|