294 lines
8.3 KiB
JavaScript
294 lines
8.3 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 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);
|
|
});
|
|
}
|