// Tests linking branch to GitLab issue via create_branch endpoint import axios from 'axios'; import { simpleGit } from 'simple-git'; import { startTaskAgent } from '../src/services/startTaskAgent'; jest.mock('simple-git', () => ({ simpleGit: jest.fn() })); jest.mock('axios'); describe('startTaskAgent linking branch to issue', () => { const gitMock: any = { status: jest.fn().mockResolvedValue({ files: [] }), getRemotes: jest.fn().mockResolvedValue([{ name: 'origin', refs: { fetch: 'git@host:ns/proj.git', push: null } }]), fetch: jest.fn(), checkout: jest.fn(), pull: jest.fn(), branch: jest.fn().mockResolvedValue({ all: [] }), checkoutBranch: jest.fn(), add: jest.fn(), commit: jest.fn().mockResolvedValue({ commit: 'sha123' }), push: jest.fn(), revparse: jest.fn(), }; beforeEach(() => { jest.clearAllMocks(); process.env.GITLAB_TOKEN = 'tok'; (simpleGit as jest.Mock).mockReturnValue(gitMock); // Mock project and issue retrieval (axios.get as jest.Mock) .mockResolvedValueOnce({ data: { id: 99 } }) .mockResolvedValueOnce({ data: { title: 'Link Test' }, status: 200, headers: { 'content-type': 'application/json' } }); // Stub create_branch endpoint (axios.post as jest.Mock).mockResolvedValue({ data: {} }); }); it('calls create_branch endpoint after pushing branch', async () => { const result = await startTaskAgent({ id: '42', gitlabToken: 'tok' }); expect(axios.post).toHaveBeenCalledWith( `/projects/99/issues/42/create_branch`, { branch: result.branch, ref: 'main' } ); expect(result).toEqual({ branch: result.branch, commitSha: 'sha123' }); }); it('logs warning when create_branch fails', async () => { const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); // Make create_branch reject (axios.post as jest.Mock).mockRejectedValue(new Error('fail link')); const result = await startTaskAgent({ id: '43', gitlabToken: 'tok' }); expect(warnSpy).toHaveBeenCalledWith( expect.stringContaining(`Impossible de lier la branche ${result.branch} à l’issue #43`) ); warnSpy.mockRestore(); }); });