dsh-brain/verify-local-memory-round5.mjs

38 lines
3.4 KiB
JavaScript

// Fifth-round migration recovery regressions.
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'brain-r5-'))
process.env.DSH_HOME = join(root, 'home')
const project = join(root, 'project'); mkdirSync(project, { recursive: true })
const results = []
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
const { LocalMemoryManager } = await import('./lib/local-memory/index.js')
const settings = () => ({ localMemoryEnabled: true, cloudMemoryEnabled: false, autoMemorySpaces: true, autoSummaryEnabled: true, autoProfileEnabled: true, localMemoryK: 5 })
const manager = new LocalMemoryManager({ cwd: () => project, settings })
await manager.ensure()
const legacy = join(project, '.mnemon-r5'); mkdirSync(legacy, { recursive: true }); writeFileSync(join(legacy, 'memories.json'), JSON.stringify({ records: [{ id: 'r5-1', content: 'r5 durable record', category: 'project' }] }))
const first = await manager.withWorkspace(project, () => manager.migrateLegacy(legacy, project))
const one = await manager.withWorkspace(project, () => manager.search('r5 durable', 5, project))
check('first import is active', first.imported === 1 && one.length === 1)
// final copy failure: destination exists and has a conflicting child type
const destination = join(root, 'copy-conflict'); mkdirSync(destination, { recursive: true }); mkdirSync(join(destination, 'memories.json'), { recursive: true })
let copyError = ''
try { await manager.withWorkspace(project, () => manager.rollbackMigration(first.copiedTo, destination)) } catch (error) { copyError = String(error) }
check('final copy failure is rejected before active mutation', copyError !== '' && (await manager.withWorkspace(project, () => manager.search('r5 durable', 5, project))).length === 1, copyError.slice(0, 80))
rmSync(destination, { recursive: true, force: true })
const good = await manager.withWorkspace(project, () => manager.rollbackMigration(first.copiedTo))
const empty = await manager.withWorkspace(project, () => manager.search('r5 durable', 5, project))
check('successful rollback removes active import and creates restore copy', good.removedImported === 1 && empty.length === 0 && existsSync(join(destination, 'memories.json')))
const again = await manager.withWorkspace(project, () => manager.migrateLegacy(legacy, project))
const restored = await manager.withWorkspace(project, () => manager.search('r5 durable', 5, project))
check('migration after successful rollback restores exactly one active record', again.imported === 1 && restored.length === 1)
const repeat = await manager.withWorkspace(project, () => manager.migrateLegacy(legacy, project))
check('retrying migration after restore is idempotent', repeat.imported === 0 && repeat.duplicates === 1 && (await manager.withWorkspace(project, () => manager.search('r5 durable', 5, project))).length === 1)
check('original legacy data is untouched', readFileSync(join(legacy, 'memories.json'), 'utf8').includes('r5 durable record'))
const failed = results.filter((result) => !result.ok)
console.log('')
console.log(failed.length ? 'FAILED ' + failed.length + '/' + results.length + ': ' + failed.map((result) => result.name).join(', ') : 'ALL ' + results.length + ' CHECKS PASSED')
rmSync(root, { recursive: true, force: true })
process.exit(failed.length ? 1 : 0)