1
0
Fork 0
polaris-task-force/tests/e2e/global-setup.ts
Z8MB1E 6be14c9815 test(test-db): template database + schema-hash cache + clone primitives
Reworks the test-db module into explicit primitives: ensureTemplateDatabase
(dropped/recreated + built once per schema hash change, cached in
.cache/testdb-template.json), cloneTemplateInto, dropDatabaseIfPresent and
dropStaleWorkerDatabases. bootstrapTestDatabase() keeps its export but is
re-expressed as guarantee-template + fresh clone for the serial databases
(vitest pre-stage-2, Playwright), so repeated runs skip drizzle push
entirely. The legacy truncate+push flow survives verbatim as
bootstrapSerialDatabase for the TEST_DATABASE_URI bisect mode. Nothing may
connect to the template while clones run (loud invariant comment).
2026-10-21 10:03:33 -04:00

130 lines
3.7 KiB
TypeScript

import { bootstrapTestDatabase } from "../test-db";
import { resolveTestDatabaseUri } from "../test-db";
/**
* Playwright global setup: ensure the dedicated test database exists and is
* freshly built from the cached schema template (push runs only when the
* schema hash changed) before the dev server (started by webServer with
* DATABASE_URI pointed at it) serves the e2e suite. Then seed the shared e2e
* login ("dev" / "Test123"): every authed spec logs in with it, and the
* dashboard smoke expects the "Welcome back, J. Fraley." heading.
*/
export default async function globalSetup(): Promise<void> {
await bootstrapTestDatabase();
// Seed against the TEST database: the payload config reads DATABASE_URI at
// import time, and .env points at the dev database.
process.env.DATABASE_URI = resolveTestDatabaseUri();
const { getPayload } = await import("payload");
const { default: config } = await import("@payload-config");
const payload = await getPayload({ config });
// Rank named after the dev user so the dashboard heading reads
// "Welcome back, J. Fraley." (the heading renders the rank's name).
let rank = (
await payload.find({
collection: "ranks",
where: { name: { equals: "J. Fraley" } },
limit: 1,
overrideAccess: true,
})
).docs[0];
if (!rank) {
rank = await payload.create({
collection: "ranks",
data: {
name: "J. Fraley",
abbreviation: "JF",
description: "E2E baseline rank",
},
overrideAccess: true,
depth: 0,
});
}
// Superuser role doc: e2e specs exercise permission-gated UI (e.g. the
// reservations "New Reservation" trigger needs operation-reservations:create),
// and permission resolution reads roleDocs -> Roles collection.
const superuserRole =
(
await payload.find({
collection: "roles",
where: { slug: { equals: "e2e-superuser" } },
limit: 1,
overrideAccess: true,
})
).docs[0] ??
(await payload.create({
collection: "roles",
data: { name: "E2E Superuser", slug: "e2e-superuser", isSuperuser: true },
overrideAccess: true,
depth: 0,
}));
const existingUser = (
await payload.find({
collection: "users",
where: { username: { equals: "dev" } },
limit: 1,
overrideAccess: true,
})
).docs[0];
const userData = {
username: "dev",
discordUsername: "dev",
displayName: "J. Fraley",
steamId: "76561190000000000",
password: "Test123",
roles: ["developer"] as ("developer" | "admin" | "user" | "guest" | "trusted")[],
roleDocs: [superuserRole.id],
};
if (existingUser) {
await payload.update({
collection: "users",
id: existingUser.id,
data: { ...userData, password: "Test123" },
overrideAccess: true,
depth: 0,
});
await payload.update({
collection: "profiles",
id: (
await payload.find({
collection: "profiles",
where: { user: { equals: existingUser.id } },
limit: 1,
overrideAccess: true,
})
).docs[0].id,
data: { rank: rank.id },
overrideAccess: true,
depth: 0,
});
} else {
const user = await payload.create({
collection: "users",
data: userData,
overrideAccess: true,
depth: 0,
});
const profile = (
await payload.find({
collection: "profiles",
where: { user: { equals: user.id } },
limit: 1,
overrideAccess: true,
})
).docs[0];
if (profile) {
await payload.update({
collection: "profiles",
id: profile.id,
data: { rank: rank.id },
overrideAccess: true,
depth: 0,
});
}
}
}