|
| 1 | +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import ConfigAssistantPanel from './ConfigAssistantPanel'; |
| 5 | + |
| 6 | +const mockConfigAssist = vi.fn(); |
| 7 | + |
| 8 | +vi.mock('../../../services/api/mcpClientsApi', () => ({ |
| 9 | + mcpClientsApi: { configAssist: (...args: unknown[]) => mockConfigAssist(...args) }, |
| 10 | +})); |
| 11 | + |
| 12 | +describe('ConfigAssistantPanel', () => { |
| 13 | + beforeEach(() => { |
| 14 | + mockConfigAssist.mockReset(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('renders the input textarea and Send button', () => { |
| 18 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 19 | + expect(screen.getByPlaceholderText(/ask a question/i)).toBeInTheDocument(); |
| 20 | + expect(screen.getByRole('button', { name: 'Send' })).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('send button is disabled when input is empty', () => { |
| 24 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 25 | + expect(screen.getByRole('button', { name: 'Send' })).toBeDisabled(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('enables send button when input has text', () => { |
| 29 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 30 | + fireEvent.change(screen.getByPlaceholderText(/ask a question/i), { |
| 31 | + target: { value: 'What env vars do I need?' }, |
| 32 | + }); |
| 33 | + expect(screen.getByRole('button', { name: 'Send' })).not.toBeDisabled(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('sends message and renders assistant reply', async () => { |
| 37 | + mockConfigAssist.mockResolvedValue({ reply: 'You need an API_KEY env var.' }); |
| 38 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 39 | + |
| 40 | + fireEvent.change(screen.getByPlaceholderText(/ask a question/i), { |
| 41 | + target: { value: 'What do I need?' }, |
| 42 | + }); |
| 43 | + |
| 44 | + await act(async () => { |
| 45 | + fireEvent.click(screen.getByRole('button', { name: 'Send' })); |
| 46 | + }); |
| 47 | + |
| 48 | + await waitFor(() => { |
| 49 | + expect(screen.getByText('You need an API_KEY env var.')).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + expect(mockConfigAssist).toHaveBeenCalledWith({ |
| 53 | + qualified_name: 'acme/test', |
| 54 | + user_message: 'What do I need?', |
| 55 | + history: [{ role: 'user', content: 'What do I need?' }], |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('shows suggested_env values and Apply button', async () => { |
| 60 | + mockConfigAssist.mockResolvedValue({ |
| 61 | + reply: 'Here are suggested values', |
| 62 | + suggested_env: { API_KEY: 'abc123' }, |
| 63 | + }); |
| 64 | + |
| 65 | + const onApply = vi.fn(); |
| 66 | + render(<ConfigAssistantPanel qualifiedName="acme/test" onApplySuggestedEnv={onApply} />); |
| 67 | + |
| 68 | + fireEvent.change(screen.getByPlaceholderText(/ask a question/i), { |
| 69 | + target: { value: 'Help me configure' }, |
| 70 | + }); |
| 71 | + |
| 72 | + await act(async () => { |
| 73 | + fireEvent.click(screen.getByRole('button', { name: 'Send' })); |
| 74 | + }); |
| 75 | + |
| 76 | + await waitFor(() => { |
| 77 | + expect(screen.getByText('Here are suggested values')).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + // Shows key name (the colon is in the same text node with whitespace) |
| 81 | + expect(screen.getByText(/API_KEY:/)).toBeInTheDocument(); |
| 82 | + |
| 83 | + // Apply button exists and calls the callback |
| 84 | + const applyBtn = screen.getByRole('button', { name: 'Apply suggested values' }); |
| 85 | + fireEvent.click(applyBtn); |
| 86 | + expect(onApply).toHaveBeenCalledWith({ API_KEY: 'abc123' }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('shows error on failed request', async () => { |
| 90 | + mockConfigAssist.mockRejectedValue(new Error('AI service unavailable')); |
| 91 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 92 | + |
| 93 | + fireEvent.change(screen.getByPlaceholderText(/ask a question/i), { |
| 94 | + target: { value: 'Hello' }, |
| 95 | + }); |
| 96 | + |
| 97 | + await act(async () => { |
| 98 | + fireEvent.click(screen.getByRole('button', { name: 'Send' })); |
| 99 | + }); |
| 100 | + |
| 101 | + await waitFor(() => { |
| 102 | + expect(screen.getByText('AI service unavailable')).toBeInTheDocument(); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('clears input after sending', async () => { |
| 107 | + mockConfigAssist.mockResolvedValue({ reply: 'OK' }); |
| 108 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 109 | + |
| 110 | + const textarea = screen.getByPlaceholderText(/ask a question/i) as HTMLTextAreaElement; |
| 111 | + fireEvent.change(textarea, { target: { value: 'Question?' } }); |
| 112 | + |
| 113 | + await act(async () => { |
| 114 | + fireEvent.click(screen.getByRole('button', { name: 'Send' })); |
| 115 | + }); |
| 116 | + |
| 117 | + expect(textarea.value).toBe(''); |
| 118 | + }); |
| 119 | + |
| 120 | + it('sends on Enter key press', async () => { |
| 121 | + mockConfigAssist.mockResolvedValue({ reply: 'reply' }); |
| 122 | + render(<ConfigAssistantPanel qualifiedName="acme/test" />); |
| 123 | + |
| 124 | + const textarea = screen.getByPlaceholderText(/ask a question/i); |
| 125 | + fireEvent.change(textarea, { target: { value: 'test message' } }); |
| 126 | + |
| 127 | + await act(async () => { |
| 128 | + fireEvent.keyDown(textarea, { key: 'Enter', shiftKey: false }); |
| 129 | + }); |
| 130 | + |
| 131 | + await waitFor(() => { |
| 132 | + expect(mockConfigAssist).toHaveBeenCalledTimes(1); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments