import { test, expect } from "@playwright/test"
import { LoginPage } from "../pages/LoginPage"
import { env } from "../support/env"

// Runs unauthenticated - unlike every other spec, it must not use the "chromium" project's
// saved storageState, since it is exercising the login form itself.
test.use({ storageState: { cookies: [], origins: [] } })

test.describe("Login", () => {
  test("valid credentials redirect to the dashboard", async ({ page }) => {
    const login = new LoginPage(page)
    await login.goto()
    await login.login(env.adminEmail, env.adminPassword)
    await login.expectLoggedIn()
  })

  test("invalid password shows an inline error and stays on /login", async ({ page }) => {
    const login = new LoginPage(page)
    await login.goto()
    await login.login(env.adminEmail, "definitely-not-the-password")
    await login.expectError()
    await expect(page).toHaveURL(/\/login/)
  })

  test("session-expired redirect shows the amber notice", async ({ page }) => {
    await page.goto("/login?next=/dashboard/receiving/grn")
    await expect(page.getByText(/session is missing or expired/i)).toBeVisible()
  })
})
