Files
sencho/backend/src/__tests__/helpers/vitestGlobalSetup.ts
T
Anso 2000653fb4 perf(test): build baseline DB once via vitest globalSetup (#829)
Each test file's setupTestDb() previously re-ran the full
DatabaseService init path: initSchema (~30 CREATE TABLE IF NOT
EXISTS), 14 idempotent migrate*() methods, a bcrypt hash, and the
admin / settings seed inserts. With 82 files this was a meaningful
slice of the per-fork cold-start cost.

Move the build into a vitest globalSetup that runs once before any
worker boots. The baseline DB lands at a fixed temp path; each
worker's setupTestDb copies it into the per-file data dir, opens the
copy via DatabaseService.getInstance() (re-running the same
idempotent init as a no-op pass), then UPDATEs the seeded local
node's compose_dir to match the per-file COMPOSE_DIR (the baseline
recorded /app/compose because COMPOSE_DIR was unset when the seed
fired in initSchema; without realigning, file-routes tests 400 on
path traversal).

TEST_JWT_SECRET moves from a per-file randomBytes assignment to a
fixed constant in a new testConstants module so the value the
baseline seeds matches the value test files import for direct token
signing. setupTestDb re-exports it for back-compat with the existing
import sites.

A baseline-less measurement on the same machine flakes 30 of 82
files at the no-cap baseline; with this baseline copy, the same
tree drops to 0-3 failures (the residual environmental Windows
flakes) and ~47-52 s wall time.
2026-04-28 10:08:39 -04:00

53 lines
1.9 KiB
TypeScript

import fs from 'fs';
import bcrypt from 'bcrypt';
import {
TEST_USERNAME,
TEST_PASSWORD,
TEST_JWT_SECRET,
BASELINE_DIR,
} from './testConstants';
/**
* Vitest global setup: build the baseline test DB once before any worker
* runs. Each test file's setupTestDb() then copies this file into its own
* temp dir instead of paying the schema-init + migrate*() + bcrypt.hash +
* seed-insert cost N times. DatabaseService.getInstance() in the worker
* opens the existing baseline; the constructor's CREATE TABLE IF NOT
* EXISTS and idempotent migrate*() methods (per CLAUDE.md guarantee)
* collapse to no-ops.
*/
export default async function setup(): Promise<void> {
if (fs.existsSync(BASELINE_DIR)) {
fs.rmSync(BASELINE_DIR, { recursive: true, force: true });
}
fs.mkdirSync(BASELINE_DIR, { recursive: true });
const prevDataDir = process.env.DATA_DIR;
process.env.DATA_DIR = BASELINE_DIR;
try {
const { DatabaseService } = await import('../../services/DatabaseService');
const db = DatabaseService.getInstance();
const passwordHash = await bcrypt.hash(TEST_PASSWORD, 1);
db.updateGlobalSetting('auth_username', TEST_USERNAME);
db.updateGlobalSetting('auth_password_hash', passwordHash);
db.updateGlobalSetting('auth_jwt_secret', TEST_JWT_SECRET);
db.addUser({ username: TEST_USERNAME, password_hash: passwordHash, role: 'admin' });
// Flush the file so workers see a complete DB on copy.
db.getDb().close();
} finally {
// Restore env so workers do not inherit DATA_DIR pointing at the
// shared baseline. setupTestDb overrides it per file regardless,
// but defense-in-depth against an early DatabaseService import.
if (prevDataDir === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = prevDataDir;
}
}
export function teardown(): void {
if (fs.existsSync(BASELINE_DIR)) {
fs.rmSync(BASELINE_DIR, { recursive: true, force: true });
}
}