Merged PR 2395: helpers for dashboard domain layer
Related work items: #23754
This commit is contained in:
@@ -0,0 +1,388 @@
|
||||
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 classifyTopThreeDeletePath = (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 sortByISODate = (arr, dateKey) => {
|
||||
return arr.sort((a, b) => new Date(b[dateKey]) - new Date(a[dateKey]));
|
||||
};
|
||||
|
||||
const sortByField = (arr, field, ascending = true) => {
|
||||
return arr.sort((a, b) => {
|
||||
if (a[field] < b[field]) return ascending ? -1 : 1;
|
||||
if (a[field] > b[field]) return ascending ? 1 : -1;
|
||||
return 0;
|
||||
});
|
||||
};
|
||||
|
||||
const applyTopThreeProjection = (showTopThree, showDetails) => {
|
||||
let showTopThreeArr = showTopThree.value;
|
||||
|
||||
Object.keys(showTopThreeArr).map((key) => {
|
||||
let createdDate = parseInt(showTopThreeArr[key].createdon);
|
||||
showTopThreeArr[key].createdDate = createdDate;
|
||||
});
|
||||
|
||||
showTopThreeArr.sort(function compare(a, b) {
|
||||
var dateA = new Date(a.createdDate);
|
||||
var dateB = new Date(b.createdDate);
|
||||
return dateB - dateA;
|
||||
});
|
||||
|
||||
let showTopThreeArrDets = showDetails;
|
||||
|
||||
showTopThreeArr = sortByField(showTopThreeArr, "ticketnumber");
|
||||
showTopThreeArrDets = sortByField(showTopThreeArrDets, "pinswg_name");
|
||||
showTopThreeArrDets = showTopThreeArrDets.flatMap((item) => item.value);
|
||||
|
||||
let mergedArray = showTopThreeArr.map((item1) => {
|
||||
let item2 = showTopThreeArrDets.find(
|
||||
(item) => item.pinswg_name === item1.ticketnumber
|
||||
);
|
||||
return { ...item1, ...item2 };
|
||||
});
|
||||
|
||||
showTopThreeArr = mergedArray;
|
||||
showTopThreeArr = sortByISODate(showTopThreeArr, "createdon");
|
||||
|
||||
return showTopThreeArr.slice(0, 3);
|
||||
};
|
||||
|
||||
const buildRecord = (
|
||||
id,
|
||||
pinswg_representationsubmitted,
|
||||
createdon,
|
||||
ticketnumber
|
||||
) => ({
|
||||
id,
|
||||
pinswg_representationsubmitted,
|
||||
createdon,
|
||||
ticketnumber
|
||||
});
|
||||
|
||||
const buildDetailWrapper = (pinswg_name, extra = {}) => ({
|
||||
value: [
|
||||
{
|
||||
pinswg_name,
|
||||
...extra
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
test("topthree delete-path classification keeps null in watched cases bucket", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z", "CAS-1")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 1,
|
||||
value: [
|
||||
{
|
||||
id: "case-null",
|
||||
pinswg_representationsubmitted: null,
|
||||
createdon: "2024-01-01T00:00:00.000Z",
|
||||
ticketnumber: "CAS-1"
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
test("topthree delete-path classification keeps undefined in watched cases bucket", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord(
|
||||
"case-undefined",
|
||||
undefined,
|
||||
"2024-01-01T00:00:00.000Z",
|
||||
"CAS-1"
|
||||
)
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 1,
|
||||
value: [
|
||||
{
|
||||
id: "case-undefined",
|
||||
createdon: "2024-01-01T00:00:00.000Z",
|
||||
ticketnumber: "CAS-1"
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
test("topthree 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",
|
||||
"CAS-1"
|
||||
)
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 0,
|
||||
value: []
|
||||
});
|
||||
});
|
||||
|
||||
test("topthree delete-path classification excludes truthy submitted marker from watched cases bucket", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("case-true", true, "2024-01-01T00:00:00.000Z", "CAS-1")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 0,
|
||||
value: []
|
||||
});
|
||||
});
|
||||
|
||||
test("topthree delete-path mixed collection preserves watched membership and count", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z", "CAS-1"),
|
||||
buildRecord(
|
||||
"case-undefined",
|
||||
undefined,
|
||||
"2024-01-02T00:00:00.000Z",
|
||||
"CAS-2"
|
||||
),
|
||||
buildRecord(
|
||||
"case-date",
|
||||
"2024-01-01",
|
||||
"2024-01-03T00:00:00.000Z",
|
||||
"CAS-3"
|
||||
),
|
||||
buildRecord("case-true", true, "2024-01-04T00:00:00.000Z", "CAS-4")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath(submittedArr)
|
||||
);
|
||||
|
||||
assert.strictEqual(result["@odata.count"], 2);
|
||||
assert.deepStrictEqual(
|
||||
result.value.map((item) => item.id),
|
||||
["case-null", "case-undefined"]
|
||||
);
|
||||
});
|
||||
|
||||
test("topthree delete-path watched-case classification matches dashboard policy helper for representative inputs", () => {
|
||||
const records = [
|
||||
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z", "CAS-1"),
|
||||
buildRecord(
|
||||
"case-undefined",
|
||||
undefined,
|
||||
"2024-01-02T00:00:00.000Z",
|
||||
"CAS-2"
|
||||
),
|
||||
buildRecord(
|
||||
"case-date",
|
||||
"2024-01-01",
|
||||
"2024-01-03T00:00:00.000Z",
|
||||
"CAS-3"
|
||||
),
|
||||
buildRecord("case-true", true, "2024-01-04T00:00:00.000Z", "CAS-4")
|
||||
];
|
||||
|
||||
const deletePathResult = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath({ value: records })
|
||||
);
|
||||
const helperResult = normalizeForAssertion(
|
||||
splitWatchedCasesBySubmissionState(records)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(deletePathResult, helperResult.watchedCases);
|
||||
});
|
||||
|
||||
test("topthree watched-case refresh output shape only contains watched bucket object", () => {
|
||||
const records = [
|
||||
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z", "CAS-1"),
|
||||
buildRecord(
|
||||
"case-date",
|
||||
"2024-01-01",
|
||||
"2024-01-03T00:00:00.000Z",
|
||||
"CAS-2"
|
||||
)
|
||||
];
|
||||
|
||||
const deletePathResult = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath({ 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"]
|
||||
);
|
||||
});
|
||||
|
||||
test("topthree applies post-classification sorting, merging, and truncation to three items", () => {
|
||||
const watchedCases = {
|
||||
value: [
|
||||
buildRecord("case-1", null, "2024-01-01T00:00:00.000Z", "CAS-002"),
|
||||
buildRecord(
|
||||
"case-2",
|
||||
undefined,
|
||||
"2024-01-04T00:00:00.000Z",
|
||||
"CAS-004"
|
||||
),
|
||||
buildRecord("case-3", null, "2024-01-03T00:00:00.000Z", "CAS-003"),
|
||||
buildRecord("case-4", null, "2024-01-02T00:00:00.000Z", "CAS-001")
|
||||
]
|
||||
};
|
||||
|
||||
const showDetails = [
|
||||
buildDetailWrapper("CAS-001", { detailId: "detail-1" }),
|
||||
buildDetailWrapper("CAS-002", { detailId: "detail-2" }),
|
||||
buildDetailWrapper("CAS-003", { detailId: "detail-3" }),
|
||||
buildDetailWrapper("CAS-004", { detailId: "detail-4" })
|
||||
];
|
||||
|
||||
const classified = classifyTopThreeDeletePath(watchedCases);
|
||||
const projected = normalizeForAssertion(
|
||||
applyTopThreeProjection(classified, showDetails)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
projected.map((item) => item.ticketnumber),
|
||||
["CAS-004", "CAS-003", "CAS-001"]
|
||||
);
|
||||
assert.strictEqual(projected.length, 3);
|
||||
assert.deepStrictEqual(
|
||||
projected.map((item) => item.detailId),
|
||||
["detail-4", "detail-3", "detail-1"]
|
||||
);
|
||||
});
|
||||
|
||||
test("topthree classification does not itself sort filtered watched membership before the later projection pipeline", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("later", null, "2024-01-03T00:00:00.000Z", "CAS-9"),
|
||||
buildRecord(
|
||||
"submitted",
|
||||
"2024-01-10",
|
||||
"2024-01-10T00:00:00.000Z",
|
||||
"CAS-8"
|
||||
),
|
||||
buildRecord(
|
||||
"earlier",
|
||||
undefined,
|
||||
"2024-01-01T00:00:00.000Z",
|
||||
"CAS-7"
|
||||
)
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifyTopThreeDeletePath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
result.value.map((item) => item.id),
|
||||
["later", "earlier"]
|
||||
);
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
for (const currentTest of tests) {
|
||||
await currentTest.fn();
|
||||
passed += 1;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Phase 22 dashboard topthree classification tests passed (${passed}/${tests.length}).`
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = run;
|
||||
|
||||
if (require.main === module) {
|
||||
run().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user