import { Command } from 'commander'; import * as tbaModule from '../src/services/teleportBotAgent'; // Mock teleportBotAgent service for CLI tests jest.mock('../src/services/teleportBotAgent', () => ({ ...jest.requireActual('../src/services/teleportBotAgent'), teleportBotAgent: jest.fn(), })); const { cli, teleportBotAgent } = tbaModule; describe('teleportBotAgent CLI', () => { beforeEach(() => jest.clearAllMocks()); it('exposes correct command and description', () => { expect(cli.command).toBe('start-app'); expect(cli.description).toMatch(/Teleport application bot/); }); it('builder registers options', () => { const cmd = new Command(); cli.builder(cmd); const longOpts = cmd.options.map(o => o.long); expect(longOpts).toEqual( expect.arrayContaining([ '--app', '--token', '--workDir', '--certificate-ttl', '--renewal-interval', ]) ); }); it('handler calls teleportBotAgent and logs result', async () => { const fakeRes = { pid: 10, appName: 'myApp', proxyServer: 'ps', certPath: '/c', keyPath: '/k', logPath: '/l', }; (teleportBotAgent as jest.Mock).mockResolvedValue(fakeRes); const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); await cli.handler({ app: 'myApp', token: 'tok', workDir: '/w', certificateTtl: 'tt', renewalInterval: 'ri' }); expect(teleportBotAgent).toHaveBeenCalledWith({ appName: 'myApp', joinToken: 'tok', workDir: '/w', certificateTtl: 'tt', renewalInterval: 'ri', }); expect(logSpy).toHaveBeenCalledWith('Bot lancé:', JSON.stringify(fakeRes)); logSpy.mockRestore(); }); it('handler prints error and exits on failure', async () => { (teleportBotAgent as jest.Mock).mockRejectedValue(new Error('bad')); const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); const exitSpy = jest.spyOn(process, 'exit').mockImplementation(((code?: number) => { throw new Error(`exit ${code}`); }) as any); await expect(cli.handler({ app: 'a' })).rejects.toThrow('exit 1'); expect(errSpy).toHaveBeenCalledWith('Erreur lancement tbot:', 'bad'); exitSpy.mockRestore(); errSpy.mockRestore(); }); });