import { analyzeWithGemini } from './gemini-analyzer' import * as fs from 'fs' import * as path from 'path' import { GoogleGenAI } from '@google/genai' // Mock the Google GenAI module jest.mock('@google/genai') // Mock fs module jest.mock('fs') // Mock path module jest.mock('path') describe('analyzeWithGemini', () => { const mockApiKey = 'test-api-key' const mockFilePath = '/test/path/image.png' const mockModel = 'gemini-2.0-flash' const mockDescription = 'A test image description' beforeEach(() => { // Reset all mocks before each test jest.clearAllMocks() // Mock fs.existsSync ;(fs.existsSync as jest.Mock).mockReturnValue(true) // Mock path.resolve ;(path.resolve as jest.Mock).mockReturnValue(mockFilePath) // Mock fs.readFileSync ;(fs.readFileSync as jest.Mock).mockReturnValue( Buffer.from('test-image-data') ) // Mock GoogleGenAI const mockGenerateContent = jest.fn().mockResolvedValue({ text: mockDescription, }) ;(GoogleGenAI as jest.Mock).mockImplementation(() => ({ models: { generateContent: mockGenerateContent, }, })) }) it('should analyze an image successfully', async () => { const result = await analyzeWithGemini(mockFilePath, mockModel, mockApiKey) expect(result).toBe(mockDescription) }) it('should throw error when no API key is provided', async () => { await expect( analyzeWithGemini(mockFilePath, mockModel, '') ).rejects.toThrow('No API key provided') }) it('should throw error for unsupported file types', async () => { const unsupportedPath = '/test/path/file.xyz' await expect( analyzeWithGemini(unsupportedPath, mockModel, mockApiKey) ).rejects.toThrow('Unsupported file type: xyz') }) it('should handle PNG files correctly', async () => { const pngPath = '/test/path/image.png' const result = await analyzeWithGemini(pngPath, mockModel, mockApiKey) expect(result).toBe(mockDescription) }) it('should handle JPG files correctly', async () => { const jpgPath = '/test/path/image.jpg' const result = await analyzeWithGemini(jpgPath, mockModel, mockApiKey) expect(result).toBe(mockDescription) }) it('should handle JPEG files correctly', async () => { const jpegPath = '/test/path/image.jpeg' const result = await analyzeWithGemini(jpegPath, mockModel, mockApiKey) expect(result).toBe(mockDescription) }) it('should handle MP4 files correctly', async () => { const mp4Path = '/test/path/video.mp4' const result = await analyzeWithGemini(mp4Path, mockModel, mockApiKey) expect(result).toBe(mockDescription) }) it('should throw error when API returns no text', async () => { const mockGenerateContent = jest.fn().mockResolvedValue({ text: null, }) ;(GoogleGenAI as jest.Mock).mockImplementation(() => ({ models: { generateContent: mockGenerateContent, }, })) await expect( analyzeWithGemini(mockFilePath, mockModel, mockApiKey) ).rejects.toThrow('No response text received from Gemini API') }) })