const assert = require("assert"); const fs = require("fs"); const path = require("path"); const { loadModule, createRes } = require("../phase21/_shared.cjs"); const rootDir = path.resolve(__dirname, "..", ".."); const tests = []; const test = (name, fn) => tests.push({ name, fn }); const read = (relativePath) => fs.readFileSync(path.join(rootDir, relativePath), "utf8"); test("documents get-search-document-details facade delegates to legacy handler", async () => { const calls = []; const mod = loadModule( "pages/api/documents/get-search-document-details.js", { legacyGetSearchDocumentDetailsHandler: async (req, res) => { calls.push({ req, res }); return res.status(200).json({ ok: true, route: "details" }); } } ); const req = { method: "GET", query: { incidentid: "inc-1" } }; const res = createRes(); await mod.default(req, res); assert.strictEqual(calls.length, 1); assert.strictEqual(calls[0].req, req); assert.strictEqual(calls[0].res, res); assert.strictEqual(res.state.statusCode, 200); assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), { ok: true, route: "details" }); }); test("documents get-search-document-details-paged facade delegates to legacy handler", async () => { const calls = []; const mod = loadModule( "pages/api/documents/get-search-document-details-paged.js", { legacyGetSearchDocumentDetailsPagedHandler: async (req, res) => { calls.push({ req, res }); return res .status(200) .json({ ok: true, route: "details-paged" }); } } ); const req = { method: "GET", query: { incidentid: "inc-2", pageNumber: "1", orderby: "createdon", fieldSort: "asc", showNumberOfRecords: "10", documentType: "all" } }; const res = createRes(); await mod.default(req, res); assert.strictEqual(calls.length, 1); assert.strictEqual(calls[0].req, req); assert.strictEqual(calls[0].res, res); assert.strictEqual(res.state.statusCode, 200); assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), { ok: true, route: "details-paged" }); }); test("documents get-search-document-types facade delegates to legacy handler", async () => { const calls = []; const mod = loadModule("pages/api/documents/get-search-document-types.js", { legacyGetSearchDocumentTypesHandler: async (req, res) => { calls.push({ req, res }); return res .status(200) .json([{ pinswg_isharedocumentlocations: 1, count: 2 }]); } }); const req = { method: "GET", query: { incidentid: "inc-3" } }; const res = createRes(); await mod.default(req, res); assert.strictEqual(calls.length, 1); assert.strictEqual(calls[0].req, req); assert.strictEqual(calls[0].res, res); assert.strictEqual(res.state.statusCode, 200); assert.deepStrictEqual(JSON.parse(JSON.stringify(res.state.jsonBody)), [ { pinswg_isharedocumentlocations: 1, count: 2 } ]); }); test("legacy document metadata endpoint files remain present", async () => { const legacyFiles = [ "pages/api/endpoint/getsearchdocumentdetails_api.js", "pages/api/endpoint/getsearchdocumentdetailspaged_api.js", "pages/api/endpoint/getsearchdocumentTypes_api.js", "pages/api/endpoint/getsearchdocumenthistory_api.js", "pages/api/endpoint/getsearchdocumenthistorypaged_api.js", "pages/api/documents/download/[id].js" ]; legacyFiles.forEach((filePath) => { assert.strictEqual( fs.existsSync(path.join(rootDir, filePath)), true, filePath ); }); }); test("searchDirectService document journey methods now target grouped documents facade routes", async () => { const source = read("actions/services/searchDirectService.js"); assert.match(source, /\/api\/documents\/get-search-document-details\?/); assert.match( source, /\/api\/documents\/get-search-document-details-paged\?/ ); assert.match(source, /\/api\/documents\/get-search-document-types\?/); }); test("searchDirectService no longer targets legacy document metadata routes for adopted methods", async () => { const source = read("actions/services/searchDirectService.js"); assert.doesNotMatch( source, /\/api\/endpoint\/getsearchdocumentdetails_api/ ); assert.doesNotMatch( source, /\/api\/endpoint\/getsearchdocumentdetailspaged_api/ ); assert.doesNotMatch(source, /\/api\/endpoint\/getsearchdocumentTypes_api/); }); test("document runtime contract cues remain unchanged in searchDirectService", async () => { const source = read("actions/services/searchDirectService.js"); assert.match(source, /incidentid=/); assert.match(source, /pageNumber=/); assert.match(source, /orderby=/); assert.match(source, /fieldSort=/); assert.match(source, /showNumberOfRecords=/); assert.match(source, /documentType=/); }); test("document download route remains the runtime download path", async () => { const metadataDetailsSource = read( "pages/api/endpoint/getsearchdocumentdetails_api.js" ); const metadataPagedSource = read( "pages/api/endpoint/getsearchdocumentdetailspaged_api.js" ); assert.match(metadataDetailsSource, /\/api\/documents\/download\//); assert.match(metadataPagedSource, /\/api\/documents\/download\//); }); async function run() { let failures = 0; for (const { name, fn } of tests) { try { await fn(); console.log(`✓ ${name}`); } catch (error) { failures += 1; console.error(`✗ ${name}`); console.error(error); } } if (failures > 0) { process.exit(1); } } run();