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', () => { beforeEach(() => jest.resetAllMocks()); it('returns metrics on successful ESLint run', async () => { const results = [ { messages: [{ severity: 2 }, { severity: 1 }, { severity: 2 }] }, { messages: [] }, ]; const stdout = JSON.stringify(results); (execSync as jest.Mock).mockReturnValue(stdout); (fs.writeFile as jest.Mock).mockResolvedValue(undefined); const res: LintAgentResult = await lintAgent(); expect(execSync).toHaveBeenCalledWith( 'npx eslint . --ext .ts,.js --format json', { encoding: 'utf-8' } ); expect(fs.writeFile).toHaveBeenCalledWith( 'eslint-report.json', stdout, 'utf-8' ); expect(res.passed).toBe(false); expect(res.metrics).toMatchObject({ errors: 2, warnings: 1 }); expect(res.metrics?.files).toBe(2); expect(res.metrics?.total).toBe(3); expect(res.metrics?.fixable).toBe(0); expect(res.detailPath).toBe('eslint-report.json'); }); it('handles execSync throwing with stderr output', async () => { const results = [{ messages: [{ severity: 2 }] }]; const raw = JSON.stringify(results); const error: any = new Error('fail'); error.stdout = raw; (execSync as jest.Mock).mockImplementation(() => { throw error; }); (fs.writeFile as jest.Mock).mockResolvedValue(undefined); const res: LintAgentResult = await lintAgent(); expect(fs.writeFile).toHaveBeenCalledWith( 'eslint-report.json', raw, 'utf-8' ); expect(res.passed).toBe(false); expect(res.metrics).toMatchObject({ errors: 1, warnings: 0 }); expect(res.metrics?.files).toBe(1); expect(res.metrics?.total).toBe(1); expect(res.metrics?.fixable).toBe(0); expect(res.detailPath).toBe('eslint-report.json'); expect(res.output).toBe(raw); }); });