import { defineConfig, loadEnv } from 'vite';
import path from 'path';

export default defineConfig(({ mode }) => {
	// Load .env from the monorepo root (two levels up from this package)
	const env = loadEnv(mode, path.resolve(process.cwd(), '../..'), '');
	return {
		test: {
			environment: 'node',
			env,
			globals: true,
			testTimeout: 30_000,
			hookTimeout: 30_000,
			// Run test files sequentially (no parallel) to avoid RLS state races.
			// But give each file its own worker fork — keeps Vitest sequential while
			// ensuring Phoenix sockets from the Realtime client don't leak between
			// files (supabase-js holds a module-level RealtimeClient singleton that
			// `disconnect()` doesn't fully reset, so a shared fork bleeds state).
			pool: 'forks',
			fileParallelism: false
		}
	};
});
