import assert from 'node:assert/strict' import test from 'node:test' import { buildStatePayload, listBots, resolveCurrentProject } from '../src/api.js' function fakeStore({ project, reviews = [], unknown = 0, artifacts = [] } = {}) { return { peek: () => ({ version: 3, seq: 10, projects: { [project.id]: project }, reviews, dispatches: [], reworks: [], artifacts: {}, leases: {}, events: [] }), unknownDispatches: () => Array.from({ length: unknown }, (_, index) => ({ id: `dsp-${index}` })), dispatchesFor: () => [], reviewsFor: match => reviews .filter(item => Object.entries(match).every(([key, value]) => item[key] === value)) .sort((a, b) => (b.seq ?? 0) - (a.seq ?? 0)), artifactsFor: () => artifacts, leaseOf: () => undefined, } } const project = { id: 'prj-1', name: '示例项目', workspaceId: 'ws-1', hostWorkspaceId: 'ws-1', workDir: 'C:/work', roomId: 'room-1', bots: { lead: 'b-lead', shangshu: 'b-shang', gongbu: 'b-gong' }, models: {}, maxReworks: 3, } const bots = { 'b-lead': { id: 'b-lead', name: '丞相', sessionId: 'sess-lead' }, 'b-shang': { id: 'b-shang', name: '尚书', sessionId: 'sess-shang' }, 'b-gong': { id: 'b-gong', name: '工部', sessionId: 'sess-gong' }, } function goalAt({ status = 'active', current = 0, stageStatus = 'active', taskStatus = 'working' } = {}) { return { id: 'goal-1', revision: 4, status, title: '目标', objective: '做出来', acceptance: '测试通过', current, stages: [ { id: 's1', title: '骨架', acceptance: '能开', status: stageStatus, reworks: 0, tasks: [{ id: 't1', botId: 'b-gong', task: '写', status: taskStatus }] }, { id: 's2', title: '内容', acceptance: '齐全', status: 'pending', reworks: 0, tasks: [] }, ], } } function build({ goal, reviews = [], unknown = 0, artifacts = [] } = {}) { const room = goal === undefined ? { id: 'room-1', name: '群' } : { id: 'room-1', name: '群', goal } const dsbot = { botOf: id => bots[id], roomOf: id => (id === 'room-1' ? room : undefined), } return buildStatePayload({ store: fakeStore({ project, reviews, unknown, artifacts }), dsbot, now: 1 }).projects[0] } test('角色卡带真实会话 id,可点击打开', () => { const card = build({ goal: goalAt() }) assert.equal(card.roles.find(item => item.role === 'lead').sessionId, 'sess-lead') assert.equal(card.roles.every(item => item.clickable), true) }) test('阶段执行中 → phase=执行中', () => { const card = build({ goal: goalAt({ taskStatus: 'working' }) }) assert.equal(card.phase, '执行中') assert.equal(card.currentStage.index, 1) }) test('阶段任务全部完成 → phase=待审查', () => { const card = build({ goal: goalAt({ taskStatus: 'done' }) }) assert.equal(card.phase, '待审查') }) test('最新阶段驳回 → phase=返工中,且 attention 指向返工', () => { const goal = goalAt({ taskStatus: 'done' }) const reviews = [{ id: 'rev-1', seq: 1, projectId: 'prj-1', goalId: 'goal-1', kind: 'stage', stageIndex: 1, verdict: 'reject', reviewerRole: 'shangshu', reason: '缺测试', at: 1, }] const card = build({ goal, reviews }) assert.equal(card.phase, '返工中') assert.equal(card.attention.some(item => item.role === 'gongbu'), true) }) test('历史驳回之后又有更新通过,不再显示旧返工', () => { const goal = goalAt({ taskStatus: 'done' }) const reviews = [ { id: 'r1', seq: 1, projectId: 'prj-1', goalId: 'goal-1', kind: 'stage', stageIndex: 1, verdict: 'reject', reviewerRole: 'shangshu', reason: '旧问题', at: 1 }, { id: 'r2', seq: 2, projectId: 'prj-1', goalId: 'goal-1', kind: 'stage', stageIndex: 1, verdict: 'pass', reviewerRole: 'shangshu', at: 2 }, ] const card = build({ goal, reviews }) assert.equal(card.phase, '待审查') assert.equal(card.attention.some(item => item.reason.includes('旧问题')), false) }) test('全部阶段通过 → phase=待审查,丞相卡为 attention', () => { const goal = { ...goalAt(), current: 2, stages: [ { id: 's1', title: '一', status: 'passed', tasks: [] }, { id: 's2', title: '二', status: 'passed', tasks: [] }, ], } const card = build({ goal }) assert.equal(card.phase, '待审查') assert.equal(card.roles.find(item => item.role === 'lead').tone, 'attention') }) test('暂停 / 完成 / 无目标 / 有未知派发的 phase', () => { assert.equal(build({ goal: goalAt({ status: 'paused' }) }).phase, '暂停') assert.equal(build({ goal: goalAt({ status: 'complete' }) }).phase, '已完成') assert.equal(build({ goal: undefined }).phase, '待执行') assert.equal(build({ goal: goalAt(), unknown: 2 }).phase, '待核实') }) test('成果快照摘要出现在当前项目卡片,历史审查可折叠数据存在', () => { const card = build({ goal: goalAt(), artifacts: [{ id: 'art-1', digest: 'abc', kind: 'git', fileCount: 3, totalBytes: 100, submissionSeq: 2, createdAt: 1 }], reviews: [{ id: 'r1', seq: 1, projectId: 'prj-1', goalId: 'goal-1', kind: 'goal', verdict: 'pass', reviewerRole: 'lead', at: 1 }], }) assert.equal(card.submissionSeq, 2) assert.equal(card.lastSubmission.digest, 'abc') assert.equal(card.reviewsHistory.length, 1) }) test('DS Bot 没有 sessionId 字段时用 Bot id 作为私聊会话 id', () => { const card = buildStatePayload({ store: fakeStore({ project }), dsbot: { botOf: id => ({ 'b-lead': { id: 'b-lead', name: '丞相' }, 'b-shang': { id: 'b-shang', name: '尚书' }, 'b-gong': { id: 'b-gong', name: '工部' } })[id], roomOf: () => ({ id: 'room-1', name: '群', goal: goalAt() }), }, now: 1, }).projects[0] assert.equal(card.roles.find(item => item.role === 'lead').sessionId, 'b-lead') assert.equal(card.roles.every(item => item.clickable), true) }) test('listBots 返回 Bot/群花名册供右栏切换,不改变项目绑定', () => { const dsbot = { snapshot: () => ({ mainBotIds: ['b-lead'], bots: { 'b-lead': { id: 'b-lead', name: '丞相', role: '丞相' }, 'b-shang': { id: 'b-shang', name: '尚书', role: '尚书', model: 'm2' }, 'b-gong': { id: 'b-gong', name: '工部', role: '工部' }, }, rooms: { 'room-1': { id: 'room-1', name: '示例群', members: ['b-lead', 'b-shang'], admin: 'b-shang' } }, }), } const store = { projectsByBot: id => (['b-lead', 'b-shang', 'b-gong'].includes(id) ? [project] : []), projectByRoom: roomId => (roomId === 'room-1' ? project : undefined), } const result = listBots({ dsbot, store }) assert.equal(result.bots.length, 3) assert.equal(result.bots.find(bot => bot.id === 'b-lead').isMain, true) assert.equal(result.bots.find(bot => bot.id === 'b-shang').projectId, 'prj-1') assert.equal(result.rooms[0].projectId, 'prj-1') assert.equal(result.bots.find(bot => bot.id === 'b-shang').sessionId, 'b-shang') }) test('resolveCurrentProject 按 workspaceId 优先,其次 session', () => { const payload = { projects: [{ id: 'prj-1', workspaceId: 'ws-1', roles: [{ sessionId: 'sess-1' }] }] } assert.equal(resolveCurrentProject(payload, { workspaceId: 'ws-1' }).project.id, 'prj-1') assert.equal(resolveCurrentProject(payload, { sessionId: 'sess-1' }).project.id, 'prj-1') assert.equal(resolveCurrentProject(payload, { workspaceId: 'ws-x', sessionId: 'sess-x' }).project, undefined) })