import { defineConfig, devices } from "@playwright/test"

export default defineConfig({
  // 改为 tests 根目录,由各 project 通过 testDir 限定范围
  testDir: "./tests",
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 2 : undefined,
  reporter: [["html", { open: "never" }], ["list"]],
  use: {
    baseURL: "http://127.0.0.1:3000",
    trace: "retain-on-failure",
    screenshot: "only-on-failure",
    video: process.env.CI ? "retain-on-failure" : "off",
  },
  // 视觉快照存储路径模板: tests/visual/__screenshots__/{testFilePath}/{arg}{ext}
  snapshotPathTemplate: "{testDir}/__screenshots__/{testFilePath}/{arg}{ext}",
  // toHaveScreenshot / toMatchSnapshot 默认选项
  expect: {
    toHaveScreenshot: {
      maxDiffPixelRatio: 0.01,
      animations: "disabled",
      caret: "hide",
    },
    toMatchSnapshot: {
      maxDiffPixelRatio: 0.01,
    },
  },
  webServer: {
    command: "npm run dev",
    port: 3000,
    reuseExistingServer: !process.env.CI,
    timeout: 180000,
    env: {
      SKIP_ENV_VALIDATION: "1",
      NEXTAUTH_SECRET: "test-nextauth-secret",
      NEXTAUTH_URL: "http://127.0.0.1:3000",
      DATABASE_URL: "mysql://test:test@127.0.0.1:3306/test_db",
    },
  },
  projects: [
    // E2E 测试项目(原有配置,限定到 tests/e2e)
    {
      name: "chromium",
      testDir: "./tests/e2e",
      use: {
        ...devices["Desktop Chrome"],
        channel: process.env.CI ? undefined : "chrome",
      },
    },
    // 视觉回归测试项目(限定到 tests/visual)
    {
      name: "visual-chromium",
      testDir: "./tests/visual",
      use: {
        ...devices["Desktop Chrome"],
        channel: process.env.CI ? undefined : "chrome",
      },
    },
  ],
})
