⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
perf(api): speed up the test setup (#714)
Boris Burgosf2fd339b3752
diff --git a/.env.test.example b/.env.test.exampleindex bd2c2dfa..a42b2869 100644--- a/.env.test.example+++ b/.env.test.example@@ -5,7 +5,7 @@## The tests are integration tests: they run the real Elysia app against a real# Postgres and create real better-auth sessions. Point DATABASE_URL at a-# dedicated TEST database — the db reset helper TRUNCATEs every table between+# dedicated TEST database — the db reset helper empties every table between# tests, so it must never point at the dev or prod database.# ─────────────────────────────────────────────────────────────diff --git a/AGENTS.md b/AGENTS.mdindex 04b3dcc7..9ace14cb 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -127,7 +127,7 @@ packages directly.** It talks to the API over HTTP (better-auth client + fetch).| `bun run typecheck` | tsc in every workspace member, then at the root || `bun run lint` | ESLint across the workspace || `bun run format:check` | Prettier check (CI runs the same) |-| `bun run test` | `scripts/` tests, then every member's suite, one package at a time |+| `bun run test` | `scripts/` tests, then every member's suite (see Gotchas) || `bun run db:generate` | generate SQL migrations from Drizzle schema || `bun run db:migrate` | apply migrations || `bun run db:migrate:test` | apply migrations to the test DB (`.env.test`) |@@ -229,9 +229,9 @@ Release config traps:`APP_URL` (see `packages/auth/AGENTS.md`).- Tests use `bun test`. They are integration tests against a real test Postgres(`.env.test`), not mocks. `apps/api` uses Eden Treaty. Its setup and test-writing rules are- in `apps/api/AGENTS.md`. api, bot and worker share one test database, and the api suite- truncates every table before each test. So the suites never run in parallel- (`--concurrency=1`).+ in `apps/api/AGENTS.md`. api, bot, worker and `@repo/db` share one test database, and the+ api suite empties every table before each test. So `bun run test` runs those suites one at+ a time (`test:database`), and the other suites alongside them (`test:standalone`).- `bun --filter` needs the `=` form: `bun --filter='@repo/db' run <script>`. With the spaceform, Bun reads `run` as the script name and fails.- Read the comments in `scripts/setup.ts` before you change it. They give the rules that keepdiff --git a/apps/api/AGENTS.md b/apps/api/AGENTS.mdindex b2ec6bc9..b4132cd4 100644--- a/apps/api/AGENTS.md+++ b/apps/api/AGENTS.md@@ -479,7 +479,13 @@ bun run db:migrate:test # migrate it (repo root)bun run test # from apps/api, or at root via turbo```-The `test` script loads `--env-file=../../.env.test`. The attachments test also needs+The `test` script loads `--env-file=../../.env.test` and runs the files in 5 workers. Each+worker uses its own copy of the test database (`<name>_1` … `<name>_5`), which+`src/__tests__/preload.ts` makes afresh when the worker starts, so nothing may be connected to+the test database at that moment. A worker holds up to 20 Postgres connections (the+`@repo/db` client and the Mastra store) and Postgres allows 100 by default, hence 5.+`bun test <path>` without `--parallel` runs on the test database itself. The same preload+hashes passwords with scrypt at its lowest cost. The attachments test also needsRustFS + `S3_*` in `.env.test` (`docker compose -f docker-compose.dev.yml up -d` createsthe bucket); the Docker test gate starts its own throwaway RustFS.diff --git a/apps/api/bunfig.toml b/apps/api/bunfig.tomlindex f1901a02..92e31045 100644--- a/apps/api/bunfig.toml+++ b/apps/api/bunfig.toml@@ -2,6 +2,7 @@# Writes an lcov file to coverage/lcov.info (read by CI and the VSCode# Coverage Gutters extension) and prints the text summary to the terminal.[test]+preload = ["./src/__tests__/preload.ts"]coverageReporter = ["text", "lcov"]coverageDir = "coverage"# Exclude test files and everything under any __tests__ folder (helpers, fixtures)diff --git a/apps/api/package.json b/apps/api/package.jsonindex 4b96c82e..3c9e15f7 100644--- a/apps/api/package.json+++ b/apps/api/package.json@@ -14,7 +14,7 @@"start": "bun run src/index.ts","lint": "eslint .","typecheck": "tsc --noEmit",- "test": "bun test --env-file=../../.env.test",+ "test": "bun test --env-file=../../.env.test --parallel=5 --no-isolate","test:coverage": "bun test --env-file=../../.env.test --coverage; bun scripts/coverage-report.ts"},"dependencies": {diff --git a/apps/api/src/__tests__/helpers/db.ts b/apps/api/src/__tests__/helpers/db.tsindex bc599f9c..c6f3cec1 100644--- a/apps/api/src/__tests__/helpers/db.ts+++ b/apps/api/src/__tests__/helpers/db.ts@@ -1,7 +1,7 @@import { db } from '@repo/db';import { sql } from 'drizzle-orm';-// TRUNCATEs every table in the test database so each test starts clean. Two+// Empties every table in the test database so each test starts clean. Two// guards keep this from ever running against a real database: NODE_ENV must be// "test" and DATABASE_URL must name a database containing "test". Call it in a// beforeEach (or beforeAll) in every integration test that touches the DB.@@ -17,13 +17,30 @@ export async function resetDb(): Promise<void> {// All application and better-auth tables live in the public schema. The// drizzle migrations bookkeeping table is excluded so migrations are not// re-run, and so is the MCP resource better-auth writes once, at startup.- // RESTART IDENTITY resets serial ids; CASCADE handles FK order.- const rows = (await db.execute(sql`- SELECT tablename FROM pg_tables- WHERE schemaname = 'public' AND tablename NOT IN ('__drizzle_migrations', 'oauth_resource')- `)) as unknown as Array<{ tablename: string }>;-- const tables = rows.map((r) => `"${r.tablename}"`).join(', ');- if (tables.length === 0) return;- await db.execute(sql.raw(`TRUNCATE ${tables} RESTART IDENTITY CASCADE`));+ // DELETE is much cheaper than TRUNCATE on the many small tables a test leaves+ // behind: TRUNCATE rewrites every relation file. The replica role skips FK+ // checks and triggers, so no delete order is needed and the revision triggers+ // do not fire; it needs a superuser, as the test Postgres is. The sequences of+ // the cleared tables restart, as RESTART IDENTITY did. One DO block is one+ // round trip and one transaction, which SET LOCAL is scoped to.+ await db.execute(sql`+ DO $$+ DECLARE t text;+ BEGIN+ SET LOCAL session_replication_role = replica;+ FOR t IN+ SELECT tablename FROM pg_tables+ WHERE schemaname = 'public' AND tablename NOT IN ('__drizzle_migrations', 'oauth_resource')+ LOOP+ EXECUTE format('DELETE FROM public.%I', t);+ END LOOP;+ PERFORM setval(d.objid, s.seqstart, false)+ FROM pg_depend d+ JOIN pg_sequence s ON s.seqrelid = d.objid+ JOIN pg_class c ON c.oid = d.refobjid+ WHERE d.classid = 'pg_class'::regclass AND d.refclassid = 'pg_class'::regclass+ AND d.deptype IN ('a', 'i') AND c.relnamespace = 'public'::regnamespace+ AND c.relname NOT IN ('__drizzle_migrations', 'oauth_resource');+ END $$+ `);}diff --git a/apps/api/src/__tests__/preload.ts b/apps/api/src/__tests__/preload.tsnew file mode 100644index 00000000..3115bd52--- /dev/null+++ b/apps/api/src/__tests__/preload.ts@@ -0,0 +1,40 @@+import { randomBytes, scryptSync, timingSafeEqual } from 'node:crypto';+import { SQL } from 'bun';+import { extendAuth } from '@repo/auth/extensions';++// better-auth's scrypt is slow by design, and nearly every test creates users, so the tests+// hash with scrypt at its lowest cost. bunfig.toml preloads this file for `bun test` only,+// so the server never hashes this way.+const lowestCost = { N: 2, r: 1, p: 1 };++extendAuth({+ password: {+ hash: async (password) => {+ const salt = randomBytes(16).toString('hex');+ return `${salt}:${scryptSync(password, salt, 32, lowestCost).toString('hex')}`;+ },+ verify: async ({ hash, password }) => {+ const [salt = '', key = ''] = hash.split(':');+ const candidate = scryptSync(password, salt, 32, lowestCost);+ const stored = Buffer.from(key, 'hex');+ return stored.length === candidate.length && timingSafeEqual(stored, candidate);+ },+ },+});++// Every test empties the whole database, so each `bun test --parallel` worker runs on its+// own copy of the migrated test database, made afresh when the worker starts. This runs+// before a test file imports @repo/db, which reads DATABASE_URL once.+const worker = process.env.BUN_TEST_WORKER_ID;+if (worker) {+ const url = new URL(process.env.DATABASE_URL!);+ const template = url.pathname.slice(1);+ const copy = `${template}_${worker}`;+ url.pathname = '/postgres';+ const admin = new SQL(url.toString());+ await admin.unsafe(`DROP DATABASE IF EXISTS "${copy}" WITH (FORCE)`);+ await admin.unsafe(`CREATE DATABASE "${copy}" TEMPLATE "${template}"`);+ await admin.close();+ url.pathname = `/${copy}`;+ process.env.DATABASE_URL = url.toString();+}diff --git a/apps/web/package.json b/apps/web/package.jsonindex 61988808..88c34fe3 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -9,7 +9,7 @@"start": "next start -p 3001","lint": "eslint .","typecheck": "tsc --noEmit",- "test": "bun test --preload ./test/setup.ts"+ "test": "bun test --preload ./test/setup.ts --parallel"},"dependencies": {"@animateicons/react": "^0.13.0",diff --git a/docker-compose.test.yml b/docker-compose.test.ymlindex 73202421..b23de8f3 100644--- a/docker-compose.test.yml+++ b/docker-compose.test.yml@@ -104,9 +104,10 @@ services:S3_BUCKET: planner-attachmentsS3_ACCESS_KEY_ID: minioadminS3_SECRET_ACCESS_KEY: minioadmin- # Migrate the throwaway DB, then run the api, bot and worker suites. bun test- # discovers src/**/*.test.ts; env above satisfies the resetDb() guard.- command: sh -c "bun run packages/db/src/migrate.ts && cd apps/api && bun test && cd ../bot && bun test && cd ../worker && bun test"+ # Migrate the throwaway DB, then run the api, bot and worker suites. The api's+ # test script runs it in parallel workers, as locally; env above satisfies the+ # resetDb() guard.+ command: sh -c "bun run packages/db/src/migrate.ts && cd apps/api && bun run test && cd ../bot && bun test && cd ../worker && bun test"# Webhook delivery worker, built from the same image as production, so the test# gate also verifies the worker image builds and boots. It idles here (no eventsdiff --git a/docs/development.md b/docs/development.mdindex 37173b5a..86d5647a 100644--- a/docs/development.md+++ b/docs/development.md@@ -52,7 +52,8 @@ pnpm: the lockfile is `bun.lock`.Integration tests against a real Postgres, not mocks. The setup prepared a separate `*_test`database with `.env.test` pointing at it; the name must contain "test", since the reset-helper TRUNCATEs every table between tests and refuses otherwise.+helper empties every table between tests and refuses otherwise. The api suite runs in+parallel workers on copies of it, named `<name>_1` … `<name>_5`.```bashbun run testdiff --git a/package.json b/package.jsonindex 054375b8..4045a811 100644--- a/package.json+++ b/package.json@@ -24,7 +24,9 @@"format": "prettier . --write","format:check": "prettier . --check","typecheck": "turbo run typecheck && tsc --noEmit",- "test": "bun test scripts/ && turbo run test --concurrency=1",+ "test": "bun test scripts/ && bun run --parallel test:database test:standalone",+ "test:database": "turbo run test --concurrency=1 --filter=api --filter=bot --filter=worker --filter=@repo/db",+ "test:standalone": "turbo run test --filter='!api' --filter='!bot' --filter='!worker' --filter='!@repo/db'","test:coverage": "cd apps/api && bun run test:coverage","db:generate": "bun --filter='@repo/db' run db:generate","db:migrate": "bun --filter='@repo/db' run db:migrate",diff --git a/packages/auth/src/extensions.ts b/packages/auth/src/extensions.tsindex fd6a6cfd..13c1e3f8 100644--- a/packages/auth/src/extensions.ts+++ b/packages/auth/src/extensions.ts@@ -1,14 +1,15 @@-import type { BetterAuthPlugin } from 'better-auth';+import type { BetterAuthOptions, BetterAuthPlugin } from 'better-auth';// What a build adds to the instance's better-auth: plugins, the drizzle tables they keep-// (by model name, for the adapter), and paths no request may reach, which the server still-// calls through `auth.api`. Read once, when ./index.ts creates the instance on its first-// use, so a build calls extendAuth before the first request. A later call throws rather-// than going unread.+// (by model name, for the adapter), paths no request may reach, which the server still+// calls through `auth.api`, and a password hasher in place of scrypt. Read once, when+// ./index.ts creates the instance on its first use, so a build calls extendAuth before the+// first request. A later call throws rather than going unread.export interface AuthExtensions {plugins: BetterAuthPlugin[];schema: Record<string, unknown>;disabledPaths: string[];+ password?: NonNullable<BetterAuthOptions['emailAndPassword']>['password'];}const extensions: AuthExtensions = {@@ -23,6 +24,7 @@ export function extendAuth(next: Partial<AuthExtensions>): void {extensions.plugins.push(...(next.plugins ?? []));Object.assign(extensions.schema, next.schema);extensions.disabledPaths.push(...(next.disabledPaths ?? []));+ if (next.password) extensions.password = next.password;}export function readAuthExtensions(): AuthExtensions {diff --git a/packages/auth/src/index.ts b/packages/auth/src/index.tsindex 4f4b5022..7c3570e2 100644--- a/packages/auth/src/index.ts+++ b/packages/auth/src/index.ts@@ -598,6 +598,7 @@ function createAuth() {// the old one ends with it. The signed-in change-password form sends// revokeOtherSessions for the same reason.revokeSessionsOnPasswordReset: true,+ password: extensions.password,sendResetPassword: async ({ user, url }) => {await sendAuthEmail({to: user.email,