dsh-brain/verify-mnemon-native.mjs

29 lines
2.3 KiB
JavaScript

// Verifies the local Mnemon provider's actual host registration and CRUD path.
import { mkdtempSync, mkdirSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'brain-native-r3-'))
process.env.DSH_HOME = join(root, 'home')
mkdirSync(root, { recursive: true })
const results = []
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
const mod = await import('./lib/mnemon/native.js')
check('native provider manifest has the vendored API version', mod.definition?.manifest?.apiVersion === 'dsh-brain-mnemon.memory-space-provider/v1', String(mod.definition?.manifest?.apiVersion))
let installed; mod.default.apply({}, { install: (_ctx, definition) => { installed = definition } })
check('native provider installs through the host contract', installed === mod.definition)
const authority = { runner: { effectiveDataDir: () => root }, list: () => [], providerConnection: () => ({}) }
const provider = mod.definition.create({ memorySpaces: authority, config: { timeoutMs: 1000 } })
const body = { id: 'native-space', name: 'Native', description: '', active: true, dbPath: join(root, 'workspace'), provider: { id: 'dsh-brain-native' }, createdAt: '', updatedAt: '' }
const stored = await provider.remember(body, { content: 'native provider memory' })
check('native provider writes through the local store', stored?.action === 'stored' && typeof stored.id === 'string', JSON.stringify(stored))
const searched = await provider.search(body, { query: 'native provider', limit: 5 })
check('native provider searches the same local store', searched.results.length === 1 && searched.results[0].content.includes('native provider'))
const got = await provider.get(body, stored.id)
check('native provider reads by id', got?.content === 'native provider memory')
const forgotten = await provider.forget(body, stored.id)
check('native provider forgets through a tombstone', forgotten?.action === 'deleted')
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')
process.exit(failed.length ? 1 : 0)