const fs = require("fs"); const path = require("path"); const vm = require("vm"); const assert = require("assert"); const rootDir = path.resolve(__dirname, "..", ".."); const loadNormalizationModule = () => { const filePath = path.join( rootDir, "lib", "domain", "case-lifecycle", "normalizeSpecialistProcess.js" ); let source = fs.readFileSync(filePath, "utf8"); source = source.replace( /export function\s+normalizeSpecialistProcess/, "function normalizeSpecialistProcess" ); source += "\nmodule.exports = { normalizeSpecialistProcess };\n"; const context = { module: { exports: {} }, exports: {}, require }; vm.runInNewContext(source, context, { filename: filePath }); return context.module.exports; }; const tests = []; const test = (name, fn) => tests.push({ name, fn }); const { normalizeSpecialistProcess } = loadNormalizationModule(); test("returns pinswg_specialistcaseprocess when present", () => { assert.strictEqual( normalizeSpecialistProcess({ pinswg_specialistcaseprocess: 846040001 }), 846040001 ); }); test("returns misspelled fallback when canonical field is absent", () => { assert.strictEqual( normalizeSpecialistProcess({ pinswg_speacialistcaseprocess: 846040000 }), 846040000 ); }); test("canonical field wins when both fields exist", () => { assert.strictEqual( normalizeSpecialistProcess({ pinswg_specialistcaseprocess: 846040001, pinswg_speacialistcaseprocess: 846040000 }), 846040001 ); }); test("returns empty string when neither field exists", () => { assert.strictEqual(normalizeSpecialistProcess({}), ""); }); test("returns empty string for null or undefined source", () => { assert.strictEqual(normalizeSpecialistProcess(null), ""); assert.strictEqual(normalizeSpecialistProcess(undefined), ""); }); test("preserves number values without coercing to strings", () => { const result = normalizeSpecialistProcess({ pinswg_specialistcaseprocess: 846040001 }); assert.strictEqual(typeof result, "number"); assert.strictEqual(result, 846040001); }); test("preserves string values without coercing to numbers", () => { const result = normalizeSpecialistProcess({ pinswg_specialistcaseprocess: "846040001" }); assert.strictEqual(typeof result, "string"); assert.strictEqual(result, "846040001"); }); const run = async () => { let passed = 0; for (const currentTest of tests) { await currentTest.fn(); passed += 1; } console.log( `Phase 22 case-lifecycle-specialist-process-normalization tests passed (${passed}/${tests.length}).` ); }; module.exports = run; if (require.main === module) { run().catch((error) => { console.error(error); process.exit(1); }); }