|
| 1 | +import { describe, it } from "@std/testing/bdd"; |
| 2 | +import { assertEquals, assertStringIncludes } from "@std/assert"; |
| 3 | +import pako from "pako"; |
| 4 | +import { encodeBase64 } from "base64"; |
| 5 | + |
| 6 | +/** Mirrors the encoding the UI performs before POSTing (pako.deflate + base64). */ |
| 7 | +function encodePayload(mappings: unknown[]): string { |
| 8 | + return encodeBase64(pako.deflate(JSON.stringify(mappings))); |
| 9 | +} |
| 10 | + |
| 11 | +// Records the cacheId trex is asked to open a connection against. |
| 12 | +let lastGetConnectionArgs: unknown[] = []; |
| 13 | +// Records the statement trex was asked to run, so save tests can assert the |
| 14 | +// INSERT actually targets the resolved cache/schema. |
| 15 | +let lastExecute: { sql: string; params: unknown[] } | null = null; |
| 16 | +let executeImpl: (sql: string, params: unknown[]) => unknown[] = () => []; |
| 17 | + |
| 18 | +// deno-lint-ignore no-explicit-any |
| 19 | +(globalThis as any).Trex = { |
| 20 | + databaseManager: () => ({ |
| 21 | + getConnection: (...args: unknown[]) => { |
| 22 | + lastGetConnectionArgs = args; |
| 23 | + return { |
| 24 | + execute: ( |
| 25 | + sql: string, |
| 26 | + params: unknown[], |
| 27 | + cb: (err: unknown, res: unknown) => void, |
| 28 | + ) => { |
| 29 | + lastExecute = { sql, params: params.map((p) => (p as { value: unknown }).value) }; |
| 30 | + try { |
| 31 | + cb(null, executeImpl(sql, params)); |
| 32 | + } catch (e) { |
| 33 | + cb(e, null); |
| 34 | + } |
| 35 | + }, |
| 36 | + close: () => {}, |
| 37 | + }; |
| 38 | + }, |
| 39 | + }), |
| 40 | +}; |
| 41 | + |
| 42 | +const express = (await import("express")).default; |
| 43 | +const { ConceptMappingRouter } = await import("./routes.ts"); |
| 44 | + |
| 45 | +const DS = "3f2a1c44-7777-4aaa-9bbb-000000000007"; |
| 46 | +const EMPTY_PAYLOAD = "eJyLjgUAARUAuQ=="; // zlib+base64 of [] |
| 47 | + |
| 48 | +// deno-lint-ignore no-explicit-any |
| 49 | +async function withServer(fetcherFactory: any, fn: (base: string) => Promise<void>) { |
| 50 | + const app = express(); |
| 51 | + app.use(express.json()); |
| 52 | + app.use("/concept-mapping", new ConceptMappingRouter(fetcherFactory).router); |
| 53 | + |
| 54 | + const server = app.listen(0); |
| 55 | + await new Promise((resolve) => server.once("listening", resolve)); |
| 56 | + const { port } = server.address() as { port: number }; |
| 57 | + try { |
| 58 | + await fn(`http://127.0.0.1:${port}/concept-mapping`); |
| 59 | + } finally { |
| 60 | + await new Promise((resolve) => server.close(resolve)); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +describe("GET /concept-mapping validation", () => { |
| 65 | + it("returns 400 when schemaName is missing", async () => { |
| 66 | + await withServer(undefined, async (base) => { |
| 67 | + const res = await fetch(`${base}/?databaseCode=PG_MAIN`); |
| 68 | + assertEquals(res.status, 400); |
| 69 | + await res.body?.cancel(); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns 400 when datasetId is not a UUID", async () => { |
| 74 | + await withServer(undefined, async (base) => { |
| 75 | + const res = await fetch( |
| 76 | + `${base}/?databaseCode=PG_MAIN&schemaName=cdm&datasetId=not-a-uuid`, |
| 77 | + ); |
| 78 | + assertEquals(res.status, 400); |
| 79 | + await res.body?.cancel(); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("cacheId resolution over HTTP", () => { |
| 85 | + it("opens the connection with the dataset's persisted cacheId", async () => { |
| 86 | + lastGetConnectionArgs = []; |
| 87 | + executeImpl = () => [{ source_code: "X1" }]; |
| 88 | + |
| 89 | + const factory = () => (_id: string) => |
| 90 | + Promise.resolve({ cacheId: "_stored_cache", databaseCode: "PG_SHARE" }); |
| 91 | + |
| 92 | + await withServer(factory, async (base) => { |
| 93 | + const res = await fetch( |
| 94 | + `${base}/?databaseCode=PG_SHARE&schemaName=cdm&datasetId=${DS}`, |
| 95 | + ); |
| 96 | + assertEquals(res.status, 200); |
| 97 | + assertEquals(await res.json(), [{ source_code: "X1" }]); |
| 98 | + }); |
| 99 | + |
| 100 | + // Authoritative cacheId, NOT the request's databaseCode. |
| 101 | + assertEquals(lastGetConnectionArgs[0], "_stored_cache"); |
| 102 | + }); |
| 103 | + |
| 104 | + it("loads a legacy dataset via the record's databaseCode when cacheId is absent", async () => { |
| 105 | + lastGetConnectionArgs = []; |
| 106 | + executeImpl = () => [{ source_code: "LEGACY" }]; |
| 107 | + |
| 108 | + // Migrated row the cache_id backfill left unset -> guarded fallback. |
| 109 | + const factory = () => (_id: string) => |
| 110 | + Promise.resolve({ cacheId: null, databaseCode: "PG_OLDER" }); |
| 111 | + |
| 112 | + await withServer(factory, async (base) => { |
| 113 | + const res = await fetch( |
| 114 | + `${base}/?databaseCode=REQ_CODE&schemaName=cdm&datasetId=${DS}`, |
| 115 | + ); |
| 116 | + assertEquals(res.status, 200); |
| 117 | + assertEquals(await res.json(), [{ source_code: "LEGACY" }]); |
| 118 | + }); |
| 119 | + |
| 120 | + // The record's databaseCode wins over the request's, which is never trusted |
| 121 | + // once a datasetId is in play. |
| 122 | + assertEquals(lastGetConnectionArgs[0], "PG_OLDER"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("uses the databaseCode when no datasetId is supplied (infra path)", async () => { |
| 126 | + lastGetConnectionArgs = []; |
| 127 | + executeImpl = () => []; |
| 128 | + |
| 129 | + await withServer(undefined, async (base) => { |
| 130 | + const res = await fetch(`${base}/?databaseCode=PG_MAIN&schemaName=cdm`); |
| 131 | + assertEquals(res.status, 200); |
| 132 | + assertEquals(await res.json(), []); |
| 133 | + }); |
| 134 | + |
| 135 | + assertEquals(lastGetConnectionArgs[0], "PG_MAIN"); |
| 136 | + }); |
| 137 | + |
| 138 | + it("returns 502 when the dataset lookup fails, never a guessed cache", async () => { |
| 139 | + const factory = () => (_id: string) => Promise.reject(new Error("portal down")); |
| 140 | + |
| 141 | + await withServer(factory, async (base) => { |
| 142 | + const res = await fetch( |
| 143 | + `${base}/?databaseCode=PG_SHARE&schemaName=cdm&datasetId=${DS}`, |
| 144 | + ); |
| 145 | + assertEquals(res.status, 502); |
| 146 | + assertStringIncludes(await res.text(), "cache id"); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +describe("POST /concept-mapping save path", () => { |
| 152 | + it("writes the mappings to the dataset's persisted cacheId", async () => { |
| 153 | + lastGetConnectionArgs = []; |
| 154 | + lastExecute = null; |
| 155 | + // rowCount is derived from the driver result length. |
| 156 | + executeImpl = () => [{}, {}]; |
| 157 | + |
| 158 | + const factory = () => (_id: string) => |
| 159 | + Promise.resolve({ cacheId: "_stored_cache", databaseCode: "PG_SHARE" }); |
| 160 | + |
| 161 | + const payload = encodePayload([ |
| 162 | + { source_code: "A1", target_concept_id: 111 }, |
| 163 | + { source_code: "B2", target_concept_id: 222 }, |
| 164 | + ]); |
| 165 | + |
| 166 | + await withServer(factory, async (base) => { |
| 167 | + const res = await fetch( |
| 168 | + `${base}/?databaseCode=PG_SHARE&schemaName=cdm&datasetId=${DS}`, |
| 169 | + { |
| 170 | + method: "POST", |
| 171 | + headers: { "Content-Type": "application/json" }, |
| 172 | + body: JSON.stringify({ |
| 173 | + sourceVocabularyId: "MY_VOCAB", |
| 174 | + conceptMappings: payload, |
| 175 | + }), |
| 176 | + }, |
| 177 | + ); |
| 178 | + assertEquals(res.status, 200); |
| 179 | + assertStringIncludes(await res.text(), "Inserted 2 rows"); |
| 180 | + }); |
| 181 | + |
| 182 | + // The write targets the authoritative cacheId, NOT the request databaseCode. |
| 183 | + assertEquals(lastGetConnectionArgs[0], "_stored_cache"); |
| 184 | + assertStringIncludes(lastExecute!.sql, "INSERT INTO cdm.source_to_concept_map"); |
| 185 | + // sourceVocabularyId is stamped onto every row. |
| 186 | + assertEquals( |
| 187 | + lastExecute!.params.filter((p) => p === "MY_VOCAB").length, |
| 188 | + 2, |
| 189 | + ); |
| 190 | + assertEquals(lastExecute!.params.includes("A1"), true); |
| 191 | + assertEquals(lastExecute!.params.includes("B2"), true); |
| 192 | + }); |
| 193 | + |
| 194 | + it("saves against the databaseCode when no datasetId is supplied (infra path)", async () => { |
| 195 | + lastGetConnectionArgs = []; |
| 196 | + executeImpl = () => [{}]; |
| 197 | + |
| 198 | + await withServer(undefined, async (base) => { |
| 199 | + const res = await fetch(`${base}/?databaseCode=PG_MAIN&schemaName=cdm`, { |
| 200 | + method: "POST", |
| 201 | + headers: { "Content-Type": "application/json" }, |
| 202 | + body: JSON.stringify({ |
| 203 | + sourceVocabularyId: "V", |
| 204 | + conceptMappings: encodePayload([{ source_code: "A1" }]), |
| 205 | + }), |
| 206 | + }); |
| 207 | + assertEquals(res.status, 200); |
| 208 | + await res.body?.cancel(); |
| 209 | + }); |
| 210 | + |
| 211 | + assertEquals(lastGetConnectionArgs[0], "PG_MAIN"); |
| 212 | + }); |
| 213 | + |
| 214 | + it("returns 502 without writing when the dataset lookup fails", async () => { |
| 215 | + lastGetConnectionArgs = []; |
| 216 | + lastExecute = null; |
| 217 | + executeImpl = () => [{}]; |
| 218 | + |
| 219 | + const factory = () => (_id: string) => Promise.reject(new Error("portal down")); |
| 220 | + |
| 221 | + await withServer(factory, async (base) => { |
| 222 | + const res = await fetch( |
| 223 | + `${base}/?databaseCode=PG_SHARE&schemaName=cdm&datasetId=${DS}`, |
| 224 | + { |
| 225 | + method: "POST", |
| 226 | + headers: { "Content-Type": "application/json" }, |
| 227 | + body: JSON.stringify({ |
| 228 | + sourceVocabularyId: "V", |
| 229 | + conceptMappings: encodePayload([{ source_code: "A1" }]), |
| 230 | + }), |
| 231 | + }, |
| 232 | + ); |
| 233 | + assertEquals(res.status, 502); |
| 234 | + assertStringIncludes(await res.text(), "cache id"); |
| 235 | + }); |
| 236 | + |
| 237 | + // Critically: no connection opened and nothing written to a guessed cache. |
| 238 | + assertEquals(lastGetConnectionArgs.length, 0); |
| 239 | + assertEquals(lastExecute, null); |
| 240 | + }); |
| 241 | +}); |
| 242 | + |
| 243 | +describe("POST /concept-mapping error contract", () => { |
| 244 | + it("returns 400 for an empty mappings payload even when portal is down", async () => { |
| 245 | + const factory = () => (_id: string) => Promise.reject(new Error("portal down")); |
| 246 | + |
| 247 | + await withServer(factory, async (base) => { |
| 248 | + const res = await fetch( |
| 249 | + `${base}/?databaseCode=PG_SHARE&schemaName=cdm&datasetId=${DS}`, |
| 250 | + { |
| 251 | + method: "POST", |
| 252 | + headers: { "Content-Type": "application/json" }, |
| 253 | + body: JSON.stringify({ |
| 254 | + sourceVocabularyId: "vocab", |
| 255 | + conceptMappings: EMPTY_PAYLOAD, |
| 256 | + }), |
| 257 | + }, |
| 258 | + ); |
| 259 | + // Empty payload is rejected before cacheId resolution -> 400, not 502. |
| 260 | + assertEquals(res.status, 400); |
| 261 | + assertStringIncludes(await res.text(), "No concept mappings to save"); |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + it("returns 400 when conceptMappings is an empty string", async () => { |
| 266 | + await withServer(undefined, async (base) => { |
| 267 | + const res = await fetch(`${base}/?databaseCode=PG_MAIN&schemaName=cdm`, { |
| 268 | + method: "POST", |
| 269 | + headers: { "Content-Type": "application/json" }, |
| 270 | + body: JSON.stringify({ sourceVocabularyId: "v", conceptMappings: "" }), |
| 271 | + }); |
| 272 | + assertEquals(res.status, 400); |
| 273 | + await res.body?.cancel(); |
| 274 | + }); |
| 275 | + }); |
| 276 | +}); |
0 commit comments