|
| 1 | +import React from 'react'; |
| 2 | +import { MemoryRouter } from 'react-router-dom'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 5 | +import type { TInsightsParams } from 'librechat-data-provider'; |
| 6 | +import InsightsView from '../InsightsView'; |
| 7 | + |
| 8 | +const insightsData = { |
| 9 | + agents: [ |
| 10 | + { id: 'agent-1', name: 'Alpha' }, |
| 11 | + { id: 'agent-2', name: 'Beta' }, |
| 12 | + { id: 'agent-3', name: 'Gamma' }, |
| 13 | + ], |
| 14 | + summary: { totalConversations: 4, totalUsers: 2, totalMessages: 8, totalTokens: 100 }, |
| 15 | + daily: [], |
| 16 | + latest: { conversations: [], page: 1, pages: 1 }, |
| 17 | + topUsers: [], |
| 18 | + churnedUsers: [], |
| 19 | +}; |
| 20 | + |
| 21 | +const mockUseInsightsQuery = jest.fn((params: TInsightsParams) => ({ |
| 22 | + data: insightsData, |
| 23 | + isLoading: false, |
| 24 | + isFetching: false, |
| 25 | + error: null, |
| 26 | + params, |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('~/data-provider', () => ({ |
| 30 | + useGetStartupConfig: () => ({ data: { insightsEnabled: true }, isLoading: false }), |
| 31 | + useInsightsQuery: (params: TInsightsParams) => mockUseInsightsQuery(params), |
| 32 | +})); |
| 33 | + |
| 34 | +jest.mock('~/hooks', () => ({ |
| 35 | + useLocalize: () => (key: string, options?: { count?: number; name?: string }) => { |
| 36 | + if (options?.count != null) { |
| 37 | + return `${key}:${options.count}`; |
| 38 | + } |
| 39 | + return options?.name ? `${key}:${options.name}` : key; |
| 40 | + }, |
| 41 | + useAuthContext: () => ({ user: { id: 'user-1' } }), |
| 42 | + useDocumentTitle: () => undefined, |
| 43 | +})); |
| 44 | + |
| 45 | +jest.mock('react-i18next', () => ({ |
| 46 | + useTranslation: () => ({ i18n: { language: 'en', resolvedLanguage: 'en' } }), |
| 47 | +})); |
| 48 | + |
| 49 | +jest.mock('~/components/Chat/Menus/OpenSidebar', () => ({ |
| 50 | + __esModule: true, |
| 51 | + default: () => null, |
| 52 | +})); |
| 53 | + |
| 54 | +jest.mock('~/components/ui', () => ({ |
| 55 | + LocalizedDateRangePicker: () => null, |
| 56 | +})); |
| 57 | + |
| 58 | +function lastQueryParams(): TInsightsParams { |
| 59 | + return mockUseInsightsQuery.mock.calls[mockUseInsightsQuery.mock.calls.length - 1][0]; |
| 60 | +} |
| 61 | + |
| 62 | +function renderView() { |
| 63 | + return render( |
| 64 | + <MemoryRouter initialEntries={['/insights']}> |
| 65 | + <InsightsView /> |
| 66 | + </MemoryRouter>, |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +async function openAgentMenu(user: ReturnType<typeof userEvent.setup>) { |
| 71 | + await user.click(screen.getByText('com_insights_all_agents:3')); |
| 72 | + await screen.findByRole('option', { name: 'Alpha' }); |
| 73 | +} |
| 74 | + |
| 75 | +describe('InsightsView agent selection', () => { |
| 76 | + beforeEach(() => { |
| 77 | + mockUseInsightsQuery.mockClear(); |
| 78 | + mockUseInsightsQuery.mockImplementation((params) => ({ |
| 79 | + data: insightsData, |
| 80 | + isLoading: false, |
| 81 | + isFetching: false, |
| 82 | + error: null, |
| 83 | + params, |
| 84 | + })); |
| 85 | + }); |
| 86 | + |
| 87 | + it('filters names without changing selection and clears search on close', async () => { |
| 88 | + mockUseInsightsQuery.mockImplementation((params) => ({ |
| 89 | + data: { |
| 90 | + ...insightsData, |
| 91 | + agents: Array.from({ length: 12 }, (_, i) => ({ id: `agent-${i}`, name: `Agent ${i}` })), |
| 92 | + }, |
| 93 | + isLoading: false, |
| 94 | + isFetching: false, |
| 95 | + error: null, |
| 96 | + params, |
| 97 | + })); |
| 98 | + const user = userEvent.setup(); |
| 99 | + renderView(); |
| 100 | + await user.click(screen.getByText('com_insights_all_agents:12')); |
| 101 | + const search = screen.getByRole('textbox', { name: 'com_insights_search_agents' }); |
| 102 | + await user.type(search, 'AGENT 11'); |
| 103 | + expect(screen.getAllByRole('option')).toHaveLength(1); |
| 104 | + expect(lastQueryParams().agentIds).toBeUndefined(); |
| 105 | + await user.click(screen.getByRole('button', { name: 'com_ui_clear_all' })); |
| 106 | + await user.click(screen.getByRole('button', { name: 'com_insights_select_all_agents' })); |
| 107 | + expect(screen.getByText('com_insights_all_agents:12')).toBeInTheDocument(); |
| 108 | + await user.clear(search); |
| 109 | + await user.type(search, 'no match'); |
| 110 | + expect(screen.getByRole('status')).toHaveTextContent('com_insights_no_agents_found'); |
| 111 | + await user.keyboard('{Escape}'); |
| 112 | + await user.click(screen.getByText('com_insights_all_agents:12')); |
| 113 | + expect(screen.getByRole('textbox', { name: 'com_insights_search_agents' })).toHaveValue(''); |
| 114 | + expect(screen.getAllByRole('option')).toHaveLength(12); |
| 115 | + }); |
| 116 | + |
| 117 | + it('clears every agent without committing the empty selection', async () => { |
| 118 | + const user = userEvent.setup(); |
| 119 | + renderView(); |
| 120 | + await openAgentMenu(user); |
| 121 | + |
| 122 | + await user.click(screen.getByRole('button', { name: 'com_ui_clear_all' })); |
| 123 | + |
| 124 | + expect(screen.getByText('com_insights_no_agents_selected')).toBeInTheDocument(); |
| 125 | + for (const option of screen.getAllByRole('option')) { |
| 126 | + expect(option).toHaveAttribute('aria-selected', 'false'); |
| 127 | + } |
| 128 | + expect(lastQueryParams().agentIds).toBeUndefined(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('commits only the agents picked after clearing', async () => { |
| 132 | + const user = userEvent.setup(); |
| 133 | + renderView(); |
| 134 | + await openAgentMenu(user); |
| 135 | + |
| 136 | + await user.click(screen.getByRole('button', { name: 'com_ui_clear_all' })); |
| 137 | + await user.click(screen.getByRole('option', { name: 'Beta' })); |
| 138 | + |
| 139 | + await waitFor(() => expect(lastQueryParams().agentIds).toEqual(['agent-2'])); |
| 140 | + }); |
| 141 | + |
| 142 | + it('restores the committed selection when the menu closes while empty', async () => { |
| 143 | + const user = userEvent.setup(); |
| 144 | + renderView(); |
| 145 | + await openAgentMenu(user); |
| 146 | + |
| 147 | + await user.click(screen.getByRole('button', { name: 'com_ui_clear_all' })); |
| 148 | + await user.keyboard('{Escape}'); |
| 149 | + |
| 150 | + await waitFor(() => expect(screen.getByText('com_insights_all_agents:3')).toBeInTheDocument()); |
| 151 | + expect(lastQueryParams().agentIds).toBeUndefined(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('re-enables select all from the cleared state', async () => { |
| 155 | + const user = userEvent.setup(); |
| 156 | + renderView(); |
| 157 | + await openAgentMenu(user); |
| 158 | + |
| 159 | + await user.click(screen.getByRole('button', { name: 'com_ui_clear_all' })); |
| 160 | + const selectAll = screen.getByRole('button', { name: 'com_insights_select_all_agents' }); |
| 161 | + |
| 162 | + await user.click(selectAll); |
| 163 | + expect(screen.getByText('com_insights_all_agents:3')).toBeInTheDocument(); |
| 164 | + expect(lastQueryParams().agentIds).toBeUndefined(); |
| 165 | + }); |
| 166 | +}); |
0 commit comments