200 lines
6.4 KiB
JavaScript
200 lines
6.4 KiB
JavaScript
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("search basic-paged facade delegates to legacy handler", async () => {
|
|
const calls = [];
|
|
const mod = loadModule("pages/api/search/basic-paged.js", {
|
|
legacyBasicSearchPagedHandler: async (req, res) => {
|
|
calls.push({ req, res });
|
|
return res.status(200).json({ ok: true, route: "basic-paged" });
|
|
}
|
|
});
|
|
|
|
const req = {
|
|
method: "GET",
|
|
query: {
|
|
searchString: "cas",
|
|
pageNumber: "1",
|
|
orderby: "createdon",
|
|
fieldSort: "desc",
|
|
showNumberOfRecords: "10"
|
|
}
|
|
};
|
|
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);
|
|
});
|
|
|
|
test("search advanced facade delegates to legacy handler", async () => {
|
|
const calls = [];
|
|
const mod = loadModule("pages/api/search/advanced.js", {
|
|
legacyAdvancedSearchHandler: async (req, res) => {
|
|
calls.push({ req, res });
|
|
return res.status(200).json({ ok: true, route: "advanced" });
|
|
}
|
|
});
|
|
|
|
const req = { method: "GET", query: { searchstring: '{"q":"cas"}' } };
|
|
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);
|
|
});
|
|
|
|
test("search advanced-paged facade delegates to legacy handler", async () => {
|
|
const calls = [];
|
|
const mod = loadModule("pages/api/search/advanced-paged.js", {
|
|
legacyAdvancedSearchPagedHandler: async (req, res) => {
|
|
calls.push({ req, res });
|
|
return res.status(200).json({ ok: true, route: "advanced-paged" });
|
|
}
|
|
});
|
|
|
|
const req = {
|
|
method: "GET",
|
|
query: {
|
|
searchstring: '{"q":"cas"}',
|
|
pageNumber: "1",
|
|
orderby: "createdon",
|
|
fieldSort: "desc",
|
|
showNumberOfRecords: "10"
|
|
}
|
|
};
|
|
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);
|
|
});
|
|
|
|
test("search basic-details-paged facade delegates to legacy handler", async () => {
|
|
const calls = [];
|
|
const mod = loadModule("pages/api/search/basic-details-paged.js", {
|
|
legacyBasicSearchDetailsPagedHandler: async (req, res) => {
|
|
calls.push({ req, res });
|
|
return res.status(200).json({ value: [{ ticketnumber: "CAS-1" }] });
|
|
}
|
|
});
|
|
|
|
const req = {
|
|
method: "GET",
|
|
query: {
|
|
appealTypeName: "pinswg_planningappeals78s",
|
|
primaryIdAttribute: "pinswg_planningappeals78id",
|
|
incidentID: "_pinswg_planningappeals78ids_value eq abc"
|
|
}
|
|
};
|
|
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);
|
|
});
|
|
|
|
test("legacy public search endpoint handlers remain present", async () => {
|
|
const legacyFiles = [
|
|
"pages/api/endpoint/getbasicsearchpaged_api.js",
|
|
"pages/api/endpoint/getadvancedsearch_api.js",
|
|
"pages/api/endpoint/getadvancedsearchpaged_api.js",
|
|
"pages/api/endpoint/getbasicsearchdetailspaged_api.js",
|
|
"pages/api/endpoint/getbasicsearch_api.js",
|
|
"pages/api/endpoint/getbasicsearchdetails_api.js"
|
|
];
|
|
|
|
legacyFiles.forEach((filePath) => {
|
|
assert.strictEqual(
|
|
fs.existsSync(path.join(rootDir, filePath)),
|
|
true,
|
|
filePath
|
|
);
|
|
});
|
|
});
|
|
|
|
test("searchDirectService active public search methods now target grouped search routes", async () => {
|
|
const source = read("actions/services/searchDirectService.js");
|
|
|
|
assert.match(source, /\/api\/search\/basic-paged\?/);
|
|
assert.match(source, /\/api\/search\/advanced\?/);
|
|
assert.match(source, /\/api\/search\/advanced-paged\?/);
|
|
assert.match(source, /\/api\/search\/basic-details-paged\?/);
|
|
});
|
|
|
|
test("searchDirectService adopted public search methods no longer target legacy endpoint URLs", async () => {
|
|
const source = read("actions/services/searchDirectService.js");
|
|
|
|
assert.doesNotMatch(source, /\/api\/endpoint\/getbasicsearchpaged_api/);
|
|
assert.doesNotMatch(source, /\/api\/endpoint\/getadvancedsearch_api/);
|
|
assert.doesNotMatch(source, /\/api\/endpoint\/getadvancedsearchpaged_api/);
|
|
assert.doesNotMatch(
|
|
source,
|
|
/\/api\/endpoint\/getbasicsearchdetailspaged_api/
|
|
);
|
|
});
|
|
|
|
test("searchDirectService excluded routes remain outside grouped public search adoption scope", async () => {
|
|
const source = read("actions/services/searchDirectService.js");
|
|
|
|
assert.match(source, /\/api\/endpoint\/getbasicsearch_api/);
|
|
assert.match(source, /\/api\/endpoint\/getbasicsearchdetails_api/);
|
|
assert.match(
|
|
source,
|
|
/\/api\/endpoint\/getbasicsearch_by_address_paged_api/
|
|
);
|
|
assert.match(source, /\/api\/endpoint\/getbasicsearch_by_address_api/);
|
|
assert.match(source, /\/api\/endpoint\/getbasicdnssearch_api/);
|
|
assert.match(source, /\/api\/endpoint\/getbasicdnssearchpaged_api/);
|
|
});
|
|
|
|
test("searchDirectService public search request contract strings are preserved", async () => {
|
|
const source = read("actions/services/searchDirectService.js");
|
|
|
|
assert.match(source, /searchString=/);
|
|
assert.match(source, /searchstring=/);
|
|
assert.match(source, /pageNumber=/);
|
|
assert.match(source, /orderby=/);
|
|
assert.match(source, /fieldSort=/);
|
|
assert.match(source, /showNumberOfRecords=/);
|
|
assert.match(source, /appealTypeName=/);
|
|
assert.match(source, /primaryIdAttribute=/);
|
|
assert.match(source, /incidentID=/);
|
|
});
|
|
|
|
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();
|