import path from 'node:path'
import { defineConfig, devices } from '@playwright/test'

const chromiumExecutablePath = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH?.trim()
const launchOptions =
  chromiumExecutablePath && path.isAbsolute(chromiumExecutablePath)
    ? {
        executablePath: chromiumExecutablePath,
        args: [
          '--disable-dev-shm-usage',
          '--disable-gpu',
          '--disable-extensions',
        ],
      }
    : {
        args: [
          '--disable-dev-shm-usage',
          '--disable-gpu',
          '--disable-extensions',
        ],
      }

export default defineConfig({
  testDir: './apps/web/tests',
  fullyParallel: true,
  workers: 1,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  reporter: 'list',
  use: {
    baseURL: 'http://localhost:4321',
    headless: true,
    trace: 'on-first-retry',
    launchOptions,
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
  ],
  webServer: {
    command: 'pnpm --filter @overdeck/web exec astro dev --force',
    url: 'http://localhost:4321',
    reuseExistingServer: !process.env.CI,
    timeout: 120_000,
    env: {
      COLLECTOR_URL: 'http://127.0.0.1:14980',
      COLLECTOR_TOKEN: 'overdeck-dev',
    },
  },
})
