Merged PR 2395: helpers for dashboard domain layer
Related work items: #23754
This commit is contained in:
@@ -0,0 +1,293 @@
|
||||
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 classifySearchResultsDeleteRefreshPath = (submittedArr) => {
|
||||
const data = splitWatchedCasesBySubmissionState(
|
||||
submittedArr.value
|
||||
).watchedCases;
|
||||
|
||||
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,
|
||||
extra = {}
|
||||
) => ({
|
||||
id,
|
||||
pinswg_representationsubmitted,
|
||||
createdon,
|
||||
...extra
|
||||
});
|
||||
|
||||
test("searchresults classification keeps null in watched cases bucket", () => {
|
||||
const submittedArr = {
|
||||
value: [buildRecord("case-null", null, "2024-01-01T00:00:00.000Z")]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 1,
|
||||
value: [
|
||||
{
|
||||
id: "case-null",
|
||||
pinswg_representationsubmitted: null,
|
||||
createdon: "2024-01-01T00:00:00.000Z"
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
test("searchresults classification keeps undefined in watched cases bucket", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("case-undefined", undefined, "2024-01-01T00:00:00.000Z")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 1,
|
||||
value: [
|
||||
{
|
||||
id: "case-undefined",
|
||||
createdon: "2024-01-01T00:00:00.000Z"
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
test("searchresults 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(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 0,
|
||||
value: []
|
||||
});
|
||||
});
|
||||
|
||||
test("searchresults 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(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(result, {
|
||||
"@odata.count": 0,
|
||||
value: []
|
||||
});
|
||||
});
|
||||
|
||||
test("searchresults mixed collection preserves watched membership, count, and excludes submitted records", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("case-null", null, "2024-01-02T00:00:00.000Z"),
|
||||
buildRecord(
|
||||
"case-undefined",
|
||||
undefined,
|
||||
"2024-01-04T00:00:00.000Z"
|
||||
),
|
||||
buildRecord("case-date", "2024-01-01", "2024-01-03T00:00:00.000Z"),
|
||||
buildRecord("case-true", true, "2024-01-05T00:00:00.000Z")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.strictEqual(result["@odata.count"], 2);
|
||||
assert.deepStrictEqual(
|
||||
result.value.map((item) => item.id),
|
||||
["case-undefined", "case-null"]
|
||||
);
|
||||
});
|
||||
|
||||
test("searchresults applies sorting after filtering rather than preserving original filtered order", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("watched-earlier", null, "2024-01-01T00:00:00.000Z"),
|
||||
buildRecord(
|
||||
"submitted-middle",
|
||||
"2024-01-10",
|
||||
"2024-01-10T00:00:00.000Z"
|
||||
),
|
||||
buildRecord("watched-later", undefined, "2024-01-03T00:00:00.000Z")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
result.value.map((item) => item.id),
|
||||
["watched-later", "watched-earlier"]
|
||||
);
|
||||
});
|
||||
|
||||
test("searchresults watched-case classification is equivalent to dashboard policy helper watched bucket 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 searchResult = normalizeForAssertion(
|
||||
classifySearchResultsDeleteRefreshPath({ value: records })
|
||||
);
|
||||
const helperResult = normalizeForAssertion(
|
||||
splitWatchedCasesBySubmissionState(records)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
searchResult.value.map((item) => item.id),
|
||||
["case-undefined", "case-null"]
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
helperResult.watchedCases.value.map((item) => item.id),
|
||||
["case-null", "case-undefined"]
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
helperResult.submittedRepresentations.map((item) => item.id),
|
||||
["case-date", "case-true"]
|
||||
);
|
||||
assert.strictEqual(
|
||||
searchResult["@odata.count"],
|
||||
helperResult.watchedCases["@odata.count"]
|
||||
);
|
||||
});
|
||||
|
||||
test("searchresults output shape is a watched bucket object with odata count and value array only", () => {
|
||||
const submittedArr = {
|
||||
value: [
|
||||
buildRecord("case-null", null, "2024-01-01T00:00:00.000Z"),
|
||||
buildRecord("case-date", "2024-01-01", "2024-01-03T00:00:00.000Z")
|
||||
]
|
||||
};
|
||||
|
||||
const result = normalizeForAssertion(
|
||||
classifySearchResultsDeleteRefreshPath(submittedArr)
|
||||
);
|
||||
|
||||
assert.deepStrictEqual(Object.keys(result), ["@odata.count", "value"]);
|
||||
assert.deepStrictEqual(
|
||||
result.value.map((item) => item.id),
|
||||
["case-null"]
|
||||
);
|
||||
});
|
||||
|
||||
test("searchresults watched-case classification is coupled to delete refresh and downstream details refresh, not result card rendering", () => {
|
||||
const searchResultsSource = fs.readFileSync(
|
||||
path.join(rootDir, "components", "search", "searchresults.js"),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
assert.match(
|
||||
searchResultsSource,
|
||||
/topThreeType == "watchedCases"[\s\S]*?splitWatchedCasesBySubmissionState\([\s\S]*?\.watchedCases/
|
||||
);
|
||||
assert.match(
|
||||
searchResultsSource,
|
||||
/data\.value\.sort\(function compare\(a, b\)/
|
||||
);
|
||||
assert.match(searchResultsSource, /deleteWatchedCases\(caseID\)/);
|
||||
assert.match(searchResultsSource, /getWatchedCasesProxy\(/);
|
||||
assert.match(searchResultsSource, /setWatchedCases\(data\)/);
|
||||
assert.match(
|
||||
searchResultsSource,
|
||||
/getDetailsProxy\(data, "myWatchedCases"\)/
|
||||
);
|
||||
assert.doesNotMatch(
|
||||
searchResultsSource,
|
||||
/resultsArr\.map\([\s\S]*pinswg_representationsubmitted/
|
||||
);
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
for (const currentTest of tests) {
|
||||
await currentTest.fn();
|
||||
passed += 1;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Phase 22 dashboard searchresults classification tests passed (${passed}/${tests.length}).`
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = run;
|
||||
|
||||
if (require.main === module) {
|
||||
run().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
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 classifyWatchedCases = (watchedCases) => {
|
||||
const result = normalizeForAssertion(
|
||||
splitWatchedCasesBySubmissionState(watchedCases.value)
|
||||
);
|
||||
|
||||
return {
|
||||
filteredWatchedCases: result.watchedCases,
|
||||
mySubmittedReps: result.submittedRepresentations
|
||||
};
|
||||
};
|
||||
|
||||
const buildRecord = (id, pinswg_representationsubmitted) => ({
|
||||
id,
|
||||
pinswg_representationsubmitted
|
||||
});
|
||||
|
||||
test("null submitted marker remains in watched cases bucket", () => {
|
||||
const watchedCases = {
|
||||
value: [buildRecord("case-null", null)]
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.strictEqual(result.filteredWatchedCases["@odata.count"], 1);
|
||||
assert.deepStrictEqual(
|
||||
result.filteredWatchedCases.value.map((item) => item.id),
|
||||
["case-null"]
|
||||
);
|
||||
assert.deepStrictEqual(result.mySubmittedReps, []);
|
||||
});
|
||||
|
||||
test("undefined submitted marker remains in watched cases bucket", () => {
|
||||
const watchedCases = {
|
||||
value: [buildRecord("case-undefined", undefined)]
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.strictEqual(result.filteredWatchedCases["@odata.count"], 1);
|
||||
assert.deepStrictEqual(
|
||||
result.filteredWatchedCases.value.map((item) => item.id),
|
||||
["case-undefined"]
|
||||
);
|
||||
assert.deepStrictEqual(result.mySubmittedReps, []);
|
||||
});
|
||||
|
||||
test("date-string submitted marker remains in submitted representations bucket", () => {
|
||||
const watchedCases = {
|
||||
value: [buildRecord("case-date", "2024-01-01")]
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.strictEqual(result.filteredWatchedCases["@odata.count"], 0);
|
||||
assert.deepStrictEqual(result.filteredWatchedCases.value, []);
|
||||
assert.deepStrictEqual(
|
||||
result.mySubmittedReps.map((item) => item.id),
|
||||
["case-date"]
|
||||
);
|
||||
});
|
||||
|
||||
test("truthy submitted marker remains in submitted representations bucket", () => {
|
||||
const watchedCases = {
|
||||
value: [buildRecord("case-true", true)]
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.strictEqual(result.filteredWatchedCases["@odata.count"], 0);
|
||||
assert.deepStrictEqual(result.filteredWatchedCases.value, []);
|
||||
assert.deepStrictEqual(
|
||||
result.mySubmittedReps.map((item) => item.id),
|
||||
["case-true"]
|
||||
);
|
||||
});
|
||||
|
||||
test("mixed collection preserves current split counts and membership", () => {
|
||||
const watchedCases = {
|
||||
value: [
|
||||
buildRecord("case-null", null),
|
||||
buildRecord("case-undefined", undefined),
|
||||
buildRecord("case-date", "2024-01-01"),
|
||||
buildRecord("case-true", true)
|
||||
]
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.strictEqual(result.filteredWatchedCases["@odata.count"], 2);
|
||||
assert.deepStrictEqual(
|
||||
result.filteredWatchedCases.value.map((item) => item.id),
|
||||
["case-null", "case-undefined"]
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
result.mySubmittedReps.map((item) => item.id),
|
||||
["case-date", "case-true"]
|
||||
);
|
||||
});
|
||||
|
||||
test("empty collection preserves empty watched and submitted outputs", () => {
|
||||
const watchedCases = {
|
||||
value: []
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.deepStrictEqual(result.filteredWatchedCases, {
|
||||
"@odata.count": 0,
|
||||
value: []
|
||||
});
|
||||
assert.deepStrictEqual(result.mySubmittedReps, []);
|
||||
});
|
||||
|
||||
test("classification preserves input order within both buckets", () => {
|
||||
const watchedCases = {
|
||||
value: [
|
||||
buildRecord("watched-first", null),
|
||||
buildRecord("submitted-first", "2024-01-01"),
|
||||
buildRecord("watched-second", undefined),
|
||||
buildRecord("submitted-second", true)
|
||||
]
|
||||
};
|
||||
|
||||
const result = classifyWatchedCases(watchedCases);
|
||||
|
||||
assert.deepStrictEqual(
|
||||
result.filteredWatchedCases.value.map((item) => item.id),
|
||||
["watched-first", "watched-second"]
|
||||
);
|
||||
assert.deepStrictEqual(
|
||||
result.mySubmittedReps.map((item) => item.id),
|
||||
["submitted-first", "submitted-second"]
|
||||
);
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
for (const currentTest of tests) {
|
||||
await currentTest.fn();
|
||||
passed += 1;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Phase 22 dashboard watched-case 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