import { fireEvent, screen, waitFor } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { CommandPalette } from '../components/CommandPalette'; import { jsonResponse, renderUi } from '../test/utils'; import { AskChat, HERMES_NOTICE } from './AskChat'; const urlOf = (input: RequestInfo | URL) => (typeof input === 'string' ? input : input instanceof URL ? input.href : input.url); const methodOf = (input: RequestInfo | URL, init?: RequestInit) => (init?.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase(); const bodyOf = async (input: RequestInfo | URL, init?: RequestInit) => (input instanceof Request ? input.clone().json() : JSON.parse(String(init?.body))); const me = { id: 1, username: 'c', role: 'consultant', permissions: ['read', 'run.start'] }; function backend(hermes: { enabled: boolean; configured: boolean }, ask: (body: { question: string; conversation_id?: string }) => Response) { const calls: { method: string; url: string; body?: unknown }[] = []; vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const u = urlOf(input); const method = methodOf(input, init); if (u.endsWith('/api/auth/me')) return jsonResponse(me); if (u.includes('/api/assistant/status')) return jsonResponse({ ...hermes, reachable: true }); if (u.includes('/api/assistant/ask')) { const body = await bodyOf(input, init); calls.push({ method, url: u, body }); return ask(body); } if (u.includes('/api/runs')) return jsonResponse({ items: [], total: 0, limit: 40, offset: 0 }); if (u.includes('/api/pods')) return jsonResponse([]); // the top bar's environment cue if (u.endsWith('/api/chat/conversations') && method === 'GET') return jsonResponse({ items: [] }); // the history column calls.push({ method, url: u }); return jsonResponse({ id: 'conv-1' }); }); return calls; } describe('Ask and the Hermes assistant', () => { afterEach(() => vi.restoreAllMocks()); it('when Hermes is on: the exact notice under the input, the answer, and the conversation id kept for follow-ups', async () => { let n = 0; const calls = backend({ enabled: true, configured: true }, () => jsonResponse({ answer: `Answer ${++n}`, conversation_id: 'h-42', model: 'nvidia/test-model' })); renderUi(, { auth: true, route: '/assistant' }); expect(await screen.findByTestId('hermes-notice')).toHaveTextContent(HERMES_NOTICE); expect(HERMES_NOTICE).toBe("Answered by the Hermes assistant using NVIDIA's AI model. Don't paste client data."); expect(screen.getByRole('log', { name: 'Conversation with the co-worker' })).toBeInTheDocument(); expect(await screen.findByRole('navigation', { name: 'Conversations' })).toBeInTheDocument(); const input = screen.getByRole('textbox', { name: 'Ask the co-worker' }); fireEvent.change(input, { target: { value: 'What does 26C change in invoice matching?' } }); fireEvent.keyDown(input, { key: 'Enter' }); expect(await screen.findByText('Answer 1')).toBeInTheDocument(); expect(screen.getByTestId('hermes-answered')).toHaveTextContent('Answered by the Hermes assistant (nvidia/test-model).'); fireEvent.change(input, { target: { value: 'And in payments?' } }); fireEvent.keyDown(input, { key: 'Enter' }); expect(await screen.findByText('Answer 2')).toBeInTheDocument(); const asks = calls.filter((c) => c.url.includes('/api/assistant/ask')); expect(asks.map((c) => c.body)).toEqual([{ question: 'What does 26C change in invoice matching?' }, { question: 'And in payments?', conversation_id: 'h-42' }]); expect(calls.some((c) => c.method === 'POST' && c.url.includes('/api/chat/'))).toBe(false); // the built-in chat was not asked }); it('a rate limit is said in plain words with the wait from Retry-After', async () => { backend({ enabled: true, configured: true }, () => new Response(JSON.stringify({ detail: 'Too many questions', code: 'RATE_LIMITED' }), { status: 429, headers: { 'Content-Type': 'application/json', 'Retry-After': '30' } })); renderUi(, { auth: true, route: '/assistant' }); await screen.findByTestId('hermes-notice'); const input = screen.getByRole('textbox', { name: 'Ask the co-worker' }); fireEvent.change(input, { target: { value: 'Is Procure-to-Pay ready?' } }); fireEvent.keyDown(input, { key: 'Enter' }); expect(await screen.findByRole('alert')).toHaveTextContent('You have asked many questions in a short time. Try again in 30 seconds.'); }); it('when Hermes is off: no notice, and questions go to the built-in assistant', async () => { const calls = backend({ enabled: false, configured: false }, () => jsonResponse({}, 409)); renderUi(, { auth: true, route: '/assistant' }); const input = await screen.findByRole('textbox', { name: 'Ask the co-worker' }); await waitFor(() => expect(screen.queryByTestId('hermes-notice')).not.toBeInTheDocument()); fireEvent.change(input, { target: { value: 'Is Procure-to-Pay ready for 26C?' } }); fireEvent.keyDown(input, { key: 'Enter' }); await waitFor(() => expect(calls.some((c) => c.method === 'POST' && c.url.includes('/api/chat/conversations'))).toBe(true)); expect(calls.some((c) => c.url.includes('/api/assistant/ask'))).toBe(false); }); it('requests to do something stay with the built-in assistant even when Hermes is on (they keep their confirm cards)', async () => { const calls = backend({ enabled: true, configured: true }, () => jsonResponse({ answer: 'x', conversation_id: 'h-1', model: 'm' })); renderUi(, { auth: true, route: '/assistant' }); await screen.findByTestId('hermes-notice'); const input = screen.getByRole('textbox', { name: 'Ask the co-worker' }); fireEvent.change(input, { target: { value: 'start a quick simulation of Procure-to-Pay' } }); fireEvent.keyDown(input, { key: 'Enter' }); await waitFor(() => expect(calls.some((c) => c.method === 'POST' && c.url.includes('/api/chat/conversations'))).toBe(true)); expect(calls.some((c) => c.url.includes('/api/assistant/ask'))).toBe(false); }); }); describe('the command palette (Ctrl K)', () => { afterEach(() => vi.restoreAllMocks()); it('lists Go to and Actions, filters by the words typed, and ends with the Ask row', async () => { backend({ enabled: false, configured: false }, () => jsonResponse({})); renderUi( undefined} />, { auth: true }); expect(await screen.findByRole('group', { name: 'Go to' })).toBeInTheDocument(); const input = screen.getByRole('combobox', { name: 'Search or ask' }); fireEvent.change(input, { target: { value: 'defe' } }); expect(screen.getByRole('option', { name: /^Defects/ })).toBeInTheDocument(); expect(screen.queryByRole('option', { name: /^Scenarios/ })).not.toBeInTheDocument(); fireEvent.change(input, { target: { value: 'new' } }); expect(await screen.findByRole('option', { name: /New test run/ })).toBeInTheDocument(); fireEvent.change(input, { target: { value: 'zzzz' } }); expect(screen.getByText('Nothing matches.')).toBeInTheDocument(); }); });