import { buildChangeRequestOptions, validateChangeRequestOptions, } from './changeRequestOptions'; import { errorAndExit } from './errorAndExit'; jest.mock('./errorAndExit'); const fakeErrorSymbol = Symbol('errorAndExit'); describe('changeRequestOptions', () => { beforeEach(() => { jest.clearAllMocks(); (errorAndExit as unknown as jest.Mock).mockImplementation(() => { throw fakeErrorSymbol; }); }); describe('validateChangeRequestOptions', () => { it('accepts no change request flags at all', () => { expect(() => validateChangeRequestOptions({})).not.toThrow(); expect(errorAndExit).not.toHaveBeenCalled(); }); it('accepts --chg-number on its own', () => { expect(() => validateChangeRequestOptions({ chgNumber: 'CHG1234567' }) ).not.toThrow(); expect(errorAndExit).not.toHaveBeenCalled(); }); it('accepts the ciid trio when all three are non-empty', () => { expect(() => validateChangeRequestOptions({ ciid: 'CI2410930', activityType: 'Operational Task', description: 'reviewing secret values', }) ).not.toThrow(); expect(errorAndExit).not.toHaveBeenCalled(); }); it('rejects --chg-number combined with any of the ciid trio', () => { expect(() => validateChangeRequestOptions({ chgNumber: 'CHG1234567', ciid: 'CI2410930', }) ).toThrow(); expect(errorAndExit).toHaveBeenCalledWith( expect.stringContaining('Do not provide --ciid') ); }); it('rejects a partial ciid trio', () => { expect(() => validateChangeRequestOptions({ ciid: 'CI2410930' }) ).toThrow(); expect(errorAndExit).toHaveBeenCalledWith( expect.stringContaining('all three must be specified and non-empty') ); }); it.each([ ['whitespace-only', ' ', ' ', ' '], ['empty string', '', 'Operational Task', 'reviewing secret values'], ['whitespace-only description', 'CI2410930', 'Operational Task', ' '], ])('rejects %s values', (_label, ciid, activityType, description) => { expect(() => validateChangeRequestOptions({ ciid, activityType, description }) ).toThrow(); expect(errorAndExit).toHaveBeenCalledWith( expect.stringContaining('all three must be specified and non-empty') ); }); it('rejects non-string values', () => { expect(() => validateChangeRequestOptions({ ciid: 2410930, activityType: true, description: {}, }) ).toThrow(); expect(errorAndExit).toHaveBeenCalled(); }); it('treats null as an absent flag rather than an invalid one', () => { expect(() => validateChangeRequestOptions({ ciid: null, activityType: null, description: null, }) ).not.toThrow(); expect(errorAndExit).not.toHaveBeenCalled(); }); }); describe('buildChangeRequestOptions', () => { it('builds a changeNumber payload from --chg-number', () => { expect( buildChangeRequestOptions({ chgNumber: 'CHG1234567' }) ).toStrictEqual({ changeNumber: 'CHG1234567', }); }); it('builds a ciid payload from the trio', () => { expect( buildChangeRequestOptions({ ciid: 'CI2410930', activityType: 'Operational Task', description: 'reviewing secret values', }) ).toStrictEqual({ ciid: 'CI2410930', activityType: 'Operational Task', description: 'reviewing secret values', }); }); it('builds an all-undefined payload when nothing was supplied', () => { expect(buildChangeRequestOptions({})).toStrictEqual({ ciid: undefined, activityType: undefined, description: undefined, }); }); }); });