import assert from "node:assert/strict"; import test from "node:test"; const { registerWebMcpTools, createWebMcpRegistrar } = await import("../src/lib/webmcp.ts"); const { archieWebMcpTools } = await import("../src/lib/webmcp-tools.ts"); interface FakeRegistered { name: string; description: string; inputSchema: unknown; annotations: Record | undefined; signal: AbortSignal | undefined; execute: (input: Record) => unknown; } function installModelContext(): { registered: FakeRegistered[] } { const registered: FakeRegistered[] = []; Object.defineProperty(globalThis, "document", { configurable: true, value: { modelContext: { registerTool: async ( tool: Omit, options?: { signal?: AbortSignal }, ) => { registered.push({ ...tool, signal: options?.signal }); }, }, }, }); return { registered }; } function installNoModelContext(): void { Object.defineProperty(globalThis, "document", { configurable: true, value: {}, }); } function fakeClient(): Parameters[0] { const reply = (method: string, args: unknown[]) => ({ ok: true, method, args }); return { health: async () => reply("health", []), captures: async (limit?: number) => reply("captures", [limit]), }; } test("registration no-ops without document.modelContext instead of throwing", async () => { installNoModelContext(); const registration = await registerWebMcpTools( archieWebMcpTools(fakeClient()), ); assert.equal(registration.registered, false); assert.doesNotThrow(() => registration.dispose()); }); test("registration passes each tool's schema and annotations, and disposes via the signal", async () => { const { registered } = installModelContext(); const tools = archieWebMcpTools(fakeClient()); const registration = await registerWebMcpTools(tools); assert.equal(registration.registered, true); assert.deepEqual( registered.map((tool) => tool.name), tools.map((tool) => tool.name), ); for (const tool of registered) { assert.ok(tool.description.length > 0, `${tool.name} has no description`); assert.equal(typeof tool.inputSchema, "object"); assert.ok( tool.signal instanceof AbortSignal, `${tool.name} has no abort signal`, ); assert.equal(tool.signal.aborted, false); } registration.dispose(); for (const tool of registered) { assert.equal( tool.signal?.aborted, true, `${tool.name} was not unregistered`, ); } }); test("every tool is read-only", () => { const annotations = new Map( archieWebMcpTools(fakeClient()).map((tool) => [ tool.name, tool.annotations, ]), ); for (const name of ["recent_events", "daemon_health"]) { assert.equal( annotations.get(name)?.readOnlyHint, true, `${name} should be read-only`, ); assert.notEqual( annotations.get(name)?.consequentialHint, true, `${name} must not be consequential`, ); } }); test("tools returning agent, operator or inbound content carry untrustedContentHint", () => { const annotations = new Map( archieWebMcpTools(fakeClient()).map((tool) => [ tool.name, tool.annotations, ]), ); for (const name of ["recent_events"]) { assert.equal( annotations.get(name)?.untrustedContentHint, true, `${name} returns untrusted content`, ); } assert.equal(annotations.get("daemon_health")?.untrustedContentHint, false); }); test("re-registering replaces the previous registration", async () => { const { registered } = installModelContext(); const register = createWebMcpRegistrar( archieWebMcpTools(fakeClient()), ); const count = archieWebMcpTools(fakeClient()).length; const first = await register(); const second = await register(); assert.notEqual(first, second); assert.equal(registered.length, count * 2); for (const tool of registered.slice(0, count)) { assert.equal( tool.signal?.aborted, true, "the first registration was left live", ); } });