import { defineConfig, mergeConfig } from 'vitest/config'

function withPreset(preset, override = {}) {
  return mergeConfig(defineConfig(preset), defineConfig(override))
}

export const vitestConfig = {
  base(override) {
    return withPreset(
      {
        test: {
          environment: 'node',
        },
      },
      override,
    )
  },

  react(override) {
    return withPreset(
      {
        test: {
          environment: 'jsdom',
        },
      },
      override,
    )
  },

  realPg(override) {
    return withPreset(
      {
        test: {
          environment: 'node',
          pool: 'forks',
          poolOptions: {
            forks: {
              singleFork: true,
            },
          },
          // Postgres-backed suites (embedded-PG boot / PGLite WASM boot) run on
          // a shared dev box where the gate is deprioritized below interactive
          // load; contention stretches wall-clock 10x (observed: 77s for tests
          // that take <8s idle). Timeouts here bound genuine hangs, not speed.
          hookTimeout: 300_000,
          testTimeout: 180_000,
        },
      },
      override,
    )
  },
}
