import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest'; import { createCli } from '../src/cli'; import { addCommands } from '../src/commands'; import yargs from 'yargs'; import { AdderServiceImpl, InvalidArgumentTypeError, ArgumentOutOfRangeError } from '../src/modules/adder'; vi.mock('process'); describe('blah', () => { let cli: yargs.Argv; let exit: vi.Mock; let exitReal: typeof process.exit; beforeAll(() => { exitReal = process.exit.bind(process); const processMut = process as unknown as Record; processMut.exit = exit = vi.fn(); }); afterAll(() => { process.exit = exitReal = process.exit.bind(process); }); it('returns result when successful', async () => { cli = createCli(['add', '1', '2']); addCommands(cli); await cli.parse(); expect(exit).toHaveBeenCalledWith(0); }); it('returns error when given invalid inputs', async () => { vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => { throw new InvalidArgumentTypeError('Invalid input'); }); cli = createCli(['add', '1', '2']); addCommands(cli); await cli.parse(); expect(exit).toHaveBeenCalledWith(-1); }); it('returns error when given out-of-range inputs', async () => { vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => { throw new ArgumentOutOfRangeError('Out of range'); }); cli = createCli(['add', '1', '2']); addCommands(cli); await cli.parse(); expect(exit).toHaveBeenCalledWith(-2); }); it('returns error when an unexpected error occurs', async () => { vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => { throw new Error('Unexpected error'); }); cli = createCli(['add', '1', '2']); addCommands(cli); await cli.parse(); expect(exit).toHaveBeenCalledWith(-2); }); });