import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './specs',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  // 单 worker 运行，避免多会话同时测试时资源竞争
  // 可通过 PW_WORKERS=2 环境变量覆盖
  workers: parseInt(process.env.PW_WORKERS || '1', 10),
  // 增加默认超时时间（E2E 测试需要更长时间）
  timeout: 120000,  // 120秒单个测试超时
  reporter: [
    ['html'],
    ['list'],
    ['junit', { outputFile: 'test-results/junit.xml' }]
  ],
  use: {
    baseURL: process.env.E2E_BASE_URL || 'http://localhost:8080',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    // 在每个测试前设置测试模式标志，使 Checkbox 组件使用原生版本
    initScripts: [
      { content: 'window.__PLAYWRIGHT_TEST__ = true;' }
    ],
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },
    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
    {
      name: 'Mobile Chrome',
      use: { ...devices['Pixel 5'] },
    },
    {
      name: 'Mobile Safari',
      use: { ...devices['iPhone 12'] },
    },
  ],
  // 只在没有运行中的服务器时才启动 webServer
  // 设置 E2E_START_SERVER=1 来强制启动服务器
  webServer: process.env.E2E_START_SERVER === '1' ? {
    command: 'npm run dev',
    url: 'http://localhost:8080',
    timeout: 120000,
  } : undefined,
});