import test from 'node:test' import assert from 'node:assert/strict' import { setupService } from './helpers.js' import { createSecretaryRouting } from '../src/host/secretary-routing.js' async function fixture() { const env = await setupService({ prompt: true }) const { project } = await env.service.open({ path: '/secretary-routing', name: '主簿接线' }) const calls = { work: [], order: [], report: [], send: [] } const approvals = [] const routing = createSecretaryRouting({ store: env.store, async startWork(projectId, role, work) { const run = { id: work.id, childId: 'child:' + work.id, status: 'started', role, work } await env.store.mutate(state => { state.projects[projectId].secretaryRuns.push(run) }) calls.work.push(run) return { runId: run.id } }, async startOrder(order) { calls.order.push(order) }, async sendTask(projectId, taskId, deliveryId) { calls.send.push({ projectId, taskId, deliveryId }) return env.service.taskDispatch(projectId, taskId, { deliveryId, viaSecretary: true }) }, async recordReport(projectId, report) { calls.report.push({ projectId, ...report }) }, pendingApprovals: () => approvals, }) env.service.secretaryRouting = routing return { ...env, projectId: project.id, routing, calls, approvals } } test('已授权发布经记室、对口主簿和中书接令后才正式投递', async () => { const { service, store, projectId, routing, calls } = await fixture() const { requirement } = await service.requirementUpsert(projectId, { title: '完成动画', originalText: '请做骑行画面' }) const lead = { seatId: 'lead', sessionId: 'lead-session' } await assert.rejects(service.requirementPublish(projectId, requirement.id, {}, lead), /宿主未能验证/) assert.equal(store.get(projectId).secretaryInbox.length, 0) const published = await service.requirementPublish(projectId, requirement.id, { command: '发布 R-001', commandRef: { seq: 7, messageId: 'publish-7' }, expectedRevision: requirement.revision, }, lead) await service.taskDispatch(projectId, published.taskId, { deliveryId: published.deliveryId }) await routing.drain(projectId) const [clerk] = calls.work assert.equal(clerk.role, 'requirement') const clerkHandoff = { projectId, role: clerk.role, childId: clerk.childId, work: clerk.work } await routing.onHandoff(clerkHandoff) assert.equal((await routing.onHandoff(clerkHandoff)).duplicate, true) assert.equal(store.get(projectId).requirementLog[0].text, '请做骑行画面') await routing.drain(projectId) const secretary = calls.work[1] assert.equal(secretary.role, 'zhongshu') await routing.onHandoff({ projectId, role: secretary.role, childId: secretary.childId, work: secretary.work }) assert.equal(calls.order[0].expectedRevision, requirement.revision) assert.equal(calls.send.length, 0) const run = { id: 'order-1', taskId: published.taskId, requirementId: requirement.id, requirementRevision: requirement.revision, seatId: 'zhongshu', sourceSessionId: secretary.childId } await routing.onOrderSettled({ projectId, run, result: '已核对' }) await routing.onOrderSettled({ projectId, run, result: '重复' }) assert.equal(calls.send.length, 1) assert.equal((await service.taskGet(projectId, published.taskId)).task.deliveryState, 'accepted') assert.equal(store.get(projectId).secretaryInbox.filter(item => item.id === 'receipt:order-1').length, 1) }) test('普通进度留在主簿记录,审批失效后拦住迟到呈报', async () => { const { routing, projectId, calls, approvals } = await fixture() await routing.routeReport({ projectId, report: { reportId: 'progress', event: 'dept_done', summary: '工部已完工' } }) approvals.push({ id: 'native' }) await routing.routeReport({ projectId, report: { reportId: 'approval', transitionId: 'approval:native', event: 'approval_request', ownerSeatId: 'menxia', needsUserDecision: true, summary: '门下审批', } }) await routing.drain(projectId) const progress = calls.work.find(run => run.role === 'shangshu') const approval = calls.work.find(run => run.role === 'menxia') const report = { kind: 'final', summary: '白话摘要' } await routing.onReport({ projectId, work: progress.work, report }) assert.equal(calls.report.length, 0) await routing.onReport({ projectId, work: approval.work, report }) assert.match(calls.report[0].summary, /审批编号:native/) approvals.length = 0 await routing.onReport({ projectId, work: approval.work, report }) assert.equal(calls.report.length, 1) })