import { resolveRole } from './role'; describe('resolveRole', () => { it('defaults per entry point outside production', () => { expect(resolveRole('main', {})).toBe('all'); expect(resolveRole('worker', {})).toBe('worker'); expect(resolveRole('cli', {})).toBe('job'); }); it('treats a blank APICORE_ROLE as unset', () => { expect(resolveRole('main', { APICORE_ROLE: ' ' })).toBe('all'); }); it('requires APICORE_ROLE in production', () => { expect(() => resolveRole('main', { NODE_ENV: 'production' })).toThrow( 'APICORE_ROLE is required in production', ); }); it('rejects all in production', () => { expect(() => resolveRole('main', { NODE_ENV: 'production', APICORE_ROLE: 'all' }), ).toThrow('APICORE_ROLE=all is for local development and tests only'); }); it('rejects an unknown role', () => { expect(() => resolveRole('main', { APICORE_ROLE: 'admin' })).toThrow( 'APICORE_ROLE must be one of api, worker, job, all', ); }); it('rejects a role that does not belong to the entry point', () => { expect(() => resolveRole('main', { APICORE_ROLE: 'worker' })).toThrow( 'APICORE_ROLE=worker cannot start dist/main.js', ); expect(() => resolveRole('worker', { APICORE_ROLE: 'api' })).toThrow( 'APICORE_ROLE=api cannot start dist/worker.js', ); expect(() => resolveRole('cli', { APICORE_ROLE: 'all' })).toThrow( 'APICORE_ROLE=all cannot start dist/cli.js', ); }); it('accepts api for main in production', () => { expect( resolveRole('main', { NODE_ENV: 'production', APICORE_ROLE: 'api' }), ).toBe('api'); }); it('accepts worker and job for their entry points in production', () => { expect( resolveRole('worker', { NODE_ENV: 'production', APICORE_ROLE: 'worker' }), ).toBe('worker'); expect( resolveRole('cli', { NODE_ENV: 'production', APICORE_ROLE: 'job' }), ).toBe('job'); }); });