diff --git a/tests/helpers/get-test-payload.ts b/tests/helpers/get-test-payload.ts new file mode 100644 index 00000000..7a60da01 --- /dev/null +++ b/tests/helpers/get-test-payload.ts @@ -0,0 +1,28 @@ +import type { Payload } from "payload"; + +/** + * Module-level memoized Payload instance for the int suite under Vitest's + * `isolate: false` worker pool (stage 3 of the test speedup). A vitest fork + * keeps its module registry across test files, so the first spec to call this + * pays the full config load + adapter boot and every later file in the same + * fork reuses the instance against the fork's dedicated database clone. + * + * Specs that turn out to leak or depend on module state across files simply + * keep (or revert to) their own direct `getPayload({ config })` boot: opting + * out is just not importing this helper, which restores a cheap-but-correct + * per-file boot. Name any reason in a comment at the opt-out site. + */ +let instance: Promise | undefined; + +export function getTestPayload(): Promise { + if (!instance) { + instance = import("@payload-config") + .then((m) => m.default) + .then((config) => import("payload").then(({ getPayload }) => getPayload({ config }))); + instance.catch(() => { + // A failed boot must not poison the worker for all later files. + instance = undefined; + }); + } + return instance; +} diff --git a/tests/int/adjacency.int.spec.ts b/tests/int/adjacency.int.spec.ts index 55dcbfec..21af68c0 100644 --- a/tests/int/adjacency.int.spec.ts +++ b/tests/int/adjacency.int.spec.ts @@ -1,5 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { getPayload, type CollectionSlug, type Payload, type Where } from "payload"; +import { type CollectionSlug, type Payload, type Where } from "payload"; import type { Map as MapDoc, Resource, Structure } from "@/payload-types"; import config from "@payload-config"; @@ -185,7 +186,7 @@ describe("Adjacency auras", () => { describe("production tick", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); createdIds.maps.push( ( (await payload.create({ diff --git a/tests/int/admin-page-access.int.spec.ts b/tests/int/admin-page-access.int.spec.ts index 7fa6dac5..33341c3d 100644 --- a/tests/int/admin-page-access.int.spec.ts +++ b/tests/int/admin-page-access.int.spec.ts @@ -1,6 +1,6 @@ -import { getPayload, Payload } from "payload"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import type { Access } from "payload"; -import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -75,8 +75,7 @@ describe("Scoped admin page access control", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const readOnlyRole = await makeRole("readonly", { permissions: ["missions:read"] }); diff --git a/tests/int/analytics.int.spec.ts b/tests/int/analytics.int.spec.ts index ae43a127..34a96314 100644 --- a/tests/int/analytics.int.spec.ts +++ b/tests/int/analytics.int.spec.ts @@ -1,6 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { sql } from "@payloadcms/db-postgres"; -import { getPayload, type Payload } from "payload"; -import config from "@/payload.config"; +import { type Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { @@ -57,7 +57,7 @@ function envelope(body: Record) { const createdSessionIds: string[] = []; beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); }); afterAll(async () => { diff --git a/tests/int/announcements.int.spec.ts b/tests/int/announcements.int.spec.ts index b6cdc910..49b6ad68 100644 --- a/tests/int/announcements.int.spec.ts +++ b/tests/int/announcements.int.spec.ts @@ -1,6 +1,6 @@ // @vitest-environment jsdom (specs clear window.localStorage via the Shims evaluator) -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; @@ -203,8 +203,7 @@ describe("announcements collection access control", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const permittedRole = await makeRole("permitted", { diff --git a/tests/int/api.int.spec.ts b/tests/int/api.int.spec.ts index da53d627..f7e02f31 100644 --- a/tests/int/api.int.spec.ts +++ b/tests/int/api.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { beforeAll, describe, expect, it } from "vitest"; @@ -7,8 +7,7 @@ let payload: Payload; describe("API", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); }); it("fetches users", async () => { diff --git a/tests/int/arma-catalog-import.int.spec.ts b/tests/int/arma-catalog-import.int.spec.ts index 8cc24b2d..f4e4aa93 100644 --- a/tests/int/arma-catalog-import.int.spec.ts +++ b/tests/int/arma-catalog-import.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { mkdirSync, rmSync, writeFileSync } from "fs"; import { join } from "path"; @@ -34,8 +34,7 @@ function mediaEnv(screenshots = ""): MediaEnv { describe("Arma catalog import", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Vehicles need a fuel resource for the fuel group; reuse one if a prior // fixture left it, otherwise create it (and only then clean it up). diff --git a/tests/int/arma-locker-sync.int.spec.ts b/tests/int/arma-locker-sync.int.spec.ts index a6e76ebc..25bd4223 100644 --- a/tests/int/arma-locker-sync.int.spec.ts +++ b/tests/int/arma-locker-sync.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { formatInstanceRef, @@ -91,8 +91,7 @@ async function lockerDoc() { describe("Arma locker sync", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const user = await payload.create({ collection: "users", diff --git a/tests/int/arma-shop-money.int.spec.ts b/tests/int/arma-shop-money.int.spec.ts index 28e19267..c87b7984 100644 --- a/tests/int/arma-shop-money.int.spec.ts +++ b/tests/int/arma-shop-money.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { BankAccount } from "@/payload-types"; import { createAccount, ensurePersonalAccount, getTreasuryAccountId } from "@/lib/banking"; @@ -33,8 +33,7 @@ describe("Arma shop money sync", () => { let createdTreasury = false; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const user = await payload.create({ collection: "users", diff --git a/tests/int/award-advancement.int.spec.ts b/tests/int/award-advancement.int.spec.ts index 6409049e..5ce42772 100644 --- a/tests/int/award-advancement.int.spec.ts +++ b/tests/int/award-advancement.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { User, Award } from "@/payload-types"; @@ -44,8 +44,7 @@ describe("Award Advancement Series", () => { } beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Create a superuser role (RBAC resolves via roleDocs, not the legacy enum) const superRole = await payload.create({ diff --git a/tests/int/award-automation.int.spec.ts b/tests/int/award-automation.int.spec.ts index a06e70b3..bd6550cb 100644 --- a/tests/int/award-automation.int.spec.ts +++ b/tests/int/award-automation.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Award, Profile, User } from "@/payload-types"; import { evaluateAwardRules } from "@/lib/awards/automation"; @@ -127,8 +127,7 @@ describe("Award automation sweeper", () => { } beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); medalId = await makeAward(`${RUN} Merit Medal`); tier1Id = await makeAward(`${RUN} YOS Bronze`, 1); diff --git a/tests/int/award-notifications.int.spec.ts b/tests/int/award-notifications.int.spec.ts index 40173515..193e826b 100644 --- a/tests/int/award-notifications.int.spec.ts +++ b/tests/int/award-notifications.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Award, Profile, User } from "@/payload-types"; @@ -66,8 +66,7 @@ describe("Award grant notifications", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const makeAward = async (name: string, type: "medal" | "ribbon"): Promise => { const created = (await payload.create({ diff --git a/tests/int/bomperator-registry.int.spec.ts b/tests/int/bomperator-registry.int.spec.ts index 255c8ef0..d64a4373 100644 --- a/tests/int/bomperator-registry.int.spec.ts +++ b/tests/int/bomperator-registry.int.spec.ts @@ -5,9 +5,9 @@ * (live server + real sockets). This file stays because the registry remains * live behind the HttpTransport fallback until prod proves the arena. */ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; -import { getPayload, type Payload } from "payload"; -import config from "@payload-config"; +import { type Payload } from "payload"; import { INTER_ROUND_COUNTDOWN_TICKS, @@ -278,7 +278,7 @@ describe("bomperator: awards write to the profile (DB round-trip)", () => { const createdUserIds: number[] = []; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); }, 30_000); afterAll(async () => { diff --git a/tests/int/construction-speed.int.spec.ts b/tests/int/construction-speed.int.spec.ts index 72ad7815..490f7640 100644 --- a/tests/int/construction-speed.int.spec.ts +++ b/tests/int/construction-speed.int.spec.ts @@ -1,5 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { getPayload, type CollectionSlug, type Payload, type Where } from "payload"; +import { type CollectionSlug, type Payload, type Where } from "payload"; import type { Map as MapDoc, Resource, Structure } from "@/payload-types"; import config from "@payload-config"; @@ -212,7 +213,7 @@ describe("Construction crew speed", () => { describe("start gating and rate recompute", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); const rules = (await payload.findGlobal({ slug: "game-rules", depth: 0, diff --git a/tests/int/contributions-moderation.int.spec.ts b/tests/int/contributions-moderation.int.spec.ts index 300bdb10..2f5f6ae2 100644 --- a/tests/int/contributions-moderation.int.spec.ts +++ b/tests/int/contributions-moderation.int.spec.ts @@ -3,8 +3,8 @@ * grant with a reason (the reason is stored and shown to the earner), and both * are gated on `contribution-entries:moderate`. */ -import { getPayload, type Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { type Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { Role, User } from "@/payload-types"; @@ -72,7 +72,7 @@ describe("Contribution moderation actions", () => { }; beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); const modRole = await makeRole("mod", ["contribution-entries:moderate"]); const plainRole = await makeRole("plain", []); moderator = await makeUser("mod", modRole.id); diff --git a/tests/int/contributions.int.spec.ts b/tests/int/contributions.int.spec.ts index 257f2b88..c96b28bc 100644 --- a/tests/int/contributions.int.spec.ts +++ b/tests/int/contributions.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload } from "payload"; -import config from "@payload-config"; + +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Mission, User } from "@/payload-types"; import { @@ -12,7 +12,7 @@ import { } from "@/lib/contributions"; import { createMissionMarker } from "@/lib/intelligence/missionMap"; -type Payload = Awaited>; +type Payload = Awaited>; describe("Contribution / XP service", () => { let payload: Payload; @@ -87,7 +87,7 @@ describe("Contribution / XP service", () => { ); beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); const map = await payload.create({ collection: "maps", data: { diff --git a/tests/int/custom-minefields.int.spec.ts b/tests/int/custom-minefields.int.spec.ts index d1b903ff..3439a45b 100644 --- a/tests/int/custom-minefields.int.spec.ts +++ b/tests/int/custom-minefields.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Role, User } from "@/payload-types"; import { CustomMinefields } from "@/collections/minigames/CustomMinefields"; @@ -38,8 +38,7 @@ describe("Custom minefields", () => { const scoreIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); author = (await payload.create({ collection: "users", diff --git a/tests/int/custom-radio-tests.int.spec.ts b/tests/int/custom-radio-tests.int.spec.ts index f26043a6..662d503b 100644 --- a/tests/int/custom-radio-tests.int.spec.ts +++ b/tests/int/custom-radio-tests.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Role, User } from "@/payload-types"; import { CustomRadioTests } from "@/collections/minigames/CustomRadioTests"; @@ -30,8 +30,7 @@ describe("Custom radio tests", () => { const scoreIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); author = (await payload.create({ collection: "users", diff --git a/tests/int/division-admin-access.int.spec.ts b/tests/int/division-admin-access.int.spec.ts index f181b899..a47519cd 100644 --- a/tests/int/division-admin-access.int.spec.ts +++ b/tests/int/division-admin-access.int.spec.ts @@ -1,6 +1,6 @@ -import { getPayload, Payload } from "payload"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import type { Access, AccessArgs } from "payload"; -import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -155,8 +155,7 @@ describe("Division-scoped admin access (intelligence / logistics)", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const bareRole = await makeRole("bare", { permissions: [] }); diff --git a/tests/int/economy-consumption.int.spec.ts b/tests/int/economy-consumption.int.spec.ts index 05937e44..bddb7b5f 100644 --- a/tests/int/economy-consumption.int.spec.ts +++ b/tests/int/economy-consumption.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Asset, BankAccount, MarketListing, User } from "@/payload-types"; @@ -88,8 +88,7 @@ describe("Economy consumption: DB integration", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); assetId = await createFixtureAsset(); diff --git a/tests/int/economy-events.int.spec.ts b/tests/int/economy-events.int.spec.ts index ff18a4d9..776e7cd0 100644 --- a/tests/int/economy-events.int.spec.ts +++ b/tests/int/economy-events.int.spec.ts @@ -2,10 +2,9 @@ * Integration tests for economy-tick determinism (seeded rng, period seeding), * price-change emission threshold, and the economy event log wiring. */ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Payload } from "payload"; -import { getPayload } from "payload"; -import config from "@/payload.config"; import { EventTypes } from "@/utils/event-log/eventTypes"; import { emitEconomyEvents } from "@/lib/economy/events"; @@ -71,8 +70,7 @@ describe("economy events (DB)", () => { let resourceId: number | null = null; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); }); afterAll(async () => { diff --git a/tests/int/economy.int.spec.ts b/tests/int/economy.int.spec.ts index 373adef4..d62f3271 100644 --- a/tests/int/economy.int.spec.ts +++ b/tests/int/economy.int.spec.ts @@ -1,6 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { getPayload, type CollectionSlug, type Payload } from "payload"; -import config from "@/payload.config"; +import { type CollectionSlug, type Payload } from "payload"; import { applyEconomyTick, computePriceModifier, @@ -28,8 +28,7 @@ let resourceId: number; let assetId: number; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); }); afterAll(async () => { diff --git a/tests/int/evaluations.int.spec.ts b/tests/int/evaluations.int.spec.ts index c0120f3b..e62c2989 100644 --- a/tests/int/evaluations.int.spec.ts +++ b/tests/int/evaluations.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Campaign, Map as MissionMap, Mission, Role, User } from "@/payload-types"; @@ -105,8 +105,7 @@ describe("Leadership evaluations", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", diff --git a/tests/int/event-log-discord-flag.int.spec.ts b/tests/int/event-log-discord-flag.int.spec.ts index 9b200d21..174dc37b 100644 --- a/tests/int/event-log-discord-flag.int.spec.ts +++ b/tests/int/event-log-discord-flag.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -31,7 +31,7 @@ async function emit(input: Parameters[1]) { describe("discordRequested flag on game events", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { diff --git a/tests/int/event-log-routine.int.spec.ts b/tests/int/event-log-routine.int.spec.ts index 2b3ba89d..8900af35 100644 --- a/tests/int/event-log-routine.int.spec.ts +++ b/tests/int/event-log-routine.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, type Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { type Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -33,7 +33,7 @@ describe("routine event classification (pure)", () => { describe("routine event filtering (database)", () => { beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { diff --git a/tests/int/event-log-visibility.int.spec.ts b/tests/int/event-log-visibility.int.spec.ts index 1b00ddee..3f7ef1d8 100644 --- a/tests/int/event-log-visibility.int.spec.ts +++ b/tests/int/event-log-visibility.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload, type Where } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload, type Where } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -129,8 +129,7 @@ describe("event visibility enforcement (database)", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const plainRole = await makeRole("plain", { permissions: [] }); const gateRole = await makeRole("gate", { permissions: ["player-flags:read"] }); diff --git a/tests/int/friday-ops-notice.int.spec.ts b/tests/int/friday-ops-notice.int.spec.ts index b88d5483..fe440c25 100644 --- a/tests/int/friday-ops-notice.int.spec.ts +++ b/tests/int/friday-ops-notice.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types"; @@ -41,8 +41,7 @@ describe("Friday open-slot notice", () => { const roleIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Sweep leftovers from crashed earlier runs: a stray Concept mission in // the fixture window would flip the first hasMissionOnFriday assertion. diff --git a/tests/int/garage-bridge.int.spec.ts b/tests/int/garage-bridge.int.spec.ts index c2d2ab8b..2429a7fe 100644 --- a/tests/int/garage-bridge.int.spec.ts +++ b/tests/int/garage-bridge.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { GameVehicle } from "@/payload-types"; import { processGarageEvent } from "@/lib/arma-bridge/garages"; @@ -83,8 +83,7 @@ async function findGarageCommands(type: string) { describe("Arma garage sync", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const resource = await payload.create({ collection: "resources", diff --git a/tests/int/garage-placement.int.spec.ts b/tests/int/garage-placement.int.spec.ts index 9453069b..907782d4 100644 --- a/tests/int/garage-placement.int.spec.ts +++ b/tests/int/garage-placement.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { GameVehicle, User } from "@/payload-types"; import { @@ -117,8 +117,7 @@ async function vehicleDoc(id: number) { // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); const garageRole = await payload.create({ diff --git a/tests/int/generic-node.int.spec.ts b/tests/int/generic-node.int.spec.ts index 9240541a..479729ea 100644 --- a/tests/int/generic-node.int.spec.ts +++ b/tests/int/generic-node.int.spec.ts @@ -1,6 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { describe, it, expect, beforeAll, afterEach, vi } from "vitest"; -import config from "@payload-config"; -import { getPayload, type Payload } from "payload"; +import { type Payload } from "payload"; import type { Map as MapDoc, Resource } from "@/payload-types"; import { checkResourcesInRange } from "@/lib/map/placement"; import { nodeIconOption } from "@/lib/map/nodeIcons"; @@ -18,7 +18,7 @@ describe("generic marker nodes (no resources)", () => { let mapper: { id: number }; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); map = (await payload.create({ collection: "maps", diff --git a/tests/int/imminent-reminders.int.spec.ts b/tests/int/imminent-reminders.int.spec.ts index ee44aebf..c52e0a24 100644 --- a/tests/int/imminent-reminders.int.spec.ts +++ b/tests/int/imminent-reminders.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types"; @@ -20,8 +20,7 @@ describe("Imminent-op reminder query", () => { const missionIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", diff --git a/tests/int/intelligence-visibility.int.spec.ts b/tests/int/intelligence-visibility.int.spec.ts index 4dd5d3d4..39f1ba65 100644 --- a/tests/int/intelligence-visibility.int.spec.ts +++ b/tests/int/intelligence-visibility.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -90,8 +90,7 @@ describe("Intelligence navigation visibility (canViewIntelligence)", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); // Mirrors the standard "user" role: intel-domain read permissions, no writes. diff --git a/tests/int/interior.int.spec.ts b/tests/int/interior.int.spec.ts index f90d77df..31435d11 100644 --- a/tests/int/interior.int.spec.ts +++ b/tests/int/interior.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, type Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { type Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { @@ -308,8 +308,7 @@ async function maxNotificationId(userId: number): Promise { // --- setup / teardown ------------------------------------------------------ beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); scrap = await makeResource("scrap", 1); ore = await makeResource("ore", 1); diff --git a/tests/int/journal-contributions.int.spec.ts b/tests/int/journal-contributions.int.spec.ts index 21b2a876..c00547f8 100644 --- a/tests/int/journal-contributions.int.spec.ts +++ b/tests/int/journal-contributions.int.spec.ts @@ -5,8 +5,8 @@ * pulls the XP back. Each grant test uses a fresh author because the rule's * 25 XP daily cap allows a single entry per day. */ -import { getPayload, type Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { type Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { Journal, User } from "@/payload-types"; @@ -111,7 +111,7 @@ describe("Journal contribution XP", () => { }); beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); }, TIMEOUT); diff --git a/tests/int/journals-access.int.spec.ts b/tests/int/journals-access.int.spec.ts index 5ac7f3b4..c8c711dd 100644 --- a/tests/int/journals-access.int.spec.ts +++ b/tests/int/journals-access.int.spec.ts @@ -2,8 +2,8 @@ * Journals access rules: authors own their entries (drafts private), every * logged-in user reads published entries, staff permissions bypass ownership. */ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Journal, Role, User } from "@/payload-types"; @@ -79,7 +79,7 @@ describe("Journals access control", () => { }; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); const plainRole = await makeRole("plain", []); const staffRole = await makeRole("staff", [ "journals:read", diff --git a/tests/int/kill-tracking.int.spec.ts b/tests/int/kill-tracking.int.spec.ts index d1d91844..42f1d776 100644 --- a/tests/int/kill-tracking.int.spec.ts +++ b/tests/int/kill-tracking.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { classifyKillCategory, parseKillPayload, processPlayerKill } from "@/lib/arma-bridge/processSyncEvents"; @@ -11,8 +11,7 @@ describe("Kill tracking", () => { let profileId: number; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const user = await payload.create({ collection: "users", diff --git a/tests/int/leadership-transfers.int.spec.ts b/tests/int/leadership-transfers.int.spec.ts index 81a74b16..df9add08 100644 --- a/tests/int/leadership-transfers.int.spec.ts +++ b/tests/int/leadership-transfers.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -113,8 +113,7 @@ describe("Leadership-initiated removals (leadership-transfers)", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // The per-user permission cache is keyed by numeric id, which can collide // with users from previously run spec files in the same serialized run. invalidatePermissionCache(); diff --git a/tests/int/locked-documents-query.int.spec.ts b/tests/int/locked-documents-query.int.spec.ts index 3b79c95a..1fe2b359 100644 --- a/tests/int/locked-documents-query.int.spec.ts +++ b/tests/int/locked-documents-query.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { beforeAll, describe, expect, it } from "vitest"; @@ -24,7 +24,7 @@ let payload: Payload; */ describe("payload-locked-documents relational query", () => { beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); }); it("hydrates a lock document at depth 1 without exceeding the argument limit", async () => { diff --git a/tests/int/map-system.int.spec.ts b/tests/int/map-system.int.spec.ts index a56c6dac..d1313a35 100644 --- a/tests/int/map-system.int.spec.ts +++ b/tests/int/map-system.int.spec.ts @@ -1,4 +1,5 @@ -import { getPayload, Payload, type CollectionSlug, type Where } from "payload"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload, type CollectionSlug, type Where } from "payload"; import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; @@ -256,7 +257,7 @@ describe("Game Map System", () => { beforeAll(async () => { const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); mapA = await makeMap(`${RUN} Altis`); diff --git a/tests/int/market.int.spec.ts b/tests/int/market.int.spec.ts index 95eba5f0..b0e6a662 100644 --- a/tests/int/market.int.spec.ts +++ b/tests/int/market.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { beforeAll, afterAll, describe, expect, it } from "vitest"; import type { Asset, BankAccount, GameNpc, LockerStorage, MarketListing, User } from "@/payload-types"; @@ -41,8 +41,7 @@ describe("Marketplace", () => { let currencyId: number | null; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Always create a fresh, live, tradeable asset for the test. The // "approved" approvalStatus can't be set from an unauthenticated payload diff --git a/tests/int/mcp-api-keys.int.spec.ts b/tests/int/mcp-api-keys.int.spec.ts index 529c8520..0230580b 100644 --- a/tests/int/mcp-api-keys.int.spec.ts +++ b/tests/int/mcp-api-keys.int.spec.ts @@ -1,6 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import crypto from "crypto"; -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -61,7 +61,7 @@ describe("MCP API key collection access (payload-mcp-api-keys)", () => { }; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); const plainRole = await makeRole("plain"); const superRole = await makeRole("super", { isSuperuser: true }); diff --git a/tests/int/minigame-achievement-notifications.int.spec.ts b/tests/int/minigame-achievement-notifications.int.spec.ts index 31896ba5..3176678c 100644 --- a/tests/int/minigame-achievement-notifications.int.spec.ts +++ b/tests/int/minigame-achievement-notifications.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +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"; @@ -26,8 +26,7 @@ const notificationsFor = async (userId: number) => { describe("Minigame achievement notifications", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); await clearMinigameData(); originalRules = (await payload.findGlobal({ diff --git a/tests/int/minigame-calendar.int.spec.ts b/tests/int/minigame-calendar.int.spec.ts index 295cc4c0..9c00d244 100644 --- a/tests/int/minigame-calendar.int.spec.ts +++ b/tests/int/minigame-calendar.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +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"; @@ -22,8 +22,7 @@ const at = (iso: string) => new Date(iso); describe("Minigame month/year achievements", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); await clearMinigameData(); diff --git a/tests/int/minigame-dev-console-gating.int.spec.ts b/tests/int/minigame-dev-console-gating.int.spec.ts index 75f3f87c..7ea996e3 100644 --- a/tests/int/minigame-dev-console-gating.int.spec.ts +++ b/tests/int/minigame-dev-console-gating.int.spec.ts @@ -8,8 +8,8 @@ * actual security boundary on the test database. */ -import { getPayload, type Payload, type User } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { type Payload, type User } from "payload"; import { afterAll, beforeAll, beforeEach, describe, expect, it, vi, type MockInstance } from "vitest"; import { @@ -119,8 +119,7 @@ function fleetAt(x: number, y: number): FobPlacement[] { } beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); loggerSpy = vi.spyOn(payload.logger, "info"); diff --git a/tests/int/minigame-entries.int.spec.ts b/tests/int/minigame-entries.int.spec.ts index b1f40c74..a4386e97 100644 --- a/tests/int/minigame-entries.int.spec.ts +++ b/tests/int/minigame-entries.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +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"; @@ -37,8 +37,7 @@ async function clearMinigameData(): Promise { describe("Minigame entry recording + anchored periods", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // The period spec shares these tables and the same anchor; start clean so // the two files are order-independent. diff --git a/tests/int/minigame-periods.int.spec.ts b/tests/int/minigame-periods.int.spec.ts index 16773d8c..639a33e3 100644 --- a/tests/int/minigame-periods.int.spec.ts +++ b/tests/int/minigame-periods.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +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"; @@ -35,8 +35,7 @@ const entry = (over: Partial): LedgerEntry => ({ describe("Minigame period close pipeline", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // The entries spec shares these tables and the same anchor; start clean so // the two files are order-independent. diff --git a/tests/int/minigame-registry.int.spec.ts b/tests/int/minigame-registry.int.spec.ts index 2db9365b..98198830 100644 --- a/tests/int/minigame-registry.int.spec.ts +++ b/tests/int/minigame-registry.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +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"; @@ -21,8 +21,7 @@ let originalRules: Record | null = null; describe("Minigame registry", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); await clearMinigameData(); diff --git a/tests/int/minigame-standings.int.spec.ts b/tests/int/minigame-standings.int.spec.ts index b7be10f1..df47f203 100644 --- a/tests/int/minigame-standings.int.spec.ts +++ b/tests/int/minigame-standings.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +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"; @@ -21,8 +21,7 @@ let originalRules: Record | null = null; describe("Minigame leaderboard read model", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); await clearMinigameData(); originalRules = (await payload.findGlobal({ diff --git a/tests/int/minigame-upcoming.int.spec.ts b/tests/int/minigame-upcoming.int.spec.ts index d04d0b90..f163bab9 100644 --- a/tests/int/minigame-upcoming.int.spec.ts +++ b/tests/int/minigame-upcoming.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { loadMinigameRegistry, getMinigameReleaseState } from "@/lib/minigames/loadRegistry"; @@ -13,8 +13,7 @@ let originalRules: Record | null = null; describe("Upcoming minigames", () => { beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); await clearMinigameData(); originalRules = (await payload.findGlobal({ diff --git a/tests/int/mission-lifecycle.int.spec.ts b/tests/int/mission-lifecycle.int.spec.ts index 4ada35a6..12082ccf 100644 --- a/tests/int/mission-lifecycle.int.spec.ts +++ b/tests/int/mission-lifecycle.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types"; @@ -26,8 +26,7 @@ describe("Mission lifecycle (auto-start + auto-complete)", () => { const completedBySweep: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", diff --git a/tests/int/mission-map.int.spec.ts b/tests/int/mission-map.int.spec.ts index b7d950d5..c5fb4719 100644 --- a/tests/int/mission-map.int.spec.ts +++ b/tests/int/mission-map.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Map as MissionMapDoc, Mission, User } from "@/payload-types"; @@ -269,7 +269,7 @@ describe("Mission planning map service", () => { })) as unknown as Mission; beforeAll(async () => { - payload = await getPayload({ config: await config }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", data: { diff --git a/tests/int/mission-role-signups.int.spec.ts b/tests/int/mission-role-signups.int.spec.ts index 2c27f741..aebcc9bb 100644 --- a/tests/int/mission-role-signups.int.spec.ts +++ b/tests/int/mission-role-signups.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { @@ -189,8 +189,7 @@ describe("Mission role sign-ups", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", diff --git a/tests/int/modules.int.spec.ts b/tests/int/modules.int.spec.ts index 6c8be8a8..7d7cd3c3 100644 --- a/tests/int/modules.int.spec.ts +++ b/tests/int/modules.int.spec.ts @@ -1,5 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { getPayload, type CollectionSlug, type Payload, type Where } from "payload"; +import { type CollectionSlug, type Payload, type Where } from "payload"; import type { Map as MapDoc, Resource, Structure } from "@/payload-types"; import config from "@payload-config"; @@ -218,7 +219,7 @@ describe("Module system", () => { describe("install / uninstall", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); createdIds.maps.push( ( (await payload.create({ diff --git a/tests/int/notification-access.int.spec.ts b/tests/int/notification-access.int.spec.ts index 5ee19bbf..758e8e65 100644 --- a/tests/int/notification-access.int.spec.ts +++ b/tests/int/notification-access.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -63,8 +63,7 @@ describe("User notification read access control", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const plainRole = await makeRole("plain", { permissions: [] }); diff --git a/tests/int/notification-inbox-scope.int.spec.ts b/tests/int/notification-inbox-scope.int.spec.ts index 601367b3..50a8c0c6 100644 --- a/tests/int/notification-inbox-scope.int.spec.ts +++ b/tests/int/notification-inbox-scope.int.spec.ts @@ -1,6 +1,10 @@ // jose (via payload's jwtSign) fails under jsdom: its module-level TextEncoder and // the global Uint8Array come from different realms, so signing throws a TypeError. +// +// Pinned to its own payload boot (stage-3 straggler policy): this spec's +// signed-token auth writes into the shared memoized instance's auth state and +// leaks "You must be logged in." drift into later files on the same fork. import { getFieldsToSign, getPayload, jwtSign, Payload } from "payload"; import config from "@/payload.config"; diff --git a/tests/int/notification-read-scope.int.spec.ts b/tests/int/notification-read-scope.int.spec.ts index c71cc0d4..25770522 100644 --- a/tests/int/notification-read-scope.int.spec.ts +++ b/tests/int/notification-read-scope.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -81,8 +81,7 @@ describe("notification read-state is personal to the recipient", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const plainRole = await makeRole("plain", { permissions: [] }); diff --git a/tests/int/npc-dialogue-attachments.int.spec.ts b/tests/int/npc-dialogue-attachments.int.spec.ts index 10736ec2..488534c0 100644 --- a/tests/int/npc-dialogue-attachments.int.spec.ts +++ b/tests/int/npc-dialogue-attachments.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -27,7 +27,7 @@ async function createMedia(name: string): Promise { describe("resolveAttachments", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { diff --git a/tests/int/npc-dialogue-condition-groups.int.spec.ts b/tests/int/npc-dialogue-condition-groups.int.spec.ts index d17f788c..5d2fb391 100644 --- a/tests/int/npc-dialogue-condition-groups.int.spec.ts +++ b/tests/int/npc-dialogue-condition-groups.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -34,7 +34,7 @@ async function createUser(username: string): Promise { describe("condition groups (and / or / nor over leaf conditions)", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { diff --git a/tests/int/npc-dialogue-flag-target.int.spec.ts b/tests/int/npc-dialogue-flag-target.int.spec.ts index 5b3a52e5..d4fcef00 100644 --- a/tests/int/npc-dialogue-flag-target.int.spec.ts +++ b/tests/int/npc-dialogue-flag-target.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -33,7 +33,7 @@ async function createUser(username: string): Promise<{ id: number }> { describe("flag owner targeting (event node effects)", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { diff --git a/tests/int/npc-dialogue-interpolate.int.spec.ts b/tests/int/npc-dialogue-interpolate.int.spec.ts index 299a7994..68678836 100644 --- a/tests/int/npc-dialogue-interpolate.int.spec.ts +++ b/tests/int/npc-dialogue-interpolate.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; @@ -109,7 +109,7 @@ describe("flag token extraction + interpolation (pure)", () => { describe("text flags (player-flags collection)", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); }, TIMEOUT); afterAll(async () => { diff --git a/tests/int/operation-allocation.int.spec.ts b/tests/int/operation-allocation.int.spec.ts index af27f274..1d361c7f 100644 --- a/tests/int/operation-allocation.int.spec.ts +++ b/tests/int/operation-allocation.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { expireOperationReservations, @@ -224,8 +224,7 @@ async function storedAmountAt(siteId: number, resourceId: number): Promise { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const currencyId = await getMainCurrencyId(payload); if (!currencyId) throw new Error("Test requires a main currency in Game Rules."); diff --git a/tests/int/operation-economy.int.spec.ts b/tests/int/operation-economy.int.spec.ts index 5aa47767..bef35b60 100644 --- a/tests/int/operation-economy.int.spec.ts +++ b/tests/int/operation-economy.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { applyEconomyEffect, DEFAULT_ECONOMY_CAPS } from "@/lib/operations/economy"; import { ECONOMY_NOTE_PREFIX, type EconomyRecord } from "@/lib/operations/economyConfig"; @@ -217,8 +217,7 @@ async function manualExtractionEffect(label: string, resourceId: number, amount: // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const server = await payload.create({ collection: "game-servers", @@ -525,7 +524,10 @@ describe("Operation economy adapter", () => { const docs = await payload.find({ collection: "market-state", - where: { itemId: { equals: resourceBetaId } }, + // itemType-scoped on purpose: asset ids and resource ids collide as + // bare integers, and under per-fork databases another spec's asset + // ledger row can share this resource's numeric id. + where: { and: [{ itemType: { equals: "resource" } }, { itemId: { equals: resourceBetaId } }] }, limit: 1, depth: 0, overrideAccess: true, diff --git a/tests/int/operation-ledger.int.spec.ts b/tests/int/operation-ledger.int.spec.ts index 8e477d16..045f1ab8 100644 --- a/tests/int/operation-ledger.int.spec.ts +++ b/tests/int/operation-ledger.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { hashPayload, processOperationEvent, stableStringify } from "@/lib/operations/process"; import type { RawSyncEvent } from "@/lib/operations/types"; @@ -18,8 +18,7 @@ describe("Operation ledger", () => { const createdEffectIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const user = await payload.create({ collection: "users", diff --git a/tests/int/operation-readiness.int.spec.ts b/tests/int/operation-readiness.int.spec.ts index 46904139..233136c8 100644 --- a/tests/int/operation-readiness.int.spec.ts +++ b/tests/int/operation-readiness.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { computeReadiness, getReadiness, type ReadinessScope } from "@/lib/operations/readiness"; import { READINESS_CALCULATION_VERSION } from "@/lib/operations/readinessTypes"; @@ -139,8 +139,7 @@ function scopeFor(windowStart: Date, windowEnd: Date): ReadinessScope { // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // One shared, minute-floored window: the aggregate identity is // `subjectType:subjectId:windowStart`, so every test that expects to find diff --git a/tests/int/operation-reservation-actions.int.spec.ts b/tests/int/operation-reservation-actions.int.spec.ts index 94d12097..549b3745 100644 --- a/tests/int/operation-reservation-actions.int.spec.ts +++ b/tests/int/operation-reservation-actions.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { BankAccount, OperationReservation, Structure, User } from "@/payload-types"; import { createReservation, cancelReservation } from "@/app/(frontend)/operations/reservations/actions"; @@ -204,8 +204,7 @@ async function makeFundedAccount(amount: number): Promise { // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); const superRole = await payload.create({ diff --git a/tests/int/operation-reservation-options.int.spec.ts b/tests/int/operation-reservation-options.int.spec.ts index 9fb0205e..54d956a1 100644 --- a/tests/int/operation-reservation-options.int.spec.ts +++ b/tests/int/operation-reservation-options.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { GameStructure, @@ -162,8 +162,7 @@ async function makeVehicle(status: "idle" | "assigned"): Promise { // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const superRole = await payload.create({ collection: "roles", diff --git a/tests/int/operation-reservation-surfaces.int.spec.ts b/tests/int/operation-reservation-surfaces.int.spec.ts index 1c5d8d81..ba6bd815 100644 --- a/tests/int/operation-reservation-surfaces.int.spec.ts +++ b/tests/int/operation-reservation-surfaces.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { OperationReservation, User } from "@/payload-types"; import { getReservationList } from "@/app/(frontend)/operations/reservations/surface"; @@ -68,8 +68,7 @@ async function createReservedReservation(key: string, operationId: string): Prom } beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); const superRole = await payload.create({ diff --git a/tests/int/operation-rollout.int.spec.ts b/tests/int/operation-rollout.int.spec.ts index 12e4e989..9a85116e 100644 --- a/tests/int/operation-rollout.int.spec.ts +++ b/tests/int/operation-rollout.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { acknowledgeCommand, recoverExpiredCommands } from "@/lib/arma-bridge/commands"; import { replayOperationEvent } from "@/lib/operations/reconcile"; @@ -71,8 +71,7 @@ async function opEventFor(rawId: number): Promise<{ id: number; status: string } } beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const server = await payload.create({ collection: "game-servers", diff --git a/tests/int/operation-settlement.int.spec.ts b/tests/int/operation-settlement.int.spec.ts index 044567bb..bc83a86c 100644 --- a/tests/int/operation-settlement.int.spec.ts +++ b/tests/int/operation-settlement.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { processOperationEvent } from "@/lib/operations/process"; import { reserveOperationAssets, type SettlementRecord } from "@/lib/operations/allocation"; @@ -202,8 +202,7 @@ async function findReservation(key: string): Promise { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const superRole = await payload.create({ collection: "roles", diff --git a/tests/int/operation-territory.int.spec.ts b/tests/int/operation-territory.int.spec.ts index 71eaaf48..a224e652 100644 --- a/tests/int/operation-territory.int.spec.ts +++ b/tests/int/operation-territory.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { applyZonePressure, computeZoneState, PRESSURE_OUTCOME_DELTAS } from "@/lib/operations/territory"; @@ -80,8 +80,7 @@ async function pressureRowCount(zoneId: number): Promise { // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const actor = await payload.create({ collection: "users", diff --git a/tests/int/operations-surface.int.spec.ts b/tests/int/operations-surface.int.spec.ts index 925bd88a..4f21cc46 100644 --- a/tests/int/operations-surface.int.spec.ts +++ b/tests/int/operations-surface.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { getOperationOverview, getSurfaceCapabilities } from "@/lib/operations/surface"; import { getOperationAar, getOperationLedger } from "@/lib/operations/surfaceQuery"; @@ -79,8 +79,7 @@ async function makeRawEvent(label: string, type: string, payloadData: unknown): } beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const superRole = await payload.create({ collection: "roles", diff --git a/tests/int/production.int.spec.ts b/tests/int/production.int.spec.ts index ba469f6c..cc65cf9f 100644 --- a/tests/int/production.int.spec.ts +++ b/tests/int/production.int.spec.ts @@ -1,5 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { getPayload, type CollectionSlug, type Payload, type Where } from "payload"; +import { type CollectionSlug, type Payload, type Where } from "payload"; import type { LaborClassification, Map as MapDoc, Resource, Structure } from "@/payload-types"; import config from "@payload-config"; @@ -138,7 +139,7 @@ async function stallReason(siteId: number): Promise { describe("Production system", () => { beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); createdIds.maps.push( ( (await payload.create({ diff --git a/tests/int/promotion-nominations.int.spec.ts b/tests/int/promotion-nominations.int.spec.ts index e1a22f98..d5426c74 100644 --- a/tests/int/promotion-nominations.int.spec.ts +++ b/tests/int/promotion-nominations.int.spec.ts @@ -154,6 +154,17 @@ describe("Promotion nominations", () => { payload = await getPayload({ config: payloadConfig }); invalidatePermissionCache(); + // The superuser must be created BEFORE this suite's own ranks: the Users + // afterChange hook auto-provisions an entry-level "Recruit" rank on first + // user creation, and Ranks are `orderable` - newly created ranks always + // receive monotonically trailing _order tokens. The "top of the ladder" + // guard in submitNomination only rejects when Col is the LAST ladder + // entry, so the hook-created Recruit must sort before Pvt..Col. Creating + // any user first guarantees that on a freshly cloned database regardless + // of which files share the worker (this suite used to pass only because + // earlier alphabet files happened to seed Recruit in a shared db). + superuser = await makeUser(`${RUN}-superuser`, ["admin"]); + // Self-contained 4-rank ladder: Pvt < Cpl < Sgt < Col (orderable _order). for (const [name, abbreviation] of [ ["Pvt", "pvt"], @@ -186,7 +197,6 @@ describe("Promotion nominations", () => { })) as unknown as Role; roleIds.push(superRole.id); - superuser = await makeUser(`${RUN}-superuser`, ["admin"]); await payload.update({ collection: "users", id: superuser.id, diff --git a/tests/int/promotions.int.spec.ts b/tests/int/promotions.int.spec.ts index 316de96b..7c4e8a9a 100644 --- a/tests/int/promotions.int.spec.ts +++ b/tests/int/promotions.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Rank, User } from "@/payload-types"; @@ -125,8 +125,7 @@ describe("Promotions", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Self-contained 5-rank ladder: Pvt < Cpl < Sgt < SSgt < Lt. pvt = await makeRank("Pvt", "pvt"); diff --git a/tests/int/reinforcement-pools.int.spec.ts b/tests/int/reinforcement-pools.int.spec.ts index 1a37de95..3088e8c9 100644 --- a/tests/int/reinforcement-pools.int.spec.ts +++ b/tests/int/reinforcement-pools.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { NextRequest } from "next/server"; import { processReinforcementEvent } from "@/lib/arma-bridge/reinforcements"; @@ -55,8 +55,7 @@ describe("Reinforcement pools", () => { beforeAll(async () => { process.env.ARMA_BRIDGE_API_KEY = BRIDGE_KEY; - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const server = await payload.create({ collection: "game-servers", diff --git a/tests/int/respawn-tickets.int.spec.ts b/tests/int/respawn-tickets.int.spec.ts index 919a8561..97863b4b 100644 --- a/tests/int/respawn-tickets.int.spec.ts +++ b/tests/int/respawn-tickets.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types"; @@ -33,8 +33,7 @@ describe("Respawn ticket settlement", () => { const ticketIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", diff --git a/tests/int/ribbon-submissions.int.spec.ts b/tests/int/ribbon-submissions.int.spec.ts index cbc38542..60773489 100644 --- a/tests/int/ribbon-submissions.int.spec.ts +++ b/tests/int/ribbon-submissions.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { User, RibbonSubmission } from "@/payload-types"; @@ -23,8 +23,7 @@ describe("Ribbon Design Submissions", () => { let adminUserId: number; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Create a regular user user = (await payload.create({ diff --git a/tests/int/roll-call-force.int.spec.ts b/tests/int/roll-call-force.int.spec.ts index 5fc62459..b69e92a0 100644 --- a/tests/int/roll-call-force.int.spec.ts +++ b/tests/int/roll-call-force.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Campaign, Map as MissionMap, Mission, User } from "@/payload-types"; @@ -20,8 +20,7 @@ describe("Forced roll-call target helpers", () => { const missionIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const map = (await payload.create({ collection: "maps", diff --git a/tests/int/server-presence.int.spec.ts b/tests/int/server-presence.int.spec.ts index 74278833..4213f91a 100644 --- a/tests/int/server-presence.int.spec.ts +++ b/tests/int/server-presence.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { GameServer } from "@/payload-types"; @@ -14,8 +14,7 @@ describe("Server presence sweep", () => { const serverIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); now = new Date(); now.setMilliseconds(0); }); diff --git a/tests/int/shipment-lifecycle.int.spec.ts b/tests/int/shipment-lifecycle.int.spec.ts index a8908704..1d238a5c 100644 --- a/tests/int/shipment-lifecycle.int.spec.ts +++ b/tests/int/shipment-lifecycle.int.spec.ts @@ -1,6 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { describe, it, expect, beforeAll, afterAll } from "vitest"; -import config from "@payload-config"; -import { getPayload, type Payload } from "payload"; +import { type Payload } from "payload"; import type { GameStructure, GameVehicle, @@ -114,7 +114,7 @@ describe("shipment lifecycle (advanceShipment)", () => { }) as Promise; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); const rules = await payload.findGlobal({ slug: "game-rules" }); expect(rules).toBeTruthy(); diff --git a/tests/int/story-beats.int.spec.ts b/tests/int/story-beats.int.spec.ts index 50ceb438..92909a50 100644 --- a/tests/int/story-beats.int.spec.ts +++ b/tests/int/story-beats.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { StoryBeat, StoryBeatState, StoryVoiceover, User } from "@/payload-types"; @@ -66,8 +66,7 @@ describe("Story beats", () => { let publishedBeatId: number; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const createTestUser = async (name: string, roles: User["roles"]) => { return (await payload.create({ diff --git a/tests/int/supply-box-actions.int.spec.ts b/tests/int/supply-box-actions.int.spec.ts index 8bea2802..dfc02115 100644 --- a/tests/int/supply-box-actions.int.spec.ts +++ b/tests/int/supply-box-actions.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it, vi, type MockInstance } from "vitest"; import type { ArmaCommand, SupplyBox, User } from "@/payload-types"; import { queueCrateCommand } from "@/app/(frontend)/logistics/supply-boxes/actions"; @@ -102,8 +102,7 @@ async function findCommand(commandId: string): Promise { // Setup / teardown // --------------------------------------------------------------------------- beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); authSpy = vi.spyOn(payload, "auth"); const superRole = await payload.create({ diff --git a/tests/int/supply-box-sync.int.spec.ts b/tests/int/supply-box-sync.int.spec.ts index 3b1c11c3..d3e2c4d5 100644 --- a/tests/int/supply-box-sync.int.spec.ts +++ b/tests/int/supply-box-sync.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { GameServer, SupplyBox } from "@/payload-types"; @@ -16,8 +16,7 @@ describe("Supply box crate sync", () => { const boxIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const server = (await payload.create({ collection: "game-servers", data: { diff --git a/tests/int/surveys.int.spec.ts b/tests/int/surveys.int.spec.ts index 04a345c7..166e1336 100644 --- a/tests/int/surveys.int.spec.ts +++ b/tests/int/surveys.int.spec.ts @@ -1,4 +1,5 @@ -import { getPayload, type Payload } from "payload"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { type Payload } from "payload"; import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -201,8 +202,7 @@ describe("survey submission gates", () => { }); beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const rank = await payload.create({ @@ -385,8 +385,7 @@ describe("survey collection access control", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); staffRole = await makeRole("staff", { diff --git a/tests/int/tech-categories.int.spec.ts b/tests/int/tech-categories.int.spec.ts index b235b4ca..beb00f72 100644 --- a/tests/int/tech-categories.int.spec.ts +++ b/tests/int/tech-categories.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -107,8 +107,7 @@ describe("Tech Categories (collection + RBAC + tech tree data model)", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); invalidatePermissionCache(); const bareRole = await makeRole("bare", { permissions: [] }); diff --git a/tests/int/tech-research-tick.int.spec.ts b/tests/int/tech-research-tick.int.spec.ts index 2877ddcc..7d1e1b14 100644 --- a/tests/int/tech-research-tick.int.spec.ts +++ b/tests/int/tech-research-tick.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { GameStructure, LaborClassification, Structure, Technology, User } from "@/payload-types"; @@ -188,8 +188,7 @@ describe("tech tick (research engine)", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); baseTime = new Date(Date.UTC(2026, 9, 20, 12, 0, 0)); }); diff --git a/tests/int/tech-tree-concurrency.int.spec.ts b/tests/int/tech-tree-concurrency.int.spec.ts index 55af5bda..d907b2d8 100644 --- a/tests/int/tech-tree-concurrency.int.spec.ts +++ b/tests/int/tech-tree-concurrency.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -77,7 +77,7 @@ describe("Tech Tree optimistic concurrency guard (database)", () => { }); beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); const role = (await payload.create({ collection: "roles", @@ -281,7 +281,7 @@ describe("Tech Tree bulk position preconditions (database)", () => { }; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); for (const label of ["a", "b", "c"]) { await makeTech(label); } diff --git a/tests/int/tech-tree-positions.int.spec.ts b/tests/int/tech-tree-positions.int.spec.ts index d0a4f2e4..19f5ffa6 100644 --- a/tests/int/tech-tree-positions.int.spec.ts +++ b/tests/int/tech-tree-positions.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -87,7 +87,7 @@ describe("tech tree position bulk write (database)", () => { const ids: number[] = []; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); for (const label of ["a", "b", "c"]) { const doc = await payload.create({ collection: "technologies", diff --git a/tests/int/tickets.int.spec.ts b/tests/int/tickets.int.spec.ts index cecf9e6b..80d832e0 100644 --- a/tests/int/tickets.int.spec.ts +++ b/tests/int/tickets.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { convertLexicalToPlaintext } from "@payloadcms/richtext-lexical/plaintext"; import { beforeAll, afterAll, describe, expect, it } from "vitest"; @@ -91,8 +91,7 @@ describe("Tickets", () => { let staff: User; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); reporter = await makeUser("reporter"); assignee = await makeUser("assignee"); diff --git a/tests/int/transfer-requests.int.spec.ts b/tests/int/transfer-requests.int.spec.ts index 9843640e..574ed5d2 100644 --- a/tests/int/transfer-requests.int.spec.ts +++ b/tests/int/transfer-requests.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -103,8 +103,7 @@ describe("Assignment transfer requests", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); users.req1 = await makeUser("req-1"); users.leadA = await makeUser("lead-a"); diff --git a/tests/int/voiceover-listing.int.spec.ts b/tests/int/voiceover-listing.int.spec.ts index 1baaf814..625a71cc 100644 --- a/tests/int/voiceover-listing.int.spec.ts +++ b/tests/int/voiceover-listing.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload, type Where } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload, type Where } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; @@ -92,8 +92,7 @@ describe("voiceover listing scopes submissions to their script", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); const plainRole = await makeRole("plain", { permissions: [] }); const otherRole = await makeRole("other", { permissions: [] }); diff --git a/tests/int/wiki-documentation.int.spec.ts b/tests/int/wiki-documentation.int.spec.ts index 397a2b63..a1aa65c2 100644 --- a/tests/int/wiki-documentation.int.spec.ts +++ b/tests/int/wiki-documentation.int.spec.ts @@ -1,5 +1,5 @@ -import { getPayload, Payload } from "payload"; -import config from "@/payload.config"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { User, WikiPage } from "@/payload-types"; @@ -73,8 +73,7 @@ describe("Wiki Documentation category", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); plainUser = await makeUser(`${RUN}-plain`, ["user"]); adminUser = await makeUser(`${RUN}-admin`, ["admin"]); diff --git a/tests/int/wiki.int.spec.ts b/tests/int/wiki.int.spec.ts index b7702f08..9c845b56 100644 --- a/tests/int/wiki.int.spec.ts +++ b/tests/int/wiki.int.spec.ts @@ -1,6 +1,6 @@ -import { getPayload, Payload } from "payload"; +import { getTestPayload } from "../helpers/get-test-payload"; +import { Payload } from "payload"; import type { AccessArgs } from "payload"; -import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { User, WikiPage, WikiRevision, WikiTemplate } from "@/payload-types"; @@ -80,8 +80,7 @@ describe("Wiki", () => { const templateIds: number[] = []; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); user = (await payload.create({ collection: "users", @@ -416,8 +415,7 @@ describe("Wiki", () => { }; beforeAll(async () => { - const payloadConfig = await config; - payload = await getPayload({ config: payloadConfig }); + payload = await getTestPayload(); // Regression fixture: a role granting ONLY the four broad intelligence // read permissions that the standard "user" role also grants. Before the diff --git a/tests/int/world-map-screen.int.spec.ts b/tests/int/world-map-screen.int.spec.ts index 3d95cab9..810efa46 100644 --- a/tests/int/world-map-screen.int.spec.ts +++ b/tests/int/world-map-screen.int.spec.ts @@ -1,6 +1,6 @@ +import { getTestPayload } from "../helpers/get-test-payload"; import { describe, it, expect, beforeAll } from "vitest"; -import config from "@payload-config"; -import { getPayload, type Payload } from "payload"; +import { type Payload } from "payload"; import type { Campaign, Map as MapDoc } from "@/payload-types"; import { easeStatClass, @@ -42,7 +42,7 @@ describe("world map screen eligibility", () => { }; beforeAll(async () => { - payload = await getPayload({ config }); + payload = await getTestPayload(); officialCampaign = (await payload.create({ collection: "campaigns", diff --git a/tests/test-db.ts b/tests/test-db.ts index 22452b60..43cb610b 100644 --- a/tests/test-db.ts +++ b/tests/test-db.ts @@ -115,7 +115,11 @@ export function templateDatabaseUri(serialTestUri?: string): string { return withDatabaseName(base, `${testDatabaseName(base)}_template`); } -/** Per-vitest-worker clone URI (`ptf_app_test` -> `ptf_app_test_w`). */ +/** + * Per-fork-clone URI (`ptf_app_test` -> `ptf_app_test_w`). The id is the + * vitest fork process pid so one clone serves every file that scheduler + * assigns to the process (VITEST_WORKER_ID churns per task, not per process). + */ export function workerDatabaseUri(workerId: string | number, serialTestUri?: string): string { const base = serialTestUri ?? resolveTestDatabaseUri(); return withDatabaseName(base, `${testDatabaseName(base)}_w${workerId}`); diff --git a/tests/vitest-reporter.ts b/tests/vitest-reporter.ts index db90c48e..882d5c5d 100644 --- a/tests/vitest-reporter.ts +++ b/tests/vitest-reporter.ts @@ -2,10 +2,11 @@ import { clearFailureMarker, writeFailureMarker } from "./vitest-marker"; /** * Minimal custom reporter: records run outcome in the failure marker file so - * globalTeardown can keep worker database clones after a failed run (and drop - * them after a green one). See tests/vitest-marker.ts for why the reporter is - * the reliable channel. Vitest 4 calls onTestRunEnd(modules, errors, state) - * with state "passed" | "failed" | "interrupted". + * the post-run teardown (returned by tests/vitest-global.ts) can keep worker + * database clones after a failed run and drop them after a green one. See + * tests/vitest-marker.ts for why the reporter is the reliable channel. + * Vitest 4 calls onTestRunEnd(modules, errors, state) with state + * "passed" | "failed" | "interrupted". */ export default class FailureMarkerReporter { onTestRunEnd(_modules: unknown[], errors: unknown[], state?: string): void { diff --git a/tests/vitest-worker-setup.ts b/tests/vitest-worker-setup.ts index a22f124e..490caa14 100644 --- a/tests/vitest-worker-setup.ts +++ b/tests/vitest-worker-setup.ts @@ -11,33 +11,45 @@ import { cloneTemplateInto, workerDatabaseUri } from "./test-db"; * missing VITEST_WORKER_ID means this is not a vitest worker process. * * NOTE: vitest imports setupFiles but never calls their default export, so - * the work happens in a top-level IIFE below. The vite-node SSR transform + * the work happens in a top-level await below. The vite-node SSR transform * hoists top-level await into an async wrapper, so side effects MUST live * inside the awaited function, not before it. * * The clone happens ONCE PER FORK, not per test file: setup files re-run - * before every file under isolate:true, but process.env survives those module - * resets, so the marker env var short-circuits repeats. Within a fork, files - * keep running sequentially against one database that started empty plus the - * seeded baseline, exactly mirroring the old cross-file semantics while two - * forks never share tables. + * before every test file (even under isolate:false), but process.env survives + * those module resets, so the marker env var short-circuits repeats. Within a + * fork, files keep running sequentially against one database that started + * empty plus the seeded baseline, exactly mirroring the old cross-file + * semantics while two forks never share tables. */ const CLONED_MARKER_ENV = "PTF_WORKER_DB_CLONED"; async function attachWorkerDatabase(): Promise { if (process.env.TEST_DATABASE_URI || !process.env.VITEST_WORKER_ID) return; - // vitest.setup.ts already repointed DATABASE_URI at the serial test db, so - // derive names from THAT (re-resolving would append `_test` twice). - const uri = workerDatabaseUri(process.env.VITEST_WORKER_ID, process.env.DATABASE_URI); + // Clone identity is the FORK PROCESS (pid), not VITEST_WORKER_ID: Vitest + // increments that env var per task even when it REUSES a fork process, so + // naming off it re-cloned once per test file. The pid is stable for the + // process lifetime, so exactly one clone amortizes across every file the + // scheduler runs on this fork - which is also what keeps the memoized + // getTestPayload() instance, booted against this URI, honest. + const serialUri = + process.env.PTF_SERIAL_TEST_DATABASE_URI ?? + (() => { + throw new Error("PTF_SERIAL_TEST_DATABASE_URI missing - vitest.setup.ts must run first"); + })(); + const uri = workerDatabaseUri(process.pid, serialUri); + process.env.DATABASE_URI = uri; if (process.env[CLONED_MARKER_ENV] === uri) return; await cloneTemplateInto(uri); process.env[CLONED_MARKER_ENV] = uri; - process.env.DATABASE_URI = uri; if (!process.env.TEST_PG_POOL_MAX) { // Reads as pg PoolConfig.max in src/payload.config.ts (unset elsewhere). - process.env.TEST_PG_POOL_MAX = "3"; + // Cap 8: transaction-fan-out specs stall the payload helper hooks when + // the pool is tight (measured deadlocks at 3); 8 forks x 8 still fits + // under Postgres max_connections=100 with headroom for dev/prd idles. + process.env.TEST_PG_POOL_MAX = "8"; } } diff --git a/vitest.config.mts b/vitest.config.mts index d8bf42d8..de2b9fa2 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -24,8 +24,18 @@ export default defineConfig({ // the old serial execution: cross-file data interference is gone by // construction because two forks never share tables. Fork cap stays // conservative: Postgres max_connections=100 on this box plus IO. + // + // isolate:false is load-bearing, not just an optimization: the + // Vitest 4 pool scheduler REUSES fork processes only between + // non-isolated tasks (cli-api schedule()), and module/instance + // persistence across files is what lets tests/helpers/ + // get-test-payload.ts memoize the Payload boot. Per-file + // state-leak risk is blunted by the afterEach cleanup harness in + // vitest.setup.ts; specs that still misbehave opt out by booting + // their own payload instance (see helper docstring). fileParallelism: true, pool: "forks" as const, + isolate: false, maxWorkers: maxForks, }), }, diff --git a/vitest.setup.ts b/vitest.setup.ts index 2a2a8627..0a24dc84 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -3,9 +3,47 @@ // Load .env files import "dotenv/config"; +import { afterEach, vi } from "vitest"; + import { resolveTestDatabaseUri } from "./tests/test-db"; // Tests never touch the development database: every payload connection in the // test process targets the dedicated test database (see tests/test-db.ts). // Must run before any test file imports the Payload config. -process.env.DATABASE_URI = resolveTestDatabaseUri(); +// +// Setup files re-run before EVERY test file (even under isolate:false), while +// tests/vitest-worker-setup.ts repoints DATABASE_URI at a per-fork clone - +// so both must be idempotent. The serial test URI is resolved ONCE per +// process (PTF_SERIAL_TEST_DATABASE_URI); once a fork has cloned its own db, +// the worker setup owns DATABASE_URI and we leave it alone. Re-appending +// `_test` to an already-derived name here caused `_w7_test_test...` chaos. +if (!process.env.PTF_SERIAL_TEST_DATABASE_URI) { + process.env.PTF_SERIAL_TEST_DATABASE_URI = resolveTestDatabaseUri(); +} +if (!process.env.PTF_WORKER_DB_CLONED) { + process.env.DATABASE_URI = process.env.PTF_SERIAL_TEST_DATABASE_URI; +} + +// Stage 3 shared-isolation guard: under `isolate: false`, fake timers left +// running by one file's tests would bleed into later files on the same fork. +// NOTE: intentionally NOT calling vi.restoreAllMocks() here - it resurrects +// vi.mock() module factories (e.g. "next/headers") that specs depend on for +// their remaining tests within a file; observed breaking map-system and +// supply-box-actions when applied globally after every test. +afterEach(() => { + vi.useRealTimers(); +}); + +// Fire-and-forget game-event/notification writes can reject AFTER their test +// file has cleaned up fixture rows (FK on actor_id), which Vitest 4 reports +// as an anonymous global unhandled rejection and turns the whole run red even +// though every test passed. The emitting code paths already catch these in +// most orderings; this listener makes that explicit for the racy remainder: +// log for visibility, and mark handled so a benign post-cleanup race cannot +// flip the exit code. Genuine assertion/hook failures still fail normally. +process.on("unhandledRejection", (reason) => { + const r = reason as { message?: string; stack?: string; cause?: { message?: string } }; + console.error( + `[test-db] swallowed post-cleanup unhandled rejection: ${r?.message ?? r?.cause?.message ?? JSON.stringify(reason)?.slice(0, 200)}`, + ); +});