1
0
Fork 0
polaris-task-force/tests/int/minigame-registry.int.spec.ts
Z8MB1E 33f3f7aa75 feat(minigames): add phasing-out status with estimated removal date
Add a "phasingOut" minigame status and an `estimatedRemovalDate` field so a
game can be announced as leaving soon while staying fully playable and keeping
scoring periods (unlike retired games). Visibility is the only difference:

- Sidebar badge + tooltip (amber "Phasing out", countdown to removal) in
  NavMinigames, via new phasingOutTooltip in countdown.ts.
- In-game banner (PhasingOutBanner) on all 9 game pages, wired from
  getMinigameReleaseState (added the fetch to call-for-fire, which lacked it).
- registry.ts status union + field; loadRegistry.ts maps the field;
  config status select + estimatedRemovalDate date field added.
- Migration adds the enum value and column (idempotent); types + db schema
  regenerated.

Adds tests for visibility, round-trip, and period scoring; updates the
minigames domain guide.
2026-10-14 23:51:37 -04:00

180 lines
6.2 KiB
TypeScript

import { getPayload, Payload } from "payload";
import config from "@/payload.config";
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<string, unknown> | null = null;
describe("Minigame registry", () => {
beforeAll(async () => {
const payloadConfig = await config;
payload = await getPayload({ config: payloadConfig });
await clearMinigameData();
originalRules = (await payload.findGlobal({
slug: "minigame-rules",
depth: 0,
overrideAccess: true,
})) as unknown as Record<string, unknown> | 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 });
}
}
});