Files
familysync/apps/api/test/setup.ts
T
Lucas Berger ef558b65be test(05-01): add Wave-0 RED scaffolds + VAPID fixture + setup truncation
- tests/fixtures/vapid.ts: static TEST_VAPID keypair for offline unit tests
- tests/lib/pushDispatcher.test.ts: RED — 410/404 prune + 201/5xx no-delete
- tests/lib/pushCoalescer.test.ts: RED — burst coalesce fires once with count=N; excludeUserId
- tests/broker/reminderScheduler.test.ts: RED — shared+timed filter; dedup by (uid,minuteBucket)
- tests/lib/eventChangeDispatcher.test.ts: RED — create/meaningful-update fires; description-only silent; actor excluded
- tests/routes/push.test.ts: RED — POST 201/401; DELETE removes rows; GET vapid-public-key
- test/setup.ts: import pushSubscriptions + add db.delete(pushSubscriptions) in afterEach
- all 5 RED files fail on missing-module (correct; implementations in Plans 05-02..05-06)
2026-06-09 20:50:35 -04:00

40 lines
1.5 KiB
TypeScript

/**
* Vitest global test setup for apps/api.
*
* Establishes shared test infrastructure for API tests:
* - DB pool access via the existing client.ts (DB_HOST/DB_NAME from env)
* - Per-test cleanup for list tables (truncate between tests so state is isolated)
*
* Usage:
* This file is referenced in vitest.config.ts via test.setupFiles.
* Pure-logic tests (listEmitter, fractional rank) do NOT require DB — the
* cleanup function is a no-op when the tables are empty.
*
* Environment:
* Set DB_HOST, DB_USER, DB_PASSWORD, DB_NAME in the test environment.
* Tests run against a local MariaDB dev database; production data is never touched.
*/
import { afterEach } from 'vitest'
import { db } from '../src/db/client.js'
import { lists, listItems, listShares, pushSubscriptions } from '../src/db/schema.js'
/**
* Truncate list and push tables in FK-safe order after each test.
* list_items and list_shares have FK to lists; delete children first.
* pushSubscriptions has FK to users via user_id; deleted before lists (no FK to lists).
* Called automatically via afterEach — no per-test setup needed.
*/
afterEach(async () => {
try {
// Delete child rows first to avoid FK constraint violations
await db.delete(listItems)
await db.delete(listShares)
await db.delete(pushSubscriptions)
await db.delete(lists)
} catch {
// DB may not be available in pure-unit test runs (no DB_HOST configured).
// Swallow the error — pure-logic tests do not need cleanup.
}
})