|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { compileDocumentWithCache } from "../src/compile-cache.js"; |
| 7 | +import { loadOpenApiDocument } from "../src/openapi.js"; |
| 8 | + |
| 9 | +test("compileDocumentWithCache keys cache off spec contents, not mutated in-memory docs", async (t) => { |
| 10 | + const dir = await mkdtemp(join(tmpdir(), "mcp-openapi-cache-")); |
| 11 | + t.after(async () => { |
| 12 | + await rm(dir, { recursive: true, force: true }); |
| 13 | + }); |
| 14 | + |
| 15 | + const specPath = join(dir, "spec.json"); |
| 16 | + const cachePath = join(dir, "compile-cache.json"); |
| 17 | + const spec = { |
| 18 | + openapi: "3.0.3", |
| 19 | + info: { title: "Cache test", version: "1.0.0" }, |
| 20 | + servers: [{ url: "https://api.example.com" }], |
| 21 | + paths: { |
| 22 | + "/widgets": { |
| 23 | + get: { |
| 24 | + operationId: "listWidgets", |
| 25 | + responses: { |
| 26 | + "200": { description: "ok" } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + await writeFile(specPath, JSON.stringify(spec), "utf8"); |
| 34 | + |
| 35 | + const doc = await loadOpenApiDocument(specPath); |
| 36 | + const first = await compileDocumentWithCache(doc, specPath, undefined, cachePath); |
| 37 | + assert.deepEqual([...first.keys()], ["listWidgets"]); |
| 38 | + |
| 39 | + const mutatedDoc = structuredClone(doc); |
| 40 | + delete ((mutatedDoc.paths as Record<string, unknown>)["/widgets"] as Record<string, unknown>).get; |
| 41 | + |
| 42 | + const second = await compileDocumentWithCache(mutatedDoc, specPath, undefined, cachePath); |
| 43 | + assert.deepEqual([...second.keys()], ["listWidgets"]); |
| 44 | +}); |
| 45 | + |
| 46 | +test("compileDocumentWithCache invalidates when spec contents change", async (t) => { |
| 47 | + const dir = await mkdtemp(join(tmpdir(), "mcp-openapi-cache-")); |
| 48 | + t.after(async () => { |
| 49 | + await rm(dir, { recursive: true, force: true }); |
| 50 | + }); |
| 51 | + |
| 52 | + const specPath = join(dir, "spec.json"); |
| 53 | + const cachePath = join(dir, "compile-cache.json"); |
| 54 | + const initialSpec = { |
| 55 | + openapi: "3.0.3", |
| 56 | + info: { title: "Cache test", version: "1.0.0" }, |
| 57 | + servers: [{ url: "https://api.example.com" }], |
| 58 | + paths: { |
| 59 | + "/widgets": { |
| 60 | + get: { |
| 61 | + operationId: "listWidgets", |
| 62 | + responses: { |
| 63 | + "200": { description: "ok" } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + await writeFile(specPath, JSON.stringify(initialSpec), "utf8"); |
| 71 | + |
| 72 | + const firstDoc = await loadOpenApiDocument(specPath); |
| 73 | + const first = await compileDocumentWithCache(firstDoc, specPath, undefined, cachePath); |
| 74 | + assert.deepEqual([...first.keys()], ["listWidgets"]); |
| 75 | + |
| 76 | + const updatedSpec = { |
| 77 | + ...initialSpec, |
| 78 | + paths: { |
| 79 | + "/gadgets": { |
| 80 | + post: { |
| 81 | + operationId: "createGadget", |
| 82 | + responses: { |
| 83 | + "201": { description: "created" } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + }; |
| 89 | + await writeFile(specPath, JSON.stringify(updatedSpec), "utf8"); |
| 90 | + |
| 91 | + const secondDoc = await loadOpenApiDocument(specPath); |
| 92 | + const second = await compileDocumentWithCache(secondDoc, specPath, undefined, cachePath); |
| 93 | + assert.deepEqual([...second.keys()], ["createGadget"]); |
| 94 | +}); |
0 commit comments