import { execSync } from 'child_process'; import fs from 'fs/promises'; import { testAgent, TestAgentResult } from '../src/services/qualimetrie/testAgent'; jest.mock('child_process', () => ({ execSync: jest.fn() })); jest.mock('fs/promises'); describe('testAgent', () => { const summaryJson = JSON.stringify({ total: { lines: { pct: 90 }, functions: { pct: 80 }, branches: { pct: 70 }, statements: { pct: 60 } } }); const jestResults = JSON.stringify({ numTotalTests: 5, numPassedTests: 4, numFailedTests: 1 }); beforeEach(() => { (execSync as jest.Mock).mockClear(); (fs.readFile as jest.Mock).mockClear(); }); it('should return metrics and summary on successful run', async () => { // Simulate execSync doing nothing (execSync as jest.Mock).mockImplementation(() => undefined); // First readFile call: coverage-summary.json (fs.readFile as jest.Mock) .mockResolvedValueOnce(summaryJson) .mockResolvedValueOnce(jestResults); const res: TestAgentResult = await testAgent(); expect(res.success).toBe(true); expect(res.metrics).toEqual({ lines: 90, functions: 80, branches: 70, statements: 60 }); expect(res.summary).toMatchObject({ total: 5, passed: 4, failed: 1 }); }); it('should handle execSync throwing', async () => { const error = new Error('fail'); (execSync as jest.Mock).mockImplementation(() => { throw error; }); const res: TestAgentResult = await testAgent(); expect(res.success).toBe(false); expect(res.output).toContain('fail'); }); });