import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { test } from "node:test"; import { computeStandings, tiebreakers } from "../src/standings.ts"; import { csvRows, extract, extractMatches, isMatchExport, parseTable } from "../src/tables.ts"; const fixture = (f: string) => parseTable(readFileSync(new URL(`./fixtures/${f}`, import.meta.url), "utf8")); test("matches export: detects shape, reads byes and null GameWins", () => { const rows = fixture("matches.json"); assert.ok(isMatchExport(rows)); assert.deepEqual(extractMatches(rows), [ { round: 4, a: "Eve Evans", b: null, w1: 2, w2: 0, draws: 0 }, { round: 4, a: "Alice Archer", b: "Bob Baker", w1: 2, w2: 0, draws: 0 }, { round: 4, a: "Cara Chen", b: "Dan Doyle", w1: 1, w2: 2, draws: 0 }, ]); }); test("matches export: unreported matches are skipped, empty file rejected", () => { assert.throws(() => extractMatches([{ HasResult: false, Competitors: [] }]), /No completed matches/); }); test("standings export: only the final round is used, real names", () => { const rows = fixture("standings.json"); assert.ok(!isMatchExport(rows)); assert.deepEqual(extract(rows).map((r) => [r.name, r.points, r.wdl]), [ ["Alice Archer", 6, [2, 0, 0]], ["Bob Baker", 1, [0, 1, 1]], ]); }); test("generic CSV: quoted commas, header aliases, helpful error", () => { assert.deepEqual(extract(parseTable('Rank,Player,Points,Deck\n1,"Bob, Jr",6,Elves\n')), [{ name: "Bob, Jr", points: 6, deck: "Elves", wdl: null }]); assert.throws(() => extract(parseTable("Rank,Foo\n1,x")), /Headers in the file: Rank, Foo/); assert.deepEqual(csvRows('1,"a ""q"" b",x', ","), [["1", 'a "q" b', "x"]]); }); test("rejects non-numeric scores in uploaded JSON", () => { const bad = [{ Team: { Players: [{ Name: "A" }] }, RoundNumber: 1, Points: 3, MatchWins: "x", MatchDraws: 0, MatchLosses: 0 }]; assert.throws(() => extract(bad), /Bad numbers/); }); test("leaderboard: best-N sessions count, bye is a win, W-D-L unknown for points-only imports", () => { const names = new Map([[1, "Ann"], [2, "Bob"], [3, "Cy"]]); const ms = [ { session_id: 1, p1_id: 1, p2_id: 2, p1_deck: "Burn", p2_deck: "Elves", p1_wins: 2, p2_wins: 1 }, { session_id: 1, p1_id: 3, p2_id: null, p1_deck: null, p2_deck: null, p1_wins: 2, p2_wins: 0 }, ]; const rs = [ { session_id: 2, player_id: 1, points: 9, deck: "Burn", wins: 3, draws: 0, losses: 0 }, { session_id: 2, player_id: 2, points: 4, deck: null, wins: null, draws: null, losses: null }, ]; const { rows, decks } = computeStandings(ms, rs, names, 1); assert.deepEqual(rows.map((r) => [r.name, r.score, r.total, r.rec]), [ ["Ann", 9, 12, [4, 0, 0]], // best 1 of {3, 9} ["Bob", 4, 4, null], ["Cy", 3, 3, [1, 0, 0]], ]); assert.deepEqual(decks, [["Burn", [4, 0, 0]], ["Elves", [0, 0, 1]]]); }); test("a session with match rows ignores its imported standings", () => { const names = new Map([[1, "Ann"], [2, "Bob"]]); const ms = [{ session_id: 1, p1_id: 1, p2_id: 2, p1_deck: null, p2_deck: null, p1_wins: 2, p2_wins: 0 }]; const rs = [{ session_id: 1, player_id: 2, points: 99, deck: null, wins: 9, draws: 0, losses: 0 }]; assert.equal(computeStandings(ms, rs, names, 6).rows.find((r) => r.name === "Bob")!.score, 0); }); const match = (p1_id: number, p2_id: number | null, p1_wins: number, p2_wins: number, draws = 0) => ({ session_id: 1, p1_id, p2_id, p1_deck: null, p2_deck: null, p1_wins, p2_wins, draws }); test("Melee tiebreakers: a bye is a 2-0 win but not an opponent, opponents floored at 1/3", () => { const tb = tiebreakers([match(1, null, 2, 0), match(1, 2, 2, 1)]); assert.deepEqual(tb.get(1), { omw: 1 / 3, gw: 12 / 15, ogw: 1 / 3 }); // Fay went 0-1 and 1-2, both floored assert.deepEqual(tb.get(2), { omw: 1, gw: 1 / 3, ogw: 12 / 15 }); }); test("leaderboard: points ties fall to OMW%, then GW%, before name", () => { const names = new Map([[1, "Ann"], [2, "Bob"], [3, "Cy"], [4, "Dee"]]); const ms = [match(1, 2, 2, 0), match(3, 4, 2, 0), match(1, 3, 2, 1), match(2, 4, 2, 0)]; const { rows } = computeStandings(ms, [], names, 6); // Bob and Cy: 3 points, equal OMW (Ann 100%, Dee floored to 33%); Cy's GW% 60% beats Bob's 50% assert.deepEqual(rows.map((r) => r.name), ["Ann", "Cy", "Bob", "Dee"]); });