import { chromium } from '@playwright/test' const BACKEND_URL = 'http://localhost:5000' const BASE_URL = 'https://localhost:5173' export const E2E_EMAIL = 'e2e@test.com' export const E2E_PASSWORD = 'E2eTestPass1!' export const E2E_PIN = '1234' export const STORAGE_STATE = 'tests/.auth/user.json' // Matches PARENT_AUTH_KEY and PARENT_AUTH_EXPIRY_PERSISTENT in src/stores/auth.ts const PARENT_AUTH_KEY = 'parentAuth' const TWO_DAYS_MS = 172_800_000 export default async function globalSetup() { // Reset all tables and insert a verified test user directly via the backend const seedRes = await fetch(`${BACKEND_URL}/auth/e2e-seed`, { method: 'POST' }) if (!seedRes.ok) { throw new Error(`e2e-seed failed: ${seedRes.status} ${await seedRes.text()}`) } // Use a real browser to log in so that HttpOnly auth cookies are captured correctly const browser = await chromium.launch() const context = await browser.newContext({ ignoreHTTPSErrors: true }) const page = await context.newPage() await page.goto(`${BASE_URL}/auth/login`) await page.fill('#email', E2E_EMAIL) await page.fill('#password', E2E_PASSWORD) await page.click('button[type="submit"]') // After login the router redirects away from /auth — wait for that navigation await page.waitForURL(/\/(child|parent)/) // Inject persistent parent auth into localStorage so tests can access /parent routes // without navigating through the PIN prompt UI await page.evaluate( ({ key, expiresAt }) => localStorage.setItem(key, JSON.stringify({ expiresAt })), { key: PARENT_AUTH_KEY, expiresAt: Date.now() + TWO_DAYS_MS }, ) await context.storageState({ path: STORAGE_STATE }) await browser.close() }