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.
164 lines
5.2 KiB
TypeScript
164 lines
5.2 KiB
TypeScript
import { getTestPayload } from "../helpers/get-test-payload";
|
|
import { Payload } from "payload";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
MAX_POSITION_UPDATES,
|
|
parsePositionUpdates,
|
|
writeTreePositions,
|
|
} from "@/lib/tech-tree/positions";
|
|
|
|
const RUN = `ttpos-${Date.now().toString(36)}`;
|
|
const TIMEOUT = 30_000;
|
|
|
|
describe("tech tree position bulk-write validation", () => {
|
|
it("accepts pins and clears", () => {
|
|
const result = parsePositionUpdates({
|
|
positions: [
|
|
{ id: 1, x: 10.4, y: 20.6 },
|
|
{ id: 2, x: null, y: null },
|
|
],
|
|
});
|
|
expect(result).toEqual({
|
|
ok: true,
|
|
updates: [
|
|
{ id: 1, x: 10, y: 21, expectedUpdatedAt: null },
|
|
{ id: 2, x: null, y: null, expectedUpdatedAt: null },
|
|
],
|
|
});
|
|
});
|
|
|
|
it("keeps the last write for a repeated id", () => {
|
|
const result = parsePositionUpdates({
|
|
positions: [
|
|
{ id: 3, x: 1, y: 1 },
|
|
{ id: 3, x: null, y: null },
|
|
],
|
|
});
|
|
expect(result).toEqual({
|
|
ok: true,
|
|
updates: [{ id: 3, x: null, y: null, expectedUpdatedAt: null }],
|
|
});
|
|
});
|
|
|
|
it("carries and validates the optimistic concurrency token", () => {
|
|
const accepted = parsePositionUpdates({
|
|
positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: "2026-10-15T12:00:00.000Z" }],
|
|
});
|
|
expect(accepted).toEqual({
|
|
ok: true,
|
|
updates: [{ id: 1, x: 5, y: 5, expectedUpdatedAt: "2026-10-15T12:00:00.000Z" }],
|
|
});
|
|
// Null means unconditional; malformed values are rejected.
|
|
expect(
|
|
parsePositionUpdates({ positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: null }] }),
|
|
).toEqual({ ok: true, updates: [{ id: 1, x: 5, y: 5, expectedUpdatedAt: null }] });
|
|
expect(
|
|
parsePositionUpdates({ positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: "soon" }] }).ok,
|
|
).toBe(false);
|
|
expect(
|
|
parsePositionUpdates({ positions: [{ id: 1, x: 5, y: 5, ifUnmodifiedAt: 123 }] }).ok,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects malformed bodies", () => {
|
|
expect(parsePositionUpdates(null).ok).toBe(false);
|
|
expect(parsePositionUpdates({ positions: "nope" }).ok).toBe(false);
|
|
expect(parsePositionUpdates({ positions: [{ id: 0, x: 1, y: 1 }] }).ok).toBe(false);
|
|
expect(parsePositionUpdates({ positions: [{ id: 1.5, x: 1, y: 1 }] }).ok).toBe(false);
|
|
// Half-cleared and non-finite coordinates are both invalid.
|
|
expect(parsePositionUpdates({ positions: [{ id: 1, x: 5, y: null }] }).ok).toBe(false);
|
|
expect(parsePositionUpdates({ positions: [{ id: 1, x: Number.NaN, y: 1 }] }).ok).toBe(false);
|
|
});
|
|
|
|
it("caps the batch size", () => {
|
|
const positions = Array.from({ length: MAX_POSITION_UPDATES + 1 }, (_, i) => ({
|
|
id: i + 1,
|
|
x: 0,
|
|
y: 0,
|
|
}));
|
|
expect(parsePositionUpdates({ positions }).ok).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("tech tree position bulk write (database)", () => {
|
|
let payload: Payload;
|
|
const ids: number[] = [];
|
|
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
for (const label of ["a", "b", "c"]) {
|
|
const doc = await payload.create({
|
|
collection: "technologies",
|
|
data: {
|
|
name: `${RUN}-${label}`,
|
|
summary: "Bulk position write fixture.",
|
|
type: "upgrade",
|
|
approvalStatus: "in_progress",
|
|
researchCosts: { minimumResearchDuration: 1 },
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
ids.push(doc.id);
|
|
}
|
|
}, TIMEOUT);
|
|
|
|
afterAll(async () => {
|
|
for (const id of ids) {
|
|
await payload.delete({ collection: "technologies", id, overrideAccess: true });
|
|
}
|
|
}, TIMEOUT);
|
|
|
|
const positionOf = async (id: number) => {
|
|
const doc = await payload.findByID({
|
|
collection: "technologies",
|
|
id,
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
});
|
|
return doc.treePosition ?? null;
|
|
};
|
|
|
|
it(
|
|
"pins many technologies in one write and clears them again",
|
|
async () => {
|
|
const pinned = await writeTreePositions(payload, [
|
|
{ id: ids[0], x: 46, y: 92, expectedUpdatedAt: null },
|
|
{ id: ids[1], x: 368, y: 184, expectedUpdatedAt: null },
|
|
{ id: ids[2], x: 0, y: 0, expectedUpdatedAt: null },
|
|
]);
|
|
expect(pinned.updated).toBe(3);
|
|
expect(pinned.conflicts).toEqual([]);
|
|
expect(await positionOf(ids[0])).toMatchObject({ x: 46, y: 92 });
|
|
expect(await positionOf(ids[1])).toMatchObject({ x: 368, y: 184 });
|
|
expect(await positionOf(ids[2])).toMatchObject({ x: 0, y: 0 });
|
|
|
|
// Auto-arrange path: clear every pin in a single statement.
|
|
const cleared = await writeTreePositions(
|
|
payload,
|
|
ids.map((id) => ({ id, x: null, y: null, expectedUpdatedAt: null })),
|
|
);
|
|
expect(cleared.updated).toBe(3);
|
|
for (const id of ids) {
|
|
const position = await positionOf(id);
|
|
expect(position?.x ?? null).toBeNull();
|
|
expect(position?.y ?? null).toBeNull();
|
|
}
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"ignores ids that do not exist",
|
|
async () => {
|
|
const touched = await writeTreePositions(payload, [
|
|
{ id: 999_999_999, x: 1, y: 1, expectedUpdatedAt: null },
|
|
]);
|
|
expect(touched.updated).toBe(0);
|
|
expect(touched.conflicts).toEqual([]);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
});
|