Merged PR 2395: helpers for dashboard domain layer
Related work items: #23754
This commit is contained in:
@@ -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