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

/**
 * End-to-end tests. Uses the Chrome already installed on the machine (channel 'chrome'), so no
 * browser download is needed. Start Postgres first (`pnpm db:start`) and seed it (`pnpm db:seed`);
 * the web app and game server are started automatically if they are not already running.
 */
const webUrl = process.env.E2E_WEB_URL ?? 'http://localhost:3000';
const gameUrl = process.env.E2E_GAME_URL ?? 'http://localhost:4000';

export default defineConfig({
  testDir: './e2e',
  timeout: 90_000,
  expect: { timeout: 15_000 },
  fullyParallel: false,
  workers: 1,
  retries: 0,
  reporter: [['list']],
  use: {
    baseURL: webUrl,
    channel: 'chrome',
    trace: 'retain-on-failure',
    screenshot: 'only-on-failure',
  },
  projects: [
    { name: 'desktop', use: { ...devices['Desktop Chrome'], channel: 'chrome' } },
    { name: 'phone', use: { ...devices['Pixel 7'], channel: 'chrome' }, testMatch: /marketing\.spec\.ts/ },
  ],
  webServer: [
    {
      command: 'pnpm --filter @pintrush/game-server start',
      env: { GAME_TIMINGS_SCALE: '0.25' },
      url: `${gameUrl}/health`,
      reuseExistingServer: true,
      timeout: 60_000,
    },
    {
      command: 'pnpm --filter @pintrush/web start',
      url: webUrl,
      reuseExistingServer: true,
      timeout: 120_000,
    },
  ],
});
