dsh-brain/verify-plugin-entry.mjs

200 lines
7.6 KiB
JavaScript

// Entry-point smoke test: a plugin that throws from `apply` takes the whole
// web boot down, so the cheapest insurance is to activate the real entries
// against a fake host context and check the route/tool tables they register.
//
// Deliberately not a unit test of every timer: it proves `apply` runs, wires
// the expected routes, tears down through the disposers it returned, and —
// since the five-row split — that each sub-plugin entry registers exactly its
// own slice and that /status reflects which siblings are enabled.
import { mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
process.env.DSH_HOME = mkdtempSync(join(tmpdir(), 'brain-entry-'))
const results = []
function check(name, ok, detail) {
results.push({ name, ok })
console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === undefined ? '' : ` :: ${detail}`}`)
}
const noop = () => {}
const quietLogger = { info: noop, warn: noop }
/** A fresh host-shaped context recording its own routes and tools. */
function fakeCtx() {
const routes = []
const tools = []
const disposers = []
return {
routes,
tools,
disposers,
ctx: {
logger: () => quietLogger,
effect: (fn) => {
const result = fn()
if (typeof result === 'function') disposers.push(result)
return () => {}
},
on: () => () => {},
inject: (deps, fn) => {
fn({
tools: {
register: (tool) => {
tools.push(tool)
return () => {}
},
},
effect: (inner) => {
inner()
return () => {}
},
})
},
webServer: {
register: (route) => {
routes.push(route)
return () => {}
},
},
},
}
}
const { resetBrainKernel, brainKernel } = await import('./lib/kernel.js')
const cloudsyncEntry = await import('./lib/entries/cloudsync.js')
const memoryEntry = await import('./lib/entries/memory.js')
const skillsEntry = await import('./lib/entries/skills.js')
const mcpEntry = await import('./lib/entries/mcp.js')
const settingsUiEntry = await import('./lib/entries/settings-ui.js')
const { buildStatusPayload } = await import('./lib/routes.js')
const root = await import('./lib/index.js')
const SEED = { baseUrl: 'https://your-memory-host/api/longmemory', clientId: 'smoke' }
// --- per-entry slice checks -------------------------------------------------
resetBrainKernel()
let ui = fakeCtx()
settingsUiEntry.apply(ui.ctx, SEED)
const uiPaths = ui.routes.map((route) => `${route.kind}:${route.path}`)
check('settings-ui entry applies and registers the page + core routes', [
'exact:/dsh-brain',
'exact:/dsh-brain/status',
'exact:/dsh-brain/capabilities',
'exact:/dsh-brain/config',
'prefix:/dsh-brain/migrate',
'exact:/cloud-memory',
'exact:/cloud-memory/status',
'exact:/cloud-memory/capabilities',
'exact:/cloud-memory/config',
'prefix:/cloud-memory/migrate',
].every((item) => uiPaths.includes(item)), uiPaths.join(' | '))
check('settings-ui entry registers no tools', ui.tools.length === 0, `${ui.tools.length} tools`)
const mcpCtx = fakeCtx()
mcpEntry.apply(mcpCtx.ctx, SEED)
check('mcp entry applies and registers no routes and no tools',
mcpCtx.routes.length === 0 && mcpCtx.tools.length === 0,
`${mcpCtx.routes.length} routes, ${mcpCtx.tools.length} tools`)
const sync = fakeCtx()
cloudsyncEntry.apply(sync.ctx, SEED)
const syncPaths = sync.routes.map((route) => `${route.kind}:${route.path}`)
check('cloudsync entry applies and registers login/test/flush/sync routes', [
'exact:/dsh-brain/login',
'exact:/dsh-brain/test',
'exact:/dsh-brain/flush',
'prefix:/dsh-brain/sync',
'exact:/cloud-memory/login',
'exact:/cloud-memory/test',
'exact:/cloud-memory/flush',
'prefix:/cloud-memory/sync',
].every((item) => syncPaths.includes(item)), syncPaths.join(' | '))
check('cloudsync entry registers no tools', sync.tools.length === 0, `${sync.tools.length} tools`)
const mem = fakeCtx()
memoryEntry.apply(mem.ctx, SEED)
check('memory entry applies and registers the memory tools', mem.tools.length >= 5, `${mem.tools.length} tools`)
check('memory entry registers no routes', mem.routes.length === 0, `${mem.routes.length} routes`)
const skill = fakeCtx()
skillsEntry.apply(skill.ctx, SEED)
const skillPaths = skill.routes.map((route) => `${route.kind}:${route.path}`)
check('skills entry applies and registers skills/market/clients routes', [
'prefix:/dsh-brain/skills',
'prefix:/dsh-brain/market',
'prefix:/dsh-brain/clients',
'prefix:/cloud-memory/skills',
'prefix:/cloud-memory/market',
'prefix:/cloud-memory/clients',
].every((item) => skillPaths.includes(item)), skillPaths.join(' | '))
check('skills entry registers the skill tools', skill.tools.length >= 1, `${skill.tools.length} tools`)
// --- active-bundle gating ---------------------------------------------------
const kernel = brainKernel(SEED, quietLogger)
const withAll = buildStatusPayload(kernel.routeDeps())
check('status reports skills and sync with all five entries applied',
withAll.capabilities.features.skills === true
&& withAll.capabilities.features.skillsSync === true
&& withAll.capabilities.features.mcpBridge === 'host-plugin',
JSON.stringify(withAll.capabilities.features))
kernel.deactivate('skills')
const withoutSkills = buildStatusPayload(kernel.routeDeps())
check('deactivating the skills entry turns the skill capabilities off',
withoutSkills.capabilities.features.skills === false
&& withoutSkills.capabilities.features.skillInstallPlans === false
&& withoutSkills.capabilities.features.skillUpdate === false
&& withoutSkills.capabilities.features.multiClient === false,
JSON.stringify(withoutSkills.capabilities.features))
kernel.deactivate('mcp')
kernel.deactivate('cloudsync')
const withoutMcpOrSync = buildStatusPayload(kernel.routeDeps())
check('deactivating mcp and cloudsync gates the tab and sync flags',
withoutMcpOrSync.capabilities.features.mcpBridge === 'unavailable'
&& withoutMcpOrSync.capabilities.features.skillsSync === false
&& withoutMcpOrSync.capabilities.features.syncResolve === false,
JSON.stringify(withoutMcpOrSync.capabilities.features))
// --- compat root -------------------------------------------------------------
resetBrainKernel()
const rootHost = fakeCtx()
let applied = true
try {
root.apply(rootHost.ctx, SEED)
} catch (failure) {
applied = false
console.log('apply threw:', failure)
}
check('compat root apply() completes against a host-shaped context', applied)
const expected = [
'exact:/cloud-memory',
'exact:/cloud-memory/status',
'exact:/cloud-memory/capabilities',
'exact:/cloud-memory/config',
'exact:/cloud-memory/login',
'exact:/cloud-memory/test',
'exact:/cloud-memory/flush',
'prefix:/cloud-memory/skills',
'prefix:/cloud-memory/market',
'prefix:/cloud-memory/sync',
'prefix:/cloud-memory/clients',
'prefix:/cloud-memory/migrate',
]
const rootPaths = rootHost.routes.map((route) => `${route.kind}:${route.path}`)
check('root registers every route the card depends on (legacy aliases included)',
expected.every((item) => rootPaths.includes(item)), rootPaths.join(' | '))
check('root registers the modern /dsh-brain route table',
['exact:/dsh-brain', 'exact:/dsh-brain/status', 'prefix:/dsh-brain/skills', 'prefix:/dsh-brain/sync']
.every((item) => rootPaths.includes(item)))
check('the memory tools are still registered', rootHost.tools.length >= 3, `${rootHost.tools.length} tools`)
const failed = results.filter((item) => !item.ok)
console.log(failed.length === 0 ? 'ALL PASS' : `${failed.length} FAILURE(S)`)
process.exitCode = failed.length === 0 ? 0 : 1