1
0
Fork 0
polaris-task-force/tests/e2e/global-setup.ts
Z8MB1E d16e721ccc test(e2e): seed e2e user, serialize workers, and deflake specs
The suite could not pass on the test database: global setup now seeds the shared dev/Test123 login with a superuser Roles doc and the dashboard rank, and points its own Payload connection at the test database. Playwright runs one worker with generous test and expect timeouts because first navigation to a route triggers a cold webpack compile, and gains E2E_BROWSER_PATH to swap the browser binary when the installed Vivaldi crashes under automation. The landing page gains the missing h1 the guest specs assert, login flows wait for the redirect to land, strict-mode locator collisions get exact or level-scoped matches, the session banner test re-signs the login claims with a short expiry using the runtime secret, and the reservation dialog click forces the dispatch and verifies the dialog opened (the trigger goes aria-hidden behind the modal mid-click, which stranded the retry loop).
2026-09-30 18:43:29 -04:00

129 lines
3.6 KiB
TypeScript

import { bootstrapTestDatabase } from "../test-db";
import { resolveTestDatabaseUri } from "../test-db";
/**
* Playwright global setup: ensure the dedicated test database exists, is
* migrated, seeded and emptied 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,
});
}
}
}