← back to Directory Core

test/db.test.ts

58 lines

// test/db.test.ts
// Smoke tests for src/db.ts — verifies the module exports the expected
// function-shaped exports without touching a real database.
//
// db.ts throws at module evaluation if DATABASE_URL is unset, so we set a fake
// URL before the dynamic import. Pool construction is safe (no connection
// attempt until .query() is called), so we import the module and inspect
// exports only — no queries, no transactions.

import { describe, it, before } from 'node:test';
import assert from 'node:assert/strict';

// Set before any import of db.ts (or code that transitively imports it)
process.env.DATABASE_URL = process.env.DATABASE_URL || 'postgresql://fake:fake@localhost:5432/fake_test';

describe('db module — export shape smoke test', () => {
  // We use a dynamic import so the DATABASE_URL assignment above has run
  // before the module is evaluated.
  let dbModule: Record<string, unknown>;

  before(async () => {
    dbModule = await import('../src/db.js') as Record<string, unknown>;
  });

  it('exports a query function', () => {
    assert.equal(typeof dbModule.query, 'function', 'query should be a function');
  });

  it('exports a withTx function', () => {
    assert.equal(typeof dbModule.withTx, 'function', 'withTx should be a function');
  });

  it('exports a closePool function', () => {
    assert.equal(typeof dbModule.closePool, 'function', 'closePool should be a function');
  });

  it('exports a pool object', () => {
    // pool is a pg.Pool instance — just confirm it's an object (not a function)
    assert.equal(typeof dbModule.pool, 'object', 'pool should be an object');
    assert.notEqual(dbModule.pool, null, 'pool should not be null');
  });

  it('query has arity >= 1 (accepts text param)', () => {
    const fn = dbModule.query as Function;
    assert.ok(fn.length >= 1, `query.length should be >= 1, got ${fn.length}`);
  });

  it('withTx has arity >= 1 (accepts callback)', () => {
    const fn = dbModule.withTx as Function;
    assert.ok(fn.length >= 1, `withTx.length should be >= 1, got ${fn.length}`);
  });

  it('closePool has arity 0 (no required params)', () => {
    const fn = dbModule.closePool as Function;
    assert.equal(fn.length, 0, `closePool.length should be 0, got ${fn.length}`);
  });
});