import { defineConfig } from "vitest/config";
import path from "node:path";
import { allowedNodeEnvironmentFlags } from "node:process";

const baseExclude = [
  "**/node_modules/**",
  "**/dist/**",
  "**/.next/**",
  "e2e/**",
];

const browserTestFiles = [
  "src/data/repositories/__tests__/chat_media_cache_coordinator.test.ts",
  "src/lib/analytics/__tests__/behavior_analytics.test.ts",
  "src/utils/__tests__/result.test.ts",
  "src/utils/__tests__/pwa.test.ts",
];

const workerExecArgv = allowedNodeEnvironmentFlags.has(
  "--no-experimental-webstorage",
)
  ? ["--no-experimental-webstorage"]
  : [];

export default defineConfig({
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  test: {
    projects: [
      {
        extends: true,
        test: {
          name: "node",
          environment: "node",
          execArgv: workerExecArgv,
          include: ["src/**/*.{test,spec}.ts"],
          exclude: [...baseExclude, ...browserTestFiles],
        },
      },
      {
        extends: true,
        test: {
          name: "jsdom",
          environment: "jsdom",
          execArgv: workerExecArgv,
          include: ["src/**/*.{test,spec}.tsx", ...browserTestFiles],
          exclude: baseExclude,
        },
      },
    ],
  },
});
