|
| 1 | +import { CohereRerank } from '@langchain/cohere'; |
| 2 | +import { mock } from 'jest-mock-extended'; |
| 3 | +import type { ISupplyDataFunctions } from 'n8n-workflow'; |
| 4 | + |
| 5 | +import { logWrapper } from '@utils/logWrapper'; |
| 6 | + |
| 7 | +import { RerankerCohere } from '../RerankerCohere.node'; |
| 8 | + |
| 9 | +// Mock the CohereRerank class |
| 10 | +jest.mock('@langchain/cohere', () => ({ |
| 11 | + CohereRerank: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +// Mock the logWrapper utility |
| 15 | +jest.mock('@utils/logWrapper', () => ({ |
| 16 | + logWrapper: jest.fn().mockImplementation((obj) => ({ logWrapped: obj })), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('RerankerCohere', () => { |
| 20 | + let rerankerCohere: RerankerCohere; |
| 21 | + let mockSupplyDataFunctions: ISupplyDataFunctions; |
| 22 | + let mockCohereRerank: jest.Mocked<CohereRerank>; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + rerankerCohere = new RerankerCohere(); |
| 26 | + |
| 27 | + // Reset the mock |
| 28 | + jest.clearAllMocks(); |
| 29 | + |
| 30 | + // Create a mock CohereRerank instance |
| 31 | + mockCohereRerank = { |
| 32 | + compressDocuments: jest.fn(), |
| 33 | + } as unknown as jest.Mocked<CohereRerank>; |
| 34 | + |
| 35 | + // Make the CohereRerank constructor return our mock instance |
| 36 | + (CohereRerank as jest.MockedClass<typeof CohereRerank>).mockImplementation( |
| 37 | + () => mockCohereRerank, |
| 38 | + ); |
| 39 | + |
| 40 | + // Create mock supply data functions |
| 41 | + mockSupplyDataFunctions = mock<ISupplyDataFunctions>({ |
| 42 | + logger: { |
| 43 | + debug: jest.fn(), |
| 44 | + error: jest.fn(), |
| 45 | + info: jest.fn(), |
| 46 | + warn: jest.fn(), |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + // Mock specific methods with proper jest functions |
| 51 | + mockSupplyDataFunctions.getNodeParameter = jest.fn(); |
| 52 | + mockSupplyDataFunctions.getCredentials = jest.fn(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should create CohereRerank with default model and return wrapped instance', async () => { |
| 56 | + // Setup mocks |
| 57 | + const mockCredentials = { apiKey: 'test-api-key' }; |
| 58 | + (mockSupplyDataFunctions.getNodeParameter as jest.Mock).mockReturnValue('rerank-v3.5'); |
| 59 | + (mockSupplyDataFunctions.getCredentials as jest.Mock).mockResolvedValue(mockCredentials); |
| 60 | + |
| 61 | + // Execute |
| 62 | + const result = await rerankerCohere.supplyData.call(mockSupplyDataFunctions, 0); |
| 63 | + |
| 64 | + expect(mockSupplyDataFunctions.getNodeParameter).toHaveBeenCalledWith( |
| 65 | + 'modelName', |
| 66 | + 0, |
| 67 | + 'rerank-v3.5', |
| 68 | + ); |
| 69 | + expect(mockSupplyDataFunctions.getCredentials).toHaveBeenCalledWith('cohereApi'); |
| 70 | + expect(CohereRerank).toHaveBeenCalledWith({ |
| 71 | + apiKey: 'test-api-key', |
| 72 | + model: 'rerank-v3.5', |
| 73 | + }); |
| 74 | + expect(logWrapper).toHaveBeenCalledWith(mockCohereRerank, mockSupplyDataFunctions); |
| 75 | + expect(result.response).toEqual({ logWrapped: mockCohereRerank }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should create CohereRerank with custom model', async () => { |
| 79 | + // Setup mocks |
| 80 | + const mockCredentials = { apiKey: 'custom-api-key' }; |
| 81 | + (mockSupplyDataFunctions.getNodeParameter as jest.Mock).mockReturnValue( |
| 82 | + 'rerank-multilingual-v3.0', |
| 83 | + ); |
| 84 | + (mockSupplyDataFunctions.getCredentials as jest.Mock).mockResolvedValue(mockCredentials); |
| 85 | + |
| 86 | + // Execute |
| 87 | + await rerankerCohere.supplyData.call(mockSupplyDataFunctions, 0); |
| 88 | + |
| 89 | + // Verify |
| 90 | + expect(CohereRerank).toHaveBeenCalledWith({ |
| 91 | + apiKey: 'custom-api-key', |
| 92 | + model: 'rerank-multilingual-v3.0', |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should handle different item indices', async () => { |
| 97 | + // Setup mocks |
| 98 | + const mockCredentials = { apiKey: 'test-api-key' }; |
| 99 | + (mockSupplyDataFunctions.getNodeParameter as jest.Mock).mockReturnValue('rerank-english-v3.0'); |
| 100 | + (mockSupplyDataFunctions.getCredentials as jest.Mock).mockResolvedValue(mockCredentials); |
| 101 | + |
| 102 | + // Execute with different item index |
| 103 | + await rerankerCohere.supplyData.call(mockSupplyDataFunctions, 2); |
| 104 | + |
| 105 | + // Verify the correct item index is passed |
| 106 | + expect(mockSupplyDataFunctions.getNodeParameter).toHaveBeenCalledWith( |
| 107 | + 'modelName', |
| 108 | + 2, |
| 109 | + 'rerank-v3.5', |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should throw error when credentials are missing', async () => { |
| 114 | + // Setup mocks |
| 115 | + (mockSupplyDataFunctions.getNodeParameter as jest.Mock).mockReturnValue('rerank-v3.5'); |
| 116 | + (mockSupplyDataFunctions.getCredentials as jest.Mock).mockRejectedValue( |
| 117 | + new Error('Missing credentials'), |
| 118 | + ); |
| 119 | + |
| 120 | + // Execute and verify error |
| 121 | + await expect(rerankerCohere.supplyData.call(mockSupplyDataFunctions, 0)).rejects.toThrow( |
| 122 | + 'Missing credentials', |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should use fallback model when parameter is not provided', async () => { |
| 127 | + // Setup mocks - getNodeParameter returns the fallback value |
| 128 | + const mockCredentials = { apiKey: 'test-api-key' }; |
| 129 | + (mockSupplyDataFunctions.getNodeParameter as jest.Mock).mockReturnValue('rerank-v3.5'); // fallback value |
| 130 | + (mockSupplyDataFunctions.getCredentials as jest.Mock).mockResolvedValue(mockCredentials); |
| 131 | + |
| 132 | + // Execute |
| 133 | + await rerankerCohere.supplyData.call(mockSupplyDataFunctions, 0); |
| 134 | + |
| 135 | + // Verify fallback is used |
| 136 | + expect(CohereRerank).toHaveBeenCalledWith({ |
| 137 | + apiKey: 'test-api-key', |
| 138 | + model: 'rerank-v3.5', |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments