import test from "node:test"; import assert from "node:assert/strict"; import { createOverlayLiveMarks, formatPitClock, passingFlashAttrs, renderLiveMarkBadges, PASSING_FLASH_MS } from "../src/overlay_live_marks.js"; const escapeHtml = (value) => String(value); const formatLap = (ms) => `${Math.floor(ms / 60000)}:${String(Math.floor(ms % 60000 / 1000)).padStart(2, "0")}.${String(ms % 1000).padStart(3, "0")}`; test("a new time flashes the row for a moment, but nothing flashes when the overlay first opens", () => { let clock = 1_000_000; const marks = createOverlayLiveMarks({ now: () => clock }); const row = { key: "d1", lastTimestamp: 500 }; assert.equal(marks.observe([row]).get("d1").passingAgeMs, null, "first sight: no flash"); clock += 100; assert.equal(marks.observe([row]).get("d1").passingAgeMs, null, "same time again: no flash"); clock += 100; assert.equal(marks.observe([{ ...row, lastTimestamp: 14_000 }]).get("d1").passingAgeMs, 0); clock += 250; const mark = marks.observe([{ ...row, lastTimestamp: 14_000 }]).get("d1"); assert.equal(mark.passingAgeMs, 250, "a re-render keeps the flash going"); assert.deepEqual(passingFlashAttrs(mark), { className: " is-passing", style: ' style="--pass-delay:-250ms"' }); clock += PASSING_FLASH_MS; assert.equal(marks.observe([{ ...row, lastTimestamp: 14_000 }]).get("d1").passingAgeMs, null); }); test("pit: a running clock from this computer's time, then the stop time for a few seconds", () => { let clock = 2_000_000; const marks = createOverlayLiveMarks({ now: () => clock }); marks.observe([{ key: "d1", lastTimestamp: 1, pitStops: 0 }]); clock += 1000; const inPit = marks.observe([{ key: "d1", lastTimestamp: 1, pitActive: true, pitEnteredAt: 42, pitStops: 0 }]).get("d1"); assert.deepEqual(inPit.pit, { active: true, startAt: clock }, "the decoder's entry time is not used once the overlay saw the entry"); assert.match(renderLiveMarkBadges(inPit, { formatLap, escapeHtml, nowMs: clock + 12_400 }), /data-pit-clock="\d+".*0:12\.4/s); clock += 24_000; const out = marks.observe([{ key: "d1", lastTimestamp: 1, pitActive: false, pitStops: 1, lastPitStopMs: 24_512 }]).get("d1"); assert.equal(out.pit.stopMs, 24_512); assert.match(renderLiveMarkBadges(out, { formatLap, escapeHtml }), /is-done.*0:24\.512/s); clock += 5000; assert.equal(marks.observe([{ key: "d1", lastTimestamp: 1, pitStops: 1, lastPitStopMs: 24_512 }]).get("d1").pit, null); }); test("a car already in the pits when the overlay opens gets its clock from the entry time", () => { const clock = 3_000_000; const marks = createOverlayLiveMarks({ now: () => clock }); assert.equal(marks.observe([{ key: "d1", pitActive: true, pitEnteredAt: clock - 8000 }]).get("d1").pit.startAt, clock - 8000); const odd = createOverlayLiveMarks({ now: () => clock }); assert.equal(odd.observe([{ key: "d1", pitActive: true, pitEnteredAt: clock + 60_000 }]).get("d1").pit.startAt, clock, "a decoder clock ahead of ours starts at 0"); }); test("a new split shows for a few seconds with its gain against the previous lap", () => { let clock = 4_000_000; const marks = createOverlayLiveMarks({ now: () => clock }); marks.observe([{ key: "d1", lastSplitTimestamp: 10 }]); clock += 50; const mark = marks.observe([{ key: "d1", lastSplitTimestamp: 20, lastSplitMs: 5123, lastSplitDeltaMs: -120 }]).get("d1"); assert.match(renderLiveMarkBadges(mark, { formatLap, escapeHtml }), /S 0:05\.123 −0\.120/); clock += 4000; assert.equal(marks.observe([{ key: "d1", lastSplitTimestamp: 20, lastSplitMs: 5123 }]).get("d1").split, null); }); test("formatPitClock", () => { assert.equal(formatPitClock(12_480), "0:12.4"); assert.equal(formatPitClock(75_000), "1:15.0"); assert.equal(formatPitClock(-5), "0:00.0"); });