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.
140 lines
5.3 KiB
TypeScript
140 lines
5.3 KiB
TypeScript
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";
|
|
|
|
let payload: Payload;
|
|
const RUN = `kill-${Date.now().toString(36)}`;
|
|
|
|
describe("Kill tracking", () => {
|
|
let userId: number;
|
|
let profileId: number;
|
|
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
|
|
const user = await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username: `${RUN}-killer`,
|
|
discordUsername: `${RUN}-killer-discord`,
|
|
displayName: `${RUN} Killer`,
|
|
steamId: `${RUN}-steam-001`,
|
|
password: "Test1234",
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
userId = user.id;
|
|
|
|
const profiles = await payload.find({
|
|
collection: "profiles",
|
|
where: { user: { equals: userId } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
profileId = profiles.docs[0].id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
if (profileId) {
|
|
await payload.delete({ collection: "profiles", id: profileId, overrideAccess: true }).catch(() => {});
|
|
}
|
|
if (userId) {
|
|
await payload.delete({ collection: "users", id: userId, overrideAccess: true }).catch(() => {});
|
|
}
|
|
});
|
|
|
|
describe("classifyKillCategory", () => {
|
|
it("classifies infantry kills", () => {
|
|
expect(classifyKillCategory("O_T_Soldier_AT_F")).toBe("infantryKills");
|
|
expect(classifyKillCategory("B_Soldier_GL_F")).toBe("infantryKills");
|
|
expect(classifyKillCategory(null)).toBe("infantryKills");
|
|
expect(classifyKillCategory("")).toBe("infantryKills");
|
|
});
|
|
|
|
it("classifies vehicle kills", () => {
|
|
expect(classifyKillCategory("B_T_LTV_01_recon_F")).toBe("vehicleKills");
|
|
expect(classifyKillCategory("O_T_MRAP_01_hmg_F")).toBe("vehicleKills");
|
|
});
|
|
|
|
it("classifies armor kills", () => {
|
|
expect(classifyKillCategory("B_T_MBT_01_cannon_F")).toBe("armorKills");
|
|
expect(classifyKillCategory("O_T_APC_Wheeled_02_rcws_F")).toBe("armorKills");
|
|
expect(classifyKillCategory("I_LT_01_AT_F")).toBe("armorKills");
|
|
});
|
|
|
|
it("classifies air kills", () => {
|
|
expect(classifyKillCategory("B_Heli_Light_01_armed_F")).toBe("airKills");
|
|
expect(classifyKillCategory("O_T_VTOL_02_infantry_dynamicLoadout_F")).toBe("airKills");
|
|
expect(classifyKillCategory("B_Plane_Fighter_01_stealth_F")).toBe("airKills");
|
|
});
|
|
});
|
|
|
|
describe("parseKillPayload", () => {
|
|
it("parses valid kill payload", () => {
|
|
const result = parseKillPayload([
|
|
"Qi Fu", "", "[Capt.] J. Fraley", "76561198091303179",
|
|
"[Capt.] J. Fraley", "76561198091303179", "O_T_Soldier_AT_F", "B_Officer_Parade_Veteran_F",
|
|
]);
|
|
expect(result).toEqual({
|
|
victimName: "Qi Fu",
|
|
killerName: "[Capt.] J. Fraley",
|
|
killerSteamId: "76561198091303179",
|
|
victimVehicle: "O_T_Soldier_AT_F",
|
|
killerVehicle: "B_Officer_Parade_Veteran_F",
|
|
});
|
|
});
|
|
|
|
it("returns null for invalid payload", () => {
|
|
expect(parseKillPayload(null)).toBeNull();
|
|
expect(parseKillPayload("string")).toBeNull();
|
|
expect(parseKillPayload([])).toBeNull();
|
|
expect(parseKillPayload([1, 2, 3])).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("processPlayerKill", () => {
|
|
it("increments infantryKills for soldier victim", async () => {
|
|
const before = await payload.findByID({ collection: "profiles", id: profileId, depth: 0, overrideAccess: true });
|
|
const beforeCount = (before.progression?.scoreboard as Record<string, number>)?.infantryKills ?? 0;
|
|
|
|
await processPlayerKill(payload, {
|
|
id: 999001,
|
|
payload: ["Qi Fu", "", `${RUN} Killer`, `${RUN}-steam-001`,
|
|
`${RUN} Killer`, `${RUN}-steam-001`, "O_T_Soldier_AT_F", "B_Officer_Parade_Veteran_F"],
|
|
occurredAt: new Date().toISOString(),
|
|
});
|
|
|
|
const after = await payload.findByID({ collection: "profiles", id: profileId, depth: 0, overrideAccess: true });
|
|
const afterCount = (after.progression?.scoreboard as Record<string, number>)?.infantryKills ?? 0;
|
|
expect(afterCount).toBe(beforeCount + 1);
|
|
});
|
|
|
|
it("increments armorKills for armored victim", async () => {
|
|
const before = await payload.findByID({ collection: "profiles", id: profileId, depth: 0, overrideAccess: true });
|
|
const beforeCount = (before.progression?.scoreboard as Record<string, number>)?.armorKills ?? 0;
|
|
|
|
await processPlayerKill(payload, {
|
|
id: 999002,
|
|
payload: ["Tank", "", `${RUN} Killer`, `${RUN}-steam-001`,
|
|
`${RUN} Killer`, `${RUN}-steam-001`, "B_T_MBT_01_cannon_F", "B_T_MBT_01_cannon_F"],
|
|
occurredAt: new Date().toISOString(),
|
|
});
|
|
|
|
const after = await payload.findByID({ collection: "profiles", id: profileId, depth: 0, overrideAccess: true });
|
|
const afterCount = (after.progression?.scoreboard as Record<string, number>)?.armorKills ?? 0;
|
|
expect(afterCount).toBe(beforeCount + 1);
|
|
});
|
|
|
|
it("ignores events for unknown Steam IDs", async () => {
|
|
await processPlayerKill(payload, {
|
|
id: 999003,
|
|
payload: ["Victim", "", "Unknown Player", "00000000000000000",
|
|
"Unknown Player", "00000000000000000", "O_T_Soldier_AT_F", "B_Soldier_F"],
|
|
occurredAt: new Date().toISOString(),
|
|
});
|
|
});
|
|
});
|
|
});
|