import assert from "node:assert/strict"; import test from "node:test"; import type { CompetitionGameView, GameId, GameSlotId, SeriesId, SeriesProjection, TeamId, } from "@lolympus/contracts"; import { competitionHistoryGameDetailPath, createCompetitionSeriesHistory, formatCompetitionHistoryDate, formatCompetitionHistoryDayHeading, formatCompetitionHistoryTime, formatSeriesGameDuration, getSeriesHistoryRecordLabel, getSeriesHistoryResultLabel, getSeriesHistoryStatusLabel, shouldDisplaySeriesHistoryScore, } from "../src/components/competition/competition-series-history.ts"; import { isSeriesEntryWinner } from "../src/components/competition/series-score-card-model.ts"; test("aggregates Games under their Series and preserves Series slot scores", () => { const teamA = teamId("team-a"); const teamB = teamId("team-b"); const series = completedSeries({ id: "series-score", teamA, teamB, scoreA: 2, scoreB: 1, completedAt: "2026-07-12T13:00:00.000Z", }); const laterGame = game({ id: "game-later", seriesId: series.seriesId, ordinal: 2, displayNumber: 4, blueTeamId: teamB, redTeamId: teamA, winnerTeamId: teamB, startedAt: "2026-07-12T12:30:00.000Z", }); const earlierGame = game({ id: "game-earlier", seriesId: series.seriesId, ordinal: 1, displayNumber: 19, blueTeamId: teamA, redTeamId: teamB, winnerTeamId: teamA, startedAt: "2026-07-12T12:00:00.000Z", }); const history = createCompetitionSeriesHistory({ series: [series], games: [laterGame, earlierGame], }); assert.equal(history.length, 1); assert.equal(history[0].dateKey, "2026-07-12"); assert.equal(history[0].rows.length, 1); assert.deepEqual( history[0].rows[0].series.entries.map((entry) => entry.gameWins), [2, 1], ); assert.deepEqual( history[0].rows[0].games.map(({ ordinal }) => ordinal), [1, 2], ); assert.equal(history[0].rows[0].occurredAt, earlierGame.startedAt); assert.equal(history[0].rows[0].timestampSource, "game-start"); }); test("builds Game detail routes from global display numbers, not Series ordinals", () => { const recordedGame = game({ id: "globally-numbered-game", seriesId: seriesId("second-series"), ordinal: 1, displayNumber: 17, blueTeamId: teamId("team-a"), redTeamId: teamId("team-b"), winnerTeamId: teamId("team-a"), startedAt: "2026-07-13T09:00:00.000Z", }); assert.equal( competitionHistoryGameDetailPath( "community-1", "match-1", recordedGame, ), "/communities/community-1/internal-stats/match/match-1/game/17", ); assert.equal( competitionHistoryGameDetailPath("community-1", "match-1", { displayNumber: 0, }), null, ); assert.equal( competitionHistoryGameDetailPath("community-1", "match-1", { displayNumber: 1.5, }), null, ); }); test("groups and orders Series by the Asia/Seoul calendar day", () => { const firstDay = playedSeriesWithGame({ id: "first-day", displayOrder: 3, startedAt: "2026-07-12T14:55:00.000Z", }); const secondDayLater = playedSeriesWithGame({ id: "second-day-later", displayOrder: 2, startedAt: "2026-07-12T16:05:00.000Z", }); const secondDayEarlier = playedSeriesWithGame({ id: "second-day-earlier", displayOrder: 1, startedAt: "2026-07-12T15:05:00.000Z", }); const history = createCompetitionSeriesHistory({ series: [secondDayLater.series, firstDay.series, secondDayEarlier.series], games: [secondDayLater.game, secondDayEarlier.game, firstDay.game], }); assert.deepEqual( history.map(({ dateKey }) => dateKey), ["2026-07-12", "2026-07-13"], ); assert.deepEqual( history[1].rows.map(({ series }) => series.seriesId), [secondDayEarlier.series.seriesId, secondDayLater.series.seriesId], ); }); test("includes recorded in-progress Series and game-less completed decisions only", () => { const teamA = teamId("team-a"); const teamB = teamId("team-b"); const running = inProgressSeries("running-recorded", teamA, teamB, 1, 0); const runningGame = game({ id: "running-game", seriesId: running.seriesId, ordinal: 1, displayNumber: 1, blueTeamId: teamA, redTeamId: teamB, winnerTeamId: teamA, startedAt: "2026-07-13T09:00:00.000Z", }); const administrative = completedSeries({ id: "administrative", teamA, teamB, scoreA: 0, scoreB: 0, completedAt: "2026-07-13T10:00:00.000Z", outcome: "administrative", }); const unrecordedRunning = inProgressSeries( "running-unrecorded", teamA, teamB, 0, 0, ); const history = createCompetitionSeriesHistory({ series: [ readySeries("ready", teamA, teamB), waitingSeries("waiting"), inactiveSeries("inactive"), unrecordedRunning, administrative, running, ], games: [runningGame], }); const rows = history.flatMap(({ rows }) => rows); assert.deepEqual( rows.map(({ series }) => series.seriesId), [running.seriesId, administrative.seriesId], ); assert.equal(rows[0].timestampSource, "game-start"); assert.equal(rows[1].timestampSource, "decision"); assert.equal(rows[1].occurredAt, administrative.outcome.completedAt); }); test("sorts tied timestamps deterministically and ignores orphan Games", () => { const teamA = teamId("team-a"); const teamB = teamId("team-b"); const earlierDisplayOrder = completedSeries({ id: "series-z", teamA, teamB, scoreA: 2, scoreB: 0, displayOrder: 1, completedAt: "2026-07-13T12:00:00.000Z", }); const laterDisplayOrder = completedSeries({ id: "series-a", teamA, teamB, scoreA: 2, scoreB: 1, displayOrder: 2, completedAt: "2026-07-13T12:00:00.000Z", }); const orphanGame = game({ id: "orphan-game", seriesId: seriesId("missing-series"), ordinal: 1, displayNumber: 99, blueTeamId: teamA, redTeamId: teamB, winnerTeamId: teamA, startedAt: "2026-07-13T08:00:00.000Z", }); const rows = createCompetitionSeriesHistory({ series: [laterDisplayOrder, earlierDisplayOrder], games: [orphanGame], }).flatMap(({ rows }) => rows); assert.deepEqual( rows.map(({ series }) => series.seriesId), [earlierDisplayOrder.seriesId, laterDisplayOrder.seriesId], ); assert.equal(rows.every(({ games }) => games.length === 0), true); }); test("separates lifecycle status from played and adjudicated result labels", () => { const teamA = teamId("team-a"); const teamB = teamId("team-b"); const running = inProgressSeries("running", teamA, teamB, 1, 0); const played = completedSeries({ id: "played", teamA, teamB, scoreA: 2, scoreB: 1, }); const playedDraw = completedSeries({ id: "played-draw", teamA, teamB, scoreA: 1, scoreB: 1, outcome: "draw", }); const administrativeDraw = { ...playedDraw, seriesId: seriesId("administrative-draw-label"), outcome: { ...playedDraw.outcome, basis: { type: "administrative" as const, decisionFactId: "draw-label-fact" as never, }, }, }; const walkover = completedSeries({ id: "walkover", teamA, teamB, scoreA: 0, scoreB: 0, outcome: "walkover", }); const forfeit = completedSeries({ id: "forfeit", teamA, teamB, scoreA: 1, scoreB: 1, outcome: "forfeit", }); const administrative = completedSeries({ id: "administrative", teamA, teamB, scoreA: 0, scoreB: 0, outcome: "administrative", }); assert.equal(getSeriesHistoryStatusLabel(running), "진행 중"); for (const completed of [ played, playedDraw, administrativeDraw, walkover, forfeit, administrative, ]) { assert.equal(getSeriesHistoryStatusLabel(completed), "최종"); } assert.equal(getSeriesHistoryResultLabel(running), null); assert.equal(getSeriesHistoryResultLabel(played), null); assert.equal(getSeriesHistoryResultLabel(playedDraw), "무승부"); assert.equal( getSeriesHistoryResultLabel(administrativeDraw), "판정 무승부", ); assert.equal(getSeriesHistoryResultLabel(walkover), "부전승"); assert.equal(getSeriesHistoryResultLabel(forfeit), "기권승"); assert.equal(getSeriesHistoryResultLabel(administrative), "판정승"); }); test("describes recorded Games, decisions, and legacy missing records", () => { const teamA = teamId("record-team-a"); const teamB = teamId("record-team-b"); const played = completedSeries({ id: "recorded-played", teamA, teamB, scoreA: 2, scoreB: 0, }); const decision = completedSeries({ id: "recorded-decision", teamA, teamB, scoreA: 0, scoreB: 0, outcome: "walkover", }); assert.equal(getSeriesHistoryRecordLabel(played, 2, false), "게임 상세 (2)"); assert.equal(getSeriesHistoryRecordLabel(played, 2, true), "게임 상세 접기"); assert.equal(getSeriesHistoryRecordLabel(decision, 0, false), "경기 없이 결정"); assert.equal(getSeriesHistoryRecordLabel(played, 0, false), "게임 기록 미등록"); }); test("shows a numeric score only for played records", () => { const teamA = teamId("team-a"); const teamB = teamId("team-b"); const playedDraw = completedSeries({ id: "played-draw", teamA, teamB, scoreA: 1, scoreB: 1, outcome: "draw", }); const administrativeDraw = { ...playedDraw, seriesId: seriesId("administrative-draw"), outcome: { ...playedDraw.outcome, basis: { type: "administrative" as const, decisionFactId: "draw-fact" as never, }, }, }; assert.equal( shouldDisplaySeriesHistoryScore( inProgressSeries("running-score", teamA, teamB, 1, 0), ), true, ); assert.equal( shouldDisplaySeriesHistoryScore( completedSeries({ id: "played-score", teamA, teamB, scoreA: 2, scoreB: 1, }), ), true, ); assert.equal(shouldDisplaySeriesHistoryScore(playedDraw), true); assert.equal(shouldDisplaySeriesHistoryScore(administrativeDraw), false); assert.equal( shouldDisplaySeriesHistoryScore( completedSeries({ id: "forfeit-score", teamA, teamB, scoreA: 1, scoreB: 1, outcome: "forfeit", }), ), false, ); }); test("uses the official outcome winner even when adjudicated score order differs", () => { const teamA = teamId("official-winner"); const teamB = teamId("score-leader"); const series = completedSeries({ id: "adjudicated-winner", teamA, teamB, scoreA: 0, scoreB: 1, outcome: "forfeit", }); assert.equal(isSeriesEntryWinner(series, teamA), true); assert.equal(isSeriesEntryWinner(series, teamB), false); }); test("formats recorded Game duration and omits unknown legacy zero values", () => { assert.equal(formatSeriesGameDuration(1_875), "31:15"); assert.equal(formatSeriesGameDuration(3_661), "1:01:01"); assert.equal(formatSeriesGameDuration(0), null); assert.equal(formatSeriesGameDuration(-1), null); }); test("formats history dates and times explicitly in Asia/Seoul", () => { assert.equal(formatCompetitionHistoryDate("2026-07-05"), "7월 5일 (일)"); assert.equal(formatCompetitionHistoryDate("2026-07-13"), "7월 13일 (월)"); assert.equal( formatCompetitionHistoryDayHeading("2026-07-05", 1), "7월 5일 (일) · 1경기", ); assert.equal( formatCompetitionHistoryDayHeading("2026-07-13", 2), "7월 13일 (월) · 2경기", ); assert.equal( formatCompetitionHistoryTime("2026-07-12T15:05:00.000Z"), "00:05", ); assert.equal(formatCompetitionHistoryDate("unknown"), "일시 정보 없음"); assert.equal( formatCompetitionHistoryDayHeading("unknown", 1), "일시 정보 없음 · 1경기", ); }); function playedSeriesWithGame({ id, displayOrder, startedAt, }: { id: string; displayOrder: number; startedAt: string; }) { const teamA = teamId(`${id}-a`); const teamB = teamId(`${id}-b`); const series = completedSeries({ id, teamA, teamB, scoreA: 2, scoreB: 0, displayOrder, completedAt: startedAt, }); return { series, game: game({ id: `${id}-game`, seriesId: series.seriesId, ordinal: 1, displayNumber: displayOrder, blueTeamId: teamA, redTeamId: teamB, winnerTeamId: teamA, startedAt, }), }; } function completedSeries({ id, teamA, teamB, scoreA, scoreB, displayOrder = 1, completedAt = "2026-07-13T00:00:00.000Z", outcome = "played", }: { id: string; teamA: TeamId; teamB: TeamId; scoreA: number; scoreB: number; displayOrder?: number; completedAt?: string; outcome?: "played" | "draw" | "walkover" | "forfeit" | "administrative"; }): Extract { const base = { seriesId: seriesId(id), stage: { type: "upper" as const, roundNumber: 1, displayOrder }, rule: { type: "first-to" as const, winsRequired: 2, maxGames: 3 }, status: "completed" as const, entries: [ { slot: "A" as const, teamId: teamA, gameWins: scoreA }, { slot: "B" as const, teamId: teamB, gameWins: scoreB }, ] as const, }; if (outcome === "draw") { return { ...base, outcome: { type: "draw", basis: { type: "played", decidedByGameIds: [] }, completedAt, }, }; } if (outcome !== "played") { return { ...base, outcome: { type: outcome, winnerTeamId: teamA, loserTeamId: teamB, decisionFactId: `fact-${id}` as never, completedAt, }, }; } return { ...base, outcome: { type: "played", winnerTeamId: teamA, loserTeamId: teamB, decidedByGameIds: [], completedAt, }, }; } function inProgressSeries( id: string, teamA: TeamId, teamB: TeamId, scoreA: number, scoreB: number, ): Extract { return { seriesId: seriesId(id), stage: { type: "upper", roundNumber: 1, displayOrder: 1 }, rule: { type: "first-to", winsRequired: 2, maxGames: 3 }, status: "in-progress", entries: [ { slot: "A", teamId: teamA, gameWins: scoreA }, { slot: "B", teamId: teamB, gameWins: scoreB }, ], ...(scoreA === scoreB ? {} : { leaderTeamId: scoreA > scoreB ? teamA : teamB }), }; } function readySeries( id: string, teamA: TeamId, teamB: TeamId, ): Extract { return { seriesId: seriesId(id), stage: { type: "upper", roundNumber: 1, displayOrder: 1 }, rule: { type: "first-to", winsRequired: 2, maxGames: 3 }, status: "ready", entries: [ { slot: "A", teamId: teamA, gameWins: 0 }, { slot: "B", teamId: teamB, gameWins: 0 }, ], }; } function waitingSeries( id: string, ): Extract { return { seriesId: seriesId(id), stage: { type: "upper", roundNumber: 1, displayOrder: 1 }, rule: { type: "first-to", winsRequired: 2, maxGames: 3 }, status: "waiting", entries: [ { slot: "A", teamId: null, gameWins: 0 }, { slot: "B", teamId: null, gameWins: 0 }, ], }; } function inactiveSeries( id: string, ): Extract { return { ...waitingSeries(id), status: "inactive" }; } function game({ id, seriesId: parentSeriesId, ordinal, displayNumber, blueTeamId, redTeamId, winnerTeamId, startedAt, }: { id: string; seriesId: SeriesId; ordinal: number; displayNumber: number; blueTeamId: TeamId; redTeamId: TeamId; winnerTeamId: TeamId; startedAt: string; }): CompetitionGameView { return { id: id as GameId, seriesId: parentSeriesId, gameSlotId: `${id}-slot` as GameSlotId, ordinal, displayNumber, evidence: { type: "legacy", legacyReference: id }, teams: { blue: { side: "blue", teamId: blueTeamId }, red: { side: "red", teamId: redTeamId }, }, winnerTeamId, startedAt, durationSeconds: 1_875, participants: [], }; } function seriesId(value: string): SeriesId { return value as SeriesId; } function teamId(value: string): TeamId { return value as TeamId; }