import { execSync } from 'child_process'; import fs from 'fs/promises'; import { lintAgent, LintAgentResult } from '../src/services/qualimetrie/lintAgent'; jest.mock('child_process', () => ({ execSync: jest.fn(), })); jest.mock('fs/promises'); describe('lintAgent', () => { const dummyResults = [ { filePath: 'file1.ts', messages: [ { severity: 2 }, { severity: 1 }, { severity: 2 } ] }, { filePath: 'file2.ts', messages: [ { severity: 1 } ] }, ]; beforeEach(() => { (execSync as jest.Mock).mockClear(); (fs.writeFile as jest.Mock).mockClear(); }); it('should parse ESLint output and count errors and warnings', async () => { (execSync as jest.Mock).mockReturnValue(JSON.stringify(dummyResults)); const res: LintAgentResult = await lintAgent(); expect(res.passed).toBe(false); expect(res.metrics).toMatchObject({ errors: 2 + 0, warnings: 1 + 1 }); expect(res.metrics?.files).toBe(2); expect(res.metrics?.total).toBe(4); expect(res.metrics?.fixable).toBe(0); expect(fs.writeFile).toHaveBeenCalledWith('eslint-report.json', expect.any(String), 'utf-8'); }); it('should handle execSync throwing and still return metrics', async () => { const error: any = new Error('fail'); error.stdout = JSON.stringify(dummyResults); (execSync as jest.Mock).mockImplementation(() => { throw error; }); const res: LintAgentResult = await lintAgent(); expect(res.passed).toBe(false); expect(res.metrics).toMatchObject({ errors: 2, warnings: 2 }); expect(res.metrics?.files).toBe(2); expect(res.metrics?.total).toBe(4); expect(res.metrics?.fixable).toBe(0); expect(res.output).toBe(JSON.stringify(dummyResults)); }); });