import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { TuningApp } from './TuningApp'; beforeEach(() => { localStorage.clear(); sessionStorage.clear(); vi.unstubAllGlobals(); }); describe('TuningApp', () => { it('连接调参服务并显示 DeepSeek 能力与新建表单', async () => { const fetchMock = vi.fn((input: string | URL | Request, _init?: RequestInit) => { void _init; const url = String(input); if (url.endsWith('/api/tuning/capabilities')) return Promise.resolve( new Response( JSON.stringify({ ready: true, configured: true, apiKeyConfigured: true, frameworkInstalled: true, model: 'deepseek-v4-flash', baseUrl: 'https://api.deepseek.com', }), { status: 200, headers: { 'Content-Type': 'application/json' } }, ), ); return Promise.resolve( new Response(JSON.stringify({ sessions: [] }), { status: 200, headers: { 'Content-Type': 'application/json' }, }), ); }); vi.stubGlobal('fetch', fetchMock); const { container } = render(); expect(container.firstElementChild).toHaveClass('h-full', 'overflow-y-auto'); fireEvent.change(screen.getByLabelText('访问令牌(仅当前标签页)'), { target: { value: 'training-secret' }, }); fireEvent.click(screen.getByRole('button', { name: '连接/刷新' })); expect(await screen.findByText(/deepseek-v4-flash/)).toBeInTheDocument(); expect(screen.getByText('新建 Unitree-Go2-Flat 调参 Session')).toBeInTheDocument(); const trialCount = screen.getByLabelText('调参次数(候选配置数)'); expect(trialCount).toHaveValue(12); fireEvent.change(trialCount, { target: { value: '36' } }); expect(trialCount).toHaveValue(36); await waitFor(() => expect(fetchMock).toHaveBeenCalledTimes(2)); for (const call of fetchMock.mock.calls) { expect(String(call[0])).not.toContain('training-secret'); const init = call[1]; expect(init).toBeDefined(); expect(new Headers(init?.headers).get('Authorization')).toBe('Bearer training-secret'); } }); });