Files
pedwfrontend/tests/phase22/api-grouping-facade-subscriptions.test.cjs
T
2026-06-25 08:11:14 +00:00

194 lines
6.3 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("subscriptions get-watched-cases facade delegates to legacy handler", async () => {
const calls = [];
const mod = loadModule("pages/api/subscriptions/get-watched-cases.js", {
legacyGetWatchedCasesHandler: async (req, res) => {
calls.push({ req, res });
return res.status(200).json({ ok: true, route: "get" });
}
});
const req = { method: "GET", query: { loggedInUserId: "user-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: "get"
});
});
test("subscriptions create-watched-case facade delegates to legacy handler", async () => {
const calls = [];
const mod = loadModule("pages/api/subscriptions/create-watched-case.js", {
legacyCreateWatchedCaseHandler: async (req, res) => {
calls.push({ req, res });
return res.status(200).json({ ok: true, route: "create" });
}
});
const req = {
method: "POST",
body: {
"pinswg_WatchedCase@odata.bind": "/incidents(inc-1)",
"pinswg_Contact@odata.bind": "/contacts(con-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: "create"
});
});
test("subscriptions delete-watched-case facade delegates to legacy handler", async () => {
const calls = [];
const mod = loadModule("pages/api/subscriptions/delete-watched-case.js", {
legacyDeleteWatchedCaseHandler: async (req, res) => {
calls.push({ req, res });
return res.status(200).json({ ok: true, route: "delete" });
}
});
const req = { method: "DELETE", query: { watchedCaseID: "watch-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: "delete"
});
});
test("subscriptions get-watched-cases-proxy facade delegates to legacy handler", async () => {
const calls = [];
const mod = loadModule(
"pages/api/subscriptions/get-watched-cases-proxy.js",
{
legacyGetWatchedCasesProxyHandler: async (req, res) => {
calls.push({ req, res });
return res.status(200).json({ ok: true, route: "proxy-get" });
}
}
);
const req = { method: "GET", query: { loggedInUserId: "user-2" } };
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: "proxy-get"
});
});
test("legacy watched-case endpoint files remain present", async () => {
const legacyFiles = [
"pages/api/endpoint/getwatchedcases_api.js",
"pages/api/endpoint/getwatchedcasesproxy_api.js",
"pages/api/endpoint/createwatchedcases_api.js",
"pages/api/endpoint/deletewatchedcases_api.js"
];
legacyFiles.forEach((filePath) => {
assert.strictEqual(
fs.existsSync(path.join(rootDir, filePath)),
true,
filePath
);
});
});
test("portalDirectService watched-case methods now target subscriptions facade routes", async () => {
const source = read("actions/services/portalDirectService.js");
assert.match(source, /\/api\/subscriptions\/get-watched-cases/);
assert.match(source, /\/api\/subscriptions\/get-watched-cases-proxy/);
assert.match(source, /\/api\/subscriptions\/create-watched-case/);
assert.match(source, /\/api\/subscriptions\/delete-watched-case/);
});
test("portalDirectService no longer targets legacy watched-case routes for adopted methods", async () => {
const source = read("actions/services/portalDirectService.js");
assert.doesNotMatch(source, /\/api\/endpoint\/getwatchedcases_api/);
assert.doesNotMatch(source, /\/api\/endpoint\/getwatchedcasesproxy_api/);
assert.doesNotMatch(source, /\/api\/endpoint\/createwatchedcases_api/);
assert.doesNotMatch(source, /\/api\/endpoint\/deletewatchedcases_api/);
});
test("portalDirectService still preserves watched-case runtime contracts around inputs", async () => {
const source = read("actions/services/portalDirectService.js");
assert.match(source, /loggedInUserId/);
assert.match(source, /watchedCaseID/);
assert.match(source, /method:\s*"post"/);
});
test("delete watched-case proxy route remains outside the grouped subscriptions pilot scope", async () => {
const source = read("actions/services/portalDirectService.js");
assert.doesNotMatch(source, /deletewatchedcasesproxy_api/);
assert.strictEqual(
fs.existsSync(
path.join(
rootDir,
"pages/api/endpoint/deletewatchedcasesproxy_api.js"
)
),
true
);
});
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();