diff --git a/AGENTS.md b/AGENTS.md index 0cacac92..9acce4a9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -61,7 +61,7 @@ Known pre-existing errors (not yours, don't widen scope to fix them): `src/colle - **Dedicated test databases**: all tests run against databases derived from `DATABASE_URI`, never the dev database. The default mode builds them from a template: `_test_template` is ensured once per schema change (`tests/test-db.ts`: ensure db, drizzle `push` with auto-accepted warnings via `tests/push-schema.ts`, analytics DDL, `seedRoles` + baseline seed, sequence repair; a broad schema-hash is cached in `.cache/testdb-template.json` and warm runs skip push/seed entirely), then vitest forks clone it into `_test_w` at startup (`CREATE DATABASE ... TEMPLATE`; per-fork attach in `tests/vitest-worker-setup.ts`). Two forks never share tables, so files run sequentially-within-a-fork and in parallel across forks (pool `forks`, `isolate: false`, which is required for worker reuse and for the memoized `tests/helpers/get-test-payload.ts` boot). Teardown (returned by `tests/vitest-global.ts`; Vitest 4 removed the `globalTeardown` config) drops clones after green runs and keeps them after failures for postmortem; leftover clones from crashed runs are dropped at the next startup. E2E keeps the single serial `_test`, recreated fresh from the template each run, then seeded with the shared e2e login. Environment flag matrix: - `TEST_DATABASE_URI`: forces legacy serial mode against exactly this database (truncate-per-run, no templates/clones, `fileParallelism: false`), the documented bisect hatch for flaky specs. - `TEST_DB_HARD_RESET=1`: ignores the template cache and rebuilds `_test_template` from scratch. - - `KEEP_TEST_DBS=1`: teardown keeps worker clones even after green runs. + - `KEEP_TEST_DBS=1`: teardown keeps worker clones even after green runs; `KEEP_TEST_DBS=0` forces dropping even after failures. - `VITEST_MAX_FORKS`: fork cap (default 8). - `TEST_PG_POOL_MAX`: internal (set by worker setup, default 8), caps the node-postgres pool per Payload boot in workers mode so N forks stay under `max_connections=100`. - **Integration tests**: `tests/int/**/*.int.spec.ts` - Vitest, default `node` environment with component specs opting in via a `// @vitest-environment jsdom` docblock. Requires the live PostgreSQL test databases (bootstrapped automatically via `vitest-global.ts`; `vitest.setup.ts` points the process at it). Uses `dotenv/config` via `vitest.setup.ts`. diff --git a/tests/test-db.ts b/tests/test-db.ts index 43cb610b..fb24203b 100644 --- a/tests/test-db.ts +++ b/tests/test-db.ts @@ -43,7 +43,7 @@ import "dotenv/config"; * - `TEST_DB_HARD_RESET=1`: ignore the template cache and rebuild the * template from scratch. * - `KEEP_TEST_DBS=1`: teardown keeps worker clones even after green runs - * (postmortem hatch). + * (postmortem hatch); `KEEP_TEST_DBS=0` forces dropping even after failures. */ /** Tables that must survive truncation: PostGIS internals and migration journals. */ diff --git a/tests/vitest-teardown.ts b/tests/vitest-teardown.ts index 68fde72c..94b6a465 100644 --- a/tests/vitest-teardown.ts +++ b/tests/vitest-teardown.ts @@ -8,9 +8,10 @@ import { failureMarkerPresent } from "./vitest-marker"; * leftovers are dropped by the next run's globalSetup sweep regardless. * * - Workers mode: drop every `_w` clone after a green run; keep them - * when the failure marker exists (postmortem) or `KEEP_TEST_DBS=1`. The - * template itself survives across runs and is only rebuilt on hash mismatch - * or `TEST_DB_HARD_RESET=1`. + * when the failure marker exists (postmortem) or `KEEP_TEST_DBS=1`. + * `KEEP_TEST_DBS=0` forces dropping even after failures. The template + * itself survives across runs and is only rebuilt on hash mismatch or + * `TEST_DB_HARD_RESET=1`. * - Legacy serial mode (`TEST_DATABASE_URI` set): truncate for hygiene. */ export default async function vitestTeardown(): Promise { @@ -19,7 +20,9 @@ export default async function vitestTeardown(): Promise { await truncateAllData(resolveTestDatabaseUri()); return; } - if (process.env.KEEP_TEST_DBS === "1" || failureMarkerPresent()) { + const forceKeep = process.env.KEEP_TEST_DBS === "1"; + const forceDrop = process.env.KEEP_TEST_DBS === "0"; + if (!forceDrop && (forceKeep || failureMarkerPresent())) { console.log("[test-db] keeping worker clones (failed run or KEEP_TEST_DBS=1)"); return; }