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.
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import { getTestPayload } from "../helpers/get-test-payload";
|
|
import { Payload } from "payload";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import { resolveAttachments } from "@/lib/npc-dialogue/attachments";
|
|
|
|
let payload: Payload;
|
|
|
|
const TIMEOUT = 30_000;
|
|
|
|
/** Create a tiny media doc and return its id. */
|
|
async function createMedia(name: string): Promise<number> {
|
|
const doc = await payload.create({
|
|
collection: "media",
|
|
data: { alt: name },
|
|
file: {
|
|
data: Buffer.from(`attachment fixture: ${name}`),
|
|
mimetype: "text/plain",
|
|
name,
|
|
size: 24,
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
return doc.id;
|
|
}
|
|
|
|
describe("resolveAttachments", () => {
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
}, TIMEOUT);
|
|
|
|
afterAll(async () => {
|
|
// Nothing persists: each test cleans up its own docs below.
|
|
});
|
|
|
|
it(
|
|
"returns nothing for missing or empty input",
|
|
async () => {
|
|
expect(await resolveAttachments(payload, undefined)).toEqual([]);
|
|
expect(await resolveAttachments(payload, [])).toEqual([]);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"resolves pointers to renderable references, honouring custom labels",
|
|
async () => {
|
|
const id = await createMedia("b2-int-transcript.txt");
|
|
try {
|
|
const [resolved] = await resolveAttachments(payload, [
|
|
{ mediaId: id, label: "Intercept transcript" },
|
|
]);
|
|
expect(resolved).toMatchObject({
|
|
label: "Intercept transcript",
|
|
url: "/api/media/file/b2-int-transcript.txt",
|
|
filename: "b2-int-transcript.txt",
|
|
mimeType: "text/plain",
|
|
});
|
|
} finally {
|
|
await payload.delete({ collection: "media", id, overrideAccess: true });
|
|
}
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"falls back to the stored filename when no label is authored",
|
|
async () => {
|
|
const id = await createMedia("b2-int-order.txt");
|
|
try {
|
|
const [resolved] = await resolveAttachments(payload, [{ mediaId: id }]);
|
|
expect(resolved?.label).toBe("b2-int-order.txt");
|
|
} finally {
|
|
await payload.delete({ collection: "media", id, overrideAccess: true });
|
|
}
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
|
|
it(
|
|
"skips deleted media and malformed pointers instead of rendering dead chips",
|
|
async () => {
|
|
const doomed = await createMedia("b2-int-doomed.txt");
|
|
await payload.delete({ collection: "media", id: doomed, overrideAccess: true });
|
|
const resolved = await resolveAttachments(payload, [
|
|
{ mediaId: doomed },
|
|
{ mediaId: 0 },
|
|
{ mediaId: "not-a-number" },
|
|
{ mediaId: -3 },
|
|
]);
|
|
expect(resolved).toEqual([]);
|
|
},
|
|
TIMEOUT,
|
|
);
|
|
});
|