// Tests startTaskAgent behavior when origin exists initially but local-only mode on second check import fs from 'fs'; 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 local-only mode', () => { let gitMock: any; beforeEach(() => { jest.clearAllMocks(); process.env.GITLAB_TOKEN = 'tok'; gitMock = { addConfig: jest.fn(), status: jest.fn().mockResolvedValue({ files: [] }), getRemotes: jest .fn() // first call: origin retrieval .mockResolvedValueOnce([{ name: 'origin', refs: { fetch: 'git@host:ns/proj.git', push: null } }]) // second call: hasOrigin detection -> none .mockResolvedValueOnce([]), 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: 'sha' }), push: jest.fn(), revparse: jest.fn().mockResolvedValue('sha'), }; (simpleGit as jest.Mock).mockReturnValue(gitMock); // Mock project and issue API (axios.get as jest.Mock) .mockResolvedValueOnce({ data: { id: 10 } }) .mockResolvedValueOnce({ data: { title: 'Local Mode Test' }, status: 200, headers: { 'content-type': 'application/json' } }); // Stub create_branch (axios.post as jest.Mock).mockResolvedValue({ data: {} }); // Stub TLS config as none jest.spyOn(fs, 'existsSync').mockReturnValue(false); }); it('warns about local-only mode and does not pull or push', async () => { const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); const res = await startTaskAgent({ id: '7', gitlabToken: 'tok' }); expect(warnSpy).toHaveBeenCalledWith( 'Aucun remote origin détecté, mode local : pas de fetch/pull/push.' ); // fetch, pull, push should not be called expect(gitMock.fetch).not.toHaveBeenCalled(); expect(gitMock.pull).not.toHaveBeenCalled(); expect(gitMock.push).not.toHaveBeenCalled(); expect(res).toEqual({ branch: expect.any(String), commitSha: 'sha' }); warnSpy.mockRestore(); }); });