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

const TEST_USER = {
  email: "admin@example.com",
  password: "admin@123",
};

test.describe("登录跳转测试", () => {
  test("登录成功后应跳转到dashboard并保持在dashboard", async ({ page }) => {
    await page.goto("/login");

    await page.locator("#email").fill(TEST_USER.email);
    await page.locator("#password").fill(TEST_USER.password);
    await page.getByRole("button", { name: /登录/ }).click();

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

    await page.waitForTimeout(3000);
    await expect(page).toHaveURL(/\/dashboard/);

    await expect(page.getByText("品牌健康中心")).toBeVisible({ timeout: 15000 });
  });

  test("登录后应能访问dashboard子页面", async ({ page }) => {
    await page.goto("/login");

    await page.locator("#email").fill(TEST_USER.email);
    await page.locator("#password").fill(TEST_USER.password);
    await page.getByRole("button", { name: /登录/ }).click();

    await expect(page).toHaveURL(/\/dashboard/, { timeout: 30000 });
    await page.waitForLoadState("networkidle");

    const pages = [
      "/dashboard/analytics",
      "/dashboard/knowledge",
      "/dashboard/content",
      "/dashboard/distribution",
    ];

    for (const url of pages) {
      const response = await page.goto(url, { timeout: 30000, waitUntil: "domcontentloaded" }).catch(() => null);
      if (response) {
        await page.waitForLoadState("domcontentloaded").catch(() => {});
        await expect(page).not.toHaveURL(/\/login/, { timeout: 5000 });
      }
    }
  });
});
