import { bootstrapTestDatabase, dropStaleWorkerDatabases, ensureTemplateDatabase } from "./test-db";
import { clearFailureMarker } from "./vitest-marker";
import vitestTeardown from "./vitest-teardown";
/**
* Vitest global setup. Two modes:
*
* - Workers mode (default): guarantee the template database, then drop any
* `_w` clones left behind by crashed runs. Per-fork cloning lives
* in tests/vitest-worker-setup.ts; nothing here truncates. After this
* function returns no further connections touch the template until the next
* build (CLONING INVARIANT in tests/test-db.ts).
* - Legacy serial mode (`TEST_DATABASE_URI` set): today's single-database
* truncate bootstrap, for bisecting flakes.
*
* The returned function is executed after the run: Vitest 4 removed the
* `globalTeardown` config option, returning a teardown from globalSetup is
* the supported contract (see tests/vitest-teardown.ts).
*/
export default async function globalSetup(): Promise<() => Promise> {
clearFailureMarker();
if (process.env.TEST_DATABASE_URI) {
await bootstrapTestDatabase();
} else {
await ensureTemplateDatabase();
await dropStaleWorkerDatabases();
}
return vitestTeardown;
}