80 lines
3.7 KiB
JavaScript
80 lines
3.7 KiB
JavaScript
import test from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { createSeatActivity, RUNTIME_FAILURE_TTL_MS } from '../src/host/seat-activity.js'
|
||
import { createSeatDelivery } from '../src/host/seat-delivery.js'
|
||
import { createApprovalCenter } from '../src/host/approval-center.js'
|
||
|
||
function activityFixture() {
|
||
let now = 1000
|
||
const seat = { status: 'bound', sessionId: 's1' }
|
||
const store = {
|
||
async ensureLoaded() {},
|
||
get: id => (id === 'p1' ? { id: 'p1', seats: { gongbu: seat } } : null),
|
||
list: () => [{ id: 'p1', seats: { gongbu: seat } }],
|
||
}
|
||
const session = { id: 's1' }
|
||
const agent = { id: 's1', status: 'idle', session }
|
||
const agents = new Map([['s1', agent]])
|
||
const handlers = {}
|
||
const ctx = { on(name, fn) { handlers[name] = fn; return () => {} } }
|
||
const approvalCenter = createApprovalCenter({
|
||
ctx: { on() {} },
|
||
resolveSeatBySessionId: id => (id === 's1' ? { projectId: 'p1', seatId: 'gongbu' } : null),
|
||
})
|
||
const activity = createSeatActivity({ store, host: { agents: { get: id => agents.get(id) } }, approvalCenter, now: () => now })
|
||
activity.install(ctx)
|
||
return { activity, agent, session, agents, handlers, setNow: value => { now = value } }
|
||
}
|
||
|
||
test('seat-activity:对齐 DSH 0.2.2-rc.5 事件签名(turn/tool/failure TTL/Agent 替换)', () => {
|
||
const f = activityFixture()
|
||
|
||
f.agent.status = 'running'
|
||
f.handlers['session/event'](f.session, { type: 'turn/start', data: { turn: 1 } })
|
||
assert.equal(f.activity.stateOf('p1', 'gongbu').label, '运行中')
|
||
assert.equal(f.activity.stateOf('p1', 'gongbu').turn, 1)
|
||
|
||
f.handlers['session/event'](f.session, { type: 'tool/call', data: { turn: 1, step: 1, callId: 'c1', name: 'read', arguments: '{}' } })
|
||
assert.equal(f.activity.stateOf('p1', 'gongbu').tool, 'read')
|
||
|
||
f.handlers['session/event'](f.session, { type: 'tool/result', data: { turn: 1, step: 1, message: { role: 'tool', toolCallId: 'c1' } } })
|
||
assert.equal(f.activity.stateOf('p1', 'gongbu').tool, null)
|
||
|
||
f.agent.status = 'idle'
|
||
f.handlers['session/event'](f.session, { type: 'turn/end', data: { turn: 1, reason: { kind: 'error', error: { code: 'LLM_FAIL', message: 'boom' } } } })
|
||
const failed = f.activity.stateOf('p1', 'gongbu')
|
||
assert.equal(failed.label, '上次运行失败')
|
||
assert.equal(failed.error, 'LLM_FAIL')
|
||
|
||
f.setNow(1000 + RUNTIME_FAILURE_TTL_MS + 1)
|
||
assert.equal(f.activity.stateOf('p1', 'gongbu').label, '待命')
|
||
|
||
// 替换 Agent 实例后清空旧运行状态(turn/tool/failure)
|
||
f.handlers['session/event'](f.session, { type: 'turn/start', data: { turn: 2 } })
|
||
f.handlers['session/event'](f.session, { type: 'tool/call', data: { turn: 2, step: 1, callId: 'c2', name: 'write', arguments: '{}' } })
|
||
const newAgent = { id: 's1', status: 'idle', session: { id: 's1' } }
|
||
f.agents.set('s1', newAgent)
|
||
f.handlers['agent/status']({ agent: newAgent, status: 'idle' })
|
||
const afterSwap = f.activity.stateOf('p1', 'gongbu')
|
||
assert.equal(afterSwap.label, '待命')
|
||
assert.equal(afterSwap.turn, null)
|
||
assert.equal(afterSwap.tool, null)
|
||
})
|
||
|
||
test('seat-delivery:resolveAgent 后 followup,source 永远不是 user', async () => {
|
||
let followed = null
|
||
const fake = {
|
||
sessionController: { async resolveAgent() { return { agent: fake.agent } } },
|
||
agents: { get: () => fake.agent },
|
||
}
|
||
fake.agent = { followup: async message => { followed = message }, session: { id: 's1' } }
|
||
const delivery = createSeatDelivery({ host: fake })
|
||
const result = await delivery.deliverToSeat({
|
||
sessionId: 's1', text: '任务内容', taskId: 't1', deliveryId: 'd1', attemptId: 'a1',
|
||
sourceKind: 'sansheng-task', verifyClaim: () => true,
|
||
})
|
||
assert.equal(result.status, 'sent')
|
||
assert.equal(followed.source.kind, 'sansheng-task')
|
||
assert.equal(followed.source.kind === 'user', false)
|
||
})
|