mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-07-10 16:23:44 +00:00
* fix: centralize outbound proxy handling * chore: sort proxy imports * test: update proxy helper mocks * fix: honor proxy bypasses consistently * fix: support http axios proxy targets
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
const { fetch } = require('undici');
|
|
const TavilySearchResults = require('../TavilySearchResults');
|
|
const { getEnvProxyDispatcher } = require('@librechat/api');
|
|
|
|
jest.mock('undici');
|
|
jest.mock('@librechat/api', () => ({
|
|
getEnvProxyDispatcher: jest.fn(),
|
|
}));
|
|
|
|
describe('TavilySearchResults', () => {
|
|
let originalEnv;
|
|
const mockApiKey = 'mock_api_key';
|
|
|
|
beforeAll(() => {
|
|
originalEnv = { ...process.env };
|
|
});
|
|
|
|
beforeEach(() => {
|
|
jest.resetModules();
|
|
jest.clearAllMocks();
|
|
process.env = {
|
|
...originalEnv,
|
|
TAVILY_API_KEY: mockApiKey,
|
|
};
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
it('should throw an error if TAVILY_API_KEY is missing', () => {
|
|
delete process.env.TAVILY_API_KEY;
|
|
expect(() => new TavilySearchResults()).toThrow('Missing TAVILY_API_KEY environment variable.');
|
|
});
|
|
|
|
it('should use mockApiKey when TAVILY_API_KEY is not set in the environment', () => {
|
|
const instance = new TavilySearchResults({
|
|
TAVILY_API_KEY: mockApiKey,
|
|
});
|
|
expect(instance.apiKey).toBe(mockApiKey);
|
|
});
|
|
|
|
describe('proxy support', () => {
|
|
const mockResponse = {
|
|
ok: true,
|
|
json: jest.fn().mockResolvedValue({ results: [] }),
|
|
};
|
|
|
|
beforeEach(() => {
|
|
fetch.mockResolvedValue(mockResponse);
|
|
});
|
|
|
|
it('should use a shared proxy dispatcher when configured', async () => {
|
|
const mockProxyDispatcher = { type: 'proxy-dispatcher' };
|
|
getEnvProxyDispatcher.mockReturnValue(mockProxyDispatcher);
|
|
|
|
const instance = new TavilySearchResults({ TAVILY_API_KEY: mockApiKey });
|
|
await instance._call({ query: 'test query' });
|
|
|
|
expect(getEnvProxyDispatcher).toHaveBeenCalled();
|
|
expect(fetch).toHaveBeenCalledWith(
|
|
'https://api.tavily.com/search',
|
|
expect.objectContaining({
|
|
dispatcher: mockProxyDispatcher,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('should not attach a dispatcher when no proxy is configured', async () => {
|
|
getEnvProxyDispatcher.mockReturnValue(undefined);
|
|
|
|
const instance = new TavilySearchResults({ TAVILY_API_KEY: mockApiKey });
|
|
await instance._call({ query: 'test query' });
|
|
|
|
expect(getEnvProxyDispatcher).toHaveBeenCalled();
|
|
expect(fetch).toHaveBeenCalledWith(
|
|
'https://api.tavily.com/search',
|
|
expect.not.objectContaining({
|
|
dispatcher: expect.anything(),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
});
|