import path from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "../../frontend/node_modules/@playwright/test/index.mjs";

const testingDir = path.dirname(fileURLToPath(import.meta.url));
const frontendRoot = path.resolve(testingDir, "../../frontend");
const previewCommand = "npm run preview -- --host 127.0.0.1 --port 4173 --strictPort";
const webServerCommand = process.env.PLAYWRIGHT_REUSE_DIST === "1" ? previewCommand : `npm run build && ${previewCommand}`;

export default defineConfig({
  testDir: path.resolve(testingDir, "e2e"),
  retries: process.env.CI ? 1 : 0,
  workers: 1,
  timeout: 60000,
  expect: {
    timeout: 10000,
  },
  use: {
    baseURL: "http://127.0.0.1:4173",
    trace: "on-first-retry",
    viewport: { width: 1440, height: 1080 },
  },
  webServer: {
    command: webServerCommand,
    cwd: frontendRoot,
    url: "http://127.0.0.1:4173",
    reuseExistingServer: !process.env.CI,
    timeout: process.env.CI ? 420000 : 120000,
  },
  reporter: [["list"], ["junit", { outputFile: path.resolve(testingDir, "../../build/junit-frontend-e2e.xml") }]],
});
