import fs from 'fs'; import * as stModule from '../src/services/startTaskAgent'; import { cli } from '../src/services/startTaskAgent'; // Mock startTaskAgent and teleportBotAgent for CLI jest.mock('fs'); jest.mock('../src/services/teleportBotAgent', () => ({ teleportBotAgent: jest.fn() })); jest.mock('../src/services/startTaskAgent', () => { const actual = jest.requireActual('../src/services/startTaskAgent'); return { ...actual, startTaskAgent: jest.fn() }; }); const teleportBotAgent = require('../src/services/teleportBotAgent').teleportBotAgent as jest.Mock; const startTaskAgent = stModule.startTaskAgent as jest.Mock; describe('startTaskAgent CLI', () => { const id = '42'; const opts: any = { repoPath: '/r', baseBranch: 'dev', gitlabToken: 'tok', gitlabUrl: 'url', workDir: '/w', teleportToken: 'jt' }; const crtPath = '/w/dest/tls.crt'; const keyPath = '/w/dest/tls.key'; beforeEach(() => jest.clearAllMocks()); it('builder registers id and options', () => { const { builder } = cli; const cmd = { argument: jest.fn().mockReturnThis(), option: jest.fn().mockReturnThis() }; builder(cmd as any); expect(cmd.argument).toHaveBeenCalledWith('', expect.any(String)); expect(cmd.option).toHaveBeenCalled(); }); it('handler calls startTaskAgent when certificates exist', async () => { (fs.existsSync as jest.Mock).mockImplementation((p: string) => p === crtPath || p === keyPath); const fakeRes = { branch: 'feat/gl-42-test', commitSha: 'abc' }; startTaskAgent.mockResolvedValue(fakeRes); const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); await cli.handler(id, opts); expect(teleportBotAgent).not.toHaveBeenCalled(); expect(startTaskAgent).toHaveBeenCalledWith({ id, ...opts }); expect(logSpy).toHaveBeenCalledWith( `Branche créée: ${fakeRes.branch}, commit SHA: ${fakeRes.commitSha}` ); logSpy.mockRestore(); }); it('handler generates certificates when missing and calls teleportBotAgent', async () => { (fs.existsSync as jest.Mock).mockReturnValue(false); teleportBotAgent.mockResolvedValue(undefined); (fs.existsSync as jest.Mock).mockImplementation((p: string) => false); startTaskAgent.mockResolvedValue({ branch: 'b', commitSha: 'c' }); const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); await cli.handler(id, opts); expect(teleportBotAgent).toHaveBeenCalledWith({ appName: 'gitlab', joinToken: 'jt', workDir: '/w' }); expect(startTaskAgent).toHaveBeenCalled(); logSpy.mockRestore(); }); });