78 lines
4.4 KiB
JavaScript
78 lines
4.4 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createRequire } from "node:module";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
const { keepStartedSessions, keepRaceControl } = require("../server/session_guard.js");
|
|
|
|
const stored = () => ({
|
|
sessions: [
|
|
{ id: "a1", status: "finished", startedAt: 100, endedAt: 400 },
|
|
{ id: "a2", status: "running", startedAt: 500 },
|
|
{ id: "a3", status: "ready", startedAt: null },
|
|
],
|
|
resultsBySession: { a1: { competitors: { k: { laps: 14 } } }, a2: { competitors: { k: { laps: 3 } } } },
|
|
});
|
|
|
|
test("a save from an older copy (the second computer, not master) can't put run heats back to ready", () => {
|
|
const incoming = {
|
|
sessions: [{ id: "a1", status: "ready", startedAt: null }, { id: "a2", status: "ready", startedAt: null }, { id: "a3", status: "ready", startedAt: null }],
|
|
resultsBySession: {},
|
|
};
|
|
const { state, kept } = keepStartedSessions(incoming, stored());
|
|
assert.deepEqual(kept, ["a1", "a2"]);
|
|
assert.equal(state.sessions[0].status, "finished");
|
|
assert.equal(state.sessions[1].status, "running");
|
|
assert.equal(state.resultsBySession.a1.competitors.k.laps, 14, "A final 1's laps are kept");
|
|
});
|
|
|
|
test("a copy from while a heat was running can't undo its finish", () => {
|
|
const incoming = { sessions: [{ id: "a1", status: "running", startedAt: 100 }], resultsBySession: { a1: { competitors: {} } } };
|
|
const { state, kept } = keepStartedSessions(incoming, stored());
|
|
assert.deepEqual(kept, ["a1"]);
|
|
assert.equal(state.sessions[0].status, "finished");
|
|
});
|
|
|
|
test("Reset still works, and a copy from before the reset can't bring the old run back", () => {
|
|
const reset = { sessions: [{ id: "a1", status: "ready", startedAt: null, resultsResetAt: 900 }], resultsBySession: { a1: { competitors: {} } } };
|
|
const after = keepStartedSessions(reset, stored());
|
|
assert.deepEqual(after.kept, []);
|
|
assert.equal(after.state.sessions[0].status, "ready");
|
|
const old = { sessions: [{ id: "a1", status: "finished", startedAt: 100 }] };
|
|
const { state, kept } = keepStartedSessions(old, { sessions: reset.sessions, resultsBySession: reset.resultsBySession });
|
|
assert.deepEqual(kept, ["a1"]);
|
|
assert.equal(state.sessions[0].resultsResetAt, 900);
|
|
});
|
|
|
|
test("normal progress, new and deleted heats pass through untouched", () => {
|
|
const incoming = { sessions: [{ id: "a2", status: "finished", startedAt: 500, endedAt: 800 }, { id: "a3", status: "running", startedAt: 900 }, { id: "new", status: "ready" }] };
|
|
const { state, kept } = keepStartedSessions(incoming, stored());
|
|
assert.deepEqual(kept, []);
|
|
assert.deepEqual(state.sessions.map((session) => session.id), ["a2", "a3", "new"], "a1 deleted on purpose stays deleted");
|
|
assert.equal(keepStartedSessions(null, stored()).state, null);
|
|
});
|
|
|
|
test("a save from the computer that is not master keeps the master's race control but its other edits", () => {
|
|
const master = { ...stored(), activeSessionId: "a2", sessions: [...stored().sessions, { id: "q1", status: "finished", startedAt: 50, endedAt: 90 }] };
|
|
master.resultsBySession.q1 = { competitors: { k: { laps: 9 } } };
|
|
const slaveCopy = {
|
|
activeSessionId: "a3",
|
|
drivers: [{ id: "d1", name: "Renamed on the slave" }],
|
|
// older copy: a2 not started yet, a3 picked to look at, q1 missing, a new heat added
|
|
sessions: [{ id: "a1", status: "finished", startedAt: 100, endedAt: 400, name: "A final 1" }, { id: "a2", status: "ready", startedAt: null, driverIds: ["d1", "d2"] }, { id: "a3", status: "ready", calledAt: 5 }, { id: "b1", status: "ready" }],
|
|
resultsBySession: {},
|
|
};
|
|
const state = keepRaceControl(slaveCopy, master);
|
|
assert.equal(state.activeSessionId, "a2", "looking at a heat on the slave doesn't move the master's focus");
|
|
assert.equal(state.drivers[0].name, "Renamed on the slave");
|
|
const a2 = state.sessions.find((session) => session.id === "a2");
|
|
assert.equal(a2.status, "running");
|
|
assert.equal(a2.startedAt, 500);
|
|
assert.deepEqual(a2.driverIds, ["d1", "d2"], "the grid edit from the slave is kept");
|
|
assert.equal("calledAt" in state.sessions.find((session) => session.id === "a3"), false);
|
|
assert.ok(state.sessions.some((session) => session.id === "b1"), "a heat added on the slave is kept");
|
|
assert.ok(state.sessions.some((session) => session.id === "q1"), "a run heat missing from the old copy is not deleted");
|
|
assert.equal(state.resultsBySession.q1.competitors.k.laps, 9);
|
|
assert.equal(state.resultsBySession.a1.competitors.k.laps, 14);
|
|
});
|