import { defineConfig, mergeConfig } from "vitest/config";
import { playwright } from "@vitest/browser-playwright";
import viteConfig from "./vitest.config.common";
import path from "path";
import { existsSync, readFileSync } from "node:fs";
import { parseEnv } from "node:util";
import { grantClipboardPermissions, openWebPeer, closeWebPeer, acceptWebPeer } from "./test/lib/commands";

const loadEnvFile = (path: string) => (existsSync(path) ? parseEnv(readFileSync(path, "utf-8")) : undefined);
const defEnv = loadEnvFile(".env");
const testEnv = loadEnvFile(".test.env");
const env = Object.assign({}, defEnv, testEnv);
const debuggerEnabled = env?.ENABLE_DEBUGGER === "true";
const enableUI = env?.ENABLE_UI === "true";
const headless = !debuggerEnabled && !enableUI;
export default mergeConfig(
    viteConfig,
    defineConfig({
        resolve: {
            alias: {
                obsidian: path.resolve(__dirname, "./test/harness/obsidian-mock.ts"),
            },
        },
        test: {
            env: env,
            testTimeout: 40000,
            hookTimeout: 50000,
            fileParallelism: false,
            isolate: true,
            watch: false,

            // environment: "browser",
            include: ["test/**/*.test.ts"],
            coverage: {
                include: ["src/**/*.ts", "src/lib/src/**/*.ts", "src/**/*.svelte"],
                exclude: ["**/*.test.ts", "src/lib/**"],
                provider: "v8",
                reporter: ["text", "json", "html"],
                // ignoreEmptyLines: true,
            },
            browser: {
                isolate: true,
                commands: {
                    grantClipboardPermissions,
                    openWebPeer,
                    closeWebPeer,
                    acceptWebPeer,
                },
                provider: playwright({
                    launchOptions: {
                        args: ["--js-flags=--expose-gc"],
                        // chromiumSandbox: true,
                    },
                }),
                enabled: true,
                screenshotFailures: false,
                instances: [
                    {
                        execArgv: ["--js-flags=--expose-gc"],
                        browser: "chromium",
                        headless,
                        isolate: true,
                        inspector: debuggerEnabled
                            ? {
                                  waitForDebugger: true,
                                  enabled: true,
                              }
                            : undefined,
                        printConsoleTrace: debuggerEnabled,
                        onUnhandledError(error) {
                            // Ignore certain errors
                            const msg = error.message || "";
                            if (msg.includes("Cannot create so many PeerConnections")) {
                                return false;
                            }
                        },
                    },
                ],
                headless,
                fileParallelism: false,
                ui: debuggerEnabled || enableUI ? true : false,
            },
        },
    })
);
