import { Command } from 'commander'; import * as taModule from '../src/services/qualimetrie/testAgent'; // Mock testAgent to control CLI handler behavior jest.mock('../src/services/qualimetrie/testAgent', () => ({ ...jest.requireActual('../src/services/qualimetrie/testAgent'), testAgent: jest.fn(), })); const { cli, testAgent } = taModule; describe('testAgent CLI', () => { beforeEach(() => jest.clearAllMocks()); it('exposes correct command and description', () => { expect(cli.command).toBe('qualimetrie:test'); expect(cli.description).toMatch(/Exécute les tests/); }); it('builder returns the same command', () => { const cmd = new Command(); const res = cli.builder(cmd); expect(res).toBe(cmd); }); it('handler prints summary on success', async () => { const fakeRes = { success: true, metrics: { lines: 90, functions: 80, branches: 70, statements: 60 }, summary: { total: 3, passed: 3, failed: 0, duration: '0.12s' }, }; (testAgent as jest.Mock).mockResolvedValue(fakeRes); const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); await cli.handler(undefined); expect(logSpy).toHaveBeenCalledWith('Test Summary:'); expect(logSpy).toHaveBeenCalledWith('- Total: 3'); expect(logSpy).toHaveBeenCalledWith('- Passed: 3'); expect(logSpy).toHaveBeenCalledWith('- Failed: 0'); expect(logSpy).toHaveBeenCalledWith('- Duration: 0.12s'); expect(logSpy).toHaveBeenCalledWith('Coverage Summary:'); expect(logSpy).toHaveBeenCalledWith('- Lines: **90%**'); expect(logSpy).toHaveBeenCalledWith('- Functions: **80%**'); expect(logSpy).toHaveBeenCalledWith('- Branches: **70%**'); expect(logSpy).toHaveBeenCalledWith('- Statements: **60%**'); logSpy.mockRestore(); }); it('handler prints errors and exits on failure', async () => { const fakeErr = { success: false, output: 'err message' }; (testAgent as jest.Mock).mockResolvedValue(fakeErr); const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const exitSpy = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => { throw new Error(`exit ${code}`); }) as any); await expect(cli.handler(undefined)).rejects.toThrow('exit 1'); expect(errSpy).toHaveBeenCalledWith("❌ Erreur lors de l'exécution des tests ou de la collecte de couverture:"); expect(errSpy).toHaveBeenCalledWith('err message'); exitSpy.mockRestore(); }); });