dsh-sansheng-liubu/sansheng/test/new-modules.test.js

89 lines
5.0 KiB
JavaScript
Raw Permalink 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 { createTurnCommands, pickLastUserCommand, internalSource } from '../src/host/message-source.js'
import { putCommand, getCommand } from '../src/host/command-ledger.js'
import { createApprovalCenter } from '../src/host/approval-center.js'
import { createSeatActivity } from '../src/host/seat-activity.js'
test('message-source:只认 source.kind=user 且有 clientTimeZone 的最后一条用户消息', () => {
const internal = { role: 'user', source: internalSource('sansheng-task', { seatId: 'zhongshu', taskId: 't1' }), content: [{ type: 'text', text: '发布 R-001 给中书' }] }
const synthetic = { role: 'user', source: { kind: 'skill-catalog' }, content: [{ type: 'text', text: '发布 R-002' }] }
const real = { role: 'user', id: 'm1', seq: 9, source: { kind: 'user', clientTimeZone: 'Asia/Shanghai' }, content: [{ type: 'text', text: '发布 R-001 给中书' }] }
assert.equal(pickLastUserCommand([real, internal]).text, '发布 R-001 给中书')
assert.equal(pickLastUserCommand([real]).text, '发布 R-001 给中书')
assert.equal(pickLastUserCommand([internal]), null)
assert.equal(pickLastUserCommand([real, synthetic]).text, '发布 R-001 给中书')
})
test('command-ledger:同一 sessionId+seq 记录可查', () => {
const project = {}
putCommand(project, { sessionId: 's1', seq: 12, outcome: 'published', taskId: 't1' })
assert.equal(getCommand(project, 's1', 12).taskId, 't1')
assert.equal(getCommand(project, 's1', 13), null)
})
test('approval-center:非十席直接 next;十席可 list/answer 且只接受精确身份', async () => {
const ctx = { handlers: {}, on(name, fn, options) { this.handlers[name] = { fn, options }; return () => {} } }
const center = createApprovalCenter({ ctx, resolveSeatBySessionId: id => (id === 's1' ? { projectId: 'p1', seatId: 'gongbu' } : null) })
center.install()
assert.equal(ctx.handlers['approval/request'].options.prepend, true)
const handler = ctx.handlers['approval/request'].fn
let nextCalled = 0
const pending = handler({ sessionId: 'other', tool: 'bash' }, async () => { nextCalled += 1; return 'native' })
assert.equal(await pending, 'native')
assert.equal(nextCalled, 1)
const waiting = handler({ sessionId: 's1', tool: 'bash', reason: 'need ok', detail: { text: 'cmd', secret: 'x' } }, async () => 'native')
await Promise.resolve()
const list = center.list('p1')
assert.equal(list.length, 1)
assert.equal(list[0].detail.secret, undefined)
assert.throws(() => center.answer(list[0].id, { projectId: 'p1', seatId: 'hubu', sessionId: 's1' }, 'allow'), error => error.code === 'sansheng/approval-forbidden')
center.answer(list[0].id, { projectId: 'p1', seatId: 'gongbu', sessionId: 's1' }, 'allow')
assert.equal(await waiting, 'allow')
assert.equal(center.list('p1').length, 0)
})
test('seat-activity:running→运行中,idle→待命,失败后 10 分钟内显示上次失败', async () => {
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()
agents.set('s1', { status: 'running' })
const activity = createSeatActivity({ store, host: { agents: { get: id => agents.get(id) } }, now: () => 1000 })
assert.equal(activity.stateOf('p1', 'gongbu').label, '运行中')
agents.set('s1', { status: 'idle' })
assert.equal(activity.stateOf('p1', 'gongbu').label, '待命')
})
test('当前轮授权只接受首步认领并落盘的 Web 用户消息', async () => {
const listeners = new Map()
const session = { id: 'lead-test' }
const agent = { session }
let live = agent
let openTurnStartSeq = 4
const ctx = {
agents: { get: () => live, turnBoundaryOf: () => ({ openTurnStartSeq }) },
on(name, fn) { listeners.set(name, fn); return () => listeners.delete(name) },
}
const tracker = createTurnCommands(ctx, { seatBySession: () => ({ seatId: 'lead' }) })
const preStep = listeners.get('agent/pre-step')
const event = listeners.get('session/event')
const human = { id: 'm-1', role: 'user', source: { kind: 'user', rpcId: 'rpc-1', clientTimeZone: 'Asia/Shanghai' }, content: [{ type: 'text', text: '就按这个做' }] }
const internal = { ...human, id: 'internal', source: { kind: 'sansheng-report' } }
const exec = { agent }
const next = () => ({ kind: 'enter' })
await preStep({ agent, messages: [internal] }, next)
await preStep({ agent, messages: [human] }, next)
event(session, { type: 'user/message', seq: 6, data: human })
assert.equal(tracker.read(exec), null)
event(session, { type: 'turn/end', seq: 7 })
openTurnStartSeq = 8
await preStep({ agent, messages: [human] }, next)
assert.equal(tracker.read(exec), null)
event(session, { type: 'user/message', seq: 10, data: human })
assert.deepEqual(tracker.read(exec), { text: '就按这个做', messageId: 'm-1', seq: 10 })
live = { session }
assert.equal(tracker.read(exec), null)
tracker.dispose()
assert.equal(listeners.size, 0)
})