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

const TEST_USER = {
  email: "admin@fischer.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();

    // 等待导航到dashboard
    await expect(page).toHaveURL(/\/dashboard/, { timeout: 15000 });

    // 等待页面稳定，确保没有跳回login
    await page.waitForTimeout(3000);
    await expect(page).toHaveURL(/\/dashboard/);

    // 验证dashboard内容可见
    await expect(page.getByText("Overview", { exact: false })).toBeVisible({ timeout: 10000 });
  });

  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: 15000 });

    // 测试各个子页面
    const pages = [
      "/dashboard/analytics",
      "/dashboard/knowledge",
      "/dashboard/content",
      "/dashboard/distribution",
    ];

    for (const url of pages) {
      await page.goto(url);
      await expect(page).toHaveURL(url, { timeout: 10000 });
      // 确保没有跳回login
      await page.waitForTimeout(1000);
      await expect(page).not.toHaveURL(/\/login/);
    }
  });
});
