Stage 3 of the int-suite speedup (gate G2 showed Vitest 4's pool scheduler only reuses worker processes between isolate:false tasks, so per-file fork spawning made clone+boot costs unavoidable at stage 2): - config: workers branch runs pool=forks with isolate:false, cap 8 via VITEST_MAX_FORKS, node default env (jsdom docblocks on DOM specs), failure-marker reporter, and idempotent setupFiles chain - vitest-worker-setup: one template clone PER FORK PROCESS named by pid (VITEST_WORKER_ID churns per task, which silently re-cloned per file); DATABASE_URI repoint + TEST_PG_POOL_MAX=8 (measured hook deadlocks at 3) - vitest.setup: once-per-process serial test URI markers; afterEach useRealTimers guard (restoreAllMocks omitted: it resurrects vi.mock module factories mid-file); logging absorber for fire-and-forget event writes that reject after their spec's cleanup deleted fixture users - helpers/get-test-payload.ts: memoized Payload boot shared across files in a fork; codemod converted all 91 self-booting specs mechanically - pinned opt-outs (own boot + comments): game-structures-labor-route, mission-reminder, notification-inbox-scope (jwtSign writes leak into the shared instance's auth state across files) - promotion-nominations: create the superuser before its rank ladder so the Users-hook Recruit rank gets leading orderable tokens (its top-of-ladder guard needs Col to remain ladder-max on fresh clones); operation-economy: itemType-scope the market-state assertion (asset and resource ids collide as bare integers under per-fork databases) Gate G3: 5 consecutive full green runs (149/149 files), warm wall 53-65s vs 598s baseline (~10x), zero clone residue after green runs. Vitest 4 note: globalTeardown config no longer exists; teardown returns from globalSetup.
179 lines
6.2 KiB
TypeScript
179 lines
6.2 KiB
TypeScript
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<string, unknown> | 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<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 });
|
|
}
|
|
}
|
|
});
|