dsh-sansheng-liubu/sansheng/test/seat-activity.test.js

50 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 agents = new Map()
const agent = { status: 'idle', statusGeneration: 1 }
agents.set('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, agents, handlers, setNow: value => { now = value } }
}
test('seat-activity:运行中/失败 TTL/身份替换/实时审批', () => {
const f = activityFixture()
f.agent.status = 'running'
assert.equal(f.activity.stateOf('p1', 'gongbu').label, '运行中')
f.agent.status = 'idle'
f.handlers['session/event']({ sessionId: 's1', type: 'turn/end', data: { reason: 'error' } })
assert.equal(f.activity.stateOf('p1', 'gongbu').label, '上次运行失败')
f.setNow(1000 + RUNTIME_FAILURE_TTL_MS + 1)
assert.equal(f.activity.stateOf('p1', 'gongbu').label, '待命')
// 旧 generation 事件不能覆盖当前 Agent
f.handlers['agent/status']({ sessionId: 's1', statusGeneration: 99, type: 'turn/start' })
assert.equal(f.activity.stateOf('p1', 'gongbu').turn, null)
})
test('seat-delivery:resolveAgent 后 followup,source 永远不是 user', async () => {
let followed = null
const fake = {
sessionController: { async resolveAgent(id) { 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)
assert.equal(delivery.attempt('a1').state, 'sent')
})