Files
pedwfrontend/tests/phase22/dashboard-viewall-classification.test.cjs
T
2026-06-17 16:06:09 +00:00

296 lines
8.1 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 normalizeForAssertion = (value) =>
JSON.parse(JSON.stringify(value ?? null));
const rootDir = path.resolve(__dirname, "..", "..");
const loadDashboardPolicyModule = () => {
const filePath = path.join(
rootDir,
"lib",
"domain",
"dashboard-policy",
"splitWatchedCasesBySubmissionState.js"
);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(
/export function\s+splitWatchedCasesBySubmissionState/,
"function splitWatchedCasesBySubmissionState"
);
source += `
module.exports = {
splitWatchedCasesBySubmissionState
};
`;
const context = {
module: { exports: {} },
exports: {},
require
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const { splitWatchedCasesBySubmissionState } = loadDashboardPolicyModule();
const classifyViewAllDeletePath = (submittedArr) => {
const required = submittedArr.value.filter((el) => {
return el.pinswg_representationsubmitted == null;
});
let newObj = {};
return Object.assign(newObj, {
"@odata.count": required.length,
value: required
});
};
const classifyViewAllEmailRefreshPath = (submittedArr) => {
const required = submittedArr.value.filter((el) => {
return (
el.pinswg_representationsubmitted === null ||
el.pinswg_representationsubmitted === undefined
);
});
let newObj = {};
const data = Object.assign(newObj, {
"@odata.count": required.length,
value: required
});
data.value.sort(function compare(a, b) {
var dateA = new Date(a.createdon);
var dateB = new Date(b.createdon);
return dateB - dateA;
});
return data;
};
const buildRecord = (id, pinswg_representationsubmitted, createdon) => ({
id,
pinswg_representationsubmitted,
createdon
});
test("viewall delete-path classification keeps null in watched cases bucket", () => {
const submittedArr = {
value: [buildRecord("case-null", null, "2024-01-01T00:00:00.000Z")]
};
const result = normalizeForAssertion(
classifyViewAllDeletePath(submittedArr)
);
assert.deepStrictEqual(result, {
"@odata.count": 1,
value: [
{
id: "case-null",
pinswg_representationsubmitted: null,
createdon: "2024-01-01T00:00:00.000Z"
}
]
});
});
test("viewall delete-path classification keeps undefined in watched cases bucket", () => {
const submittedArr = {
value: [
buildRecord("case-undefined", undefined, "2024-01-01T00:00:00.000Z")
]
};
const result = normalizeForAssertion(
classifyViewAllDeletePath(submittedArr)
);
assert.deepStrictEqual(result, {
"@odata.count": 1,
value: [
{
id: "case-undefined",
createdon: "2024-01-01T00:00:00.000Z"
}
]
});
});
test("viewall delete-path classification excludes date-string submitted marker from watched cases bucket", () => {
const submittedArr = {
value: [
buildRecord("case-date", "2024-01-01", "2024-01-01T00:00:00.000Z")
]
};
const result = normalizeForAssertion(
classifyViewAllDeletePath(submittedArr)
);
assert.deepStrictEqual(result, {
"@odata.count": 0,
value: []
});
});
test("viewall delete-path classification excludes truthy submitted marker from watched cases bucket", () => {
const submittedArr = {
value: [buildRecord("case-true", true, "2024-01-01T00:00:00.000Z")]
};
const result = normalizeForAssertion(
classifyViewAllDeletePath(submittedArr)
);
assert.deepStrictEqual(result, {
"@odata.count": 0,
value: []
});
});
test("viewall delete-path mixed collection preserves watched membership and count", () => {
const submittedArr = {
value: [
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z"),
buildRecord(
"case-undefined",
undefined,
"2024-01-02T00:00:00.000Z"
),
buildRecord("case-date", "2024-01-01", "2024-01-03T00:00:00.000Z"),
buildRecord("case-true", true, "2024-01-04T00:00:00.000Z")
]
};
const result = normalizeForAssertion(
classifyViewAllDeletePath(submittedArr)
);
assert.strictEqual(result["@odata.count"], 2);
assert.deepStrictEqual(
result.value.map((item) => item.id),
["case-null", "case-undefined"]
);
});
test("viewall delete-path preserves original filtered order", () => {
const submittedArr = {
value: [
buildRecord("watched-first", null, "2024-01-01T00:00:00.000Z"),
buildRecord(
"submitted-middle",
"2024-01-10",
"2024-01-10T00:00:00.000Z"
),
buildRecord("watched-second", undefined, "2024-01-02T00:00:00.000Z")
]
};
const result = normalizeForAssertion(
classifyViewAllDeletePath(submittedArr)
);
assert.deepStrictEqual(
result.value.map((item) => item.id),
["watched-first", "watched-second"]
);
});
test("viewall email-refresh path preserves watched-case output shape and sorts descending by createdon", () => {
const submittedArr = {
value: [
buildRecord("older-watched", null, "2024-01-01T00:00:00.000Z"),
buildRecord("newer-watched", undefined, "2024-01-02T00:00:00.000Z"),
buildRecord(
"submitted-item",
"2024-01-10",
"2024-01-10T00:00:00.000Z"
)
]
};
const result = normalizeForAssertion(
classifyViewAllEmailRefreshPath(submittedArr)
);
assert.deepStrictEqual(Object.keys(result), ["@odata.count", "value"]);
assert.strictEqual(result["@odata.count"], 2);
assert.deepStrictEqual(
result.value.map((item) => item.id),
["newer-watched", "older-watched"]
);
});
test("viewall delete-path watched-case classification matches dashboard policy helper for representative inputs", () => {
const records = [
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z"),
buildRecord("case-undefined", undefined, "2024-01-02T00:00:00.000Z"),
buildRecord("case-date", "2024-01-01", "2024-01-03T00:00:00.000Z"),
buildRecord("case-true", true, "2024-01-04T00:00:00.000Z")
];
const deletePathResult = normalizeForAssertion(
classifyViewAllDeletePath({ value: records })
);
const helperResult = normalizeForAssertion(
splitWatchedCasesBySubmissionState(records)
);
assert.deepStrictEqual(deletePathResult, helperResult.watchedCases);
});
test("viewall currently has no submitted bucket output shape in its watched-case refresh logic", () => {
const records = [
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z"),
buildRecord("case-date", "2024-01-01", "2024-01-03T00:00:00.000Z")
];
const deletePathResult = normalizeForAssertion(
classifyViewAllDeletePath({ value: records })
);
const helperResult = normalizeForAssertion(
splitWatchedCasesBySubmissionState(records)
);
assert.deepStrictEqual(Object.keys(deletePathResult), [
"@odata.count",
"value"
]);
assert.deepStrictEqual(
helperResult.submittedRepresentations.map((item) => item.id),
["case-date"]
);
});
const run = async () => {
let passed = 0;
for (const currentTest of tests) {
await currentTest.fn();
passed += 1;
}
console.log(
`Phase 22 dashboard viewall classification tests passed (${passed}/${tests.length}).`
);
};
module.exports = run;
if (require.main === module) {
run().catch((error) => {
console.error(error);
process.exit(1);
});
}