138 lines
5.3 KiB
JavaScript
138 lines
5.3 KiB
JavaScript
import assert from 'node:assert/strict'
|
||
import test from 'node:test'
|
||
import { buildStatePayload } from '../src/api.js'
|
||
|
||
function fakeStore({ projects = {}, reviews = [], dispatches = [], unknown = 0 } = {}) {
|
||
return {
|
||
peek: () => ({ version: 2, projects, reviews, dispatches, reworks: [], events: [] }),
|
||
unknownDispatches: () => Array.from({ length: unknown }, (_, index) => ({ id: `dsp-${index}` })),
|
||
dispatchesFor: () => dispatches,
|
||
reviewsFor: match => reviews.filter(item => Object.entries(match).every(([key, value]) => item[key] === value)),
|
||
}
|
||
}
|
||
|
||
function fakeDsbot({ bots = {}, rooms = {} } = {}) {
|
||
return {
|
||
botOf: id => bots[id],
|
||
roomOf: id => rooms[id],
|
||
}
|
||
}
|
||
|
||
const project = {
|
||
id: 'prj-1',
|
||
name: '示例项目',
|
||
roomId: 'room-1',
|
||
bots: { lead: 'bot-lead', shangshu: 'bot-shang', gongbu: 'bot-gong' },
|
||
models: {},
|
||
maxReworks: 3,
|
||
}
|
||
|
||
const bots = {
|
||
'bot-lead': { id: 'bot-lead', name: '丞相', model: 'magpie/m1' },
|
||
'bot-shang': { id: 'bot-shang', name: '尚书', model: 'magpie/m2' },
|
||
'bot-gong': { id: 'bot-gong', name: '工部', model: 'magpie/m3' },
|
||
}
|
||
|
||
const goal = {
|
||
id: 'goal-1',
|
||
revision: 4,
|
||
status: 'active',
|
||
title: '示例目标',
|
||
objective: '把示例功能做出来并验证',
|
||
acceptance: '测试通过',
|
||
current: 0,
|
||
stages: [
|
||
{
|
||
id: 's1',
|
||
title: '搭骨架',
|
||
status: 'active',
|
||
reworks: 1,
|
||
tasks: [
|
||
{ id: 't1', botId: 'bot-gong', task: '写脚手架', status: 'active' },
|
||
{ id: 't2', botId: 'bot-gong', task: '补测试', status: 'done' },
|
||
],
|
||
},
|
||
{ id: 's2', title: '接界面', status: 'pending', reworks: 0, tasks: [] },
|
||
],
|
||
}
|
||
|
||
test('没有项目时返回空数组', () => {
|
||
const payload = buildStatePayload({ store: fakeStore(), dsbot: fakeDsbot(), now: 1 })
|
||
assert.deepEqual(payload, { generatedAt: 1, projects: [] })
|
||
})
|
||
|
||
test('三张卡片按角色顺序给出名称、模型与阶段信息', () => {
|
||
const store = fakeStore({ projects: { 'prj-1': project }, unknown: 1 })
|
||
const dsbot = fakeDsbot({ bots, rooms: { 'room-1': { id: 'room-1', name: '示例项目 · 三省六部', goal } } })
|
||
const [card] = buildStatePayload({ store, dsbot }).projects
|
||
assert.deepEqual(card.roles.map(item => item.role), ['lead', 'shangshu', 'gongbu'])
|
||
assert.equal(card.roles[0].name, '丞相')
|
||
assert.equal(card.roles[0].model, 'magpie/m1')
|
||
assert.equal(card.roles[0].line, '目标进行中')
|
||
assert.match(card.roles[1].line, /阶段 0\/2 · 搭骨架/)
|
||
assert.match(card.roles[1].detail, /返工 1\/3/)
|
||
assert.match(card.roles[1].detail, /待核实 1/)
|
||
assert.equal(card.roles[1].tone, 'warn')
|
||
assert.match(card.roles[2].line, /工单 1\/2/)
|
||
assert.equal(card.roles[2].tone, 'active')
|
||
assert.equal(card.budget.used, 1)
|
||
assert.equal(card.goal.revision, 4)
|
||
assert.equal(card.roomName, '示例项目 · 三省六部')
|
||
})
|
||
|
||
test('模型未配置时回退到 Bot 自己的模型', () => {
|
||
const store = fakeStore({ projects: { 'prj-1': project } })
|
||
const dsbot = fakeDsbot({ bots, rooms: { 'room-1': { id: 'room-1', goal } } })
|
||
const [card] = buildStatePayload({ store, dsbot }).projects
|
||
assert.equal(card.roles[2].model, 'magpie/m3')
|
||
})
|
||
|
||
test('丞相验收记录决定丞相卡片;阶段驳回显示在工部卡片', () => {
|
||
const reviews = [
|
||
{ projectId: 'prj-1', kind: 'goal', verdict: 'pass', goalRevision: 6, reviewerRole: 'lead', at: 20 },
|
||
{ projectId: 'prj-1', kind: 'stage', verdict: 'reject', stageIndex: 0, reason: '测试没跑', reviewerRole: 'shangshu', at: 10 },
|
||
]
|
||
const store = fakeStore({ projects: { 'prj-1': project }, reviews })
|
||
const dsbot = fakeDsbot({ bots, rooms: { 'room-1': { id: 'room-1', goal } } })
|
||
const [card] = buildStatePayload({ store, dsbot }).projects
|
||
assert.equal(card.roles[0].line, '已验收 rev 6')
|
||
assert.equal(card.roles[0].tone, 'ok')
|
||
assert.match(card.roles[2].detail, /测试没跑/)
|
||
assert.equal(card.reviews.length, 2)
|
||
assert.equal(card.reviews[0].kind, 'goal')
|
||
})
|
||
|
||
test('全部阶段通过但还没验收时丞相卡片提示等待验收', () => {
|
||
const passed = {
|
||
...goal,
|
||
current: 2,
|
||
stages: [
|
||
{ id: 's1', title: '一', status: 'passed', tasks: [] },
|
||
{ id: 's2', title: '二', status: 'passed', tasks: [] },
|
||
],
|
||
}
|
||
const store = fakeStore({ projects: { 'prj-1': project } })
|
||
const dsbot = fakeDsbot({ bots, rooms: { 'room-1': { id: 'room-1', goal: passed } } })
|
||
const [card] = buildStatePayload({ store, dsbot }).projects
|
||
assert.equal(card.roles[0].line, '等待验收')
|
||
assert.equal(card.roles[0].tone, 'attention')
|
||
assert.equal(card.goal.passed, 2)
|
||
})
|
||
|
||
test('没有目标时三张卡片都给出可执行的下一步', () => {
|
||
const store = fakeStore({ projects: { 'prj-1': project } })
|
||
const dsbot = fakeDsbot({ bots, rooms: { 'room-1': { id: 'room-1' } } })
|
||
const [card] = buildStatePayload({ store, dsbot }).projects
|
||
assert.equal(card.goal, null)
|
||
assert.equal(card.roles[1].line, '未起草目标')
|
||
assert.match(card.roles[1].detail, /group_goal/)
|
||
assert.equal(card.roles[2].line, '无工单')
|
||
})
|
||
|
||
test('群被删掉时仍能返回项目,roomExists 为 false', () => {
|
||
const store = fakeStore({ projects: { 'prj-1': project } })
|
||
const [card] = buildStatePayload({ store, dsbot: fakeDsbot({ bots }) }).projects
|
||
assert.equal(card.roomExists, false)
|
||
assert.equal(card.roomName, null)
|
||
})
|