import test from "node:test"; import assert from "node:assert/strict"; import { planHouseOfRcDriverUpdates, applyHouseOfRcDriverUpdates } from "../src/houseofrc_logic.js"; const members = { "1001": { id: 68, name: "Anna Andersson", brand: "Associated", model: "B7" }, "1002": { id: 70, name: "Bo Berg", brand: "Xray", model: "XB4" }, "1003": { id: 71, name: "Cecilia Carlsson", brand: "", model: "" }, "1004": { id: 68, name: "Anders Larsson", brand: "", model: "Xray XB4 2025" }, }; const drivers = () => [ { id: "a", name: "1001", transponder: "1001", brand: "" }, { id: "b", name: "Bosse", transponder: "1002", brand: "Team Berg" }, { id: "c", name: "cecilia carlsson", transponder: "1003", brand: "" }, { id: "d", name: "Dan", transponder: "9999", brand: "" }, { id: "e", name: "anders larsson", transponder: "1004", brand: "" }, { id: "g", name: "", transponder: "1001", brand: "", guestEventId: "e1" }, ]; test("House of RC plan: only matched drivers, pre-ticked only where our field is empty or just the transponder number", () => { const plan = planHouseOfRcDriverUpdates(drivers(), members); assert.deepEqual(plan.map((row) => row.driverId), ["a", "b", "c", "e"], "unknown transponders and sponsor guests are left out"); const [a, b, c, e] = plan; assert.deepEqual([a.nameDiffers, a.nameDefault, a.brandDiffers, a.brandDefault], [true, true, true, true]); assert.deepEqual([b.nameDiffers, b.nameDefault, b.brandDiffers, b.brandDefault], [true, false, true, false], "an existing name or brand is never pre-ticked"); assert.deepEqual([c.nameDiffers, c.nameDefault, c.brandDiffers], [true, true, false], "only case/spacing differs: offered and pre-ticked; no brand from HoR"); assert.deepEqual([e.nameDefault, e.brandDefault, e.member.brand], [true, true, "Xray XB4 2025"], "no brand object: the model name is the make"); }); test("House of RC apply: writes only the ticked fields and links drivers whose name matches", () => { const list = drivers(); const changed = applyHouseOfRcDriverUpdates(list, members, { a: { name: true, brand: true }, b: { name: false, brand: true } }); const byId = Object.fromEntries(list.map((driver) => [driver.id, driver])); assert.deepEqual([byId.a.name, byId.a.brand, byId.a.houseOfRcId], ["Anna Andersson", "Associated", "68"]); assert.deepEqual([byId.b.name, byId.b.brand, byId.b.houseOfRcId], ["Bosse", "Xray", undefined], "name kept, so not linked (could be a borrowed transponder)"); assert.deepEqual([byId.c.name, byId.c.houseOfRcId], ["cecilia carlsson", "71"], "same person: linked without renaming"); assert.equal(byId.g.name, "", "sponsor guests untouched"); assert.equal(changed, 4, "c and e are linked by name even without ticks"); }); test("House of RC entries: matched on member, transponder or name; other class and new drivers", async () => { const { planHouseOfRcEntries, applyHouseOfRcEntries, matchHouseOfRcClass } = await import("../src/houseofrc_logic.js"); const register = [ { id: "d1", name: "Anna Andersson", classId: "2wd", transponder: "111", houseOfRcId: "5" }, { id: "d2", name: "Robban", classId: "2wd", transponder: "222" }, { id: "d3", name: "bo berg", classId: "2wd", transponder: "" }, { id: "d4", name: "Cid Ceder", classId: "4wd", transponder: "333", houseOfRcId: "7", profileId: "p3", club: "ÖRC" }, ]; const entries = [ { entryId: 1, houseOfRcId: 5, name: "Anna A.", transponders: ["111"], club: "ÖRC", car: "Xray XB2" }, { entryId: 2, houseOfRcId: 6, name: "Robert Nilsson", transponders: ["222"], club: "", car: "" }, { entryId: 3, houseOfRcId: 9, name: "Bo Berg", transponders: ["444"], club: "", car: "" }, { entryId: 4, houseOfRcId: 7, name: "Cid Ceder", transponders: ["555"], club: "ÖRC", car: "Associated B7" }, { entryId: 5, houseOfRcId: 8, name: "Dan Ny", transponders: ["666", "667"], club: "Sundsvall RC", car: "Mugen MSB1" }, ]; const plan = planHouseOfRcEntries(entries, register, "2wd"); assert.deepEqual(plan.map((row) => `${row.status}:${row.driverId || row.sourceId || ""}`), ["linked:d1", "linked:d2", "linked:d3", "copy:d4", "new:"], "member, then transponder, then name"); let n = 0; const { driverIds, entryIds } = applyHouseOfRcEntries(register, plan, { classId: "2wd", selected: new Set([0, 1, 2, 3, 4]), makeDriver: (fields) => ({ id: `new${++n}`, ...fields }) }); assert.deepEqual(driverIds, ["d1", "d2", "d3", "new1", "new2"]); assert.deepEqual(entryIds, { d1: 1, d2: 2, d3: 3, new1: 4, new2: 5 }, "each driver remembers their House of RC entry"); assert.deepEqual([register[1].name, register[1].houseOfRcId], ["Robban", "6"], "our name is kept, the member is linked"); assert.equal(register[2].transponder, "444", "an empty transponder is filled from the entry"); const cid = register.find((d) => d.id === "new1"); assert.deepEqual([cid.classId, cid.transponder, cid.profileId, cid.brand], ["2wd", "555", "p3", "Associated B7"], "other class: same profile, this entry's transponder"); const dan = register.find((d) => d.id === "new2"); assert.deepEqual([dan.name, dan.transponder, dan.club, dan.houseOfRcId], ["Dan Ny", "666", "Sundsvall RC", "8"]); assert.equal(matchHouseOfRcClass([{ id: "c1", name: "Offroad 2WD foam" }], "offroad 2wd Foam"), "c1"); assert.equal(matchHouseOfRcClass([{ id: "c1", name: "Touring" }], "F1"), ""); const ours = [{ id: "s", name: "Stock 17.5T" }, { id: "b2", name: "2WD Buggy" }, { id: "b4", name: "4WD Buggy" }, { id: "x2", name: "2wd" }]; assert.equal(matchHouseOfRcClass(ours, "4wd"), "b4", "a whole word of one class"); assert.equal(matchHouseOfRcClass(ours, "2wd"), "x2", "an exact name wins"); assert.equal(matchHouseOfRcClass(ours, "Buggy"), "", "two candidates: let the user choose"); assert.equal(matchHouseOfRcClass(ours, "17.5"), "", "part of a word is no match"); });