import test from 'node:test' import assert from 'node:assert/strict' import { setupService } from './helpers.js' import { installPromptSection } from '../src/host/prompt.js' test('项目共识:保存、版本递增、旧版本提交被拒绝', async () => { const { service } = await setupService() const opened = await service.open({ path: '/memory', name: '共识项目' }) const projectId = opened.project.id const first = await service.memoryUpdate(projectId, { expectedRevision: 0, goal: '做一个可验收的交付', constraints: ['不能改线上数据', '必须保留旧接口'], acceptance: ['回归测试通过'], decisions: [{ type: 'user', text: '先做最小可用版本', status: 'active' }], }) assert.equal(first.memory.revision, 1) assert.equal(first.memory.constraints.length, 2) assert.ok(first.memory.contentHash) await assert.rejects( () => service.memoryUpdate(projectId, { expectedRevision: 0, goal: '旧版本覆盖' }), err => err.code === 'sansheng/memory-conflict', ) const second = await service.memoryUpdate(projectId, { expectedRevision: 1, acceptance: ['回归测试通过', '截图可复核'] }) assert.equal(second.memory.revision, 2) const read = await service.memory(projectId) assert.deepEqual(read.memory.acceptance, ['回归测试通过', '截图可复核']) }) test('项目共识:注入到部门 system prompt,不依赖任务文字', async () => { const { service, fake, store } = await setupService() installPromptSection(fake.ctx, { store }) const opened = await service.open({ path: '/memory-prompt', name: '注入项目' }) const projectId = opened.project.id await service.memoryUpdate(projectId, { goal: '交付一个能用的版本', constraints: ['绝对不要改动生产数据'], acceptance: ['自动化测试通过'], }) const view = await service.enableSeat(projectId, 'gongbu') const seat = view.project.seats.find(item => item.id === 'gongbu') const section = fake.getPromptSection() const text = section.text({ agent: { id: seat.sessionId } }) assert.match(text, /项目共识 v1/) assert.match(text, /绝对不要改动生产数据/) assert.match(text, /自动化测试通过/) }) test('派工:把共识拼进 prompt,记录上下文版本与投递回执', async () => { const { service, fake } = await setupService({ prompt: true }) const opened = await service.open({ path: '/memory-dispatch', name: '派工项目' }) const projectId = opened.project.id const saved = await service.memoryUpdate(projectId, { goal: '完成可验收交付', constraints: ['不能动线上数据库'], decisions: [{ type: 'user', text: '优先保证稳定', status: 'active' }], }) const created = await service.taskCreate(projectId, { title: '写说明', goal: '任务文字故意不写硬约束', ownerSeatId: 'gongbu' }) const dispatched = await service.taskDispatch(projectId, created.task.id) assert.equal(dispatched.delivered, true) assert.equal(dispatched.task.contextRevision, saved.memory.revision) assert.equal(dispatched.task.deliveryState, 'accepted') assert.equal(fake.calls.prompt.length, 1) const text = fake.calls.prompt[0].content[0].text assert.match(text, /项目共识 v1/) assert.match(text, /不能动线上数据库/) assert.match(text, /任务文字故意不写硬约束/) }) test('部门标题:新建时固定为两字角色名', async () => { const { service, fake } = await setupService() const opened = await service.open({ path: '/memory-title', name: '标题项目' }) await service.enableSeat(opened.project.id, 'gongbu') const latest = fake.calls.rename.at(-1) assert.equal(latest.title, '工部') })