Isolate local API tests to a dedicated familysync_test DB (quick 260613-ndv) #18

Merged
luckberg merged 5 commits from quick/260613-ndv-test-db-isolation into main 2026-06-13 20:32:45 -04:00
Owner

Quick task 260613-ndv — isolate local API tests to a dedicated test DB

Local apps/api integration tests ran against the dev familysync DB, seeding thousands of throwaway users into real dev data (and causing the flaky lists.test.ts > re-populates list_shares timeout as the users table grew). This wires them to a dedicated, auto-provisioned familysync_test DB.

What changed

  • New apps/api/test/global-setup.ts (vitest globalSetup): when NOT in CI, root-connects to the dev MariaDB → CREATE DATABASE IF NOT EXISTS familysync_test + GRANT to the app user → applies committed Drizzle migrations → truncate-resets every table (except the migration ledger) so each run is a deterministic clean slate (no run-over-run accumulation). Root creds read from DB_ROOT_USER/DB_ROOT_PASSWORD env (dev default root), never hardcoded; GRANT identifier validated against injection.
  • apps/api/vitest.config.ts: wires globalSetup + a CI-gated test.env override forcing DB_NAME=familysync_test for local workers. Under CI the override is empty — CI keeps its own familysync service DB + db:migrate step, fully unaffected.
  • test/setup.ts header note + a README "Running API tests locally" section.

Verified locally

  • Dev familysync users unchanged (stays 3) across a full test run — dev DB no longer polluted.
  • familysync_test resets each run (186 → 93 users, not doubled), 10 migrated tables.
  • 244/244 API tests pass; the previously-flaky list_shares timeout is gone.
  • typecheck exit 0; lint / format:check / md:lint / frozen-lockfile all green.
  • CI unaffected by inspection: globalSetup early-returns under CI=true and the worker env override is empty there.

Branched off main (independent of the Phase 10 PR).

🤖 Generated with Claude Code

## Quick task 260613-ndv — isolate local API tests to a dedicated test DB Local `apps/api` integration tests ran against the **dev** `familysync` DB, seeding thousands of throwaway users into real dev data (and causing the flaky `lists.test.ts > re-populates list_shares` timeout as the users table grew). This wires them to a dedicated, auto-provisioned `familysync_test` DB. ### What changed - **New `apps/api/test/global-setup.ts`** (vitest globalSetup): when NOT in CI, root-connects to the dev MariaDB → `CREATE DATABASE IF NOT EXISTS familysync_test` + `GRANT` to the app user → applies committed Drizzle migrations → **truncate-resets every table** (except the migration ledger) so each run is a deterministic clean slate (no run-over-run accumulation). Root creds read from `DB_ROOT_USER`/`DB_ROOT_PASSWORD` env (dev default `root`), never hardcoded; GRANT identifier validated against injection. - **`apps/api/vitest.config.ts`**: wires `globalSetup` + a **CI-gated** `test.env` override forcing `DB_NAME=familysync_test` for local workers. Under CI the override is empty — CI keeps its own `familysync` service DB + `db:migrate` step, fully unaffected. - `test/setup.ts` header note + a README "Running API tests locally" section. ### Verified locally - Dev `familysync` users **unchanged (stays 3)** across a full test run — dev DB no longer polluted. - `familysync_test` resets each run (**186 → 93 users**, not doubled), 10 migrated tables. - **244/244 API tests pass**; the previously-flaky `list_shares` timeout is gone. - `typecheck` exit 0; lint / format:check / md:lint / frozen-lockfile all green. - CI unaffected by inspection: globalSetup early-returns under `CI=true` and the worker env override is empty there. Branched off `main` (independent of the Phase 10 PR). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
luckberg added 5 commits 2026-06-13 17:18:48 -04:00
- Create apps/api/test/global-setup.ts: root-provisions + grants + migrates
  familysync_test (local only); no-op when process.env.CI is truthy (T-ndv-04)
- Update apps/api/vitest.config.ts: wire globalSetup; add CI-gated test.env
  override (DB_NAME=familysync_test, DB_HOST) so workers never touch dev DB
- Update apps/api/test/setup.ts header: clarify tests run against familysync_test
  (provisioned by global-setup.ts), document users-cleanup decision (intact across
  tests), and note CI-vs-local env difference
- Add apps/api/README.md "Running API tests locally" section: documents the test
  DB isolation, run command, DB_ROOT_PASSWORD requirement, and CI no-op behaviour
- Fix apps/api/test/global-setup.ts: switch from drizzle({ client, mode }) to
  drizzle(pool, { mode }) — drizzle-orm@0.45.2 isConfig() has a tautological OR
  in the `mode` branch that always returns false, causing the combined-config form
  to pass the config object as the client (client.query is not a function); two-arg
  form routes correctly; 244/244 tests pass against familysync_test
CREATE DATABASE IF NOT EXISTS reuses the prior run's data, so the test DB
would itself accumulate users run-over-run (the same flaky list_shares
fan-out the dev DB suffered). Truncate every table (except the drizzle
migration ledger) at globalSetup start, FK-safe, so each run is a clean
deterministic slate. Verified: familysync_test users 186 -> 93 across a run
(reset, not doubled); dev familysync untouched at 3.
style(quick-260613-ndv): prettier-format global-setup.ts
CI / changes (pull_request) Successful in 2s
CI / fast-checks (pull_request) Successful in 1m24s
CI / api (pull_request) Successful in 1m3s
CI / harness (pull_request) Successful in 4m9s
CI / security (pull_request) Successful in 39s
CI / gate (pull_request) Successful in 1s
4517432dca
luckberg force-pushed quick/260613-ndv-test-db-isolation from 1d5d23a1e0 to 4517432dca 2026-06-13 17:18:48 -04:00 Compare
luckberg merged commit 770e7baa32 into main 2026-06-13 20:32:45 -04:00
luckberg deleted branch quick/260613-ndv-test-db-isolation 2026-06-13 20:32:46 -04:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: luckberg/familysync#18