114 lines
5.8 KiB
JavaScript
114 lines
5.8 KiB
JavaScript
// Read-only Mnemon bridge verification: parameter mapping, envelopes, error
|
|
// paths and the absence of write routes. Runs against the built lib/ output.
|
|
import http from 'node:http'
|
|
import { createCoreRoutes } from './lib/routes.js'
|
|
|
|
const checks = []
|
|
function check(name, ok, detail = '') {
|
|
checks.push({ name, ok })
|
|
console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === '' ? '' : ` :: ${detail}`}`)
|
|
}
|
|
|
|
const calls = []
|
|
const bridge = {
|
|
read: async (endpoint, payload) => {
|
|
calls.push({ endpoint, payload })
|
|
return { ok: true, value: { endpoint, payload } }
|
|
},
|
|
workspaceIdForPath: (path) => ({ 'C:/x/proj': 'ws-1', 'C:/x/other project': 'ws-2' })[path],
|
|
}
|
|
|
|
function serverFor(mnemon) {
|
|
const route = createCoreRoutes({ version: 'test', ...(mnemon === undefined ? {} : { mnemon }) })
|
|
.find((item) => item.path === '/dsh-brain/mnemon')
|
|
if (route === undefined) throw new Error('mnemon route is not registered')
|
|
const server = http.createServer((req, res) => route.handler(req, res))
|
|
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve(server)))
|
|
}
|
|
|
|
async function call(server, path, method = 'GET') {
|
|
const response = await fetch(`http://127.0.0.1:${server.address().port}${path}`, { method })
|
|
return { status: response.status, body: await response.json() }
|
|
}
|
|
|
|
const server = await serverFor(() => bridge)
|
|
|
|
const list = await call(server, '/dsh-brain/mnemon/list?limit=5&query=abc&category=rule&memoryBodyIds=a,b&memoryBodyIds=c&workspaceId=ws-1&sessionId=s1')
|
|
check('list maps query parameters onto Mnemon payload', list.status === 200
|
|
&& calls.at(-1).endpoint === 'list'
|
|
&& JSON.stringify(calls.at(-1).payload) === JSON.stringify({ workspaceId: 'ws-1', sessionId: 's1', limit: 5, query: 'abc', category: 'rule', memoryBodyIds: ['a', 'b', 'c'] }))
|
|
check('list preserves the Mnemon result envelope', list.body.ok === true && list.body.value.endpoint === 'list')
|
|
|
|
await call(server, '/dsh-brain/mnemon/runtime')
|
|
check('runtime maps to runtime-memory', calls.at(-1).endpoint === 'runtime-memory')
|
|
check('runtime sends only the optional scope', JSON.stringify(calls.at(-1).payload) === '{}')
|
|
|
|
await call(server, '/dsh-brain/mnemon/status?workspace=C:/x/proj')
|
|
check('status resolves a workspace path to a workspace id', calls.at(-1).endpoint === 'status'
|
|
&& JSON.stringify(calls.at(-1).payload) === JSON.stringify({ workspaceId: 'ws-1' }))
|
|
|
|
await call(server, '/dsh-brain/mnemon/search?q=docker&limit=3&mode=keyword&source=user')
|
|
check('search maps q/limit/mode/source', calls.at(-1).endpoint === 'search'
|
|
&& JSON.stringify(calls.at(-1).payload) === JSON.stringify({ query: 'docker', limit: 3, mode: 'keyword', source: 'user' }))
|
|
|
|
await call(server, '/dsh-brain/mnemon/documents')
|
|
check('documents is exposed read-only', calls.at(-1).endpoint === 'documents')
|
|
|
|
const projectEndpoints = ['list', 'status', 'documents']
|
|
const project = new URLSearchParams({ scope: 'project', workspace: 'C:/x/other project' })
|
|
for (const endpoint of projectEndpoints) {
|
|
const query = new URLSearchParams(project)
|
|
if (endpoint === 'list') query.set('limit', '1000')
|
|
const scoped = await call(server, '/dsh-brain/mnemon/' + endpoint + '?' + query)
|
|
const payload = endpoint === 'list' ? { workspaceId: 'ws-2', limit: 1000 } : { workspaceId: 'ws-2' }
|
|
check('project ' + endpoint + ' reads the selected workspace', scoped.status === 200
|
|
&& scoped.body.ok === true && scoped.body.value.endpoint === endpoint
|
|
&& JSON.stringify(scoped.body.value.payload) === JSON.stringify(payload))
|
|
}
|
|
|
|
const beforeInvalidProjectReads = calls.length
|
|
for (const endpoint of projectEndpoints) {
|
|
const invalidQueries = ['scope=project', 'scope=project&workspace=%20', 'scope=project&workspace=C:/x/missing&sessionId=stale']
|
|
const invalid = await Promise.all(invalidQueries.map((query) => call(server, '/dsh-brain/mnemon/' + endpoint + '?' + query)))
|
|
check('project ' + endpoint + ' requires a resolved workspace', invalid.every((result) =>
|
|
result.status === 400 && result.body.error === 'mnemon-project-workspace-required'))
|
|
}
|
|
check('invalid project requests never reach Mnemon', calls.length === beforeInvalidProjectReads)
|
|
|
|
const noQuery = await call(server, '/dsh-brain/mnemon/search')
|
|
check('search without a query is 400', noQuery.status === 400 && noQuery.body.error === 'query-required')
|
|
|
|
const unknown = await call(server, '/dsh-brain/mnemon/forget')
|
|
check('write-like endpoints are 404 (no write route)', unknown.status === 404 && unknown.body.error === 'unknown-mnemon-route')
|
|
|
|
const post = await call(server, '/dsh-brain/mnemon/list', 'POST')
|
|
check('POST is rejected', post.status === 405)
|
|
|
|
const failing = await serverFor(() => ({
|
|
read: async () => ({ ok: false, error: { code: 'internal', message: 'boom', details: {} } }),
|
|
workspaceIdForPath: () => undefined,
|
|
}))
|
|
const failed = await call(failing, '/dsh-brain/mnemon/list')
|
|
check('Mnemon failure keeps its error envelope with 502', failed.status === 502
|
|
&& failed.body.ok === false && failed.body.error.message === 'boom')
|
|
failing.close()
|
|
|
|
const throwing = await serverFor(() => ({
|
|
read: async () => { throw new Error('transport down') },
|
|
workspaceIdForPath: () => undefined,
|
|
}))
|
|
const thrown = await call(throwing, '/dsh-brain/mnemon/list')
|
|
check('thrown read becomes a 502 envelope', thrown.status === 502
|
|
&& thrown.body.error.code === 'mnemon-error' && thrown.body.error.message === 'transport down')
|
|
throwing.close()
|
|
|
|
const absent = await serverFor(undefined)
|
|
const unavailable = await call(absent, '/dsh-brain/mnemon/list')
|
|
check('missing Mnemon answers 503 instead of failing activation', unavailable.status === 503
|
|
&& unavailable.body.error.code === 'mnemon-unavailable')
|
|
absent.close()
|
|
server.close()
|
|
|
|
const failedChecks = checks.filter((item) => !item.ok)
|
|
console.log(`\n${checks.length - failedChecks.length}/${checks.length} checks passed`)
|
|
if (failedChecks.length > 0) process.exit(1) |