Files
pedwfrontend/tests/phase22/representation-loader-guards.test.cjs
T
2026-05-14 08:55:49 +00:00

137 lines
4.5 KiB
JavaScript

const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
const loadRepresentationLoaders = (overrides = {}) => {
const filePath = path.join(
__dirname,
"..",
"..",
"lib",
"representation",
"pageLoaders.js"
);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
source = source.replace(/export\s+const\s+/g, "const ");
source +=
"\nmodule.exports = { loadRepresentationBootstrap, loadRepresentationPage };\n";
const context = {
module: { exports: {} },
exports: {},
getSession: async () => ({
user: { id: "u-1", email: "test@example.com" }
}),
getPortalLogin: async () => ({ value: [{ contactid: "contact-1" }] }),
getPersonalAccount: async () => ({ emailaddress1: "test@example.com" }),
getRepsFromBlob: async () => ({ value: [] }),
getBasicSearch: async () => ({
value: [
{
incidentid: "i-1",
ticketnumber: "CAS-1",
title: "Case",
pinswg_appealcasetype: 1
}
]
}),
getSearchDetails: async () => [{ value: [{}] }],
getCase: async () => ({}),
getPortalModuleDetails: async () => ({}),
getFormCollectionByID: () => ({ LogicalCollectionName: "x" }),
setAccountDetails: () => ({}),
setContainerID: () => ({}),
setCurrentReference: () => ({}),
setCurrentView: () => ({}),
setFilesForRepresentations: () => ({}),
setRepresentationCapacity: () => ({}),
setMyRepresentations: () => ({}),
setMyRepresentationsDetails: () => ({}),
setSearchDetails: () => ({}),
setSearchResults: () => ({}),
setSearch: () => ({}),
getRepsFilesBlobs: async () => ({}),
consoleLogger: () => {},
...overrides
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
test("representation bootstrap redirects to signin when session is missing", async () => {
const mod = loadRepresentationLoaders({ getSession: async () => null });
const result = await mod.loadRepresentationBootstrap({
ctx: { query: {} }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
});
test("representation bootstrap redirects to signin when contact is missing", async () => {
const mod = loadRepresentationLoaders({
getPortalLogin: async () => ({ value: [] })
});
const result = await mod.loadRepresentationBootstrap({
ctx: { query: {} }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
});
test("representation page redirects to myportal when case query is missing", async () => {
const mod = loadRepresentationLoaders();
const store = { dispatch: () => {} };
const result = await mod.loadRepresentationPage({
store,
ctx: { query: {} }
});
assert.strictEqual(result.redirect.destination, "/myportal");
});
test("representation page redirects to myportal when state exists but created is missing", async () => {
const mod = loadRepresentationLoaders();
const store = { dispatch: () => {} };
const result = await mod.loadRepresentationPage({
store,
ctx: { query: { case: "CAS-1", state: "x" } }
});
assert.strictEqual(result.redirect.destination, "/myportal");
});
test("representation page redirects to myportal when existing representation search has no result", async () => {
const mod = loadRepresentationLoaders({
getBasicSearch: async () => ({ value: [] })
});
const store = { dispatch: () => {} };
const result = await mod.loadRepresentationPage({
store,
ctx: { query: { case: "CAS-1", state: "x", created: "rep-1" } }
});
assert.strictEqual(result.redirect.destination, "/myportal");
});
const run = async () => {
let passed = 0;
for (const currentTest of tests) {
await currentTest.fn();
passed += 1;
}
console.log(
`Phase 22 representation-loader-guards tests passed (${passed}/${tests.length}).`
);
};
module.exports = run;
if (require.main === module) {
run().catch((error) => {
console.error(error);
process.exit(1);
});
}