import { getTestPayload } from "../helpers/get-test-payload"; import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { User } from "@/payload-types"; import { BUILTIN_MINIGAMES } from "@/lib/minigames/registry"; import { loadMinigameRegistry, periodScoringGameKeys, visibleRegistryGames, } from "@/lib/minigames/loadRegistry"; import { recordMinigameEntry } from "@/lib/minigames/periods/record"; import { closeDuePeriods } from "@/lib/minigames/periods/lifecycle"; const ANCHOR = "2026-06-01T00:00:00.000Z"; const RUN = `mgr-${Date.now().toString(36)}`; let payload: Payload; let userA: number; let originalRules: Record | null = null; describe("Minigame registry", () => { beforeAll(async () => { payload = await getTestPayload(); await clearMinigameData(); originalRules = (await payload.findGlobal({ slug: "minigame-rules", depth: 0, overrideAccess: true, })) as unknown as Record | null; await payload.updateGlobal({ slug: "minigame-rules", data: { enabled: true, anchorDate: ANCHOR, periodLengthDays: 14, placementLadderTopN: 10, precisionMinAttempts: 1, }, overrideAccess: true, }); userA = ( (await payload.create({ collection: "users", data: { username: `${RUN}-a`, discordUsername: `${RUN}-a`, displayName: "MG A", steamId: `7656119${Math.floor(Math.random() * 1e9)}`, password: "Test123", roles: ["user"], }, overrideAccess: true, depth: 0, })) as unknown as User ).id; }); afterAll(async () => { await clearMinigameData(); if (!originalRules) return; await payload .updateGlobal({ slug: "minigame-rules", data: { enabled: Boolean(originalRules.enabled), anchorDate: originalRules.anchorDate as string, periodLengthDays: (originalRules.periodLengthDays as number) ?? 14, placementLadderTopN: (originalRules.placementLadderTopN as number) ?? 10, precisionMinAttempts: (originalRules.precisionMinAttempts as number) ?? 5, }, overrideAccess: true, }) .catch(() => undefined); }); it("falls back to the built-in eight when the registry is empty", async () => { const games = await loadMinigameRegistry(payload); expect(games).toHaveLength(BUILTIN_MINIGAMES.length); expect(games.map((game) => game.key)).toEqual(BUILTIN_MINIGAMES.map((game) => game.key)); }); it("orders by registry order and hides retired games from the nav", async () => { await createGame({ key: "zeta", label: "Zeta", category: "core", status: "retired", order: 1 }); await createGame({ key: "alpha", label: "Alpha", category: "cardtable", status: "active", order: 2 }); const games = await loadMinigameRegistry(payload); expect(games.map((game) => game.key)).toEqual(["zeta", "alpha"]); expect(visibleRegistryGames(games).map((game) => game.key)).toEqual(["alpha"]); // Neither counts for periods (both default countsForPeriods false). expect(periodScoringGameKeys(games)).toEqual([]); }); it("excludes a non-scoring game from period boards", async () => { await clearMinigameData(); await createGame({ key: "flappy", label: "Flight Simulator", category: "core", status: "active", order: 1, countsForPeriods: true }); await createGame({ key: "eod", label: "Minefield Clearance", category: "core", status: "active", order: 2, countsForPeriods: false }); expect(periodScoringGameKeys(await loadMinigameRegistry(payload))).toEqual(["flappy"]); const when = new Date("2026-06-05T12:00:00.000Z"); await recordMinigameEntry({ payload, userId: userA, game: "flappy", score: 10, durationMs: 1_000, now: when }); await recordMinigameEntry({ payload, userId: userA, game: "eod", score: 5, durationMs: 1_000, now: when }); const summary = await closeDuePeriods(payload, new Date("2026-06-16T00:00:00.000Z")); expect(summary.closed).toHaveLength(1); const achievements = await payload.find({ collection: "achievements", where: { type: { equals: "period-champion" } }, limit: 1, depth: 0, overrideAccess: true, }); // Only the flappy board scored, so the champion has its 10 ladder points; // had the non-scoring EOD board counted, this would be 20. expect(achievements.docs[0]?.value).toBe(10); }); it("keeps a phasing-out game in the nav, scoring, and round-trips its removal date", async () => { await clearMinigameData(); await createGame({ key: "leaving", label: "Leaving Soon", category: "core", status: "phasingOut", order: 1, countsForPeriods: true, estimatedRemovalDate: "2027-01-15T00:00:00.000Z", }); const games = await loadMinigameRegistry(payload); const leaving = games.find((game) => game.key === "leaving"); // Still visible in the nav (unlike retired) with its removal date attached. expect(leaving?.status).toBe("phasingOut"); expect(leaving?.estimatedRemovalDate).toBe("2027-01-15T00:00:00.000Z"); expect(visibleRegistryGames(games).map((game) => game.key)).toContain("leaving"); // Still scores periods while it is playable. expect(periodScoringGameKeys(games)).toContain("leaving"); }); async function createGame(data: { key: string; label: string; category: "core" | "cardtable"; status: "active" | "retired" | "upcoming" | "phasingOut"; order: number; countsForPeriods?: boolean; estimatedRemovalDate?: string; }) { await payload.create({ collection: "minigames", data: { ...data, route: `/minigames/${data.key}`, icon: "Gamepad2", scoreLabel: "Score", description: "", }, overrideAccess: true, depth: 0, }); } async function clearMinigameData() { for (const collection of [ "achievements", "minigame-entries", "minigame-periods", "minigames", ] as const) { await payload.delete({ collection, where: { id: { greater_than: 0 } }, overrideAccess: true }); } } });