| 1 | import assert from "node:assert/strict"; |
| 2 | import { readFileSync } from "node:fs"; |
| 3 | import { test } from "node:test"; |
| 4 | |
| 5 | import { FOLIO_RPC, folioHandler } from "./rpc.ts"; |
| 6 | |
| 7 | /** FOLIO_RPC_METHODS as the contract lists them (read from the source: Node can't load the contracts package itself). */ |
| 8 | function contractMethods(): string[] { |
| 9 | const source = readFileSync(new URL("../../../../packages/contracts/src/folios.ts", import.meta.url), "utf8"); |
| 10 | const block = source.slice(source.indexOf("export const FOLIO_RPC_METHODS = ["), source.indexOf("] as const;")); |
| 11 | return [...block.matchAll(/^\s*"([a-z_]+)",/gm)].map((m) => m[1]!); |
| 12 | } |
| 13 | |
| 14 | test("every folio RPC method in the contract is answered, and nothing else", () => { |
| 15 | const methods = contractMethods(); |
| 16 | assert.ok(methods.length >= 40); |
| 17 | assert.deepEqual(Object.keys(FOLIO_RPC).sort(), [...methods].sort()); |
| 18 | for (const m of methods) assert.equal(typeof folioHandler(m), "function", m); |
| 19 | }); |
| 20 | |
| 21 | test("Docs' own methods fall through to the legacy switch", () => { |
| 22 | for (const m of ["sidebar", "page", "create_page", "recall_for_agent", "toString", "__proto__", "constructor"]) assert.equal(folioHandler(m), null, m); |
| 23 | }); |