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 createFakeDate = (isoString) => { const RealDate = Date; function FakeDate(...args) { if (!(this instanceof FakeDate)) { return new RealDate(...args); } if (args.length === 0) { return new RealDate(isoString); } return new RealDate(...args); } FakeDate.UTC = RealDate.UTC; FakeDate.parse = RealDate.parse; FakeDate.now = () => new RealDate(isoString).getTime(); FakeDate.prototype = RealDate.prototype; return FakeDate; }; 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 windowHelpers = loadEsModuleFunctions( "lib/domain/representation-policy/resolveRepresentationWindow.js", [ "isRepresentationWindowOpen", "isRepresentationWindowClosed", "resolveRepresentationWindow" ] ); const gatingHelpers = loadEsModuleFunctions( "lib/domain/representation-policy/canShowRepresentationButtonForAppealType.js", ["canShowRepresentationButtonForAppealType"] ); const householderHelpers = loadEsModuleFunctions( "lib/domain/representation-policy/canStartHouseholderRepresentation.js", ["canStartHouseholderRepresentation"] ); 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 evaluateSearchRowRule = ({ appealType, detailsObj, isLPA, DateCtor = Date }) => { 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, DateCtor ) : true; case 846040018: return specialistProcess == 846040000 && isLPA ? true : specialistProcess == 846040001 ? true : false; case 846040019: return cpoHelpers.canStartCpoRepresentation( appealType, detailsObj, DateCtor ); default: return true; } }; const tests = []; const test = (name, fn) => tests.push({ name, fn }); const evaluateSummaryRowRule = ({ appealType, detailsObj, DateCtor = Date }) => { if (!gatingHelpers.canShowRepresentationButtonForAppealType(appealType)) { return false; } switch (appealType) { case 846040004: return householderHelpers.canStartHouseholderRepresentation( appealType, false ); case 846040015: return detailsObj.pinswg_specialistcaseprocess == 846040001 ? windowHelpers.isRepresentationWindowOpen( detailsObj.pinswg_startdate, detailsObj.pinswg_finalcommentsduedate, DateCtor ) : true; case 846040018: return detailsObj.pinswg_speacialistcaseprocess == 846040000 ? false : detailsObj.pinswg_speacialistcaseprocess == 846040001 ? true : false; case 846040019: return cpoHelpers.canStartCpoRepresentation( appealType, detailsObj, DateCtor ); default: return true; } }; test("ROW hearing specialist process is date-gated in both consumers", () => { const openDate = createFakeDate("2026-04-15T10:00:00.000Z"); const expiredDate = createFakeDate("2026-05-01T10:00:00.000Z"); const futureDate = createFakeDate("2026-03-31T10:00:00.000Z"); const detailsObj = { pinswg_specialistcaseprocess: 846040001, pinswg_startdate: "2026-04-01T00:00:00.000Z", pinswg_finalcommentsduedate: "2026-04-30T00:00:00.000Z" }; assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: openDate }), true ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: openDate }), true ); assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: expiredDate }), false ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: expiredDate }), false ); assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: futureDate }), false ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: futureDate }), false ); }); test("ROW hearing specialist process with missing dates is blocked in both consumers", () => { const FakeDate = createFakeDate("2026-04-15T10:00:00.000Z"); for (const detailsObj of [ { pinswg_specialistcaseprocess: 846040001, pinswg_finalcommentsduedate: "2026-04-30T00:00:00.000Z" }, { pinswg_specialistcaseprocess: 846040001, pinswg_startdate: "2026-04-01T00:00:00.000Z" }, { pinswg_specialistcaseprocess: 846040001 } ]) { assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), false ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), false ); } }); test("ROW non-hearing specialist process values are allowed without date gating", () => { const FakeDate = createFakeDate("2026-05-01T10:00:00.000Z"); for (const specialistValue of [846040000, 846040002, 846040003]) { const detailsObj = { pinswg_specialistcaseprocess: specialistValue, pinswg_startdate: "2026-04-01T00:00:00.000Z", pinswg_finalcommentsduedate: "2026-04-30T00:00:00.000Z" }; assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), true ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), true ); } }); test("canonical and misspelled specialist process fields differ between consumers for ROW hearing", () => { const FakeDate = createFakeDate("2026-05-01T10:00:00.000Z"); const misspelledOnly = { pinswg_speacialistcaseprocess: 846040001, pinswg_startdate: "2026-04-01T00:00:00.000Z", pinswg_finalcommentsduedate: "2026-04-30T00:00:00.000Z" }; assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj: misspelledOnly, DateCtor: FakeDate }), true, "summary consumer falls through to non-hearing behaviour when only misspelled field exists" ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj: misspelledOnly, DateCtor: FakeDate }), false, "search consumer applies hearing/date gating via normalizeSpecialistProcess fallback" ); }); test("canonical specialist process field takes precedence when both specialist fields exist", () => { const FakeDate = createFakeDate("2026-05-01T10:00:00.000Z"); const detailsObj = { pinswg_specialistcaseprocess: 846040000, pinswg_speacialistcaseprocess: 846040001, pinswg_startdate: "2026-04-01T00:00:00.000Z", pinswg_finalcommentsduedate: "2026-04-30T00:00:00.000Z" }; assert.strictEqual(normalizeSpecialistProcess(detailsObj), 846040000); assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), true ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), true ); }); test("ROW currently uses only pinswg_startdate and pinswg_finalcommentsduedate for date gating", () => { const FakeDate = createFakeDate("2026-04-15T10:00:00.000Z"); const detailsObj = { pinswg_specialistcaseprocess: 846040001, pinswg_startdates: "2026-04-01T00:00:00.000Z", pinswg_applicationacceptedasvalid: "2026-04-01T00:00:00.000Z", pinswg_statementduedate: "2026-04-30T00:00:00.000Z" }; assert.strictEqual( evaluateSummaryRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), false ); assert.strictEqual( evaluateSearchRowRule({ appealType: 846040015, detailsObj, DateCtor: FakeDate }), false ); }); test("ROW appeal type string form falls through to default allowed behaviour in both consumers", () => { const FakeDate = createFakeDate("2026-05-01T10:00:00.000Z"); const detailsObj = { pinswg_specialistcaseprocess: 846040001, pinswg_startdate: "2026-04-01T00:00:00.000Z", pinswg_finalcommentsduedate: "2026-04-30T00:00:00.000Z" }; assert.strictEqual( evaluateSummaryRowRule({ appealType: "846040015", detailsObj, DateCtor: FakeDate }), true ); assert.strictEqual( evaluateSearchRowRule({ appealType: "846040015", detailsObj, DateCtor: FakeDate }), true ); }); const run = async () => { let passed = 0; for (const currentTest of tests) { await currentTest.fn(); passed += 1; } console.log( `Phase 22 representation-row-entry-rule tests passed (${passed}/${tests.length}).` ); }; module.exports = run; if (require.main === module) { run().catch((error) => { console.error(error); process.exit(1); }); }