import { createRequire } from "node:module";
import { defineConfig, devices } from "@playwright/test";

const require = createRequire(import.meta.url);
const { shouldIncludeWebKit } =
  require("../scripts/playwrightHostDeps.cjs") as {
    shouldIncludeWebKit: () => boolean;
  };

const configuredWorkers = process.env.PLAYWRIGHT_WORKERS
  ? Number.parseInt(process.env.PLAYWRIGHT_WORKERS, 10)
  : 1;
const baseURL = process.env.PLAYWRIGHT_BASE_URL || "http://127.0.0.1:4175";
const skipWebServer =
  process.env.PLAYWRIGHT_SKIP_WEBSERVER === "1" ||
  process.env.PLAYWRIGHT_SKIP_WEBSERVER === "true";

export default defineConfig({
  testDir: "./tests",
  testMatch: ["**/devfront-refresh-token.spec.ts"],
  fullyParallel: false,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: configuredWorkers,
  reporter: [["html", { open: "never" }], ["list"]],
  use: {
    baseURL,
    trace: "on-first-retry",
  },
  projects: [
    {
      name: "chromium",
      use: { ...devices["Desktop Chrome"] },
    },
    ...(shouldIncludeWebKit()
      ? [
          {
            name: "webkit",
            use: { ...devices["Desktop Safari"] },
          },
        ]
      : []),
  ],
  webServer: skipWebServer
    ? undefined
    : {
        command:
          "VITE_OIDC_AUTHORITY=http://localhost:5000/oidc ./node_modules/.bin/vite build && ./node_modules/.bin/vite preview --host 127.0.0.1 --strictPort --port 4175",
        url: baseURL,
        reuseExistingServer: false,
      },
});
