import { mkdtempSync, mkdirSync, existsSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' const home = mkdtempSync(join(tmpdir(), 'brain-departments-')) const project = join(home, 'project') process.env.DSH_HOME = join(home, '.dsh') mkdirSync(project, { recursive: true }) const results = [] function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) } async function throws(name, job, code) { try { await job(); check(name, false, 'expected ' + code) } catch (error) { check(name, error instanceof Error && error.message === code, error instanceof Error ? error.message : String(error)) } } const { LocalMemoryManager, DepartmentManager, globalBrainRoot, projectBrainRoot } = await import('./lib/local-memory/index.js') const settings = () => ({ localMemoryEnabled: true, cloudMemoryEnabled: false, autoMemorySpaces: true, autoSummaryEnabled: false, autoProfileEnabled: false, localMemoryK: 5 }) const memory = new LocalMemoryManager({ cwd: () => project, settings }) const departments = new DepartmentManager({ memory }) await memory.remember({ content: '通用 TypeScript 经验', category: 'fact', scope: 'global' }) await memory.remember({ content: '当前项目使用 pnpm', category: 'project', scope: 'project' }) const created = await departments.create({ name: '研发部门', role: '负责本地记忆与测试', skills: ['code-graph'], allowedTools: ['memory_search'] }) check('department creates its private memory space', created.created && created.department.scope === 'project' && existsSync(join(projectBrainRoot(project), 'departments', created.department.id, 'memory-space', 'space.json'))) check('department has role, skills, tools and memory policy', created.department.role.includes('本地记忆') && created.department.skills[0] === 'code-graph' && created.department.allowedTools[0] === 'memory_search' && created.department.memory.readGlobal && created.department.memory.readProject) const duplicateDepartment = await departments.create({ name: '研发部门', role: '不应覆盖原角色' }) check('same department name is idempotent', duplicateDepartment.created === false && duplicateDepartment.department.id === created.department.id && duplicateDepartment.department.role === created.department.role) const privateMemory = await departments.remember(created.department.id, { content: '部门私有经验:使用 revision', category: 'experience' }) check('department memory records ownership', privateMemory.record.scope === 'department' && privateMemory.record.departmentId === created.department.id && privateMemory.record.spaceId === created.department.spaceId) const duplicateMemory = await departments.remember(created.department.id, { content: '部门私有经验:使用 revision', category: 'experience' }) check('department memory deduplicates exactly', duplicateMemory.duplicate && duplicateMemory.record.id === privateMemory.record.id) check('recall includes private memory', (await departments.recall(created.department.id, 'revision')).some((hit) => hit.origin === 'department' && hit.record.id === privateMemory.record.id)) check('recall includes project memory', (await departments.recall(created.department.id, 'pnpm')).some((hit) => hit.origin === 'project')) check('recall includes global memory', (await departments.recall(created.department.id, 'TypeScript')).some((hit) => hit.origin === 'global')) check('combined recall respects one total limit', (await departments.recall(created.department.id, '', 2)).length === 2) const other = await departments.create({ name: '其他部门' }) check('private memory is invisible to another department', !(await departments.recall(other.department.id, 'revision')).some((hit) => hit.record.id === privateMemory.record.id)) const session = await departments.resumeSession(created.department.id) const attached = await departments.attachSession(created.department.id, 'dsh', 'host-session-123') check('session reference supports host attachment', session.reusable && attached.hostSessionId === 'host-session-123') const checkpoint = await departments.checkpoint(created.department.id, { currentTask: '实现部门记忆', handoff: '下次验证重启恢复' }) check('checkpoint stores task and handoff', checkpoint.currentTask === '实现部门记忆' && checkpoint.handoff === '下次验证重启恢复' && checkpoint.revision === 2) await departments.update(created.department.id, { readGlobal: false, readProject: false }) check('memory policy can disable shared spaces', (await departments.recall(created.department.id, 'TypeScript')).length === 0 && (await departments.recall(created.department.id, 'pnpm')).length === 0) check('private recall remains after shared spaces are disabled', (await departments.recall(created.department.id, 'revision')).some((hit) => hit.origin === 'department')) await throws('stale department update is rejected', () => departments.update(created.department.id, { name: '冲突部门' }, 1), 'revision_conflict') const restarted = new DepartmentManager({ memory: new LocalMemoryManager({ cwd: () => project, settings }) }) const recovered = await restarted.get(created.department.id) check('department registry survives restart', recovered?.role === created.department.role && recovered?.handoff === '下次验证重启恢复') const recoveredSession = await restarted.resumeSession(created.department.id) check('restart resumes the same session reference', recoveredSession.id === session.id && recoveredSession.hostSessionId === 'host-session-123') const globalDepartment = await restarted.create({ name: '通用部门', scope: 'global' }) check('global department uses the global .dsh/brain root', existsSync(join(globalBrainRoot(), 'departments', globalDepartment.department.id, 'memory-space', 'space.json'))) await restarted.disable(other.department.id) await throws('disabled department cannot resume', () => restarted.resumeSession(other.department.id), 'department_disabled') check('disable preserves private space without resurrection', existsSync(join(projectBrainRoot(project), 'departments', other.department.id, 'memory-space', 'space.json'))) check('no .mnemon directory is created', !existsSync(join(project, '.mnemon')) && !existsSync(join(home, '.dsh', '.mnemon'))) console.log('\n' + (results.every((r) => r.ok) ? 'ALL ' + results.length + ' CHECKS PASSED' : results.filter((r) => !r.ok).length + ' FAILED')) rmSync(home, { recursive: true, force: true }) process.exit(results.every((r) => r.ok) ? 0 : 1)