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.
304 lines
10 KiB
TypeScript
304 lines
10 KiB
TypeScript
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";
|
|
|
|
let payload: Payload;
|
|
const RUN = `ops-surface-${Date.now().toString(36)}`;
|
|
|
|
const syncEventIds: number[] = [];
|
|
const opEventIds: number[] = [];
|
|
const opEffectIds: number[] = [];
|
|
const userIds: number[] = [];
|
|
const roleIds: number[] = [];
|
|
const zoneIds: number[] = [];
|
|
const mapIds: number[] = [];
|
|
const factionIds: number[] = [];
|
|
let serverId: number;
|
|
let plainUserId: number;
|
|
let permUserId: number;
|
|
let adminUserId: number;
|
|
let mapId: number;
|
|
let zoneId: number;
|
|
let factionId: number;
|
|
|
|
const OP_A = `${RUN}-op-alpha`;
|
|
const OP_B = `${RUN}-op-beta`;
|
|
|
|
async function makeUser(label: string, roleDocIds?: number[]): Promise<number> {
|
|
const user = await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username: `${RUN}-${label}`,
|
|
discordUsername: `${RUN}-${label}-discord`,
|
|
displayName: `${RUN} ${label}`,
|
|
steamId: `${RUN}-steam-${label}`,
|
|
password: "Test1234",
|
|
...(roleDocIds && roleDocIds.length > 0 ? { roleDocs: roleDocIds } : {}),
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
userIds.push(user.id);
|
|
return user.id;
|
|
}
|
|
|
|
async function makeRawEvent(label: string, type: string, payloadData: unknown): Promise<number> {
|
|
const raw = await payload.create({
|
|
collection: "arma-sync-events",
|
|
data: {
|
|
eventId: `${RUN}-${label}`,
|
|
server: serverId,
|
|
type,
|
|
occurredAt: new Date().toISOString(),
|
|
payload: payloadData as Record<string, unknown>,
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
syncEventIds.push(raw.id);
|
|
const found = await payload.find({
|
|
collection: "operation-events",
|
|
where: { sourceEvent: { equals: raw.id } },
|
|
limit: 1,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect(found.docs).toHaveLength(1);
|
|
opEventIds.push(found.docs[0].id);
|
|
const effects = await payload.find({
|
|
collection: "operation-effects",
|
|
where: { operationEvent: { equals: found.docs[0].id } },
|
|
limit: 10,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
for (const effect of effects.docs) opEffectIds.push(effect.id);
|
|
return raw.id;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
|
|
const superRole = await payload.create({
|
|
collection: "roles",
|
|
data: { name: `${RUN} Surface Admin`, slug: `${RUN}-surface-admin`, isSuperuser: true },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
roleIds.push(superRole.id);
|
|
|
|
const permRole = await payload.create({
|
|
collection: "roles",
|
|
data: {
|
|
name: `${RUN} Surface Viewer`,
|
|
slug: `${RUN}-surface-viewer`,
|
|
permissions: [
|
|
"operation-reservations:read",
|
|
"zone-pressure:read",
|
|
"operation-readiness:read",
|
|
],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
roleIds.push(permRole.id);
|
|
|
|
const faction = await payload.create({
|
|
collection: "factions",
|
|
data: { name: `${RUN} Faction`, type: "majorFaction" },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
factionIds.push(faction.id);
|
|
factionId = faction.id;
|
|
|
|
const server = await payload.create({
|
|
collection: "game-servers",
|
|
data: { serverId: `${RUN}-srv`, name: `${RUN} Server`, status: "online" },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
serverId = server.id;
|
|
|
|
const map = await payload.create({
|
|
collection: "maps",
|
|
data: { name: `${RUN} Map`, worldSizeWidth: 8192, worldSizeHeight: 8192, basemapMode: "image" },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
mapIds.push(map.id);
|
|
mapId = map.id;
|
|
|
|
const zone = await payload.create({
|
|
collection: "map-zones",
|
|
data: {
|
|
name: `${RUN} Zone Alpha`,
|
|
map: mapId,
|
|
type: "territory",
|
|
points: [
|
|
[0, 0],
|
|
[100, 0],
|
|
[100, 100],
|
|
[0, 100],
|
|
],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
zoneIds.push(zone.id);
|
|
zoneId = zone.id;
|
|
|
|
plainUserId = await makeUser("plain");
|
|
permUserId = await makeUser("perm", [permRole.id]);
|
|
adminUserId = await makeUser("admin", [superRole.id]);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
if (!payload) return;
|
|
for (const id of opEffectIds) {
|
|
await payload.delete({ collection: "operation-effects", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of opEventIds) {
|
|
await payload.delete({ collection: "operation-events", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of syncEventIds) {
|
|
await payload.delete({ collection: "arma-sync-events", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of zoneIds) {
|
|
await payload.delete({ collection: "map-zones", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of mapIds) {
|
|
await payload.delete({ collection: "maps", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of factionIds) {
|
|
await payload.delete({ collection: "factions", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of userIds) {
|
|
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of roleIds) {
|
|
await payload.delete({ collection: "roles", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
});
|
|
|
|
describe("operation surfaces: capabilities", () => {
|
|
it("grants a superuser every gated capability", async () => {
|
|
const adminUser = await payload.findByID({
|
|
collection: "users",
|
|
id: adminUserId,
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
const caps = await getSurfaceCapabilities(payload, { id: adminUserId });
|
|
expect(adminUser.id).toBe(adminUserId);
|
|
expect(caps.canSeeReservations).toBe(true);
|
|
expect(caps.canSeeReadiness).toBe(true);
|
|
expect(caps.canSeePressure).toBe(true);
|
|
});
|
|
|
|
it("denies a plain user every gated capability", async () => {
|
|
const caps = await getSurfaceCapabilities(payload, { id: plainUserId });
|
|
expect(caps.canSeeReservations).toBe(false);
|
|
expect(caps.canSeeReadiness).toBe(false);
|
|
expect(caps.canSeePressure).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("operation surfaces: overview", () => {
|
|
it("groups recent events by operation with effect status counts", async () => {
|
|
await makeRawEvent("start-a1", "operation.start", { operationId: OP_A, version: 1 });
|
|
await makeRawEvent("start-a2", "operation.start", { operationId: OP_A, version: 1 });
|
|
await makeRawEvent("start-b1", "operation.start", { operationId: OP_B, version: 1 });
|
|
|
|
const overview = await getOperationOverview(payload, { id: adminUserId });
|
|
const opA = overview.recentOperations.find((op) => op.operationId === OP_A);
|
|
expect(opA).toBeDefined();
|
|
expect(opA?.eventCount).toBe(2);
|
|
expect(opA?.appliedCount).toBe(2);
|
|
expect(opA?.failedCount).toBe(0);
|
|
expect(opA?.lastOccurredAt).toBeTruthy();
|
|
});
|
|
|
|
it("scopes my extractions to the requesting actor only", async () => {
|
|
await makeRawEvent("extract-b1", "operation.extraction", {
|
|
operationId: OP_B,
|
|
version: 1,
|
|
actorId: plainUserId,
|
|
kind: "player",
|
|
items: [{ assetId: 999_999_999, quantity: 1 }],
|
|
});
|
|
|
|
const mine = await getOperationOverview(payload, { id: plainUserId });
|
|
const myRows = mine.myExtractions.filter((ex) => ex.operationId === OP_B);
|
|
expect(myRows).toHaveLength(1);
|
|
expect(myRows[0].effectStatus).toBeTruthy();
|
|
|
|
const others = await getOperationOverview(payload, { id: permUserId });
|
|
expect(others.myExtractions.some((ex) => ex.operationId === OP_B)).toBe(false);
|
|
});
|
|
|
|
it("hides gated sections from plain users but keeps the ledger visible", async () => {
|
|
const overview = await getOperationOverview(payload, { id: plainUserId });
|
|
expect(overview.capabilities.canSeePressure).toBe(false);
|
|
expect(overview.pressure).toHaveLength(0);
|
|
expect(overview.recentOperations.length).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe("operation surfaces: ledger and AAR", () => {
|
|
it("filters the ledger by operation and reports effect counts without leaking payload", async () => {
|
|
const result = await getOperationLedger(payload, { id: adminUserId }, { operationId: OP_A });
|
|
expect(result.events).toHaveLength(2);
|
|
expect(result.events.every((ev) => ev.operationId === OP_A)).toBe(true);
|
|
expect(result.effectCounts.applied).toBe(2);
|
|
for (const ev of result.events) {
|
|
expect("payload" in ev ? ev.payload : undefined).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it("paginates the ledger", async () => {
|
|
const page1 = await getOperationLedger(payload, { id: adminUserId }, {}, 1);
|
|
expect(page1.page).toBe(1);
|
|
expect(page1.totalDocs).toBeGreaterThanOrEqual(3);
|
|
});
|
|
|
|
it("returns null for an unknown operation id", async () => {
|
|
const aar = await getOperationAar(payload, { id: adminUserId }, `${RUN}-does-not-exist`);
|
|
expect(aar).toBeNull();
|
|
});
|
|
|
|
it("builds an AAR with counts and never includes raw effect payloads", async () => {
|
|
const aar = await getOperationAar(payload, { id: adminUserId }, OP_B);
|
|
expect(aar).not.toBeNull();
|
|
expect(aar?.events.length).toBe(2);
|
|
expect(aar?.counts.validated).toBe(2);
|
|
expect(aar?.counts.applied).toBe(1);
|
|
const failed = aar?.effects.filter((eff) => eff.status === "failed") ?? [];
|
|
expect(failed.length).toBe(1);
|
|
expect(failed[0]?.effectType).toBe("operation.extraction-recorded");
|
|
for (const eff of aar?.effects ?? []) {
|
|
expect("payload" in eff ? eff.payload : undefined).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it("gates AAR pressure and reservation sections by permission", async () => {
|
|
await makeRawEvent("end-a1", "operation.end", {
|
|
operationId: OP_A,
|
|
version: 1,
|
|
outcome: "success",
|
|
zoneId,
|
|
factionId,
|
|
});
|
|
|
|
const forAdmin = await getOperationAar(payload, { id: adminUserId }, OP_A);
|
|
expect(forAdmin?.pressure).toHaveLength(1);
|
|
expect(forAdmin?.pressure[0]?.zoneId).toBe(zoneId);
|
|
expect(forAdmin?.pressure[0]?.delta).toBe(-25);
|
|
|
|
const forPlain = await getOperationAar(payload, { id: plainUserId }, OP_A);
|
|
expect(forPlain?.pressure).toHaveLength(0);
|
|
expect(forPlain?.reservations).toHaveLength(0);
|
|
});
|
|
});
|