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

/**
 * 公告模块 E2E 测试。
 * 未登录场景可独立运行；登录后场景需要 DATABASE_URL。
 */

test.describe("Announcements", () => {
  test("should redirect unauthenticated user to login", async ({ page }) => {
    await page.goto("/announcements")
    await expect(page).toHaveURL(/\/login(?:$|[/?#])/)
  })

  test("should display announcements page when authenticated", async ({ page }) => {
    test.skip(!process.env.DATABASE_URL, "requires DATABASE_URL for authenticated flow")
    const email = process.env.E2E_STUDENT_EMAIL ?? "student@e2e.local"
    const password = process.env.E2E_STUDENT_PASSWORD ?? "e2e-pass-123456"

    await page.goto("/login")
    await page.getByLabel("Email").fill(email)
    await page.getByLabel("Password").fill(password)
    await page.getByRole("button", { name: "Sign In with Email" }).click()

    await page.goto("/announcements")
    await expect(page.locator("body")).toBeVisible()
    await expect(page).not.toHaveURL(/\/login(?:$|[/?#])/)
  })
})
