Files
Live_RC/tests/houseofrc_logic.test.js
T

149 lines
12 KiB
JavaScript

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, rememberHouseOfRcClass } = 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;
assert.deepEqual([plan[0].brandDiffers, plan[0].brandDefault, plan[1].brandDiffers], [true, true, false], "a different car on an existing driver is offered; pre-ticked as our brand is empty");
register[1].brand = "Team Robban";
const robban = planHouseOfRcEntries([{ ...entries[1], car: "Mugen" }], register, "2wd")[0];
assert.deepEqual([robban.brandDiffers, robban.brandDefault, robban.currentBrand], [true, false, "Team Robban"], "our brand is never pre-ticked away");
const { driverIds, entryIds } = applyHouseOfRcEntries(register, plan, { classId: "2wd", selected: new Set([0, 1, 2, 3, 4]), brands: new Set([0]), makeDriver: (fields) => ({ id: `new${++n}`, ...fields }) });
assert.equal(register[0].brand, "Xray XB2", "the ticked car is written");
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.deepEqual(matchHouseOfRcClass([{ id: "c1", name: "Offroad 2WD foam" }], "offroad 2wd Foam"), { id: "c1", how: "exact" });
assert.deepEqual(matchHouseOfRcClass([{ id: "c1", name: "Touring" }], "F1"), { id: "", how: "" });
const ours = [{ id: "s", name: "Stock 17.5T" }, { id: "b2", name: "2WD Buggy" }, { id: "b4", name: "4WD Buggy" }, { id: "x2", name: "2wd" }];
assert.deepEqual(matchHouseOfRcClass(ours, "4wd"), { id: "b4", how: "guess" }, "a whole word of one class");
assert.deepEqual(matchHouseOfRcClass(ours, "2wd"), { id: "x2", how: "exact" }, "an exact name wins");
assert.equal(matchHouseOfRcClass(ours, "Buggy").id, "", "two candidates: let the user choose");
assert.equal(matchHouseOfRcClass(ours, "17.5").id, "", "part of a word is no match");
rememberHouseOfRcClass(ours, "b2", "2wd");
assert.deepEqual(matchHouseOfRcClass(ours, "2WD"), { id: "b2", how: "remembered" }, "the last choice wins over the name");
rememberHouseOfRcClass(ours, "s", "2wd");
assert.deepEqual([ours[1].houseOfRcNames, ours[0].houseOfRcNames], [undefined, ["2wd"]], "a name points at one class only");
});
test("House of RC re-fetch: late entries are added, withdrawn ones offered for removal, hand-added drivers left alone", async () => {
const { diffHouseOfRcEntries, applyHouseOfRcEntryChanges } = await import("../src/houseofrc_logic.js");
const drivers = [
{ id: "a", name: "Anna", classId: "2wd", transponder: "1", houseOfRcId: "5" },
{ id: "b", name: "Bo", classId: "2wd", transponder: "2", houseOfRcId: "6" },
{ id: "h", name: "Hand Added", classId: "2wd", transponder: "3" },
];
const event = { classId: "2wd", raceConfig: { driverIds: ["a", "b", "h"] }, houseOfRc: { eventId: 1, classId: 555, entryIds: { a: 11, b: 12 } } };
const entries = [
{ entryId: 11, classId: 555, houseOfRcId: 5, name: "Anna", transponders: ["1"] },
{ entryId: 14, classId: 555, houseOfRcId: 8, name: "Late Larsson", transponders: ["9"], car: "Mugen" },
{ entryId: 15, classId: 556, houseOfRcId: 9, name: "Other Class", transponders: [] },
];
const diff = diffHouseOfRcEntries(event, entries, drivers);
assert.deepEqual(diff.added.map((row) => row.entry.name), ["Late Larsson"], "only new entries of the race's class");
assert.deepEqual(diff.withdrawn.map((driver) => driver.id), ["b"], "Bo's entry is gone; the hand-added driver is not offered");
const ids = applyHouseOfRcEntryChanges(event, drivers, diff, { addIndexes: new Set([0]), removeIds: new Set(["b"]), makeDriver: (fields) => ({ id: "late", ...fields }) });
assert.deepEqual(ids, ["a", "h", "late"]);
assert.deepEqual(event.houseOfRc.entryIds, { a: 11, late: 14 });
assert.equal(drivers.find((driver) => driver.id === "late").transponder, "9");
assert.deepEqual(diffHouseOfRcEntries({ ...event, raceConfig: { driverIds: ids } }, entries, drivers), { added: [], withdrawn: [] }, "fetching again finds nothing new");
// The hand-added driver enters on House of RC later: linked, not added twice.
event.raceConfig.driverIds = ids;
const later = diffHouseOfRcEntries(event, [...entries, { entryId: 17, classId: 555, houseOfRcId: 10, name: "Hand Added", transponders: ["3"] }], drivers);
assert.deepEqual(later.added.map((row) => [row.entry.name, Boolean(row.linkOnly)]), [["Hand Added", true]]);
const linkedIds = applyHouseOfRcEntryChanges(event, drivers, later, { addIndexes: new Set([0]), removeIds: new Set(), makeDriver: () => { throw new Error("no new driver"); } });
assert.deepEqual(linkedIds, ["a", "h", "late"], "same participants, same order");
assert.equal(event.houseOfRc.entryIds.h, 17, "now gets its results sent");
});
test("House of RC results: heats and qualifying without placings, then final placings by main", async () => {
const { buildHouseOfRcResults, formatHouseOfRcTime } = await import("../src/houseofrc_logic.js");
assert.deepEqual([formatHouseOfRcTime(13201), formatHouseOfRcTime(303412), formatHouseOfRcTime(0)], ["13.201", "5:03.412", ""]);
const event = { raceConfig: { driverIds: ["a", "b", "h"] }, houseOfRc: { eventId: 1, entryIds: { a: 11, b: 12, gone: 13 } } };
const drivers = [{ id: "a", name: "Anna" }, { id: "b", name: "Bo" }, { id: "h", name: "Hand Added" }];
const heat = (name, rows) => ({ session: { name }, rows });
const overview = { phases: [
{ type: "qualification", standings: [{ driverId: "b", rank: 1 }, { driverId: "a", rank: 2 }], heats: [
heat("Kval 1", [{ pos: 1, driverId: "b", laps: 12, timeMs: 300500, bestLapMs: 13201 }, { pos: 2, driverId: "a", laps: 11, timeMs: 301000, bestLapMs: 14002 }]),
] },
{ type: "final", standings: [{ driverId: "a", rank: "A1" }, { driverId: "b", rank: "A2" }], heats: [
heat("A-final", [{ pos: 1, driverId: "a", laps: 22, timeMs: 303412, bestLapMs: 12950 }, { pos: 2, driverId: "b", laps: 21, timeMs: 0, bestLapMs: 13100, raceStatus: "dnf" }]),
] },
] };
const live = buildHouseOfRcResults(event, overview, { drivers });
assert.deepEqual(live.missing, ["Hand Added"], "a hand-added driver has no entry to write to");
assert.deepEqual(live.entries.map((entry) => entry.id), [11, 12], "only participants; a removed driver's entry is not sent");
assert.ok(live.entries.every((entry) => !("ranking" in entry)), "no placing yet, so nobody is notified");
const bo = live.entries[1];
assert.equal(bo.qualification, 1);
assert.equal(bo.tq, true);
assert.deepEqual(bo.data.result.heats[1], { heat: "A-final", laps: 21, time: "DNF", best_lap: "13.100", position: 2 });
assert.equal(bo.data.result.best_lap, "13.100");
const done = buildHouseOfRcResults(event, overview, { final: true, drivers });
assert.deepEqual(done.entries.map((entry) => [entry.id, entry.ranking, entry.data.result.main, entry.data.result.main_position]), [[11, 1, "A", 1], [12, 2, "A", 2]]);
assert.equal(done.entries[0].laps_time, "22/5:03.412");
assert.deepEqual(done.rows.map((row) => row.name), ["Anna", "Bo"], "preview in final order");
const bumped = { phases: [{ type: "final", standings: [{ driverId: "b", rank: "A1" }, { driverId: "a", rank: "A2" }, { driverId: "h", rank: "B1" }, { driverId: "b", rank: "B2" }], heats: [] }] };
const withBump = buildHouseOfRcResults({ ...event, houseOfRc: { entryIds: { a: 11, b: 12, h: 13 } } }, bumped, { final: true, drivers });
assert.deepEqual(withBump.entries.map((entry) => [entry.id, entry.ranking, entry.data.result.main, entry.data.result.bumped_from]), [[11, 2, "A", undefined], [12, 1, "A", ["B"]], [13, 3, "B", undefined]], "a bumped driver counts once, in the higher main");
const noQual = buildHouseOfRcResults(event, { phases: [] }, { drivers });
assert.ok(!("qualification" in noQual.entries[0]), "before any qualifying, House of RC's value is left alone");
});