Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

3.8 KB
Raw Blame
import mainDataSource from './data-source-main';

// The app runs the MAIN connection (auth/audit) as a separate always-SQLite connection. The
// default data-source.ts CLI only manages the DATA connection's migrations, so this standalone
// DataSource exists so the CLI can run/generate the main-owned migrations too.
describe('main CLI DataSource', () => {
  it('targets the always-SQLite main connection', () => {
    expect(mainDataSource.options.type).toBe('better-sqlite3');
  });

  it('uses the main-owned migrations dir, not the data migrations dir', () => {
    const migrations = (mainDataSource.options.migrations as string[]).join(' ');
    expect(migrations).toContain('migrations-main');
  });

  it('covers the auth and audit entities (the main connection owns them)', () => {
    const entities = (mainDataSource.options.entities as string[]).join(' ');
    expect(entities).toContain('auth');
    expect(entities).toContain('audit');
  });

  // jest.resetModules() + a typed require forces a fresh evaluation of the module with the env var
  // we set, proving MAIN_DATABASE_NAME flows into the DataSource options (not just the cached
  // top-level import, which was evaluated with whatever env existed at spec load).
  it("defaults to './data/main.sqlite' when MAIN_DATABASE_NAME is unset", () => {
    const previous = process.env.MAIN_DATABASE_NAME;
    delete process.env.MAIN_DATABASE_NAME;
    jest.resetModules();
    try {
      // require() (not dynamic import()) — the relative specifier trips TS2835 under
      // moduleResolution:nodenext; jest.resetModules() above still forces a fresh evaluation.
      // eslint-disable-next-line @typescript-eslint/no-require-imports
      const mod = require('./data-source-main') as typeof import('./data-source-main');
      expect(String(mod.default.options.database)).toBe('./data/main.sqlite');
    } finally {
      if (previous !== undefined) process.env.MAIN_DATABASE_NAME = previous;
    }
  });

  it('honors MAIN_DATABASE_NAME when set (mirrors configuration.ts)', () => {
    const previous = process.env.MAIN_DATABASE_NAME;
    process.env.MAIN_DATABASE_NAME = '/tmp/test-main.sqlite';
    jest.resetModules();
    try {
      // require() (not dynamic import()) — the relative specifier trips TS2835 under
      // moduleResolution:nodenext; jest.resetModules() above still forces a fresh evaluation.
      // eslint-disable-next-line @typescript-eslint/no-require-imports
      const mod = require('./data-source-main') as typeof import('./data-source-main');
      expect(String(mod.default.options.database)).toBe('/tmp/test-main.sqlite');
    } finally {
      if (previous !== undefined) process.env.MAIN_DATABASE_NAME = previous;
      else delete process.env.MAIN_DATABASE_NAME;
    }
  });

  it('refuses to load when DATABASE_NAME resolves to the main DB file (CLI collision guard)', () => {
    // The migration CLI never runs ConfigModule's validate(), so the SQLite main/data collision
    // guard is applied at module load instead — a shared broken env must fail the CLI too, before
    // any migration runs against the wrong file.
    const prevMain = process.env.MAIN_DATABASE_NAME;
    const prevData = process.env.DATABASE_NAME;
    process.env.MAIN_DATABASE_NAME = '/tmp/cli-guard-main.sqlite';
    process.env.DATABASE_NAME = '/tmp/cli-guard-main.sqlite';
    jest.resetModules();
    try {
      expect(() => {
        // eslint-disable-next-line @typescript-eslint/no-require-imports
        require('./data-source-main');
      }).toThrow(/DATABASE_NAME/);
    } finally {
      if (prevMain !== undefined) process.env.MAIN_DATABASE_NAME = prevMain;
      else delete process.env.MAIN_DATABASE_NAME;
      if (prevData !== undefined) process.env.DATABASE_NAME = prevData;
      else delete process.env.DATABASE_NAME;
      jest.resetModules();
    }
  });
});