import { bootstrapCli } from './cli'; import * as runCliModule from './jobs/run-cli'; const VALID_ENV: NodeJS.ProcessEnv = { DATABASE_URL: 'postgresql://job:secret@db/app', JWT_ACCESS_SECRET: 'a'.repeat(32), }; describe('bootstrapCli', () => { let runCli: jest.SpyInstance; beforeEach(() => { runCli = jest.spyOn(runCliModule, 'runCli').mockResolvedValue(0); }); afterEach(() => jest.restoreAllMocks()); // A misconfigured CronJob pod must fail before any module connects. it.each([ [ 'a role of another entry point', { ...VALID_ENV, APICORE_ROLE: 'worker' }, 'APICORE_ROLE=worker cannot start dist/cli.js', ], [ '"all" in production', { ...VALID_ENV, NODE_ENV: 'production', APICORE_ROLE: 'all' }, 'APICORE_ROLE=all is for local development and tests only', ], [ 'no role in production', { ...VALID_ENV, NODE_ENV: 'production' }, 'APICORE_ROLE is required in production', ], [ 'a missing DATABASE_URL', { JWT_ACCESS_SECRET: VALID_ENV.JWT_ACCESS_SECRET }, 'Missing required environment variable(s): DATABASE_URL', ], ])( 'refuses %s before running a job', async (_case, env: NodeJS.ProcessEnv, message: string) => { await expect(bootstrapCli(['cleanup'], env)).rejects.toThrow(message); expect(runCli).not.toHaveBeenCalled(); }, ); it('runs the named job with its arguments and returns the exit code', async () => { runCli.mockResolvedValue(1); await expect( bootstrapCli(['rekey', '--dry-run'], { ...VALID_ENV, APICORE_ROLE: 'job', }), ).resolves.toBe(1); expect(runCli).toHaveBeenCalledWith(['rekey', '--dry-run']); }); });