import * as fs from 'fs' import * as os from 'os' import { exec } from 'child_process' import { convertGifToVideo } from '../gif-converter' // Mock dependencies jest.mock('fs') jest.mock('os') jest.mock('child_process') describe('GIF Converter', () => { beforeEach(() => { jest.clearAllMocks() ;(os.tmpdir as jest.Mock).mockReturnValue('/tmp') }) it('should convert GIF to video using ffmpeg', async () => { const mockStats = { size: 1024 * 1024 } // 1MB const mockStatSync = fs.statSync as jest.Mock mockStatSync.mockReturnValue(mockStats) const mockExecAsync = jest .fn() .mockResolvedValue({ stdout: '', stderr: '' }) ;(exec as unknown as jest.Mock).mockImplementation((cmd, cb) => { if (cb) cb(null, { stdout: '', stderr: '' }) return { stdout: '', stderr: '' } }) const result = await convertGifToVideo('test.gif') expect(exec).toHaveBeenCalledWith( expect.stringContaining('ffmpeg -i "test.gif"'), expect.any(Function) ) expect(mockStatSync).toHaveBeenCalled() expect(result).toMatch(/^\/tmp\/converted-\d+-\w+\.mp4$/) }) it('should return temporary file path without cleanup', async () => { const mockStats = { size: 1024 * 1024 } // 1MB const mockStatSync = fs.statSync as jest.Mock mockStatSync.mockReturnValue(mockStats) const mockUnlinkSync = fs.unlinkSync as jest.Mock mockUnlinkSync.mockImplementation(() => {}) const mockExecAsync = jest .fn() .mockResolvedValue({ stdout: '', stderr: '' }) ;(exec as unknown as jest.Mock).mockImplementation((cmd, cb) => { if (cb) cb(null, { stdout: '', stderr: '' }) return { stdout: '', stderr: '' } }) const result = await convertGifToVideo('test.gif') // Should NOT clean up the file - that's the caller's responsibility expect(mockUnlinkSync).not.toHaveBeenCalled() expect(result).toMatch(/^\/tmp\/converted-\d+-\w+\.mp4$/) }) it('should handle ffmpeg errors', async () => { const mockExecAsync = jest.fn().mockRejectedValue(new Error('ffmpeg error')) ;(exec as unknown as jest.Mock).mockImplementation((cmd, cb) => { if (cb) cb(new Error('ffmpeg error'), { stdout: '', stderr: '' }) return { stdout: '', stderr: '' } }) await expect(convertGifToVideo('test.gif')).rejects.toThrow('ffmpeg error') }) })