Files
pedwfrontend/tests/phase22/representation-advert-entry-rule.test.cjs
T
2026-06-18 05:17:10 +00:00

464 lines
13 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const vm = require("vm");
const assert = require("assert");
const rootDir = path.resolve(__dirname, "..", "..");
const loadMapAppealTypeModule = () => {
const filePath = path.join(
rootDir,
"lib",
"domain",
"case-lifecycle",
"mapAppealType.js"
);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(/export const\s+/g, "const ");
source = source.replace(
/export function\s+mapAppealType/,
"function mapAppealType"
);
source += `
module.exports = {
caseTypeAliases,
caseTypeKeyByAppealTypeId,
normaliseCaseType,
mapAppealType
};
`;
const context = {
module: { exports: {} },
exports: {},
require
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const loadAppealTypeFamilyModule = () => {
const filePath = path.join(
rootDir,
"lib",
"domain",
"appeal-type-policy",
"getAppealTypeFamily.js"
);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(
/import\s+\{\s*mapAppealType\s*\}\s+from\s+"\.\.\/case-lifecycle\/mapAppealType";?/,
'const { mapAppealType } = require("../case-lifecycle/mapAppealType");'
);
source = source.replace(
/export function\s+getAppealTypeFamily/,
"function getAppealTypeFamily"
);
source += `
module.exports = { getAppealTypeFamily };
`;
const context = {
module: { exports: {} },
exports: {},
require: (modulePath) => {
if (modulePath === "../case-lifecycle/mapAppealType") {
return loadMapAppealTypeModule();
}
return require(modulePath);
}
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const loadEsModuleFunctions = (
relativePath,
exportNames,
contextExtras = {}
) => {
const filePath = path.join(rootDir, ...relativePath.split("/"));
let source = fs.readFileSync(filePath, "utf8");
for (const exportName of exportNames) {
source = source.replace(
new RegExp(`export function\\s+${exportName}`, "g"),
`function ${exportName}`
);
source = source.replace(
new RegExp(`export const\\s+${exportName}\\s*=`, "g"),
`const ${exportName} =`
);
}
source = source.replace(
/import\s+\{\s*isRepresentationWindowOpen\s*\}\s+from\s+"\.\/resolveRepresentationWindow";/,
'const { isRepresentationWindowOpen } = require("./resolveRepresentationWindow");'
);
source = source.replace(
/import\s+\{\s*getAppealTypeFamily\s*\}\s+from\s+"\.\.\/appeal-type-policy";?/,
'const { getAppealTypeFamily } = require("../appeal-type-policy");'
);
source += `\nmodule.exports = { ${exportNames.join(", ")} };\n`;
const context = {
module: { exports: {} },
exports: {},
require: (modulePath) => {
if (modulePath === "../appeal-type-policy") {
return loadAppealTypeFamilyModule();
}
return require(modulePath);
},
Date,
Number,
Set,
...contextExtras
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const gatingHelpers = loadEsModuleFunctions(
"lib/domain/representation-policy/canShowRepresentationButtonForAppealType.js",
["canShowRepresentationButtonForAppealType"]
);
const householderHelpers = loadEsModuleFunctions(
"lib/domain/representation-policy/canStartHouseholderRepresentation.js",
["canStartHouseholderRepresentation"]
);
const windowHelpers = loadEsModuleFunctions(
"lib/domain/representation-policy/resolveRepresentationWindow.js",
[
"isRepresentationWindowOpen",
"isRepresentationWindowClosed",
"resolveRepresentationWindow"
]
);
const cpoHelpers = loadEsModuleFunctions(
"lib/domain/representation-policy/canStartCpoRepresentation.js",
["canStartCpoRepresentation"],
{
require: (modulePath) => {
if (modulePath === "./resolveRepresentationWindow") {
return windowHelpers;
}
return require(modulePath);
}
}
);
const { normalizeSpecialistProcess } = loadEsModuleFunctions(
"lib/domain/case-lifecycle/normalizeSpecialistProcess.js",
["normalizeSpecialistProcess"]
);
const evaluateSummaryAdvertRule = ({ appealType, detailsObj, isLPA }) => {
if (!gatingHelpers.canShowRepresentationButtonForAppealType(appealType)) {
return false;
}
switch (appealType) {
case 846040004:
return householderHelpers.canStartHouseholderRepresentation(
appealType,
isLPA
);
case 846040015:
return detailsObj.pinswg_specialistcaseprocess == 846040001
? windowHelpers.isRepresentationWindowOpen(
detailsObj.pinswg_startdate,
detailsObj.pinswg_finalcommentsduedate
)
: true;
case 846040018:
return detailsObj.pinswg_speacialistcaseprocess == 846040000 &&
isLPA
? true
: detailsObj.pinswg_speacialistcaseprocess == 846040001
? true
: false;
case 846040019:
return cpoHelpers.canStartCpoRepresentation(appealType, detailsObj);
default:
return true;
}
};
const evaluateSearchAdvertRule = ({ appealType, detailsObj, isLPA }) => {
const specialistProcess = normalizeSpecialistProcess(detailsObj);
if (!gatingHelpers.canShowRepresentationButtonForAppealType(appealType)) {
return false;
}
switch (appealType) {
case 846040004:
return householderHelpers.canStartHouseholderRepresentation(
appealType,
isLPA
);
case 846040015:
return specialistProcess == 846040001
? windowHelpers.isRepresentationWindowOpen(
detailsObj.pinswg_startdate,
detailsObj.pinswg_finalcommentsduedate
)
: true;
case 846040018:
return specialistProcess == 846040000 && isLPA
? true
: specialistProcess == 846040001
? true
: false;
case 846040019:
return cpoHelpers.canStartCpoRepresentation(appealType, detailsObj);
default:
return true;
}
};
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
test("Advert written reps is allowed for LPA and blocked for non-LPA in both consumers when misspelled field is used", () => {
const detailsObj = { pinswg_speacialistcaseprocess: 846040000 };
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: true
}),
true
);
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: false
}),
false
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: true
}),
true
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: false
}),
false
);
});
test("Advert hearing is allowed for both LPA and non-LPA in both consumers when supported field is present", () => {
const summaryDetails = { pinswg_speacialistcaseprocess: 846040001 };
const searchDetails = { pinswg_specialistcaseprocess: 846040001 };
for (const isLPA of [true, false]) {
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj: summaryDetails,
isLPA
}),
true
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj: searchDetails,
isLPA
}),
true
);
}
});
test("Advert other specialist process values are blocked in both consumers when supported field is present", () => {
for (const specialistValue of [846040002, 846040003]) {
const summaryDetails = {
pinswg_speacialistcaseprocess: specialistValue
};
const searchDetails = { pinswg_specialistcaseprocess: specialistValue };
for (const isLPA of [true, false]) {
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj: summaryDetails,
isLPA
}),
false
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj: searchDetails,
isLPA
}),
false
);
}
}
});
test("Advert summary consumer depends on misspelled specialist process field, while search consumer supports normalized fallback", () => {
const canonicalOnly = { pinswg_specialistcaseprocess: 846040000 };
const misspelledOnly = { pinswg_speacialistcaseprocess: 846040000 };
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj: canonicalOnly,
isLPA: true
}),
false,
"summary consumer ignores canonical-only field for Advert branch"
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj: canonicalOnly,
isLPA: true
}),
true,
"search consumer uses canonical field via normalizeSpecialistProcess"
);
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj: misspelledOnly,
isLPA: true
}),
true
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj: misspelledOnly,
isLPA: true
}),
true
);
});
test("Advert canonical specialist process takes precedence in search consumer when both fields exist", () => {
const detailsObj = {
pinswg_specialistcaseprocess: 846040002,
pinswg_speacialistcaseprocess: 846040001
};
assert.strictEqual(normalizeSpecialistProcess(detailsObj), 846040002);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: true
}),
false
);
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: true
}),
true,
"summary consumer still reads the misspelled field directly"
);
});
test("Advert entry does not currently depend on date windows in either consumer", () => {
const detailsObj = {
pinswg_speacialistcaseprocess: 846040001,
pinswg_startdate: "2030-01-01T00:00:00.000Z",
pinswg_finalcommentsduedate: "2030-01-02T00:00:00.000Z"
};
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: false
}),
true
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: 846040018,
detailsObj,
isLPA: false
}),
true
);
});
test("Advert appeal type string form falls through to default allowed behaviour in both consumers", () => {
const detailsObj = { pinswg_speacialistcaseprocess: 846040002 };
assert.strictEqual(
evaluateSummaryAdvertRule({
appealType: "846040018",
detailsObj,
isLPA: false
}),
true
);
assert.strictEqual(
evaluateSearchAdvertRule({
appealType: "846040018",
detailsObj,
isLPA: false
}),
true
);
});
const run = async () => {
let passed = 0;
for (const currentTest of tests) {
await currentTest.fn();
passed += 1;
}
console.log(
`Phase 22 representation-advert-entry-rule tests passed (${passed}/${tests.length}).`
);
};
module.exports = run;
if (require.main === module) {
run().catch((error) => {
console.error(error);
process.exit(1);
});
}