/** * Playwright global setup for dev-system tests. * * Logs in once per user role and saves browser storage state to disk. * Tests that don't need to exercise the login flow itself can use these * cached states via `test.use({ storageState: '...' })` to avoid * hitting the auth rate limiter (5 attempts / 15 min / email). */ import { chromium, type FullConfig } from "@playwright/test"; import * as fs from "fs"; import * as path from "path"; const USERS = { admin: { email: "admin@planarchy.dev", password: "admin123" }, manager: { email: "manager@planarchy.dev", password: "manager123" }, viewer: { email: "viewer@planarchy.dev", password: "viewer123" }, } as const; async function globalSetup(config: FullConfig) { const baseURL = config.projects[0]?.use?.baseURL ?? "http://localhost:3100"; const authDir = path.join(__dirname, ".auth"); fs.mkdirSync(authDir, { recursive: true }); const browser = await chromium.launch(); for (const [role, creds] of Object.entries(USERS)) { const context = await browser.newContext(); const page = await context.newPage(); await page.goto(`${baseURL}/auth/signin`); await page.fill('input[type="email"]', creds.email); await page.fill('input[type="password"]', creds.password); await page.click('button[type="submit"]'); // Wait for successful redirect await page.waitForURL(/\/(dashboard|resources)/, { timeout: 15000 }); await context.storageState({ path: path.join(authDir, `${role}.json`) }); await context.close(); console.log(`[global-setup] Saved auth state for ${role}`); } await browser.close(); } export default globalSetup;