import { readFileSync } from "node:fs"; import assert from "node:assert/strict"; import { test } from "node:test"; import ts from "typescript"; const exports = {}; new Function("exports", ts.transpileModule(readFileSync(new URL("../src/lib/match-time.ts", import.meta.url), "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 }, }).outputText)(exports); const { matchTime, projectMatch } = exports; const base = { phase: "running", remainingMs: 10_000, elapsedMs: 170_000, pauseElapsedMs: 0, pauseRemainingMs: null, connected: true, freshForMs: 500 }; test("remaining time rounds up, elapsed time rounds down", () => { assert.equal(matchTime(1), "00:01"); assert.equal(matchTime(999, true), "00:00"); assert.equal(matchTime(180_000), "03:00"); assert.equal(matchTime(-1), "00:00"); }); test("interpolation stops at the network lease boundary and backend outage freezes immediately", () => { const v = projectMatch(base, 5000, true); assert.equal(v.remainingMs, 9500); assert.equal(v.elapsedMs, 170_500); assert.equal(v.connected, false); assert.equal(projectMatch(base, 5000, false).remainingMs, 10_000); assert.equal(projectMatch({ ...base, connected: false }, 100, true).remainingMs, 10_000); }); test("paused match freezes underlying clocks; player countdown expires without changing phase", () => { const v = projectMatch({ ...base, phase: "paused", pauseRemainingMs: 200, pauseElapsedMs: 299_800 }, 500, true); assert.equal(v.remainingMs, 10_000); assert.equal(v.elapsedMs, 170_000); assert.equal(v.pauseRemainingMs, 0); assert.equal(v.phase, "paused"); assert.equal(v.pauseElapsedMs, 300_300); assert.equal(projectMatch({ ...base, phase: "paused" }, 500, true).pauseRemainingMs, null); }); test("the UI never starts a match or resumes a pause by interpolating", () => { const v = projectMatch({ ...base, phase: "countdown", remainingMs: 100 }, 500, true); assert.equal(v.remainingMs, 0); assert.equal(v.phase, "countdown"); assert.equal(v.elapsedMs, 170_000); });