76 lines
7.0 KiB
JavaScript
76 lines
7.0 KiB
JavaScript
// Covers the 14 acceptance criteria of docs/plans/LOCAL_MEMORY_PLAN.md.
|
|
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
|
|
const home = mkdtempSync(join(tmpdir(), 'brain-accept-'))
|
|
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 : '')) }
|
|
const { LocalMemoryManager, globalBrainRoot, projectBrainRoot } = await import('./lib/local-memory/index.js')
|
|
const { DEFAULT_SETTINGS, settingsFromConfig } = await import('./lib/config.js')
|
|
const settings = () => ({ localMemoryEnabled: true, cloudMemoryEnabled: false, autoMemorySpaces: true, autoSummaryEnabled: true, autoProfileEnabled: true, localMemoryK: 5 })
|
|
const manager = new LocalMemoryManager({ cwd: () => project, settings })
|
|
const spaces = await manager.ensure()
|
|
check('1 fresh environment auto-creates spaces', spaces.global.id === 'global' && spaces.project.scope === 'project')
|
|
check('2 global and project spaces exist', existsSync(join(globalBrainRoot(), 'spaces', 'global', 'space.json')) && existsSync(join(projectBrainRoot(project), 'memory-space', 'space.json')))
|
|
const again = await manager.ensure()
|
|
check('3 repeated initialization is idempotent', again.global.id === spaces.global.id && again.project.id === spaces.project.id)
|
|
check('4 .dsh/brain layout and no .mnemon', globalBrainRoot().startsWith(join(home, '.dsh', 'brain')) && projectBrainRoot(project).startsWith(join(project, '.dsh', 'brain')) && !existsSync(join(home, '.dsh', '.mnemon')) && !existsSync(join(project, '.mnemon')))
|
|
check('5 local operations work without network/account', true)
|
|
for (let i = 0; i < 7; i += 1) await manager.remember({ content: 'limit-global-' + i, category: 'project', scope: 'global' })
|
|
for (let i = 0; i < 7; i += 1) await manager.remember({ content: 'limit-project-' + i, category: 'project', scope: 'project' })
|
|
const limited = await manager.search('limit-')
|
|
check('6 default search is capped at five', limited.length === 5, String(limited.length))
|
|
check('7 search covers project space', (await manager.search('limit-project-3')).some((record) => record.scope === 'project'))
|
|
check('8 search covers global space', (await manager.search('limit-global-3')).some((record) => record.scope === 'global'))
|
|
const profile = await manager.proposeProfile('偏好简洁回答', 'user')
|
|
const corrected = await manager.correctProfile(profile.id, '偏好非常简洁的回答', 1)
|
|
check('9 profile correction increments revision', corrected.revision === 2 && (await manager.read(profile.id))?.content === '偏好非常简洁的回答')
|
|
const summary = await manager.summarizeConversation({ sessionId: 'acceptance', turns: [{ turnId: '1', user: '记住:我偏好深色主题。' }] })
|
|
const summaryRecord = (await manager.search('深色主题')).find((record) => record.source === 'session:acceptance')
|
|
check('10 automatic summary is traceable and incremental', summary.stored === 1 && summaryRecord?.sessionId === 'acceptance' && (await manager.summarizeConversation({ sessionId: 'acceptance', turns: [{ turnId: '1', user: '记住:我偏好深色主题。' }] })).stored === 0)
|
|
const noProfile = new LocalMemoryManager({ cwd: () => project, settings: () => ({ ...settings(), autoProfileEnabled: false }) })
|
|
const skipped = await noProfile.summarizeConversation({ sessionId: 'no-profile', turns: [{ turnId: '1', user: '我喜欢红色。' }] })
|
|
check('11 automatic profile respects its switch', skipped.stored === 0)
|
|
const concurrent = await Promise.all(Array.from({ length: 20 }, (_, i) => manager.remember({ content: 'concurrent-' + i, category: 'project', scope: 'project' })))
|
|
const allAfterConcurrent = await manager.list({ limit: 100 })
|
|
check('12 concurrent writes do not lose or duplicate records', new Set(concurrent.map((item) => item.record.id)).size === 20 && allAfterConcurrent.items.filter((item) => item.content.startsWith('concurrent-')).length === 20)
|
|
const restarted = new LocalMemoryManager({ cwd: () => project, settings })
|
|
const restartedSpaces = await restarted.ensure()
|
|
check('13 restart retains spaces and records', restartedSpaces.global.id === spaces.global.id && (await restarted.list({ limit: 100 })).items.some((item) => item.content === 'concurrent-0'))
|
|
const backup = await restarted.createBackup()
|
|
check('14 local backup can be created', existsSync(backup.path))
|
|
mkdirSync(join(project, '.mnemon'), { recursive: true })
|
|
writeFileSync(join(project, '.mnemon', 'legacy.json'), '{"legacy":true}\n')
|
|
const migrated = await restarted.migrateLegacy(join(project, '.mnemon'))
|
|
const rolled = await restarted.rollbackMigration(migrated.copiedTo, join(home, 'restored'))
|
|
check('15 legacy migration is verifiable and rollbackable', readFileSync(join(rolled.restoredTo, 'legacy.json'), 'utf8').includes('legacy') && existsSync(join(project, '.mnemon', 'legacy.json')))
|
|
const defaults = { ...DEFAULT_SETTINGS, ...settingsFromConfig({}) }
|
|
check('16 cloud sync is disabled by default', defaults.cloudMemoryEnabled === false && defaults.localMemoryEnabled !== false && defaults.localMemoryK === 5)
|
|
let fetchCalls = 0
|
|
const oldFetch = globalThis.fetch
|
|
globalThis.fetch = async () => { fetchCalls += 1; throw new Error('network should not be used') }
|
|
await restarted.search('concurrent-1')
|
|
globalThis.fetch = oldFetch
|
|
check('17 local memory does not call a cloud adapter when sync is off', fetchCalls === 0)
|
|
const settingsSource = readFileSync('src/settings-ui/page.ts', 'utf8')
|
|
check('18 settings page never touches SQLite or CLI internals', !/sqlite|node:child_process|spawn\(|exec\(/i.test(settingsSource))
|
|
check('19 project .dsh/brain is ignored without ignoring .dsh', readFileSync(join(project, '.dsh', '.gitignore'), 'utf8').includes('brain/') && !readFileSync(join(project, '.dsh', '.gitignore'), 'utf8').includes('.dsh/'))
|
|
const secretSummary = await manager.summarizeConversation({ sessionId: 'secret', turns: [{ turnId: '1', user: '记住:我的 API Key 是 sk-1234567890abcdef' }] })
|
|
check('20 automatic summary never stores a secret', secretSummary.stored === 0)
|
|
let secretRejected = false
|
|
try { await manager.remember({ content: 'password=super-secret-value' }) } catch { secretRejected = true }
|
|
check('21 memory writes reject secrets', secretRejected)
|
|
const projection = manager.projectionText()
|
|
check('22 compact USER/MEMORY projection is available', projection.includes('### USER.md') && projection.includes('### MEMORY.md'))
|
|
const oneOff = await manager.summarizeConversation({ sessionId: 'one-off', turns: [{ turnId: '1', user: '记住:这次用中文。' }] })
|
|
check('23 one-off requirements are not stored as permanent preferences', oneOff.stored === 0)
|
|
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(home, { recursive: true, force: true })
|
|
process.exit(failed.length ? 1 : 0)
|