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

/**
 * E2E characterization (golden-master) net for the Platform refactor.
 *
 * Target (PW_BASE_URL): defaults to the baked localdev stack on :8080 (production-faithful,
 * same-origin, no HMR/WS gaps). Point it at http://localhost:5173 for the fast Vite loop — all
 * five characterization flows are proxy-safe (none use the WebSocket).
 *
 * The stack must already be UP (owned by `siftlode dev`); there is deliberately no `webServer`.
 * `e2e/global.setup.ts` seeds the dedicated test account and logs in once, sharing the resulting
 * session via storageState so the real tests never re-authenticate (auth endpoints are rate-limited).
 *
 * Runs serial / single-worker on purpose: the specs share ONE seeded account, and a couple of
 * them mutate server state (Settings save), so determinism beats parallelism here.
 */

const BASE_URL = process.env.PW_BASE_URL || "http://127.0.0.1:8080";
const STORAGE_STATE = "e2e/.auth/state.json";

export default defineConfig({
  testDir: "./e2e",
  fullyParallel: false,
  workers: 1,
  forbidOnly: !!process.env.CI,
  retries: 0,
  reporter: [["list"], ["html", { open: "never" }]],
  timeout: 30_000,
  expect: { timeout: 7_000 },
  use: {
    baseURL: BASE_URL,
    storageState: STORAGE_STATE,
    locale: "en-US",
    // retain-on-failure (not on-first-retry): with retries:0 a "first retry" never happens, so a
    // trace would never be captured — this keeps a full trace for any failure, discarded on success.
    trace: "retain-on-failure",
    screenshot: "only-on-failure",
  },
  projects: [
    {
      name: "setup",
      testMatch: /global\.setup\.ts/,
      use: { storageState: { cookies: [], origins: [] } },
    },
    {
      name: "chromium",
      use: { ...devices["Desktop Chrome"], storageState: STORAGE_STATE },
      dependencies: ["setup"],
    },
  ],
});
