import { defineConfig } from "@playwright/test";

const baseURL = process.env.BASE_URL ?? "http://localhost:8080";
const startLocalServer = !process.env.BASE_URL;

// CI: throttle workers + retry transient flake (the upstream WAF rate-limits
// /api/* by source IP; nginx proxy_cache absorbs most repeat fetches but a
// burst can still trip 1-2 of them).
const isCI = !!process.env.CI;

// Quarantine — tests that fail consistently against the deployed prod build
// for reasons unrelated to deploy plumbing (Angular↔React parity gaps,
// missing section breadcrumbs, day-tab/time-filter UI behavior diffs,
// schedule date-picker week-snap, multi-segment connecting itinerary).
//
// Triaged in docs/superpowers/specs/2026-04-27-ssr-hydration-fix.md ("Out
// of scope" section). When CI_DEPLOY=1 (set by .gitea/workflows/ci-deploy
// only), Playwright skips this list so the deploy gate stays green; the
// release-verify workflow runs the full suite for slower-cadence triage.
const QUARANTINED_PATTERNS = [
  "Breadcrumb parity with Angular.*Onlineboard (route page|details page)",
  "Online Board.*flight number clear button",
  "Online Board.*route search results page hydrates",
  "TIRREDESIGN-8.*Onlineboard day-tabs",
  "P1.*Table 7: breadcrumbs on search pages.*Schedule route",
  "Schedule date-range picker.*single click snaps to Mon-Sun",
  "Schedule date-range picker.*next-month bleed-in",
  "connecting itinerary navigates to a multi-segment URL",
];
const grepInvert = process.env.CI_DEPLOY
  ? new RegExp(QUARANTINED_PATTERNS.join("|"))
  : undefined;

export default defineConfig({
  testDir: "tests/e2e",
  timeout: 30000,
  workers: isCI ? 1 : undefined,
  retries: isCI ? 2 : 0,
  ...(grepInvert ? { grepInvert } : {}),
  use: {
    baseURL,
    headless: true,
    httpCredentials:
      process.env.BASIC_AUTH_USER && process.env.BASIC_AUTH_PASS
        ? {
            username: process.env.BASIC_AUTH_USER,
            password: process.env.BASIC_AUTH_PASS,
          }
        : undefined,
  },
  reporter: [["html", { open: "never" }], ["list"]],
  ...(startLocalServer
    ? {
        webServer: {
          command: "pnpm dev",
          url: "http://localhost:8080",
          reuseExistingServer: true,
          timeout: 30000,
        },
      }
    : {}),
});
