import { Command } from 'commander'; import * as bomModule from '../src/services/qualimetrie/bomAgent'; // Mock bomAgent to return predictable data jest.mock('../src/services/qualimetrie/bomAgent', () => ({ ...jest.requireActual('../src/services/qualimetrie/bomAgent'), bomAgent: jest.fn(), })); const { cli, bomAgent } = bomModule; describe('bomAgent CLI', () => { beforeEach(() => jest.clearAllMocks()); it('exposes correct command and description', () => { expect(cli.command).toBe('qualimetrie:bom'); expect(cli.description).toMatch(/Bill Of Materials/); }); it('builder returns the same command without options', () => { const cmd = new Command(); const result = cli.builder(cmd); expect(result).toBe(cmd); expect(cmd.options).toHaveLength(0); }); it('handler prints report with summary and table', async () => { const fakeDate = '2023-01-01'; // Stub Date to fixed date jest.spyOn(Date.prototype, 'toISOString').mockReturnValue(`${fakeDate}T00:00:00.000Z`); const fakePackages = [ { name: 'pkg1', version: '1.0', license: 'GPL', risk: 'High' }, { name: 'pkg2', version: '2.0', license: 'MIT', risk: 'Low' }, ]; (bomAgent as jest.Mock).mockResolvedValue({ packages: fakePackages }); const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); await cli.handler(undefined); // First logs: header and date expect(logSpy).toHaveBeenNthCalledWith(1, '# 📜 Bill Of Materials Report'); expect(logSpy).toHaveBeenNthCalledWith(2, expect.stringMatching(/^Date: 2023-01-01$/)); // Executive summary lines expect(logSpy).toHaveBeenCalledWith('- Total packages: 2'); expect(logSpy).toHaveBeenCalledWith('- High risk: 1'); expect(logSpy).toHaveBeenCalledWith('- Medium risk: 0'); expect(logSpy).toHaveBeenCalledWith('- Low risk: 1'); expect(logSpy).toHaveBeenCalledWith('- Unknown risk: 0\n'); // Details header and table expect(logSpy).toHaveBeenCalledWith('## 📜 Details'); expect(logSpy).toHaveBeenCalledWith('| Package | Version | License | Risk |'); expect(logSpy).toHaveBeenCalledWith('|---|---|---|---|'); // Detail rows expect(logSpy).toHaveBeenCalledWith('| pkg1 | 1.0 | GPL | High |'); expect(logSpy).toHaveBeenCalledWith('| pkg2 | 2.0 | MIT | Low |'); logSpy.mockRestore(); jest.restoreAllMocks(); }); });