|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { MemoryAuditSink } from "../src/audit.js"; |
| 3 | +import { createServer } from "../src/server.js"; |
| 4 | +import type { McpRequest } from "../src/types.js"; |
| 5 | + |
| 6 | +const fetchOk = (body: unknown) => |
| 7 | + vi.fn( |
| 8 | + async () => |
| 9 | + new Response(JSON.stringify(body), { |
| 10 | + status: 200, |
| 11 | + headers: { "content-type": "application/fhir+json" }, |
| 12 | + }), |
| 13 | + ); |
| 14 | + |
| 15 | +const req = (params?: Record<string, unknown>): McpRequest => ({ |
| 16 | + jsonrpc: "2.0", |
| 17 | + id: 1, |
| 18 | + method: "tools/call", |
| 19 | + ...(params ? { params } : {}), |
| 20 | +}); |
| 21 | + |
| 22 | +describe("dryRun mode", () => { |
| 23 | + it("short-circuits writes without hitting upstream and returns an informational outcome", async () => { |
| 24 | + const fetchFn = fetchOk({}); |
| 25 | + const audit = new MemoryAuditSink(); |
| 26 | + const server = createServer({ |
| 27 | + baseUrl: "https://example.test/baseR4", |
| 28 | + resourceTypes: ["Patient"], |
| 29 | + writes: ["create"], |
| 30 | + dryRun: true, |
| 31 | + audit, |
| 32 | + fetch: fetchFn, |
| 33 | + }); |
| 34 | + |
| 35 | + const res = await server.dispatcher.handleRequest( |
| 36 | + req({ |
| 37 | + name: "fhir.create", |
| 38 | + arguments: { resourceType: "Patient", params: { resourceType: "Patient", name: [{ given: ["A"] }] } }, |
| 39 | + }), |
| 40 | + ); |
| 41 | + |
| 42 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 43 | + const result = res.result as { content: Array<{ text: string }>; isError: boolean }; |
| 44 | + expect(result.isError).toBe(false); |
| 45 | + const body = JSON.parse(result.content[0]!.text); |
| 46 | + expect(body.resourceType).toBe("OperationOutcome"); |
| 47 | + expect(body.issue[0].severity).toBe("information"); |
| 48 | + expect(body.issue[0].diagnostics).toContain("dry-run"); |
| 49 | + |
| 50 | + expect(audit.events[0]?.actor).toBe("dry-run"); |
| 51 | + expect(audit.events[0]?.result.ok).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it("does not affect read verbs", async () => { |
| 55 | + const fetchFn = fetchOk({ resourceType: "Patient", id: "abc" }); |
| 56 | + const server = createServer({ |
| 57 | + baseUrl: "https://example.test/baseR4", |
| 58 | + resourceTypes: ["Patient"], |
| 59 | + dryRun: true, |
| 60 | + audit: new MemoryAuditSink(), |
| 61 | + fetch: fetchFn, |
| 62 | + }); |
| 63 | + await server.dispatcher.handleRequest( |
| 64 | + req({ name: "fhir.read", arguments: { resourceType: "Patient", id: "abc" } }), |
| 65 | + ); |
| 66 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("writeResourceTypes allowlist", () => { |
| 71 | + it("rejects writes to resource types outside the allowlist", async () => { |
| 72 | + const fetchFn = fetchOk({}); |
| 73 | + const server = createServer({ |
| 74 | + baseUrl: "https://example.test/baseR4", |
| 75 | + resourceTypes: ["Patient", "Observation"], |
| 76 | + writes: ["create"], |
| 77 | + writeResourceTypes: ["Patient"], |
| 78 | + audit: new MemoryAuditSink(), |
| 79 | + fetch: fetchFn, |
| 80 | + }); |
| 81 | + |
| 82 | + const res = await server.dispatcher.handleRequest( |
| 83 | + req({ name: "fhir.create", arguments: { resourceType: "Observation", params: {} } }), |
| 84 | + ); |
| 85 | + |
| 86 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 87 | + const body = JSON.parse((res.result as { content: Array<{ text: string }> }).content[0]!.text); |
| 88 | + expect(body.issue[0].code).toBe("forbidden"); |
| 89 | + expect(body.issue[0].diagnostics).toContain("Observation"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("allows writes to resource types inside the allowlist", async () => { |
| 93 | + const fetchFn = fetchOk({ resourceType: "Patient", id: "new" }); |
| 94 | + const server = createServer({ |
| 95 | + baseUrl: "https://example.test/baseR4", |
| 96 | + resourceTypes: ["Patient", "Observation"], |
| 97 | + writes: ["create"], |
| 98 | + writeResourceTypes: ["Patient"], |
| 99 | + audit: new MemoryAuditSink(), |
| 100 | + fetch: fetchFn, |
| 101 | + }); |
| 102 | + |
| 103 | + await server.dispatcher.handleRequest( |
| 104 | + req({ name: "fhir.create", arguments: { resourceType: "Patient", params: {} } }), |
| 105 | + ); |
| 106 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("confirmWrites", () => { |
| 111 | + it("rejects writes without confirm: true", async () => { |
| 112 | + const fetchFn = fetchOk({}); |
| 113 | + const server = createServer({ |
| 114 | + baseUrl: "https://example.test/baseR4", |
| 115 | + resourceTypes: ["Patient"], |
| 116 | + writes: ["create"], |
| 117 | + confirmWrites: true, |
| 118 | + audit: new MemoryAuditSink(), |
| 119 | + fetch: fetchFn, |
| 120 | + }); |
| 121 | + |
| 122 | + const res = await server.dispatcher.handleRequest( |
| 123 | + req({ name: "fhir.create", arguments: { resourceType: "Patient", params: {} } }), |
| 124 | + ); |
| 125 | + |
| 126 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 127 | + const body = JSON.parse((res.result as { content: Array<{ text: string }> }).content[0]!.text); |
| 128 | + expect(body.issue[0].code).toBe("required"); |
| 129 | + expect(body.issue[0].diagnostics).toContain("confirm: true"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("accepts writes when confirm: true is present", async () => { |
| 133 | + const fetchFn = fetchOk({ resourceType: "Patient", id: "new" }); |
| 134 | + const server = createServer({ |
| 135 | + baseUrl: "https://example.test/baseR4", |
| 136 | + resourceTypes: ["Patient"], |
| 137 | + writes: ["create"], |
| 138 | + confirmWrites: true, |
| 139 | + audit: new MemoryAuditSink(), |
| 140 | + fetch: fetchFn, |
| 141 | + }); |
| 142 | + |
| 143 | + await server.dispatcher.handleRequest( |
| 144 | + req({ name: "fhir.create", arguments: { resourceType: "Patient", params: {}, confirm: true } }), |
| 145 | + ); |
| 146 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 147 | + }); |
| 148 | + |
| 149 | + it("does not affect read verbs", async () => { |
| 150 | + const fetchFn = fetchOk({}); |
| 151 | + const server = createServer({ |
| 152 | + baseUrl: "https://example.test/baseR4", |
| 153 | + resourceTypes: ["Patient"], |
| 154 | + confirmWrites: true, |
| 155 | + audit: new MemoryAuditSink(), |
| 156 | + fetch: fetchFn, |
| 157 | + }); |
| 158 | + |
| 159 | + await server.dispatcher.handleRequest( |
| 160 | + req({ name: "fhir.read", arguments: { resourceType: "Patient", id: "abc" } }), |
| 161 | + ); |
| 162 | + expect(fetchFn).toHaveBeenCalledOnce(); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe("write gates compose", () => { |
| 167 | + it("dryRun + writeResourceTypes + confirmWrites all fire in order", async () => { |
| 168 | + const fetchFn = fetchOk({}); |
| 169 | + const server = createServer({ |
| 170 | + baseUrl: "https://example.test/baseR4", |
| 171 | + resourceTypes: ["Patient", "Observation"], |
| 172 | + writes: ["create"], |
| 173 | + writeResourceTypes: ["Patient"], |
| 174 | + dryRun: true, |
| 175 | + confirmWrites: true, |
| 176 | + audit: new MemoryAuditSink(), |
| 177 | + fetch: fetchFn, |
| 178 | + }); |
| 179 | + |
| 180 | + // Wrong type — fails the allowlist before dryRun even kicks in. |
| 181 | + const wrongType = await server.dispatcher.handleRequest( |
| 182 | + req({ name: "fhir.create", arguments: { resourceType: "Observation", params: {}, confirm: true } }), |
| 183 | + ); |
| 184 | + expect(JSON.parse((wrongType.result as { content: Array<{ text: string }> }).content[0]!.text).issue[0].code).toBe( |
| 185 | + "forbidden", |
| 186 | + ); |
| 187 | + |
| 188 | + // Right type, missing confirm — fails the confirmation gate. |
| 189 | + const noConfirm = await server.dispatcher.handleRequest( |
| 190 | + req({ name: "fhir.create", arguments: { resourceType: "Patient", params: {} } }), |
| 191 | + ); |
| 192 | + expect(JSON.parse((noConfirm.result as { content: Array<{ text: string }> }).content[0]!.text).issue[0].code).toBe( |
| 193 | + "required", |
| 194 | + ); |
| 195 | + |
| 196 | + // Right type + confirmed — falls through to dryRun. |
| 197 | + const dry = await server.dispatcher.handleRequest( |
| 198 | + req({ name: "fhir.create", arguments: { resourceType: "Patient", params: {}, confirm: true } }), |
| 199 | + ); |
| 200 | + expect( |
| 201 | + JSON.parse((dry.result as { content: Array<{ text: string }> }).content[0]!.text).issue[0].diagnostics, |
| 202 | + ).toContain("dry-run"); |
| 203 | + expect(fetchFn).not.toHaveBeenCalled(); |
| 204 | + }); |
| 205 | +}); |
0 commit comments