import { defineConfig } from 'vitest/config';
import { fileURLToPath } from 'node:url';

const r = (p: string) => fileURLToPath(new URL(p, import.meta.url));

export default defineConfig({
  resolve: {
    alias: {
      // Resolve workspace packages to their TS source so tests run without a
      // build step. Production builds still go through tsc/dist.
      '@insignia/iios-contracts': r('./packages/iios-contracts/src/index.ts'),
      '@insignia/iios-testkit': r('./packages/iios-testkit/src/index.ts'),
      '@insignia/iios-adapter-sdk': r('./packages/iios-adapter-sdk/src/index.ts'),
    },
  },
  test: {
    include: ['packages/**/src/**/*.{test,spec}.ts', 'test/**/*.{test,spec}.ts'],
    // DB-backed specs TRUNCATE between tests, so they run against an ISOLATED
    // `iios_test` database (created + migrated by the global setup) — never the dev
    // DB. This env overrides any DATABASE_URL from the shell.
    env: { DATABASE_URL: 'postgresql://iios:iios@localhost:5434/iios_test?schema=public' },
    globalSetup: ['./scripts/vitest-global-setup.mjs'],
    // Run every file in a single worker process, sequentially, so there is no
    // cross-file race on the shared database.
    fileParallelism: false,
    sequence: { concurrent: false },
    pool: 'forks',
    poolOptions: { forks: { singleFork: true } },
  },
});
