2025-06-23 10:22:27 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
|
|
|
|
import '@testing-library/jest-dom';
|
|
|
|
|
import AgentGrid from '../AgentGrid';
|
2025-06-23 11:42:24 -04:00
|
|
|
import { useGetMarketplaceAgentsQuery } from 'librechat-data-provider/react-query';
|
2025-06-23 10:22:27 -04:00
|
|
|
import type t from 'librechat-data-provider';
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
// Mock the marketplace agent query hook
|
2025-06-23 10:22:27 -04:00
|
|
|
jest.mock('~/hooks/Agents', () => ({
|
2025-06-23 11:42:24 -04:00
|
|
|
useGetMarketplaceAgentsQuery: jest.fn(),
|
|
|
|
|
useAgentCategories: jest.fn(() => ({
|
|
|
|
|
categories: [],
|
|
|
|
|
isLoading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
})),
|
2025-06-23 10:22:27 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Mock useLocalize hook
|
|
|
|
|
jest.mock('~/hooks/useLocalize', () => () => (key: string) => {
|
|
|
|
|
const mockTranslations: Record<string, string> = {
|
|
|
|
|
com_agents_top_picks: 'Top Picks',
|
|
|
|
|
com_agents_all: 'All Agents',
|
|
|
|
|
com_agents_recommended: 'Our recommended agents',
|
|
|
|
|
com_agents_results_for: 'Results for "{{query}}"',
|
|
|
|
|
com_agents_see_more: 'See more',
|
|
|
|
|
com_agents_error_loading: 'Error loading agents',
|
|
|
|
|
com_agents_error_searching: 'Error searching agents',
|
|
|
|
|
com_agents_no_results: 'No agents found. Try another search term.',
|
|
|
|
|
com_agents_none_in_category: 'No agents found in this category',
|
2025-06-23 11:42:24 -04:00
|
|
|
com_agents_search_empty_heading: 'No results found',
|
|
|
|
|
com_agents_empty_state_heading: 'No agents available',
|
|
|
|
|
com_agents_loading: 'Loading...',
|
|
|
|
|
com_agents_grid_announcement: '{{count}} agents in {{category}}',
|
|
|
|
|
com_agents_load_more_label: 'Load more agents from {{category}}',
|
2025-06-23 10:22:27 -04:00
|
|
|
};
|
2025-06-23 11:42:24 -04:00
|
|
|
return mockTranslations[key] || key.replace(/{{(\w+)}}/g, (match, key) => `[${key}]`);
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
// Mock SmartLoader components
|
|
|
|
|
jest.mock('../SmartLoader', () => ({
|
|
|
|
|
SmartLoader: ({ children, isLoading }: { children: React.ReactNode; isLoading: boolean }) =>
|
|
|
|
|
isLoading ? <div>Loading...</div> : <div>{children}</div>,
|
|
|
|
|
useHasData: (data: any) => !!data?.agents?.length,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Mock ErrorDisplay component
|
|
|
|
|
jest.mock('../ErrorDisplay', () => ({
|
|
|
|
|
__esModule: true,
|
|
|
|
|
default: ({ error, onRetry }: { error: string; onRetry: () => void }) => (
|
|
|
|
|
<div>
|
|
|
|
|
<div>Error: {error}</div>
|
|
|
|
|
<button onClick={onRetry}>Retry</button>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Mock AgentCard component
|
|
|
|
|
jest.mock('../AgentCard', () => ({
|
|
|
|
|
__esModule: true,
|
|
|
|
|
default: ({ agent, onClick }: { agent: t.Agent; onClick: () => void }) => (
|
|
|
|
|
<div data-testid={`agent-card-${agent.id}`} onClick={onClick}>
|
|
|
|
|
<h3>{agent.name}</h3>
|
|
|
|
|
<p>{agent.description}</p>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
2025-06-23 10:22:27 -04:00
|
|
|
}));
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
const mockUseGetMarketplaceAgentsQuery = useGetMarketplaceAgentsQuery as jest.MockedFunction<
|
|
|
|
|
typeof useGetMarketplaceAgentsQuery
|
2025-06-23 10:22:27 -04:00
|
|
|
>;
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
describe('AgentGrid Integration with useGetMarketplaceAgentsQuery', () => {
|
2025-06-23 10:22:27 -04:00
|
|
|
const mockOnSelectAgent = jest.fn();
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
const mockAgents: t.Agent[] = [
|
2025-06-23 10:22:27 -04:00
|
|
|
{
|
|
|
|
|
id: '1',
|
|
|
|
|
name: 'Test Agent 1',
|
|
|
|
|
description: 'First test agent',
|
2025-06-23 11:42:24 -04:00
|
|
|
avatar: { filepath: '/avatar1.png', source: 'local' },
|
|
|
|
|
category: 'finance',
|
|
|
|
|
authorName: 'Author 1',
|
|
|
|
|
created_at: 1672531200000,
|
|
|
|
|
instructions: null,
|
|
|
|
|
provider: 'custom',
|
|
|
|
|
model: 'gpt-4',
|
|
|
|
|
model_parameters: {},
|
2025-06-23 10:22:27 -04:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: '2',
|
|
|
|
|
name: 'Test Agent 2',
|
|
|
|
|
description: 'Second test agent',
|
2025-06-23 11:42:24 -04:00
|
|
|
avatar: { filepath: '/avatar2.png', source: 'local' },
|
|
|
|
|
category: 'finance',
|
|
|
|
|
authorName: 'Author 2',
|
|
|
|
|
created_at: 1672531200000,
|
|
|
|
|
instructions: null,
|
|
|
|
|
provider: 'custom',
|
|
|
|
|
model: 'gpt-4',
|
|
|
|
|
model_parameters: {},
|
2025-06-23 10:22:27 -04:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const defaultMockQueryResult = {
|
|
|
|
|
data: {
|
2025-06-23 11:42:24 -04:00
|
|
|
data: mockAgents,
|
2025-06-23 10:22:27 -04:00
|
|
|
pagination: {
|
|
|
|
|
current: 1,
|
|
|
|
|
hasMore: true,
|
|
|
|
|
total: 10,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
isLoading: false,
|
|
|
|
|
error: null,
|
|
|
|
|
isFetching: false,
|
2025-06-23 11:42:24 -04:00
|
|
|
refetch: jest.fn(),
|
|
|
|
|
isSuccess: true,
|
|
|
|
|
isError: false,
|
|
|
|
|
status: 'success' as const,
|
2025-06-23 10:22:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.clearAllMocks();
|
2025-06-23 11:42:24 -04:00
|
|
|
mockUseGetMarketplaceAgentsQuery.mockReturnValue(defaultMockQueryResult);
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Query Integration', () => {
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should call useGetMarketplaceAgentsQuery with correct parameters for category search', () => {
|
2025-06-23 10:22:27 -04:00
|
|
|
render(
|
|
|
|
|
<AgentGrid category="finance" searchQuery="test query" onSelectAgent={mockOnSelectAgent} />,
|
|
|
|
|
);
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(mockUseGetMarketplaceAgentsQuery).toHaveBeenCalledWith({
|
|
|
|
|
requiredPermission: 1,
|
2025-06-23 10:22:27 -04:00
|
|
|
category: 'finance',
|
2025-06-23 11:42:24 -04:00
|
|
|
search: 'test query',
|
2025-06-23 10:22:27 -04:00
|
|
|
limit: 6,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should call useGetMarketplaceAgentsQuery with promoted=1 for promoted category', () => {
|
|
|
|
|
render(<AgentGrid category="promoted" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(mockUseGetMarketplaceAgentsQuery).toHaveBeenCalledWith({
|
|
|
|
|
requiredPermission: 1,
|
|
|
|
|
promoted: 1,
|
|
|
|
|
limit: 6,
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should call useGetMarketplaceAgentsQuery without category filter for "all" category', () => {
|
|
|
|
|
render(<AgentGrid category="all" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(mockUseGetMarketplaceAgentsQuery).toHaveBeenCalledWith({
|
|
|
|
|
requiredPermission: 1,
|
2025-06-23 10:22:27 -04:00
|
|
|
limit: 6,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should not include category in search when category is "all" or "promoted"', () => {
|
|
|
|
|
render(<AgentGrid category="all" searchQuery="test" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(mockUseGetMarketplaceAgentsQuery).toHaveBeenCalledWith({
|
|
|
|
|
requiredPermission: 1,
|
|
|
|
|
search: 'test',
|
2025-06-23 10:22:27 -04:00
|
|
|
limit: 6,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
describe('Agent Display', () => {
|
|
|
|
|
it('should render agent cards when data is available', () => {
|
2025-06-23 10:22:27 -04:00
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.getByTestId('agent-card-1')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByTestId('agent-card-2')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Test Agent 1')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText('Test Agent 2')).toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should call onSelectAgent when agent card is clicked', () => {
|
|
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
fireEvent.click(screen.getByTestId('agent-card-1'));
|
|
|
|
|
expect(mockOnSelectAgent).toHaveBeenCalledWith(mockAgents[0]);
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
describe('Loading States', () => {
|
|
|
|
|
it('should show loading state when isLoading is true', () => {
|
|
|
|
|
mockUseGetMarketplaceAgentsQuery.mockReturnValue({
|
2025-06-23 10:22:27 -04:00
|
|
|
...defaultMockQueryResult,
|
|
|
|
|
isLoading: true,
|
|
|
|
|
data: undefined,
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.getByText('Loading...')).toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should show empty state when no agents are available', () => {
|
|
|
|
|
mockUseGetMarketplaceAgentsQuery.mockReturnValue({
|
2025-06-23 10:22:27 -04:00
|
|
|
...defaultMockQueryResult,
|
2025-06-23 11:42:24 -04:00
|
|
|
data: { data: [], pagination: { current: 1, hasMore: false, total: 0 } },
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.getByText('No agents available')).toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
describe('Error Handling', () => {
|
|
|
|
|
it('should show error display when query has error', () => {
|
|
|
|
|
const mockError = new Error('Failed to fetch agents');
|
|
|
|
|
mockUseGetMarketplaceAgentsQuery.mockReturnValue({
|
2025-06-23 10:22:27 -04:00
|
|
|
...defaultMockQueryResult,
|
2025-06-23 11:42:24 -04:00
|
|
|
error: mockError,
|
|
|
|
|
isError: true,
|
|
|
|
|
data: undefined,
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.getByText('Error: Failed to fetch agents')).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole('button', { name: 'Retry' })).toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
2025-06-23 11:42:24 -04:00
|
|
|
});
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
describe('Search Results', () => {
|
|
|
|
|
it('should show search results title when searching', () => {
|
|
|
|
|
render(
|
|
|
|
|
<AgentGrid category="finance" searchQuery="automation" onSelectAgent={mockOnSelectAgent} />,
|
|
|
|
|
);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.getByText('Results for "automation"')).toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should show empty search results message', () => {
|
|
|
|
|
mockUseGetMarketplaceAgentsQuery.mockReturnValue({
|
2025-06-23 10:22:27 -04:00
|
|
|
...defaultMockQueryResult,
|
2025-06-23 11:42:24 -04:00
|
|
|
data: { data: [], pagination: { current: 1, hasMore: false, total: 0 } },
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
render(
|
2025-06-23 11:42:24 -04:00
|
|
|
<AgentGrid
|
|
|
|
|
category="finance"
|
|
|
|
|
searchQuery="nonexistent"
|
|
|
|
|
onSelectAgent={mockOnSelectAgent}
|
|
|
|
|
/>,
|
2025-06-23 10:22:27 -04:00
|
|
|
);
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.getByText('No results found')).toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
expect(screen.getByText('No agents found. Try another search term.')).toBeInTheDocument();
|
|
|
|
|
});
|
2025-06-23 11:42:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Load More Functionality', () => {
|
|
|
|
|
it('should show "See more" button when hasMore is true', () => {
|
|
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByRole('button', { name: 'See more' })).toBeInTheDocument();
|
|
|
|
|
});
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
it('should not show "See more" button when hasMore is false', () => {
|
|
|
|
|
mockUseGetMarketplaceAgentsQuery.mockReturnValue({
|
2025-06-23 10:22:27 -04:00
|
|
|
...defaultMockQueryResult,
|
|
|
|
|
data: {
|
2025-06-23 11:42:24 -04:00
|
|
|
...defaultMockQueryResult.data,
|
|
|
|
|
pagination: { current: 1, hasMore: false, total: 2 },
|
2025-06-23 10:22:27 -04:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
render(<AgentGrid category="finance" searchQuery="" onSelectAgent={mockOnSelectAgent} />);
|
2025-06-23 10:22:27 -04:00
|
|
|
|
2025-06-23 11:42:24 -04:00
|
|
|
expect(screen.queryByRole('button', { name: 'See more' })).not.toBeInTheDocument();
|
2025-06-23 10:22:27 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|