const path = require("path");
const { defineConfig, devices } = require("@playwright/test");

const serverPort = 4173;
const repoRoot = path.resolve(__dirname, "../..");

module.exports = defineConfig({
    testDir: "./tests",
    timeout: 30_000,
    expect: {
        timeout: 5_000,
    },
    fullyParallel: false,
    retries: process.env.CI ? 2 : 0,
    workers: process.env.CI ? 1 : undefined,
    reporter: [["list"], ["html", { open: "never" }]],
    use: {
        baseURL: `http://127.0.0.1:${serverPort}`,
        trace: "retain-on-failure",
        screenshot: "only-on-failure",
        video: "retain-on-failure",
    },
    projects: [
        {
            name: "chromium",
            use: { ...devices["Desktop Chrome"] },
        },
    ],
    webServer: {
        command:
            `SQLX_OFFLINE=true ` +
            `DATABASE_URL=sqlite://ferrous-solitaire-e2e.db ` +
            `JWT_SECRET=ferrous_solitaire_e2e_secret_32_chars_long ` +
            `SERVER_PORT=${serverPort} ` +
            `cargo run -p solitaire_server --quiet`,
        cwd: repoRoot,
        url: `http://127.0.0.1:${serverPort}/health`,
        // CI prebuilds the server (see web-e2e.yml) so `cargo run` reuses the
        // compiled binary and starts in seconds. The generous timeout is a
        // safety margin for a cold cargo cache (e.g. first run after a deps
        // bump) where `cargo run` may still recompile.
        timeout: 300_000,
        reuseExistingServer: !process.env.CI,
    },
});
