1
0
Fork 0
polaris-task-force/tests/int/arma-shop-money.int.spec.ts
Z8MB1E 938da9bf86 test(vitest): isolate:false shared forks with per-pid template clones + memoized payload boot
Stage 3 of the int-suite speedup (gate G2 showed Vitest 4's pool scheduler
only reuses worker processes between isolate:false tasks, so per-file fork
spawning made clone+boot costs unavoidable at stage 2):

- config: workers branch runs pool=forks with isolate:false, cap 8 via
  VITEST_MAX_FORKS, node default env (jsdom docblocks on DOM specs),
  failure-marker reporter, and idempotent setupFiles chain
- vitest-worker-setup: one template clone PER FORK PROCESS named by pid
  (VITEST_WORKER_ID churns per task, which silently re-cloned per file);
  DATABASE_URI repoint + TEST_PG_POOL_MAX=8 (measured hook deadlocks at 3)
- vitest.setup: once-per-process serial test URI markers; afterEach
  useRealTimers guard (restoreAllMocks omitted: it resurrects vi.mock
  module factories mid-file); logging absorber for fire-and-forget event
  writes that reject after their spec's cleanup deleted fixture users
- helpers/get-test-payload.ts: memoized Payload boot shared across files
  in a fork; codemod converted all 91 self-booting specs mechanically
- pinned opt-outs (own boot + comments): game-structures-labor-route,
  mission-reminder, notification-inbox-scope (jwtSign writes leak into
  the shared instance's auth state across files)
- promotion-nominations: create the superuser before its rank ladder so
  the Users-hook Recruit rank gets leading orderable tokens (its
  top-of-ladder guard needs Col to remain ladder-max on fresh clones);
  operation-economy: itemType-scope the market-state assertion (asset
  and resource ids collide as bare integers under per-fork databases)

Gate G3: 5 consecutive full green runs (149/149 files), warm wall 53-65s
vs 598s baseline (~10x), zero clone residue after green runs. Vitest 4
note: globalTeardown config no longer exists; teardown returns from
globalSetup.
2026-10-21 12:54:30 -04:00

138 lines
5.1 KiB
TypeScript

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";
import { getBalancesBySteamId, parseMoneyDelta, processMoneyEvent } from "@/lib/arma-bridge/money";
let payload: Payload;
const RUN = `money-${Date.now().toString(36)}`;
const STEAM_ID = `${RUN}-steam-001`;
const UNLINKED = `${RUN}-steam-unlinked`;
async function balanceOf(id: number): Promise<number> {
const acc = (await payload.findByID({
collection: "bank-accounts",
id,
depth: 0,
overrideAccess: true,
})) as unknown as BankAccount;
return acc.balance ?? 0;
}
let eventSeq = 0;
function moneyEvent(delta: number, reason: string, detail = "") {
eventSeq += 1;
return { eventId: `${RUN}:${eventSeq}`, payload: [STEAM_ID, delta, reason, detail] };
}
describe("Arma shop money sync", () => {
let userId: number;
let accountId: number;
let treasuryId: number;
let createdTreasury = false;
beforeAll(async () => {
payload = await getTestPayload();
const user = await payload.create({
collection: "users",
data: {
username: `${RUN}-player`,
discordUsername: `${RUN}-player-discord`,
displayName: `${RUN} Player`,
steamId: STEAM_ID,
password: "Test1234",
},
overrideAccess: true,
depth: 0,
});
userId = user.id;
const existingTreasury = await getTreasuryAccountId(payload);
if (existingTreasury) {
treasuryId = existingTreasury;
} else {
const treasury = await createAccount(payload, { name: `${RUN} Treasury`, accountType: "treasury" });
treasuryId = treasury.id;
createdTreasury = true;
}
});
afterAll(async () => {
if (accountId) {
await payload.delete({ collection: "bank-accounts", id: accountId, overrideAccess: true }).catch(() => {});
}
if (createdTreasury && treasuryId) {
await payload.delete({ collection: "bank-accounts", id: treasuryId, overrideAccess: true }).catch(() => {});
}
if (userId) {
const profiles = await payload.find({
collection: "profiles",
where: { user: { equals: userId } },
depth: 0,
overrideAccess: true,
});
for (const profile of profiles.docs) {
await payload.delete({ collection: "profiles", id: profile.id, overrideAccess: true }).catch(() => {});
}
await payload.delete({ collection: "users", id: userId, overrideAccess: true }).catch(() => {});
}
});
describe("parseMoneyDelta", () => {
it("parses [steamId, delta, reason, detail] and rounds the delta", () => {
expect(parseMoneyDelta(["7656119", -250.4, "Purchase", "arifle_MX_F x1"])).toEqual({
steamId: "7656119",
delta: -250,
reason: "purchase",
detail: "arifle_MX_F x1",
});
});
it("rejects malformed or zero payloads", () => {
expect(parseMoneyDelta(null)).toBeNull();
expect(parseMoneyDelta(["7656119", 100])).toBeNull();
expect(parseMoneyDelta(["", 100, "reward"])).toBeNull();
expect(parseMoneyDelta(["7656119", 0, "reward"])).toBeNull();
expect(parseMoneyDelta(["7656119", "100", "reward"])).toBeNull();
expect(parseMoneyDelta(["7656119", 100, ""])).toBeNull();
});
});
it("creates reward money in the personal account and reports it by Steam ID", async () => {
await processMoneyEvent(payload, moneyEvent(500, "reward", "Dialogue test"));
const account = await ensurePersonalAccount(payload, userId);
accountId = account.id;
expect(await balanceOf(accountId)).toBe(500);
const balances = await getBalancesBySteamId(payload, [STEAM_ID, UNLINKED]);
expect(balances[STEAM_ID]).toBe(500);
expect(balances).not.toHaveProperty(UNLINKED);
});
it("pays purchases to the treasury and refunds them from it", async () => {
const treasuryBefore = await balanceOf(treasuryId);
await processMoneyEvent(payload, moneyEvent(-200, "purchase", "arifle_MX_F x1"));
expect(await balanceOf(accountId)).toBe(300);
expect(await balanceOf(treasuryId)).toBe(treasuryBefore + 200);
await processMoneyEvent(payload, moneyEvent(200, "refund", "arifle_MX_F x1"));
expect(await balanceOf(accountId)).toBe(500);
expect(await balanceOf(treasuryId)).toBe(treasuryBefore);
});
it("does not overdraw: a debit larger than the balance is skipped without throwing", async () => {
await expect(processMoneyEvent(payload, moneyEvent(-100000, "purchase"))).resolves.toBeUndefined();
expect(await balanceOf(accountId)).toBe(500);
});
it("skips unlinked players and malformed events without throwing", async () => {
await expect(
processMoneyEvent(payload, { eventId: `${RUN}:unlinked`, payload: [UNLINKED, 100, "reward"] }),
).resolves.toBeUndefined();
await expect(processMoneyEvent(payload, { eventId: `${RUN}:bad`, payload: "nope" })).resolves.toBeUndefined();
expect(await balanceOf(accountId)).toBe(500);
});
});