File size: 6,174 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import ApiKeyDialog from './ApiKeyDialog';
import { AuthType, SearchCategories, RerankerTypes } from 'librechat-data-provider';
import { useGetStartupConfig } from '~/data-provider';
// Mock useLocalize to just return the key
jest.mock('~/hooks', () => ({
useLocalize: () => (key: string) => key,
}));
jest.mock('~/data-provider', () => ({
useGetStartupConfig: jest.fn(),
}));
const mockRegister = (name: string) => ({
onChange: jest.fn(),
onBlur: jest.fn(),
ref: jest.fn(),
name,
});
const defaultProps = {
isOpen: true,
onOpenChange: jest.fn(),
onSubmit: jest.fn(),
onRevoke: jest.fn(),
authTypes: [
[SearchCategories.PROVIDERS, AuthType.USER_PROVIDED] as [string, AuthType],
[SearchCategories.SCRAPERS, AuthType.USER_PROVIDED] as [string, AuthType],
[SearchCategories.RERANKERS, AuthType.USER_PROVIDED] as [string, AuthType],
],
isToolAuthenticated: false,
register: mockRegister as any,
handleSubmit: (fn: any) => (e: any) => fn(e),
};
describe('ApiKeyDialog', () => {
const mockUseGetStartupConfig = useGetStartupConfig as jest.Mock;
afterEach(() => jest.clearAllMocks());
it('shows all dropdowns and both reranker fields when no config is set', () => {
mockUseGetStartupConfig.mockReturnValue({ data: {} });
render(<ApiKeyDialog {...defaultProps} />);
// Provider dropdown button
expect(
screen.getByRole('button', { name: 'com_ui_web_search_provider_serper' }),
).toBeInTheDocument();
// Scraper dropdown button
expect(
screen.getByRole('button', { name: 'com_ui_web_search_scraper_firecrawl' }),
).toBeInTheDocument();
// Reranker dropdown button
expect(
screen.getByRole('button', { name: 'com_ui_web_search_reranker_jina' }),
).toBeInTheDocument();
// Reranker fields (default is Jina)
expect(screen.getByPlaceholderText('com_ui_web_search_jina_key')).toBeInTheDocument();
// Switch to Cohere
fireEvent.click(screen.getByText('com_ui_web_search_reranker_cohere'));
expect(screen.getByPlaceholderText('com_ui_web_search_cohere_key')).toBeInTheDocument();
});
it('shows static text for provider and only provider input if provider is set', () => {
mockUseGetStartupConfig.mockReturnValue({ data: { webSearch: { searchProvider: 'serper' } } });
render(<ApiKeyDialog {...defaultProps} />);
expect(screen.getByText('com_ui_web_search_provider_serper')).toBeInTheDocument();
// Should not find a dropdown button for provider
expect(screen.queryByRole('button', { name: /provider/i })).not.toBeInTheDocument();
});
it('shows only Jina reranker field if rerankerType is set to jina', () => {
mockUseGetStartupConfig.mockReturnValue({
data: { webSearch: { rerankerType: RerankerTypes.JINA } },
});
render(<ApiKeyDialog {...defaultProps} />);
expect(screen.getByPlaceholderText('com_ui_web_search_jina_key')).toBeInTheDocument();
expect(screen.queryByPlaceholderText('com_ui_web_search_cohere_key')).not.toBeInTheDocument();
});
it('shows only Cohere reranker field if rerankerType is set to cohere', () => {
mockUseGetStartupConfig.mockReturnValue({
data: { webSearch: { rerankerType: RerankerTypes.COHERE } },
});
render(<ApiKeyDialog {...defaultProps} />);
expect(screen.getByPlaceholderText('com_ui_web_search_cohere_key')).toBeInTheDocument();
expect(screen.queryByPlaceholderText('com_ui_web_search_jina_key')).not.toBeInTheDocument();
});
it('shows documentation link for the visible reranker', () => {
mockUseGetStartupConfig.mockReturnValue({ data: {} });
render(<ApiKeyDialog {...defaultProps} />);
// Default is Jina
expect(screen.getByText('com_ui_web_search_reranker_jina_key')).toBeInTheDocument();
// Switch to Cohere
fireEvent.click(screen.getByText('com_ui_web_search_reranker_cohere'));
expect(screen.getByText('com_ui_web_search_reranker_cohere_key')).toBeInTheDocument();
});
it('does not render provider section if SYSTEM_DEFINED', () => {
mockUseGetStartupConfig.mockReturnValue({ data: {} });
const props = {
...defaultProps,
authTypes: [
[SearchCategories.PROVIDERS, AuthType.SYSTEM_DEFINED],
[SearchCategories.SCRAPERS, AuthType.USER_PROVIDED],
[SearchCategories.RERANKERS, AuthType.USER_PROVIDED],
] as [string, AuthType][],
};
render(<ApiKeyDialog {...props} />);
expect(screen.queryByText('com_ui_web_search_provider')).not.toBeInTheDocument();
expect(screen.getByText('com_ui_web_search_scraper')).toBeInTheDocument();
expect(screen.getByText('com_ui_web_search_reranker')).toBeInTheDocument();
});
it('does not render scraper section if SYSTEM_DEFINED', () => {
mockUseGetStartupConfig.mockReturnValue({ data: {} });
const props = {
...defaultProps,
authTypes: [
[SearchCategories.PROVIDERS, AuthType.USER_PROVIDED],
[SearchCategories.SCRAPERS, AuthType.SYSTEM_DEFINED],
[SearchCategories.RERANKERS, AuthType.USER_PROVIDED],
] as [string, AuthType][],
};
render(<ApiKeyDialog {...props} />);
expect(screen.getByText('com_ui_web_search_provider')).toBeInTheDocument();
expect(screen.queryByText('com_ui_web_search_scraper')).not.toBeInTheDocument();
expect(screen.getByText('com_ui_web_search_reranker')).toBeInTheDocument();
});
it('does not render reranker section if SYSTEM_DEFINED', () => {
mockUseGetStartupConfig.mockReturnValue({ data: {} });
const props = {
...defaultProps,
authTypes: [
[SearchCategories.PROVIDERS, AuthType.USER_PROVIDED],
[SearchCategories.SCRAPERS, AuthType.USER_PROVIDED],
[SearchCategories.RERANKERS, AuthType.SYSTEM_DEFINED],
] as [string, AuthType][],
};
render(<ApiKeyDialog {...props} />);
expect(screen.getByText('com_ui_web_search_provider')).toBeInTheDocument();
expect(screen.getByText('com_ui_web_search_scraper')).toBeInTheDocument();
expect(screen.queryByText('com_ui_web_search_reranker')).not.toBeInTheDocument();
});
});
|