import { fireEvent, render, screen, waitFor, within } from '@testing-library/react' import { beforeEach, describe, expect, it, vi } from 'vitest' import AiTerminalPanel from '../../src/components/ai/AiTerminalPanel' import { useAiProviderStore } from '../../src/stores/aiProviderStore' import { mockDesktopApi } from '../setup' describe('terminal AI panel', () => { beforeEach(() => { vi.clearAllMocks() useAiProviderStore.setState({ providers: [ { id: 'local-test', name: 'Local Test', protocol: 'openai-chat', authStyle: 'none', baseUrl: 'http://127.0.0.1:11434/v1', allowInsecureHttp: true, models: [{ id: 'test', model: 'test', label: 'Test', capability: 'chat', billing: 'local' }], defaultModelId: 'test', enabled: true, hasApiKey: true, }, ], activeProviderId: 'local-test', isLoaded: true, catalogProviders: [], catalogUpdatedAt: null, }) vi.mocked(mockDesktopApi.aiComplete).mockResolvedValue({ success: true, text: '建议:\n```bash\ndate\n```', model: 'test', redactedCount: 0, }) }) it('prefers selection and fills one line without pressing enter', async () => { const insert = vi.fn() render( ({ selection: 'selected error', viewport: 'whole screen' })} insert={insert} onClose={vi.fn()} /> ) fireEvent.change(screen.getByPlaceholderText('例如:分析这段报错,并给出最少的只读检查命令'), { target: { value: '怎么排查' }, }) fireEvent.click(screen.getByRole('button', { name: '生成建议' })) await waitFor(() => expect(mockDesktopApi.aiComplete).toHaveBeenCalled()) expect(vi.mocked(mockDesktopApi.aiComplete).mock.calls[0][0].context).toBe('selected error') fireEvent.click(await screen.findByRole('button', { name: '填入终端' })) expect(insert).toHaveBeenCalledWith('date') }) it('executes one confirmed command through the current SSH connection and analyzes real output', async () => { vi.mocked(mockDesktopApi.aiComplete) .mockResolvedValueOnce({ success: true, text: '```bash\nuptime\n```', model: 'test', redactedCount: 0 }) .mockResolvedValueOnce({ success: true, text: '负载正常。', model: 'test', redactedCount: 0 }) vi.mocked(mockDesktopApi.sshExec).mockResolvedValueOnce({ success: true, result: 'load average: 0.01' }) render( ({ selection: '', viewport: '' })} insert={vi.fn()} onClose={vi.fn()} connectionId="ssh-active-1" targetLabel="root@opsbase.cn" /> ) fireEvent.click(screen.getByRole('button', { name: '执行任务' })) fireEvent.change(screen.getByPlaceholderText('例如:分析这段报错,并给出最少的只读检查命令'), { target: { value: '检查服务器负载' }, }) fireEvent.click(screen.getByRole('button', { name: '准备执行方案' })) fireEvent.click(await screen.findByRole('button', { name: '执行一次' })) const dialog = await screen.findByRole('dialog') expect(within(dialog).getByText(/root@opsbase.cn/)).toBeVisible() expect(mockDesktopApi.sshExec).not.toHaveBeenCalled() fireEvent.click(within(dialog).getByRole('button', { name: '执行一次' })) await waitFor(() => expect(mockDesktopApi.sshExec).toHaveBeenCalledWith('ssh-active-1', 'uptime')) expect(await screen.findByText('load average: 0.01')).toBeVisible() expect(await screen.findByText('负载正常。')).toBeVisible() }) })