import test from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' import { collectTurnToolCalls, matchTurnCards, isApprovalCardExpired, mergePendingItems, } from '../src/client/pending-model.js' import { buildWorkbenchView, PRESET_OPTIONS, recommendedPresetForSeat, updateDraft, } from '../src/client/workbench.js' const HERE = dirname(fileURLToPath(import.meta.url)) const ROOT = dirname(HERE) test('collectTurnToolCalls 提取直调与 PTC 嵌套调用并区分普通回合', () => { const ptcTurn = { steps: [ { data: new Map([ ['assistant-step', { blocks: [{ kind: 'tool-call', callId: 'call-direct', name: 'sansheng_card' }], }], ]), nodes: [ { kind: 'tool-call', data: { root: { kind: 'tool-call', callId: 'call-ptc', call: { name: 'run_code' }, subCalls: [ { callId: 'call-ptc:ptc:1', name: 'sansheng_card' }, ], }, }, }, ], }, ], } const got = collectTurnToolCalls(ptcTurn) assert.equal(got.hasCardOrPtcCall, true) assert.deepEqual(got.callIds, ['call-direct', 'call-ptc', 'call-ptc:ptc:1']) const normalTurn = { steps: [ { assistantStep: { blocks: [{ kind: 'tool-call', callId: 'call-read', name: 'read_file' }], }, }, ], } const plain = collectTurnToolCalls(normalTurn) assert.equal(plain.hasCardOrPtcCall, false) assert.deepEqual(plain.callIds, ['call-read']) }) test('matchTurnCards 精确匹配直调 ID 与 :ptc: 前缀卡片,不误匹配相似前缀', () => { const cards = [ { id: 'c1', status: 'pending' }, { id: 'c2:ptc:1', status: 'pending' }, { id: 'c20', status: 'pending' }, ] const matched = matchTurnCards(cards, ['c1', 'c2']) assert.deepEqual(matched.map(c => c.id), ['c1', 'c2:ptc:1']) }) test('isApprovalCardExpired 仅在健康审批队列中缺少对应 approvalId 时判为已失效', () => { const card = { id: 'card-1', kind: 'approval', status: 'pending', approvalId: 'apr-1' } assert.equal(isApprovalCardExpired(card, [], null), false) assert.equal(isApprovalCardExpired(card, [], { degraded: true }), false) assert.equal(isApprovalCardExpired(card, [{ id: 'apr-1' }], { degraded: false }), false) assert.equal(isApprovalCardExpired(card, [], { degraded: false }), true) assert.equal(isApprovalCardExpired({ ...card, status: 'answered' }, [], { degraded: false }), false) assert.equal(isApprovalCardExpired({ id: 'q1', kind: 'question', status: 'pending' }, [], { degraded: false }), false) }) test('mergePendingItems 合并工具审批、审批附带卡片、问答卡、决定与流程阻塞', () => { const out = mergePendingItems({ approvals: [{ id: 'apr-1', seatId: 'menxia', tool: 'pwsh' }], approvalStatus: { available: true, degraded: false }, cards: [ { id: 'c-old', kind: 'approval', status: 'pending', approvalId: 'apr-1', title: '旧卡' }, { id: 'c-new', kind: 'approval', status: 'pending', approvalId: 'apr-1', title: '新卡' }, { id: 'c-q', kind: 'question', status: 'pending', title: '选方案 A 还是 B?' }, { id: 'c-done', kind: 'question', status: 'answered', title: '已答问题' }, ], tasks: [ { id: 't1', status: 'dispatched', workflowBlockage: { label: '从交门下审核', reason: '宿主审批已取消' }, decisions: [{ id: 'd1', status: 'pending', question: '是否提交?' }], }, { id: 't2', status: 'done', workflowBlockage: { label: '历史记录', reason: '已完结' }, }, ], }) assert.equal(out.degraded, false) assert.equal(out.approvals.length, 1) assert.equal(out.approvals[0].card?.title, '新卡') assert.deepEqual(out.questions.map(q => q.id), ['c-q']) assert.equal(out.decisions.length, 1) assert.deepEqual(out.blockedTasks.map(t => t.id), ['t1']) assert.equal(out.pendingCount, 3) const degradedOut = mergePendingItems({ approvals: [{ id: 'apr-1', seatId: 'menxia' }], approvalStatus: { degraded: true }, cards: [{ id: 'c-q', kind: 'question', status: 'pending', title: 'Q' }], tasks: [], }) assert.equal(degradedOut.degraded, true) assert.equal(degradedOut.approvals.length, 0) assert.equal(degradedOut.questions.length, 1) assert.equal(degradedOut.pendingCount, 1) }) test('workbench 推荐分工与执行模式草稿视图:中枢/三省为 standard,六部为 ops-ptc', () => { for (const id of ['lead', 'zhongshu', 'menxia', 'shangshu']) { assert.equal(recommendedPresetForSeat(id), 'standard') } for (const id of ['libu', 'hubu', 'lipu', 'bingbu', 'xingbu', 'gongbu']) { assert.equal(recommendedPresetForSeat(id), 'ops-ptc') } assert.deepEqual(PRESET_OPTIONS.map(p => p.value), ['standard', 'ops-ptc', 'minimal']) const legacyView = buildWorkbenchView({ project: { id: 'p1', name: '测试项目', status: 'ready', seats: { menxia: { id: 'menxia', status: 'bound', model: { provider: 'auto', model: 'gpt-5' } }, }, }, }, { selectedSeatId: 'menxia', draft: updateDraft({}, 'menxia', { agentPreset: 'standard' }) }) assert.equal(legacyView.project.presetSupported, false) assert.equal(legacyView.selected.recommendedPreset, 'standard') assert.equal(legacyView.selected.presetValue, 'standard') assert.equal(legacyView.selected.dirty, true) const enabledView = buildWorkbenchView({ project: { id: 'p2', name: '已启用模式后端项目', status: 'ready', capabilities: { seatPreset: true }, seats: { gongbu: { id: 'gongbu', status: 'bound', configuredPreset: 'ops-ptc', sessionPreset: 'ops-ptc' }, }, }, }, { selectedSeatId: 'gongbu' }) assert.equal(enabledView.project.presetSupported, true) assert.equal(enabledView.selected.presetValue, 'ops-ptc') assert.equal(enabledView.selected.sessionPreset, 'ops-ptc') assert.equal(enabledView.selected.presetMismatch, false) }) test('前端源码契约:index.js 同时注册 turnTail,workbench 组件无 inherit 选项值', () => { const indexSrc = readFileSync(join(ROOT, 'src/client/index.js'), 'utf8') const v2Src = readFileSync(join(ROOT, 'src/client/workbench-v2.js'), 'utf8') const mwbSrc = readFileSync(join(ROOT, 'src/client/master-workbench.js'), 'utf8') assert.match(indexSrc, /conversation\.chat\.turnTail/) assert.match(indexSrc, /dsh-sansheng:turn-tail-cards/) assert.match(v2Src, /mergePendingItems/) assert.match(mwbSrc, /mergePendingItems/) assert.doesNotMatch(v2Src, /value:\s*['"]inherit['"]/) assert.doesNotMatch(mwbSrc, /value:\s*['"]inherit['"]/) })