Files
TREK/server/tests/unit/nest/mcp-addon-gate.test.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

61 lines
1.8 KiB
TypeScript

import { ADDON_IDS } from '../../../src/addons';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import { createMcpAddonGate } from '../../../src/nest/addons/mcp-addon-gate';
import { describe, it, expect, vi } from 'vitest';
function gate(enabled: boolean) {
const addons = { isAddonEnabled: vi.fn(() => enabled) };
return { addons, mw: createMcpAddonGate(addons as unknown as AddonsService) };
}
function makeRes() {
const res = {
statusCode: 200,
ended: false,
status: vi.fn(function (this: typeof res, c: number) {
this.statusCode = c;
return this;
}),
end: vi.fn(function (this: typeof res) {
this.ended = true;
return this;
}),
};
return res;
}
/**
* createMcpAddonGate decides inside an async IIFE (Express cannot await a
* middleware), so the 404 or the next() lands a microtask after mw() returns.
* Drain the microtask queue — no timer is involved, so there is nothing to sleep
* for.
*/
async function flushMicrotasks(): Promise<void> {
for (let i = 0; i < 10; i++) await Promise.resolve();
}
describe('createMcpAddonGate', () => {
it('GATE-001: 404s with an empty body when the MCP addon is off', async () => {
const { addons, mw } = gate(false);
const res = makeRes();
const next = vi.fn();
mw({} as never, res as never, next);
await flushMicrotasks();
expect(addons.isAddonEnabled).toHaveBeenCalledWith(ADDON_IDS.MCP);
expect(res.statusCode).toBe(404);
expect(res.ended).toBe(true);
expect(next).not.toHaveBeenCalled();
});
it('GATE-002: continues when the addon is on', async () => {
const { mw } = gate(true);
const res = makeRes();
const next = vi.fn();
mw({} as never, res as never, next);
await flushMicrotasks();
expect(res.statusCode).toBe(200);
expect(next).toHaveBeenCalled();
});
});