forked from Yara724/api
fix(flow-ui): enable explicit person role registration
Expose person registration for hull claims, require selection of the supported role 166 and check that role even when a driver ID is cached. Keep claim notifier validation out of person registration and populate the verified hull driver reference after success.
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
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");
|
||||
const { spawnSync } = require("node:child_process");
|
||||
const html = fs.readFileSync(path.join(__dirname, "data/fanavaran-flow-ui.html"), "utf8");
|
||||
const shell = fs.readFileSync(path.join(__dirname, "fanavaran-flow-test.sh"), "utf8");
|
||||
|
||||
test("hull selection keeps the registration tab visible and preserves an open registration panel", () => {
|
||||
const tab = { hidden: true, classList: { contains: () => true } };
|
||||
const panel = { hidden: false };
|
||||
const element = { closest: () => ({ querySelector: () => ({}) }) };
|
||||
const context = vm.createContext({
|
||||
q: (id) => id === "FANAVARAN_PRODUCT" ? "CAR_BODY" : "v14",
|
||||
$: () => element,
|
||||
showFormTab: () => assert.fail("switching product must not eject the person tab"),
|
||||
document: {
|
||||
body: { dataset: {} },
|
||||
querySelector: (selector) => selector.includes('panel="person"') ? panel : tab,
|
||||
},
|
||||
});
|
||||
const start = html.indexOf(" function syncProduct()");
|
||||
const end = html.indexOf(" function missingRequiredNotifier", start);
|
||||
vm.runInContext(html.slice(start, end) + "\nsyncProduct();", context);
|
||||
assert.equal(tab.hidden, false);
|
||||
assert.equal(panel.hidden, false);
|
||||
});
|
||||
|
||||
function selectDriver(product, rows, afterCreate = false, requiredRole = "") {
|
||||
const start = shell.indexOf(' DRIVER_ID="$(node -e \'', shell.indexOf(afterCreate ? "create_other_person() {" : "inquire_driver_id() {"));
|
||||
const bodyStart = shell.indexOf("\n", start) + 1;
|
||||
const end = shell.indexOf("\n' ", bodyStart);
|
||||
let selected = "";
|
||||
const exited = {};
|
||||
const context = vm.createContext({
|
||||
require: () => ({ readFileSync: () => JSON.stringify(rows) }),
|
||||
process: { argv: afterCreate ? ["node", "fixture", product, requiredRole] : ["node", "fixture", "166", product, requiredRole], exit: () => { throw exited; } },
|
||||
console: { log: (value) => { selected = String(value); } },
|
||||
});
|
||||
try { vm.runInContext(shell.slice(bodyStart, end), context); }
|
||||
catch (error) { if (error !== exited) throw error; }
|
||||
return selected;
|
||||
}
|
||||
|
||||
for (const afterCreate of [false, true]) {
|
||||
test(`hull ${afterCreate ? "post-registration verification" : "inquiry"} requires role 166`, () => {
|
||||
assert.equal(selectDriver("CAR_BODY", [{ Id: 41, RoleId: 161 }], afterCreate), "");
|
||||
assert.equal(selectDriver("CAR_BODY", [{ Id: 41, RoleId: 161 }, { Id: 42, RoleId: 166 }], afterCreate), "42");
|
||||
});
|
||||
test(`third-party ${afterCreate ? "verification" : "inquiry"} retains role-161 acceptance`, () => {
|
||||
assert.equal(selectDriver("THIRD_PARTY", [{ Id: 41, RoleId: 161 }], afterCreate), "41");
|
||||
});
|
||||
}
|
||||
|
||||
test("the inline UI script parses", () => {
|
||||
const script = html.match(/<script>([\s\S]*?)<\/script>/)[1];
|
||||
new vm.Script(script);
|
||||
});
|
||||
|
||||
for (const [client, version] of [["moallem", "v14"], ["tejaratno", "v15"]]) {
|
||||
test(`${client} ${version}: registering a person does not require claim notifier fields`, () => {
|
||||
const context = vm.createContext({
|
||||
q: (id) => ({ FANAVARAN_PRODUCT: "THIRD_PARTY", FANAVARAN_CLIENT: client,
|
||||
FANAVARAN_API_VERSION: version, CLAIM_NOTIFICATION_DATE: "1405/08/30",
|
||||
NOTIFIER_PARTY_ID: "41" })[id] || "",
|
||||
lookupValue: () => "",
|
||||
stateIds: { CLAIM_ID: "" },
|
||||
});
|
||||
const start = html.indexOf(" function missingRequiredNotifier(stage)");
|
||||
const end = html.indexOf(" function envToText", start);
|
||||
vm.runInContext(html.slice(start, end), context);
|
||||
assert.equal(vm.runInContext('missingRequiredNotifier("person")', context), "");
|
||||
assert.match(vm.runInContext('missingRequiredNotifier("base")', context), /NotifierTypeId/);
|
||||
assert.match(vm.runInContext('missingRequiredNotifier("base")', context), /NotifierRelationId/);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
for (const afterCreate of [false, true]) {
|
||||
test(`manual person ${afterCreate ? "verification" : "inquiry"} enforces the selected role even in third-party mode`, () => {
|
||||
assert.equal(selectDriver("THIRD_PARTY", [{ Id: 41, RoleId: 161 }], afterCreate, "166"), "");
|
||||
assert.equal(selectDriver("THIRD_PARTY", [{ Id: 41, RoleId: 161 }, { Id: 42, RoleId: 166 }], afterCreate, "166"), "42");
|
||||
});
|
||||
}
|
||||
|
||||
test("manual role-166 registration checks live inquiry despite a cached driver ID", () => {
|
||||
const start = shell.indexOf("inquire_driver_id() {");
|
||||
const end = shell.indexOf("create_other_person() {", start);
|
||||
const script = `set -euo pipefail
|
||||
${shell.slice(start, end)}
|
||||
is_car_body() { return 1; }
|
||||
ask_var() { :; }
|
||||
info() { :; }
|
||||
ok() { :; }
|
||||
warn() { :; }
|
||||
persist_env() { :; }
|
||||
save_ids() { :; }
|
||||
fv_curl_json() { echo LIVE_INQUIRY; }
|
||||
node() { printf 42; }
|
||||
create_other_person() { echo UNEXPECTED_CREATE; }
|
||||
DRIVER_ID=41
|
||||
DRIVER_IS_INSURER=1
|
||||
DRIVER_NATIONAL_CODE=0012345678
|
||||
DRIVER_BIRTH_YEAR=1360
|
||||
DRIVER_BIRTH_MONTH=1
|
||||
DRIVER_BIRTH_DAY=2
|
||||
FANAVARAN_PRODUCT=THIRD_PARTY
|
||||
FANAVARAN_BIME_URL=https://example.invalid
|
||||
OUT_DIR=/tmp
|
||||
inquire_driver_id 166
|
||||
printf 'RESULT=%s' "$DRIVER_ID"
|
||||
`;
|
||||
const result = spawnSync("/bin/bash", ["-c", script], { encoding: "utf8", timeout: 5000 });
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
assert.match(result.stdout, /LIVE_INQUIRY/);
|
||||
assert.match(result.stdout, /RESULT=42/);
|
||||
assert.doesNotMatch(result.stdout, /UNEXPECTED_CREATE/);
|
||||
});
|
||||
|
||||
test("the registration button requires an explicit supported role before sending a request", async () => {
|
||||
const notices = [];
|
||||
let focused = false;
|
||||
const context = vm.createContext({
|
||||
busy: false,
|
||||
ping: async () => true,
|
||||
q: () => "",
|
||||
setStatus: (message) => notices.push(message),
|
||||
showFormTab: (name) => assert.equal(name, "person"),
|
||||
$: (id) => { assert.equal(id, "PERSON_ROLE_ID"); return { focus: () => { focused = true; } }; },
|
||||
api: () => assert.fail("registration cannot run before role selection"),
|
||||
});
|
||||
const start = html.indexOf(" async function runStage(stage)");
|
||||
const end = html.indexOf(' $("LOCATION").addEventListener', start);
|
||||
vm.runInContext(html.slice(start, end), context);
|
||||
await vm.runInContext('runStage("person")', context);
|
||||
assert.equal(focused, true);
|
||||
assert.match(notices[0], /166/);
|
||||
});
|
||||
Reference in New Issue
Block a user