/**
 * Smoke test for the worker/gateway mode dispatch helper in main.ts.
 *
 * We don't actually run `node main.js` because both `worker-bootstrap.start`
 * and `gateway/bootstrap.start` perform real side effects (DB open, HTTP
 * listen). Instead we mirror the dispatch logic inline and assert it
 * matches the file content — small but it stops a refactor from silently
 * defaulting to worker mode (which is the worst failure mode here).
 *
 * If the dispatch ever moves to an unrolled if/else or a constant default,
 * update the regex below.
 */
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'fs';
import { resolve } from 'path';

describe('main.ts dispatch', () => {
  const src = readFileSync(resolve('src/main.ts'), 'utf-8');

  it('defaults to worker mode when AAO_MODE is unset', () => {
    // We assert the literal default to catch refactors that drop the fallback.
    expect(src).toMatch(/AAO_MODE.*\?\?\s*['"]worker['"]/);
  });

  it('dispatches worker → worker-bootstrap', () => {
    expect(src).toMatch(/case 'worker'/);
    expect(src).toMatch(/import\(['"]\.\/worker-bootstrap\.js['"]\)/);
  });

  it('dispatches gateway → gateway/bootstrap', () => {
    expect(src).toMatch(/case 'gateway'/);
    expect(src).toMatch(/import\(['"]\.\/gateway\/bootstrap\.js['"]\)/);
  });

  it('exits non-zero on unknown mode (no silent worker fallback)', () => {
    expect(src).toMatch(/Unknown AAO_MODE/);
    expect(src).toMatch(/process\.exit\(1\)/);
  });
});
