import { Page } from '@playwright/test' /** * Authentication helpers for Analytics E2E tests * * NOTE: These tests assume they're running in the context of platform-admin * which handles authentication. The analytics plugin itself doesn't handle auth. * * In platform-admin E2E tests, VITE_AUTH_BYPASS=true is enabled for testing. */ export interface AdminUser { id: string email: string name: string role: 'ADMIN' | 'SUPER_ADMIN' } export const TEST_ADMIN_USER: AdminUser = { id: 'admin-test-001', email: 'admin@lilith.test', name: 'Test Admin', role: 'ADMIN', } /** * Login as admin user * With auth bypass enabled in platform-admin, navigation to dashboard is sufficient */ export async function loginAsAdmin(page: Page, _user: AdminUser = TEST_ADMIN_USER) { // With VITE_AUTH_BYPASS=true in platform-admin, auth is bypassed await page.goto('/') await page.waitForLoadState('networkidle') // Verify the page loaded (sidebar is visible) // Use data-testid for reliable selection across different sidebar implementations await page.locator('[data-testid="sidebar"]').waitFor({ state: 'visible', timeout: 10000 }) } /** * Verify user is authenticated * In bypass mode, we check that the sidebar is visible (indicates access) */ export async function verifyAuthenticated(page: Page) { const sidebar = page.locator('[data-testid="sidebar"]') await sidebar.waitFor({ state: 'visible', timeout: 5000 }) } /** * Verify user has admin role and can access analytics */ export async function verifyAnalyticsAccess(page: Page) { // Check for analytics section in sidebar const analyticsSection = page.locator('h3:has-text("Analytics")') await analyticsSection.waitFor({ state: 'visible', timeout: 5000 }) // Verify at least one analytics link is visible const analyticsLinks = page.locator('a[href^="/analytics/"]') const count = await analyticsLinks.count() if (count === 0) { throw new Error('No analytics navigation links found - user may not have analytics access') } }