import { test as setup } from "@playwright/test"
import { LoginPage } from "../pages/LoginPage"
import { env, AUTH_STORAGE_STATE } from "../support/env"

// Runs once before the "chromium" project (see playwright.config.ts `dependencies`). Logs
// in through the real UI form - the session is an httpOnly cookie (docs/11 §2.0), so there
// is no token to inject; driving the form is the only way to obtain it - then saves cookies
// to disk so every other spec starts already authenticated.
setup("authenticate", async ({ page }) => {
  const login = new LoginPage(page)
  await login.goto()
  await login.login(env.adminEmail, env.adminPassword)
  await login.expectLoggedIn()

  await page.context().storageState({ path: AUTH_STORAGE_STATE })
})
