From 2a941c5a5bca00c041c7874336d1e08a84871b08 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Sun, 18 Oct 2026 02:06:32 -0400 Subject: [PATCH] fix(minigames): resolve the current period before building standings boards The default ?period=current scope arrives as periodId null, and buildLeaderboardPayload built the boards before resolving currentPeriod(), so buildPeriodBoards short-circuited to zero entries: the live period's board was always empty even while ledger rows existed for it. Resolve the period first and feed its id into the boards, and cover the default scope with a real-clock regression test (the suite previously only exercised explicit period ids). --- src/lib/minigames/periods/standings.ts | 22 +++++++++++++--------- tests/int/minigame-standings.int.spec.ts | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/lib/minigames/periods/standings.ts b/src/lib/minigames/periods/standings.ts index 60128684..a6e29832 100644 --- a/src/lib/minigames/periods/standings.ts +++ b/src/lib/minigames/periods/standings.ts @@ -253,15 +253,10 @@ export async function buildLeaderboardPayload( // their sidebar entry is disabled), so they get neither a tab nor rows. const playable = registry.filter((game) => game.status !== "upcoming"); - const boards = allTime - ? await buildAllTimeBoards(payload, playable) - : await buildPeriodBoards( - payload, - query.periodId ?? null, - playable.filter((game) => game.category === "core"), - rules.placementLadderTopN, - ); - + // Resolve the period BEFORE building the boards: the default "current" + // scope arrives as periodId null, and the boards must read the resolved + // period's ledger rows rather than the null placeholder (which would show + // an empty board even while entries exist for the live period). const periods = await listPeriods(payload); let period: PeriodSummary | null = null; if (!allTime) { @@ -271,6 +266,15 @@ export async function buildLeaderboardPayload( : await currentPeriod(payload); } + const boards = allTime + ? await buildAllTimeBoards(payload, playable) + : await buildPeriodBoards( + payload, + query.periodId ?? period?.id ?? null, + playable.filter((game) => game.category === "core"), + rules.placementLadderTopN, + ); + return { mode: allTime ? "all-time" : "period", period, diff --git a/tests/int/minigame-standings.int.spec.ts b/tests/int/minigame-standings.int.spec.ts index 18922b7a..b7be10f1 100644 --- a/tests/int/minigame-standings.int.spec.ts +++ b/tests/int/minigame-standings.int.spec.ts @@ -112,6 +112,29 @@ describe("Minigame leaderboard read model", () => { expect(flappy?.rows[0].rank).toBe(1); }); + it("resolves the current period's boards when no periodId is given", async () => { + // Real-clock run: lands in the period covering `now` and lazily creates + // its period row, exactly like a production submission does. The default + // "current" scope must show it even though no periodId is passed. + await recordMinigameEntry({ + payload, + userId: userA, + game: "flappy", + score: 28, + durationMs: 34_000, + }); + + const payloadData = await buildLeaderboardPayload(payload, { scope: "period" }); + + expect(payloadData.mode).toBe("period"); + expect(payloadData.period).not.toBeNull(); + expect(payloadData.period?.id).not.toBeNull(); + const flappy = payloadData.boards.find((board) => board.key === "flappy"); + const row = flappy?.rows.find((entry) => entry.name === "Alpha Operator"); + expect(row?.bestScore).toBe(28); + expect(row?.attempts).toBe(1); + }); + it("reads All-Time boards from the untouched profile aggregates", async () => { const rankRes = await payload.find({ collection: "ranks",