All files / test/model-based helpers.ts

90.9% Statements 10/11
50% Branches 1/2
100% Functions 2/2
90.9% Lines 10/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                  765x 765x         765x     765x       765x 765x   765x 765x   765x               70x  
/**
 * Model-Based Testing Helpers
 *
 * Shared utilities for fast-check model-based tests.
 */
 
import { mkdirSync } from "node:fs";
import { join } from "node:path";
import { CONFIG_DIR_ENV_VAR, closeDatabase } from "../../src/lib/db/index.js";
 
/**
 * Create an isolated database context for model-based tests.
 * Each test run gets its own SQLite database to avoid interference.
 *
 * @returns Cleanup function to call after test completes
 */
export function createIsolatedDbContext(): () => void {
  const testBaseDir = process.env[CONFIG_DIR_ENV_VAR];
  Iif (!testBaseDir) {
    throw new Error(`${CONFIG_DIR_ENV_VAR} not set - run tests via bun test`);
  }
 
  // Close any existing database connection
  closeDatabase();
 
  // Create unique subdirectory for this test run
  const testDir = join(
    testBaseDir,
    `model-${Date.now()}-${Math.random().toString(36).slice(2)}`
  );
  mkdirSync(testDir, { recursive: true });
  process.env[CONFIG_DIR_ENV_VAR] = testDir;
 
  return () => {
    closeDatabase();
    // Restore original base dir for next test
    process.env[CONFIG_DIR_ENV_VAR] = testBaseDir;
  };
}
 
/**
 * Default number of runs for property-based and model-based tests.
 * Balance between thoroughness and CI speed.
 */
export const DEFAULT_NUM_RUNS = 50;