import { test as base, expect, type Page } from '@playwright/test'; import { CONFIG } from '../helpers'; /** * API-driven authentication fixture. * Replaces UI login flows for faster, deterministic tests. * * Usage: * import { test } from './fixtures/auth.fixture'; * test('something', async ({ listenerPage, creatorPage }) => { ... }); */ async function loginAndSetup(page: Page, email: string, password: string): Promise { const base = CONFIG.baseURL; await page.goto(`${base}/`, { waitUntil: 'commit', timeout: CONFIG.timeouts.navigation }); const response = await page.request.post(`${base}/api/v1/auth/login`, { data: { email, password, remember_me: false }, }); expect(response.ok(), `Login API failed: ${response.status()} for ${email}`).toBeTruthy(); await page.evaluate(() => { localStorage.setItem( 'auth-storage', JSON.stringify({ state: { isAuthenticated: true, isLoading: false, error: null }, version: 1 }), ); }); } type AuthFixtures = { listenerPage: Page; creatorPage: Page; adminPage: Page; moderatorPage: Page; }; export const test = base.extend({ listenerPage: async ({ page }, use) => { await loginAndSetup(page, CONFIG.users.listener.email, CONFIG.users.listener.password); await use(page); }, creatorPage: async ({ page }, use) => { await loginAndSetup(page, CONFIG.users.creator.email, CONFIG.users.creator.password); await use(page); }, adminPage: async ({ page }, use) => { await loginAndSetup(page, CONFIG.users.admin.email, CONFIG.users.admin.password); await use(page); }, moderatorPage: async ({ page }, use) => { await loginAndSetup(page, CONFIG.users.moderator.email, CONFIG.users.moderator.password); await use(page); }, }); export { expect } from '@playwright/test';