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",