import { test, expect } from '@playwright/test'

test.describe('Login', () => {

    test('login exitoso redirige al dashboard', async ({ page }) => {
        await page.goto('/login')

        await page.locator('input[id^="username"]').fill('demo')
        await page.locator('input[id^="password"]').fill('demo123')
        await page.click('button[type="submit"]')

        await expect(page).toHaveURL(/dashboard/, { timeout: 30000 })
    })

    test('dashboard muestra saludo al usuario', async ({ page }) => {
        await page.goto('/login')

        await page.locator('input[id^="username"]').fill('demo')
        await page.locator('input[id^="password"]').fill('demo123')
        await page.click('button[type="submit"]')

        await page.waitForURL(/dashboard/, { timeout: 30000 })

        const modal = page.locator('button:has-text("Cancelar")')
        if (await modal.isVisible()) {
            await modal.click()
        }

        await expect(page.locator('h1')).toBeVisible()
    })

    test('login con credenciales incorrectas muestra error', async ({ page }) => {
        await page.goto('/login')

        await page.locator('input[id^="username"]').fill('usuario_falso')
        await page.locator('input[id^="password"]').fill('password_falso')
        await page.click('button[type="submit"]')

        await expect(page).toHaveURL(/login/)
    })

})