const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const { test } = require("node:test"); // Exercise the inquiry entry point without starting the UI or contacting a provider. function inquiryContext(response) { const source = fs.readFileSync(path.join(__dirname, "fanavaran-flow-ui-server.js"), "utf8") .split("const server = http.createServer")[0]; const context = vm.createContext({ require, __dirname, process: { env: {} }, Buffer }); vm.runInContext(source, context); context.response = response; vm.runInContext(` findOfflinePlate = () => null; getSandhubToken = async () => "test-token"; httpJson = async () => response; `, context); return context; } for (const [name, json, status] of [ ["HTTP error JSON", { statusCode: 401, message: "Unauthorized" }, 401], ["provider failure", { success: false, message: "No policy found" }, 200], ["nested provider failure", { data: { hasError: true, message: "Not found" } }, 200], ["empty result", { data: {} }, 200], ]) { test(`${name} must not report success with empty vehicle inputs`, async () => { const context = inquiryContext({ code: 0, status, json }); const result = await vm.runInContext('inquireVehicle({ client: "tejaratno" })', context); assert.equal(result.ok, false); assert.ok(result.error); }); } test("successful Tejarat inquiry fills vehicle and policy fields", async () => { const context = inquiryContext({ code: 0, status: 200, json: { data: { mtrnum: "ENGINE-1", shsNam: "CHASSIS-1", vin: "VIN-1", ModelField: "1400", insuranceNumber: "POLICY-1", PlcyUnqCod: "12345", persianStartDate: "1404/01/01", persianEndDate: "1405/01/01", } } }); const result = await vm.runInContext('inquireVehicle({ client: "tejaratno" })', context); assert.equal(result.ok, true); assert.deepEqual(JSON.parse(JSON.stringify(result.vehicle)), { MOTOR_NO: "ENGINE-1", CHASSIS_NO: "CHASSIS-1", VIN: "VIN-1", BUILT_YEAR: "1400", POLICY_NO: "POLICY-1", POLICY_CI_NUMBER: "12345", BEGIN_DATE: "1404/01/01", END_DATE: "1405/01/01", }); }); test("policy aliases used by existing inquiry mappings populate all three fields", async () => { const context = inquiryContext({ code: 0, status: 200, json: { MtrNum: "ENGINE-1", LastCompanyDocumentNumber: "POLICY-2", SatrtDate: "1404/02/01", endDate: "1405/02/01", } }); const result = await vm.runInContext('inquireVehicle({ client: "tejaratno" })', context); assert.equal(result.vehicle.POLICY_NO, "POLICY-2"); assert.equal(result.vehicle.BEGIN_DATE, "1404/02/01"); assert.equal(result.vehicle.END_DATE, "1405/02/01"); }); test("flow UI includes every seed expert in each assigned branch and both Kazemi codes", () => { const context = inquiryContext({}); const branches = vm.runInContext("parsianBranchSeeds()", context); for (const [file, key, role] of [ ["file-makers.json", "fileMakers", "makers"], ["file-reviewers.json", "fileReviewers", "reviewers"], ]) { const seeds = JSON.parse(fs.readFileSync(path.join(__dirname, "data/parsian-tehran", file)))[key]; for (const expert of seeds) { for (const location of expert.locations) { assert.ok(branches[location.id][role].some(r => r.name === `${expert.firstName} ${expert.lastName}`)); } } } const kazemi = branches["210120"].reviewers.find(r => r.name.includes("کاظمي دولت سرا")); assert.equal(kazemi.tp, "3992"); assert.equal(kazemi.body, "3991"); }); test("hull flow derives all three licence replacement states from dates", () => { const script = fs.readFileSync(path.join(__dirname, "fanavaran-flow-test.sh"), "utf8"); const start = script.indexOf('const e=process.env, n=(k)=>Number(e[k]), nullable='); const builder = script.slice(start, script.indexOf('\nNODE', start)); for (const [issue, expected] of [ ["1404/08/24", 0], ["1401/08/24", null], ["1394/08/24", 1], ["1402/08/24", 0], ["1395/08/24", null], ]) { let output = ""; vm.runInNewContext(builder, { process: { env: { POLICE_REPORT_DESC: "شرح حادثه", CULPRIT_LICENCE_ISSU_DATE: issue, ACCIDENT_DATE: "1405/08/24" }, stdout: { write: text => { output += text; } }, } }); assert.equal(JSON.parse(output).IsLicenseReplaced, expected, issue); } });