0.11.0 退役老预设线:workbench/test/memory.test.mjs

This commit is contained in:
QQ 2026-11-10 22:25:27 +08:00
parent 6212092172
commit ed31d3c5d7
1 changed files with 89 additions and 1 deletions

View File

@ -1,4 +1,4 @@
// 记忆层测试:两层记忆的读写与隔离、派发注入、成果自动记账。
// 记忆层测试:三层记忆的读写与隔离、派发注入、成果自动记账。
// 用真实 WorkbenchStore(内存账本)+ 临时目录里的真实记忆文件,不 mock 存储层。
import test from 'node:test'
@ -145,3 +145,91 @@ test('result report journals into project memory once, duplicates do not re-jour
assert.equal(text.split('已完成测算').length - 1, 1, '重复汇报只记一行')
} finally { cleanup(world) }
})
// ---------- 部门记忆(第三层) ----------
test('memory store: department memories are per-role, isolated, and validated', () => {
const world = makeWorld()
try {
const { memory } = world
assert.deepEqual(memory.readDepartment('gongbu'), { text: '', exists: false })
memory.appendDepartment('gongbu', '工部经验:先量尺寸再下料')
assert.ok(memory.readDepartment('gongbu').text.includes('先量尺寸再下料'))
assert.equal(memory.readDepartment('xingbu').exists, false, '别的部门读不到工部记忆')
// 文件落在 departments/<roleId>.md,就是普通 Markdown
assert.equal(readFileSync(memory.departmentPath('gongbu'), 'utf8'), memory.readDepartment('gongbu').text)
assert.throws(() => memory.readDepartment('not-a-role'), /不合法/)
assert.throws(() => memory.appendDepartment('../evil', 'x'), /不合法/)
} finally { cleanup(world) }
})
test('memory store: excerpts put department first when a role is given', () => {
const world = makeWorld()
try {
const { memory, project } = world
memory.writeDepartment('libu', '吏部经验:名册先行')
memory.writeProject(project.id, '项目背景')
memory.writeLongTerm('长期偏好')
const withRole = memory.excerptsFor(project.id, 'libu')
assert.equal(withRole.length, 3)
assert.ok(withRole[0].startsWith('「部门记忆」'))
assert.ok(withRole[1].startsWith('「项目记忆」'))
assert.ok(withRole[2].startsWith('「长期记忆」'))
const withoutRole = memory.excerptsFor(project.id)
assert.equal(withoutRole.length, 2, '不给部门就没有部门层(旧行为不变)')
const bogusRole = memory.excerptsFor(project.id, 'not-a-role')
assert.equal(bogusRole.length, 2, '非法部门跳过部门层,不抛错')
} finally { cleanup(world) }
})
test('service memory API: department scope roundtrip and role validation', () => {
const world = makeWorld()
try {
const { service } = world
service.putMemory({ scope: 'department', roleId: 'hubu', text: '户部全局约定' })
assert.equal(service.getMemory({ scope: 'department', roleId: 'hubu' }).text, '户部全局约定')
service.appendMemory({ scope: 'department', roleId: 'hubu', text: '追加一条' })
assert.ok(service.getMemory({ scope: 'department', roleId: 'hubu' }).text.includes('追加一条'))
assert.throws(() => service.getMemory({ scope: 'department', roleId: 'nobody' }), (e) => e.code === 'INVALID_ROLE')
assert.throws(() => service.putMemory({ scope: 'department', text: 'x' }), (e) => e.code === 'INVALID_ROLE')
} finally { cleanup(world) }
})
test('dispatch injects only the owner department memory', () => {
const world = makeWorld()
try {
const { service, project } = world
service.putMemory({ scope: 'department', roleId: 'libu', text: '吏部经验:先建名册' })
service.putMemory({ scope: 'department', roleId: 'gongbu', text: '工部经验:先量尺寸' })
const task = service.createTask({ projectId: project.id, title: '丙', objective: '丙目标', ownerRoleId: 'libu' }).task
const dispatched = service.dispatchTask({ projectId: project.id, taskId: task.id, idempotencyKey: 'idem-mem-3' })
const excerpts = dispatched.contextPacket.excerpts
const dept = excerpts.find((line) => line.startsWith('「部门记忆」'))
assert.ok(dept !== undefined && dept.includes('先建名册'), '带上主责部门自己的记忆')
assert.ok(!excerpts.some((line) => line.includes('先量尺寸')), '别的部门的记忆不注入')
} finally { cleanup(world) }
})
test('result report also journals into the owner department memory, once', () => {
const world = makeWorld()
try {
const { service, memory, project } = world
const task = service.createTask({ projectId: project.id, title: '丁', objective: '丁目标', ownerRoleId: 'libu' }).task
const dispatched = service.dispatchTask({ projectId: project.id, taskId: task.id, idempotencyKey: 'idem-mem-4' })
service.startAttempt({ projectId: project.id, taskId: task.id, attemptId: dispatched.attempt.id, hostRunId: 'run-9' })
const input = {
projectId: project.id,
taskId: task.id,
attemptId: dispatched.attempt.id,
report: { summary: '名册已建好' },
runTerminal: 'succeeded',
}
service.reportTaskResult(input)
service.reportTaskResult(input)
const text = memory.readDepartment('libu').text
assert.ok(text.includes('在项目「项目一」汇报成果'), '部门记忆里带项目名,跨项目时知道出处')
assert.ok(text.includes('名册已建好'))
assert.equal(text.split('名册已建好').length - 1, 1, '重复汇报只记一行')
assert.equal(memory.readDepartment('gongbu').exists, false, '没干活的部门不记账')
} finally { cleanup(world) }
})