import { Page, expect } from '@playwright/test'; import { APP_URL, TEST_PASS, TEST_USERS } from './api'; export type Role = keyof typeof TEST_USERS; /** * Hace login en la GUI con el usuario de test del rol indicado. * Espera a que la redirección post-login termine (URL cambia a /groups). */ export async function loginAs(page: Page, role: Role): Promise { const { username } = TEST_USERS[role]; await page.goto(`${APP_URL}/auth/login`); await page.locator('input[name="username"]').fill(username); await page.locator('input[name="password"]').fill(TEST_PASS); await page.locator('button[type="submit"]').click(); await expect(page).toHaveURL(/\/groups/, { timeout: 10_000 }); } /** * Navega a una ruta y devuelve la URL final (útil para comprobar redirecciones). */ export async function navigateTo(page: Page, path: string): Promise { await page.goto(`${APP_URL}${path}`); await page.waitForLoadState('networkidle'); return page.url(); } /** Comprueba que un enlace del sidebar ES visible */ export async function expectSidebarLink(page: Page, routerLink: string, visible: true): Promise; /** Comprueba que un enlace del sidebar NO está en el DOM */ export async function expectSidebarLink(page: Page, routerLink: string, visible: false): Promise; export async function expectSidebarLink(page: Page, routerLink: string, visible: boolean): Promise { const locator = page.locator(`[routerLink="${routerLink}"], a[href="${routerLink}"]`).first(); if (visible) { await expect(locator).toBeVisible({ timeout: 5_000 }); } else { await expect(locator).not.toBeAttached({ timeout: 5_000 }); } }