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

209 lines
5.4 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const vm = require("vm");
const assert = require("assert");
const rootDir = path.resolve(__dirname, "..", "..");
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 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 loadGatingModule = () => {
const filePath = path.join(
rootDir,
"lib",
"domain",
"representation-policy",
"canShowRepresentationButtonForAppealType.js"
);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(
/import\s+\{\s*getAppealTypeFamily\s*\}\s+from\s+"\.\.\/appeal-type-policy";?/,
'const { getAppealTypeFamily } = require("../appeal-type-policy");'
);
source = source.replace(
/export function\s+canShowRepresentationButtonForAppealType/,
"function canShowRepresentationButtonForAppealType"
);
source += `
module.exports = {
canShowRepresentationButtonForAppealType
};
`;
const context = {
module: { exports: {} },
exports: {},
require: (modulePath) => {
if (modulePath === "../appeal-type-policy") {
return loadAppealTypeFamilyModule();
}
return require(modulePath);
},
Number,
Set
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
const { canShowRepresentationButtonForAppealType } = loadGatingModule();
const excludedAppealTypes = [
846040012, 846040013, 846040014, 846040020, 846040021, 846040023, 846040024
];
test("excluded appeal types return false", () => {
for (const appealType of excludedAppealTypes) {
assert.strictEqual(
canShowRepresentationButtonForAppealType(appealType),
false,
`Expected excluded appeal type ${appealType} to return false`
);
}
});
test("representative allowed appeal types return true", () => {
for (const appealType of [
846040002, 846040004, 846040011, 846040015, 846040018, 846040019
]) {
assert.strictEqual(
canShowRepresentationButtonForAppealType(appealType),
true,
`Expected allowed appeal type ${appealType} to return true`
);
}
});
test("unknown appeal types preserve default allowed behaviour", () => {
assert.strictEqual(
canShowRepresentationButtonForAppealType(999999999),
true
);
assert.strictEqual(canShowRepresentationButtonForAppealType(-1), true);
});
test("null and undefined preserve default allowed behaviour", () => {
assert.strictEqual(canShowRepresentationButtonForAppealType(null), true);
assert.strictEqual(
canShowRepresentationButtonForAppealType(undefined),
true
);
});
test("string and number appeal type values behave the same", () => {
assert.strictEqual(
canShowRepresentationButtonForAppealType("846040012"),
false
);
assert.strictEqual(
canShowRepresentationButtonForAppealType("846040002"),
true
);
});
test("non-numeric strings preserve default allowed behaviour", () => {
assert.strictEqual(
canShowRepresentationButtonForAppealType("unknown"),
true
);
assert.strictEqual(canShowRepresentationButtonForAppealType(""), true);
});
const run = async () => {
let passed = 0;
for (const currentTest of tests) {
await currentTest.fn();
passed += 1;
}
console.log(
`Phase 22 representation-appeal-type-entry-gating tests passed (${passed}/${tests.length}).`
);
};
module.exports = run;
if (require.main === module) {
run().catch((error) => {
console.error(error);
process.exit(1);
});
}