|
| 1 | +import { AIMessage, HumanMessage, SystemMessage } from '@langchain/core/messages'; |
| 2 | + |
| 3 | +import { LangChainProvider } from '../src/LangChainProvider'; |
| 4 | + |
| 5 | +// Mock LangChain dependencies |
| 6 | +jest.mock('langchain/chat_models/universal', () => ({ |
| 7 | + initChatModel: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +// Mock logger |
| 11 | +const mockLogger = { |
| 12 | + warn: jest.fn(), |
| 13 | + info: jest.fn(), |
| 14 | + error: jest.fn(), |
| 15 | + debug: jest.fn(), |
| 16 | +}; |
| 17 | + |
| 18 | +describe('LangChainProvider', () => { |
| 19 | + describe('convertMessagesToLangChain', () => { |
| 20 | + it('converts system messages to SystemMessage', () => { |
| 21 | + const messages = [{ role: 'system' as const, content: 'You are a helpful assistant.' }]; |
| 22 | + const result = LangChainProvider.convertMessagesToLangChain(messages); |
| 23 | + |
| 24 | + expect(result).toHaveLength(1); |
| 25 | + expect(result[0]).toBeInstanceOf(SystemMessage); |
| 26 | + expect(result[0].content).toBe('You are a helpful assistant.'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('converts user messages to HumanMessage', () => { |
| 30 | + const messages = [{ role: 'user' as const, content: 'Hello, how are you?' }]; |
| 31 | + const result = LangChainProvider.convertMessagesToLangChain(messages); |
| 32 | + |
| 33 | + expect(result).toHaveLength(1); |
| 34 | + expect(result[0]).toBeInstanceOf(HumanMessage); |
| 35 | + expect(result[0].content).toBe('Hello, how are you?'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('converts assistant messages to AIMessage', () => { |
| 39 | + const messages = [{ role: 'assistant' as const, content: 'I am doing well, thank you!' }]; |
| 40 | + const result = LangChainProvider.convertMessagesToLangChain(messages); |
| 41 | + |
| 42 | + expect(result).toHaveLength(1); |
| 43 | + expect(result[0]).toBeInstanceOf(AIMessage); |
| 44 | + expect(result[0].content).toBe('I am doing well, thank you!'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('converts multiple messages in order', () => { |
| 48 | + const messages = [ |
| 49 | + { role: 'system' as const, content: 'You are a helpful assistant.' }, |
| 50 | + { role: 'user' as const, content: 'What is the weather like?' }, |
| 51 | + { role: 'assistant' as const, content: 'I cannot check the weather.' }, |
| 52 | + ]; |
| 53 | + const result = LangChainProvider.convertMessagesToLangChain(messages); |
| 54 | + |
| 55 | + expect(result).toHaveLength(3); |
| 56 | + expect(result[0]).toBeInstanceOf(SystemMessage); |
| 57 | + expect(result[1]).toBeInstanceOf(HumanMessage); |
| 58 | + expect(result[2]).toBeInstanceOf(AIMessage); |
| 59 | + }); |
| 60 | + |
| 61 | + it('throws error for unsupported message role', () => { |
| 62 | + const messages = [{ role: 'unknown' as any, content: 'Test message' }]; |
| 63 | + |
| 64 | + expect(() => LangChainProvider.convertMessagesToLangChain(messages)).toThrow( |
| 65 | + 'Unsupported message role: unknown', |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('handles empty message array', () => { |
| 70 | + const result = LangChainProvider.convertMessagesToLangChain([]); |
| 71 | + |
| 72 | + expect(result).toHaveLength(0); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('createAIMetrics', () => { |
| 77 | + it('creates metrics with success=true and token usage', () => { |
| 78 | + const mockResponse = new AIMessage('Test response'); |
| 79 | + mockResponse.response_metadata = { |
| 80 | + tokenUsage: { |
| 81 | + totalTokens: 100, |
| 82 | + promptTokens: 50, |
| 83 | + completionTokens: 50, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + const result = LangChainProvider.createAIMetrics(mockResponse); |
| 88 | + |
| 89 | + expect(result).toEqual({ |
| 90 | + success: true, |
| 91 | + usage: { |
| 92 | + total: 100, |
| 93 | + input: 50, |
| 94 | + output: 50, |
| 95 | + }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('creates metrics with success=true and no usage when metadata is missing', () => { |
| 100 | + const mockResponse = new AIMessage('Test response'); |
| 101 | + |
| 102 | + const result = LangChainProvider.createAIMetrics(mockResponse); |
| 103 | + |
| 104 | + expect(result).toEqual({ |
| 105 | + success: true, |
| 106 | + usage: undefined, |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('invokeModel', () => { |
| 112 | + let mockLLM: any; |
| 113 | + let provider: LangChainProvider; |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + mockLLM = { |
| 117 | + invoke: jest.fn(), |
| 118 | + }; |
| 119 | + provider = new LangChainProvider(mockLLM, mockLogger); |
| 120 | + jest.clearAllMocks(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns success=true for string content', async () => { |
| 124 | + const mockResponse = new AIMessage('Test response'); |
| 125 | + mockLLM.invoke.mockResolvedValue(mockResponse); |
| 126 | + |
| 127 | + const messages = [{ role: 'user' as const, content: 'Hello' }]; |
| 128 | + const result = await provider.invokeModel(messages); |
| 129 | + |
| 130 | + expect(result.metrics.success).toBe(true); |
| 131 | + expect(result.message.content).toBe('Test response'); |
| 132 | + expect(mockLogger.warn).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('returns success=false for non-string content and logs warning', async () => { |
| 136 | + const mockResponse = new AIMessage({ type: 'image', data: 'base64data' } as any); |
| 137 | + mockLLM.invoke.mockResolvedValue(mockResponse); |
| 138 | + |
| 139 | + const messages = [{ role: 'user' as const, content: 'Hello' }]; |
| 140 | + const result = await provider.invokeModel(messages); |
| 141 | + |
| 142 | + expect(result.metrics.success).toBe(false); |
| 143 | + expect(result.message.content).toBe(''); |
| 144 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 145 | + 'Multimodal response not supported, expecting a string. Content type: object, Content:', |
| 146 | + JSON.stringify({ type: 'image', data: 'base64data' }, null, 2), |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('returns success=false for array content and logs warning', async () => { |
| 151 | + const mockResponse = new AIMessage(['text', { type: 'image', data: 'base64data' }] as any); |
| 152 | + mockLLM.invoke.mockResolvedValue(mockResponse); |
| 153 | + |
| 154 | + const messages = [{ role: 'user' as const, content: 'Hello' }]; |
| 155 | + const result = await provider.invokeModel(messages); |
| 156 | + |
| 157 | + expect(result.metrics.success).toBe(false); |
| 158 | + expect(result.message.content).toBe(''); |
| 159 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 160 | + 'Multimodal response not supported, expecting a string. Content type: object, Content:', |
| 161 | + JSON.stringify(['text', { type: 'image', data: 'base64data' }], null, 2), |
| 162 | + ); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('mapProvider', () => { |
| 167 | + it('maps gemini to google-genai', () => { |
| 168 | + expect(LangChainProvider.mapProvider('gemini')).toBe('google-genai'); |
| 169 | + expect(LangChainProvider.mapProvider('Gemini')).toBe('google-genai'); |
| 170 | + expect(LangChainProvider.mapProvider('GEMINI')).toBe('google-genai'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('returns provider name unchanged for unmapped providers', () => { |
| 174 | + expect(LangChainProvider.mapProvider('openai')).toBe('openai'); |
| 175 | + expect(LangChainProvider.mapProvider('anthropic')).toBe('anthropic'); |
| 176 | + expect(LangChainProvider.mapProvider('unknown')).toBe('unknown'); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments