import { type Page, type APIRequestContext, expect } from "@playwright/test"; export const TEST_ADMIN = { username: process.env.TEST_ADMIN_USER ?? "admin", password: process.env.TEST_ADMIN_PASS ?? "testpassword123", }; /** * Log in via the UI login form and wait for the dashboard to load. */ export async function loginViaUI(page: Page): Promise { await page.goto("/login"); await page.getByLabel(/username/i).fill(TEST_ADMIN.username); await page.getByLabel(/password/i).fill(TEST_ADMIN.password); await page.getByRole("button", { name: /log in|sign in/i }).click(); await expect(page).toHaveURL(/\/dashboard/); } /** * Log in via the admin API directly and store the token in localStorage. * Faster than UI login for tests that only need an authenticated session. */ export async function loginViaAPI( page: Page, request: APIRequestContext, ): Promise { const resp = await request.post("/admin/auth/login", { data: { username: TEST_ADMIN.username, password: TEST_ADMIN.password }, }); expect(resp.status()).toBe(200); const body = await resp.json(); expect(body).toHaveProperty("token"); await page.goto("/"); await page.evaluate( ({ token }) => localStorage.setItem("admin_token", token), { token: body.token as string }, ); return body.token as string; } /** * Log out via the UI and confirm redirect to /login. */ export async function logoutViaUI(page: Page): Promise { // Common patterns: a "Logout" button in the nav/header const logoutBtn = page .getByRole("button", { name: /log ?out|sign ?out/i }) .or(page.getByRole("link", { name: /log ?out|sign ?out/i })); await logoutBtn.click(); await expect(page).toHaveURL(/\/login/); }