import { Command } from 'commander'; import { addChangeRequestOptions } from './addChangeRequestOptions'; describe('addChangeRequestOptions', () => { // Env vars the options read from const envVars = [ 'ALKS_CHANGE_CIID', 'ALKS_CHANGE_ACTIVITY_TYPE', 'ALKS_CHANGE_DESCRIPTION', 'ALKS_CHANGE_NUMBER', ]; let savedEnv: Record; beforeEach(() => { // Save and clear the relevant env vars so tests are isolated savedEnv = {}; for (const key of envVars) { savedEnv[key] = process.env[key]; delete process.env[key]; } }); afterEach(() => { // Restore original env for (const key of envVars) { if (savedEnv[key] === undefined) { delete process.env[key]; } else { process.env[key] = savedEnv[key]; } } }); function buildCommand(): Command { const command = new Command(); command.exitOverride(); // throw instead of process.exit on parse errors return addChangeRequestOptions(command); } function parse(command: Command, args: string[]): Record { command.parse(args, { from: 'user' }); return command.opts(); } it('registers all four change request options', () => { const command = buildCommand(); // helpInformation lists registered options; assert each flag appears const help = command.helpInformation(); expect(help).toContain('--ciid'); expect(help).toContain('--activity-type'); expect(help).toContain('--description'); expect(help).toContain('--chg-number'); }); it('reads values from CLI arguments', () => { const command = buildCommand(); const opts = parse(command, [ '--ciid', 'CI2360509', '--activity-type', 'Using AWS CLI', '--description', 'Using alks-cli with ChangeMinder', '--chg-number', 'CHG01234567', ]); expect(opts.ciid).toBe('CI2360509'); expect(opts.activityType).toBe('Using AWS CLI'); expect(opts.description).toBe('Using alks-cli with ChangeMinder'); expect(opts.chgNumber).toBe('CHG01234567'); }); it('falls back to environment variables when CLI args are absent', () => { process.env.ALKS_CHANGE_CIID = 'CI2360509'; process.env.ALKS_CHANGE_ACTIVITY_TYPE = 'Using AWS CLI'; process.env.ALKS_CHANGE_DESCRIPTION = 'Using alks-cli with ChangeMinder'; process.env.ALKS_CHANGE_NUMBER = 'CHG01234567'; const command = buildCommand(); const opts = parse(command, []); expect(opts.ciid).toBe('CI2360509'); expect(opts.activityType).toBe('Using AWS CLI'); expect(opts.description).toBe('Using alks-cli with ChangeMinder'); expect(opts.chgNumber).toBe('CHG01234567'); }); it('prefers CLI arguments over environment variables', () => { process.env.ALKS_CHANGE_CIID = 'CI_FROM_ENV'; process.env.ALKS_CHANGE_ACTIVITY_TYPE = 'Activity From Env'; const command = buildCommand(); const opts = parse(command, [ '--ciid', 'CI_FROM_CLI', '--activity-type', 'Activity From Cli', ]); expect(opts.ciid).toBe('CI_FROM_CLI'); expect(opts.activityType).toBe('Activity From Cli'); }); it('resolves individual env vars independently', () => { process.env.ALKS_CHANGE_NUMBER = 'CHG09998888'; const command = buildCommand(); const opts = parse(command, []); expect(opts.chgNumber).toBe('CHG09998888'); expect(opts.ciid).toBeUndefined(); expect(opts.activityType).toBeUndefined(); expect(opts.description).toBeUndefined(); }); });