263 lines
8.5 KiB
JavaScript
263 lines
8.5 KiB
JavaScript
import assert from 'node:assert/strict'
|
||
import test from 'node:test'
|
||
import { goalDigest, stageDigest } from '../src/digest.js'
|
||
import { budgetOf, evaluateGoalAction } from '../src/gate.js'
|
||
|
||
const project = {
|
||
id: 'prj-1',
|
||
name: '示例项目',
|
||
bots: { lead: 'b-lead', shangshu: 'b-shang', gongbu: 'b-gong' },
|
||
maxReworks: 3,
|
||
}
|
||
|
||
const members = [
|
||
{ id: 'b-lead', name: '丞相', role: 'lead' },
|
||
{ id: 'b-shang', name: '尚书', role: 'shangshu' },
|
||
{ id: 'b-gong', name: '工部', role: 'gongbu' },
|
||
]
|
||
|
||
function actorFor(role, extra = {}) {
|
||
return {
|
||
kind: 'bot',
|
||
botId: project.bots[role],
|
||
botName: { lead: '丞相', shangshu: '尚书', gongbu: '工部' }[role],
|
||
role,
|
||
isMain: false,
|
||
...extra,
|
||
}
|
||
}
|
||
|
||
function makeGoal() {
|
||
return {
|
||
id: 'goal-1',
|
||
revision: 5,
|
||
status: 'active',
|
||
objective: '交付官网',
|
||
acceptance: '首页可访问且测试通过',
|
||
constraints: ['不改数据库'],
|
||
current: 0,
|
||
stages: [
|
||
{
|
||
id: 's1',
|
||
title: '搭骨架',
|
||
acceptance: '页面能打开',
|
||
status: 'active',
|
||
reworks: 0,
|
||
tasks: [{ id: 't1', botId: 'b-gong', task: '写页面', status: 'reported', note: '' }],
|
||
},
|
||
{
|
||
id: 's2',
|
||
title: '补内容',
|
||
acceptance: '文案齐全',
|
||
status: 'pending',
|
||
reworks: 0,
|
||
tasks: [{ id: 't2', botId: 'b-gong', task: '填文案', status: 'pending' }],
|
||
},
|
||
],
|
||
}
|
||
}
|
||
|
||
function stageReview(goal, stageIndex, verdict) {
|
||
return {
|
||
id: `rev-${stageIndex}-${verdict}`,
|
||
projectId: project.id,
|
||
goalId: goal.id,
|
||
goalRevision: goal.revision,
|
||
kind: 'stage',
|
||
stageIndex,
|
||
digest: stageDigest(goal, stageIndex),
|
||
verdict,
|
||
reviewerRole: 'shangshu',
|
||
}
|
||
}
|
||
|
||
function goalReview(goal, verdict) {
|
||
return {
|
||
id: `rev-goal-${verdict}`,
|
||
projectId: project.id,
|
||
goalId: goal.id,
|
||
goalRevision: goal.revision,
|
||
kind: 'goal',
|
||
stageIndex: null,
|
||
digest: goalDigest(goal),
|
||
verdict,
|
||
reviewerRole: 'lead',
|
||
}
|
||
}
|
||
|
||
const decide = (overrides = {}) => evaluateGoalAction({
|
||
action: 'pass',
|
||
actor: actorFor('shangshu'),
|
||
project,
|
||
goal: makeGoal(),
|
||
reviews: [],
|
||
members,
|
||
args: {},
|
||
...overrides,
|
||
})
|
||
|
||
test('未绑定项目的群保持原生行为', () => {
|
||
assert.equal(decide({ project: undefined }).allow, true)
|
||
})
|
||
|
||
test('用户身份放行', () => {
|
||
assert.equal(decide({ actor: { kind: 'user' } }).allow, true)
|
||
})
|
||
|
||
test('工部不能改目标', () => {
|
||
const result = decide({ actor: actorFor('gongbu') })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'gongbu')
|
||
})
|
||
|
||
test('丞相不能直接改目标', () => {
|
||
const result = decide({ actor: actorFor('lead') })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'lead')
|
||
})
|
||
|
||
test('Main Bot 不能操作已绑定项目', () => {
|
||
const result = decide({ actor: actorFor('shangshu', { isMain: true }) })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'main-bot')
|
||
})
|
||
|
||
test('尚书通过阶段前必须有同一版本的阶段审查', () => {
|
||
const goal = makeGoal()
|
||
const result = decide({ goal, reviews: [] })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'no-stage-review')
|
||
|
||
const ok = decide({ goal, reviews: [stageReview(goal, 1, 'pass')] })
|
||
assert.equal(ok.allow, true)
|
||
})
|
||
|
||
test('账本 revision 增长但审查对象未变时,审查仍有效(A05 无死锁)', () => {
|
||
const goal = makeGoal()
|
||
const valid = stageReview(goal, 1, 'pass')
|
||
goal.revision = 6
|
||
const result = decide({ goal, reviews: [valid] })
|
||
assert.equal(result.allow, true)
|
||
})
|
||
|
||
test('审查后任务状态变了也算证据过期', () => {
|
||
const goal = makeGoal()
|
||
const review = stageReview(goal, 1, 'pass')
|
||
goal.stages[0].tasks[0].status = 'done'
|
||
const result = decide({ goal, reviews: [review] })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'no-stage-review')
|
||
})
|
||
|
||
test('驳回需要尚书 reject 记录,且受返工预算限制', () => {
|
||
const goal = makeGoal()
|
||
const none = decide({ action: 'rework', goal, reviews: [], args: { reason: '缺测试' } })
|
||
assert.equal(none.allow, false)
|
||
assert.equal(none.code, 'no-reject-review')
|
||
|
||
const ok = decide({ action: 'rework', goal, reviews: [stageReview(goal, 1, 'reject')], args: { reason: '缺测试' } })
|
||
assert.equal(ok.allow, true)
|
||
|
||
goal.stages[0].reworks = 3
|
||
assert.deepEqual(budgetOf(goal, project), { used: 3, max: 3, left: 0 })
|
||
const spent = decide({ action: 'rework', goal, reviews: [stageReview(goal, 1, 'reject')], args: { reason: '再来' } })
|
||
assert.equal(spent.allow, false)
|
||
assert.equal(spent.code, 'budget')
|
||
})
|
||
|
||
test('revise 重建阶段把 reworks 清零后,本地返工记录仍占预算', () => {
|
||
const goal = makeGoal()
|
||
// 模拟 group_goal revise 之后:新阶段 reworks 归零,但已经返工过 3 轮。
|
||
goal.stages[0].reworks = 0
|
||
const recorded = [
|
||
{ projectId: project.id, goalId: goal.id, stageIndex: 1 },
|
||
{ projectId: project.id, goalId: goal.id, stageIndex: 1 },
|
||
{ projectId: project.id, goalId: goal.id, stageIndex: 1 },
|
||
]
|
||
assert.deepEqual(budgetOf(goal, project, recorded), { used: 3, max: 3, left: 0 })
|
||
|
||
const spent = decide({
|
||
action: 'rework',
|
||
goal,
|
||
reviews: [stageReview(goal, 1, 'reject')],
|
||
reworks: recorded,
|
||
args: { reason: '第四轮' },
|
||
})
|
||
assert.equal(spent.allow, false)
|
||
assert.equal(spent.code, 'budget')
|
||
|
||
const otherGoal = [{ projectId: project.id, goalId: 'goal-other', stageIndex: 1 }]
|
||
assert.equal(budgetOf(goal, project, otherGoal).used, 0, '别的目标的返工不算进本目标')
|
||
})
|
||
|
||
test('完成目标需要丞相 goal_pass 记录', () => {
|
||
const goal = makeGoal()
|
||
goal.current = 2
|
||
goal.stages[0].status = 'passed'
|
||
goal.stages[1].status = 'passed'
|
||
|
||
const missing = decide({ action: 'complete', goal, reviews: [], args: { evidence: 'e', summary: 's' } })
|
||
assert.equal(missing.allow, false)
|
||
assert.equal(missing.code, 'no-goal-review')
|
||
|
||
const ok = decide({ action: 'complete', goal, reviews: [goalReview(goal, 'pass')], args: { evidence: 'e', summary: 's' } })
|
||
assert.equal(ok.allow, true)
|
||
|
||
const stageOnly = decide({ action: 'complete', goal, reviews: [stageReview(goal, 1, 'pass')], args: {} })
|
||
assert.equal(stageOnly.allow, false)
|
||
})
|
||
|
||
test('阶段未全部通过时不能完成', () => {
|
||
const goal = makeGoal()
|
||
const result = decide({ action: 'complete', goal, reviews: [goalReview(goal, 'pass')], args: {} })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'not-all-passed')
|
||
})
|
||
|
||
test('工单只能派给工部', () => {
|
||
const bad = decide({ action: 'task', args: { bot: '丞相' } })
|
||
assert.equal(bad.allow, false)
|
||
assert.equal(bad.code, 'task-target')
|
||
|
||
const good = decide({ action: 'task', args: { bot: '工部' } })
|
||
assert.equal(good.allow, true)
|
||
})
|
||
|
||
test('启动目标时每个任务的执行者都必须是工部', () => {
|
||
const stages = [{ title: '一', acceptance: 'a', tasks: [{ bot: '工部', task: '做' }] }]
|
||
assert.equal(decide({ action: 'start', args: { stages } }).allow, true)
|
||
|
||
const bad = [{ title: '一', acceptance: 'a', tasks: [{ bot: '丞相', task: '做' }] }]
|
||
const result = decide({ action: 'start', args: { stages: bad } })
|
||
assert.equal(result.allow, false)
|
||
assert.equal(result.code, 'stage-target')
|
||
})
|
||
|
||
test('暂停/停止/修订由尚书放行', () => {
|
||
for (const action of ['pause', 'resume', 'stop', 'revise', 'draft']) {
|
||
const result = decide({ action, args: {} })
|
||
assert.equal(result.allow, true, `${action} 应放行`)
|
||
}
|
||
})
|
||
import { artifactDigestOf } from '../src/digest.js'
|
||
|
||
test('Q07:截断或缺失的成果快照不能用于 pass/complete', () => {
|
||
const goal = makeGoal()
|
||
const projectWithWork = { ...project, workDir: 'C:/work/example' }
|
||
const review = { ...stageReview(goal, 1, 'pass'), artifactDigest: artifactDigestOf({ kind: 'files', digest: 'd', truncated: true, exists: true }) }
|
||
const truncated = decide({
|
||
action: 'pass', project: projectWithWork, goal, reviews: [review],
|
||
artifact: { kind: 'files', digest: 'd', truncated: true, exists: true },
|
||
})
|
||
assert.equal(truncated.allow, false)
|
||
assert.equal(truncated.code, 'artifact-truncated')
|
||
|
||
const missingReview = { ...stageReview(goal, 1, 'pass'), artifactDigest: artifactDigestOf({ kind: 'missing', digest: 'm' }) }
|
||
const missing = decide({
|
||
action: 'pass', project: projectWithWork, goal, reviews: [missingReview],
|
||
artifact: { kind: 'missing', digest: 'm', truncated: false, exists: false },
|
||
})
|
||
assert.equal(missing.allow, false)
|
||
assert.equal(missing.code, 'artifact-missing')
|
||
})
|