60 lines
3.3 KiB
JavaScript
60 lines
3.3 KiB
JavaScript
// Generated, do not edit. sha256:3544f71e587319b418c7c4e6e7f8a5b1bdf589d2361fe0cbc903cf18ed737ac8
|
|||
|
|
import { describe, it } from 'node:test';
|
||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { mkdtemp } from 'node:fs/promises';
|
||
|
|
import { tmpdir } from 'node:os';
|
||
|
|
import { join } from 'node:path';
|
||
|
|
|
||
|
|
process.env.ASSETAPP_DATA_DIR = await mkdtemp(join(tmpdir(), 'assetapp-registers-'));
|
||
|
|
const { apiRoutes, dataModules } = await import('../../server/registers.mjs');
|
||
|
|
const { handleApi } = await import('../../server/api.mjs');
|
||
|
|
const { surface, isAllowed } = await import('../../server/data-api.mjs');
|
||
|
|
const { mcpTools } = await import('../../server/registers.mjs');
|
||
|
|
const { TOOLS, allTools } = await import('../../server/mcp/tools.mjs');
|
||
|
|
|
||
|
|
describe('de router en het oppervlak lezen het register', () => {
|
||
|
|
function verzoek(method, body) {
|
||
|
|
const handlers = {};
|
||
|
|
const req = {
|
||
|
|
method, socket: { remoteAddress: '127.0.0.1' },
|
||
|
|
on(ev, fn) { handlers[ev] = fn; if (ev === 'end') queueMicrotask(() => { if (body) handlers.data?.(Buffer.from(JSON.stringify(body))); fn(); }); return req; },
|
||
|
|
destroy() {},
|
||
|
|
};
|
||
|
|
const res = { status: null, body: null, writeHead(s) { this.status = s; }, end(t) { this.body = JSON.parse(t); } };
|
||
|
|
return { req, res };
|
||
|
|
}
|
||
|
|
|
||
|
|
it('een geregistreerde route antwoordt, met de body erbij, en een fout krijgt zijn eigen status', async () => {
|
||
|
|
apiRoutes.register({ id: 't.ok', method: 'POST', path: '/api/test/ok', run: ({ body }) => ({ echo: body.x }) });
|
||
|
|
apiRoutes.register({ id: 't.fout', method: 'POST', path: '/api/test/fout', errorStatus: 502, run: () => { throw new Error('weg'); } });
|
||
|
|
const ok = verzoek('POST', { x: 7 });
|
||
|
|
assert.equal(await handleApi(ok.req, ok.res, '/api/test/ok'), true);
|
||
|
|
assert.deepEqual([ok.res.status, ok.res.body], [200, { echo: 7 }]);
|
||
|
|
const fout = verzoek('POST', {});
|
||
|
|
await handleApi(fout.req, fout.res, '/api/test/fout');
|
||
|
|
assert.equal(fout.res.status, 502);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('een andere methode of een onbekend pad is geen route', async () => {
|
||
|
|
const get = verzoek('GET');
|
||
|
|
assert.equal(await handleApi(get.req, get.res, '/api/test/ok'), false);
|
||
|
|
const onbekend = verzoek('POST', {});
|
||
|
|
assert.equal(await handleApi(onbekend.req, onbekend.res, '/api/test/bestaat-niet'), false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('een MCP-tool die zich aanmeldt, staat achter de kerntools; een tool met een kernnaam faalt', () => {
|
||
|
|
const tool = { name: 'test_tool', title: 't', description: 't', scope: 'none', inputSchema: { type: 'object' }, run: async () => ({ text: '', fieldsShared: [] }) };
|
||
|
|
assert.throws(() => mcpTools.register({ id: 'andere_naam', tool }), /carries the name/);
|
||
|
|
mcpTools.register({ id: 'test_tool', tool });
|
||
|
|
assert.deepEqual(allTools().map(t => t.name), [...TOOLS.map(t => t.name), 'test_tool']);
|
||
|
|
mcpTools.register({ id: 'connection_test', tool: { ...tool, name: 'connection_test' } });
|
||
|
|
assert.throws(() => allTools(), /also a core tool/);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('een datamodule die zich NA het laden van data-api aanmeldt, staat toch in het oppervlak', () => {
|
||
|
|
assert.equal(isAllowed('test-laat', 'doe'), false);
|
||
|
|
dataModules.register({ id: 'test-laat', module: { doe: () => 1, NIET: 2 } });
|
||
|
|
assert.equal(isAllowed('test-laat', 'doe'), true);
|
||
|
|
assert.deepEqual(surface()['test-laat'], ['doe'], 'alleen functies, geen constanten');
|
||
|
|
});
|
||
|
|
});
|