|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { GeminiService } from './gemini.service'; |
| 3 | +import { GeminiVlmConfig } from '../../vlm.types'; |
| 4 | + |
| 5 | +jest.mock('zod/v3', () => { |
| 6 | + const actualZod = jest.requireActual('zod'); |
| 7 | + return actualZod; |
| 8 | +}); |
| 9 | + |
| 10 | +const mockGenerateContent = jest.fn(); |
| 11 | + |
| 12 | +jest.mock('@google/genai', () => { |
| 13 | + return { |
| 14 | + GoogleGenAI: jest.fn().mockImplementation(() => ({ |
| 15 | + models: { |
| 16 | + generateContent: mockGenerateContent, |
| 17 | + }, |
| 18 | + })), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +describe('GeminiService', () => { |
| 23 | + let service: GeminiService; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + |
| 28 | + const module: TestingModule = await Test.createTestingModule({ |
| 29 | + providers: [GeminiService], |
| 30 | + }).compile(); |
| 31 | + |
| 32 | + service = module.get<GeminiService>(GeminiService); |
| 33 | + }); |
| 34 | + |
| 35 | + const createConfig = (overrides?: Partial<GeminiVlmConfig>): GeminiVlmConfig => ({ |
| 36 | + provider: 'gemini', |
| 37 | + model: 'gemini-1.5-pro', |
| 38 | + prompt: 'Test prompt', |
| 39 | + temperature: 0.1, |
| 40 | + apiKey: 'test-api-key', |
| 41 | + ...overrides, |
| 42 | + }); |
| 43 | + |
| 44 | + const createMockResponse = (text: string) => ({ |
| 45 | + text, |
| 46 | + }); |
| 47 | + |
| 48 | + describe('generate', () => { |
| 49 | + it('should call Gemini SDK with correct parameters and return VlmProviderResponse', async () => { |
| 50 | + const config = createConfig(); |
| 51 | + const testBytes = new Uint8Array([1, 2, 3, 4]); |
| 52 | + const mockResponse = createMockResponse('{"identical": true, "description": "No differences"}'); |
| 53 | + mockGenerateContent.mockResolvedValue(mockResponse); |
| 54 | + |
| 55 | + const result = await service.generate(config, [testBytes]); |
| 56 | + |
| 57 | + expect(mockGenerateContent).toHaveBeenCalledWith({ |
| 58 | + model: config.model, |
| 59 | + contents: [ |
| 60 | + { text: config.prompt }, |
| 61 | + { |
| 62 | + inlineData: { |
| 63 | + data: expect.any(String), |
| 64 | + mimeType: 'image/png', |
| 65 | + }, |
| 66 | + }, |
| 67 | + ], |
| 68 | + config: { |
| 69 | + temperature: config.temperature, |
| 70 | + responseMimeType: 'application/json', |
| 71 | + responseJsonSchema: expect.any(Object), |
| 72 | + }, |
| 73 | + }); |
| 74 | + expect(result.content).toBe('{"identical": true, "description": "No differences"}'); |
| 75 | + }); |
| 76 | + |
| 77 | + it.each([ |
| 78 | + ['single image', [new Uint8Array([137, 80, 78, 71])], 2], |
| 79 | + ['multiple images', [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]), new Uint8Array([7, 8, 9])], 4], |
| 80 | + ])('should handle %s and convert to base64', async (_, images, expectedPartsCount) => { |
| 81 | + const config = createConfig(); |
| 82 | + const mockResponse = createMockResponse('{"identical": true}'); |
| 83 | + mockGenerateContent.mockResolvedValue(mockResponse); |
| 84 | + |
| 85 | + await service.generate(config, images); |
| 86 | + |
| 87 | + const callArgs = mockGenerateContent.mock.calls[0][0]; |
| 88 | + expect(callArgs.contents.length).toBe(expectedPartsCount); |
| 89 | + |
| 90 | + if (images.length > 0) { |
| 91 | + const imagePart = callArgs.contents[1]; |
| 92 | + expect(imagePart.inlineData.mimeType).toBe('image/png'); |
| 93 | + expect(imagePart.inlineData.data).toBe(Buffer.from(images[0]).toString('base64')); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('should always include hardcoded JSON schema', async () => { |
| 98 | + const config = createConfig(); |
| 99 | + const mockResponse = createMockResponse('{"identical": true}'); |
| 100 | + mockGenerateContent.mockResolvedValue(mockResponse); |
| 101 | + |
| 102 | + await service.generate(config, []); |
| 103 | + |
| 104 | + const callArgs = mockGenerateContent.mock.calls[0][0]; |
| 105 | + expect(callArgs.config.responseMimeType).toBe('application/json'); |
| 106 | + const schema = callArgs.config.responseJsonSchema; |
| 107 | + expect(schema).toBeDefined(); |
| 108 | + expect(schema).toBeTruthy(); |
| 109 | + }); |
| 110 | + |
| 111 | + it.each([ |
| 112 | + ['API key is missing', { apiKey: '' }, 'Gemini API key is required'], |
| 113 | + ['SDK call fails', { apiKey: 'test-api-key' }, 'API Error'], |
| 114 | + ])('should throw error when %s', async (_, overrides, expectedError) => { |
| 115 | + const config = createConfig(overrides); |
| 116 | + |
| 117 | + if (expectedError === 'API Error') { |
| 118 | + mockGenerateContent.mockRejectedValue(new Error(expectedError)); |
| 119 | + } |
| 120 | + |
| 121 | + await expect(service.generate(config, [])).rejects.toThrow(expectedError); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments