/** * 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. } });