|
| 1 | +import { JinaReranker } from './rerankers'; |
| 2 | +import { createDefaultLogger } from './utils'; |
| 3 | + |
| 4 | +describe('JinaReranker', () => { |
| 5 | + const mockLogger = createDefaultLogger(); |
| 6 | + |
| 7 | + describe('constructor', () => { |
| 8 | + it('should use default API URL when no apiUrl is provided', () => { |
| 9 | + const reranker = new JinaReranker({ |
| 10 | + apiKey: 'test-key', |
| 11 | + logger: mockLogger, |
| 12 | + }); |
| 13 | + |
| 14 | + // Access private property for testing |
| 15 | + const apiUrl = (reranker as any).apiUrl; |
| 16 | + expect(apiUrl).toBe('https://api.jina.ai/v1/rerank'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should use custom API URL when provided', () => { |
| 20 | + const customUrl = 'https://custom-jina-endpoint.com/v1/rerank'; |
| 21 | + const reranker = new JinaReranker({ |
| 22 | + apiKey: 'test-key', |
| 23 | + apiUrl: customUrl, |
| 24 | + logger: mockLogger, |
| 25 | + }); |
| 26 | + |
| 27 | + const apiUrl = (reranker as any).apiUrl; |
| 28 | + expect(apiUrl).toBe(customUrl); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should use environment variable JINA_API_URL when available', () => { |
| 32 | + const originalEnv = process.env.JINA_API_URL; |
| 33 | + process.env.JINA_API_URL = 'https://env-jina-endpoint.com/v1/rerank'; |
| 34 | + |
| 35 | + const reranker = new JinaReranker({ |
| 36 | + apiKey: 'test-key', |
| 37 | + logger: mockLogger, |
| 38 | + }); |
| 39 | + |
| 40 | + const apiUrl = (reranker as any).apiUrl; |
| 41 | + expect(apiUrl).toBe('https://env-jina-endpoint.com/v1/rerank'); |
| 42 | + |
| 43 | + // Restore original environment |
| 44 | + if (originalEnv) { |
| 45 | + process.env.JINA_API_URL = originalEnv; |
| 46 | + } else { |
| 47 | + delete process.env.JINA_API_URL; |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('should prioritize explicit apiUrl over environment variable', () => { |
| 52 | + const originalEnv = process.env.JINA_API_URL; |
| 53 | + process.env.JINA_API_URL = 'https://env-jina-endpoint.com/v1/rerank'; |
| 54 | + |
| 55 | + const customUrl = 'https://explicit-jina-endpoint.com/v1/rerank'; |
| 56 | + const reranker = new JinaReranker({ |
| 57 | + apiKey: 'test-key', |
| 58 | + apiUrl: customUrl, |
| 59 | + logger: mockLogger, |
| 60 | + }); |
| 61 | + |
| 62 | + const apiUrl = (reranker as any).apiUrl; |
| 63 | + expect(apiUrl).toBe(customUrl); |
| 64 | + |
| 65 | + // Restore original environment |
| 66 | + if (originalEnv) { |
| 67 | + process.env.JINA_API_URL = originalEnv; |
| 68 | + } else { |
| 69 | + delete process.env.JINA_API_URL; |
| 70 | + } |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('rerank method', () => { |
| 75 | + it('should log the API URL being used', async () => { |
| 76 | + const customUrl = 'https://test-jina-endpoint.com/v1/rerank'; |
| 77 | + const reranker = new JinaReranker({ |
| 78 | + apiKey: 'test-key', |
| 79 | + apiUrl: customUrl, |
| 80 | + logger: mockLogger, |
| 81 | + }); |
| 82 | + |
| 83 | + const logSpy = jest.spyOn(mockLogger, 'debug'); |
| 84 | + |
| 85 | + try { |
| 86 | + await reranker.rerank('test query', ['document1', 'document2'], 2); |
| 87 | + } catch (error) { |
| 88 | + // Expected to fail due to missing API key, but we can check the log |
| 89 | + } |
| 90 | + |
| 91 | + expect(logSpy).toHaveBeenCalledWith( |
| 92 | + expect.stringContaining(`Reranking 2 chunks with Jina using API URL: ${customUrl}`) |
| 93 | + ); |
| 94 | + |
| 95 | + logSpy.mockRestore(); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('createReranker', () => { |
| 101 | + const { createReranker } = require('./rerankers'); |
| 102 | + |
| 103 | + it('should create JinaReranker with jinaApiUrl when provided', () => { |
| 104 | + const customUrl = 'https://custom-jina-endpoint.com/v1/rerank'; |
| 105 | + const reranker = createReranker({ |
| 106 | + rerankerType: 'jina', |
| 107 | + jinaApiKey: 'test-key', |
| 108 | + jinaApiUrl: customUrl, |
| 109 | + }); |
| 110 | + |
| 111 | + expect(reranker).toBeInstanceOf(JinaReranker); |
| 112 | + const apiUrl = (reranker as any).apiUrl; |
| 113 | + expect(apiUrl).toBe(customUrl); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should create JinaReranker with default URL when jinaApiUrl is not provided', () => { |
| 117 | + const reranker = createReranker({ |
| 118 | + rerankerType: 'jina', |
| 119 | + jinaApiKey: 'test-key', |
| 120 | + }); |
| 121 | + |
| 122 | + expect(reranker).toBeInstanceOf(JinaReranker); |
| 123 | + const apiUrl = (reranker as any).apiUrl; |
| 124 | + expect(apiUrl).toBe('https://api.jina.ai/v1/rerank'); |
| 125 | + }); |
| 126 | +}); |
0 commit comments