diff --git a/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/header.spec.tsx b/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/header.spec.tsx new file mode 100644 index 00000000000..6831b80f672 --- /dev/null +++ b/web/features/agent-v2/agent-detail/configure/components/preview/__tests__/header.spec.tsx @@ -0,0 +1,82 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { fireEvent, render, screen, waitFor } from '@testing-library/react' +import { AgentPreviewHeader } from '../header' + +const mocks = vi.hoisted(() => ({ + refreshDebugConversation: vi.fn(), +})) + +vi.mock('@/service/client', () => ({ + consoleQuery: { + agent: { + byAgentId: { + get: { + queryKey: () => ['agent'], + }, + debugConversation: { + refresh: { + post: { + mutationOptions: (options?: { onSuccess?: (data: { debug_conversation_id: string }) => void }) => ({ + mutationFn: mocks.refreshDebugConversation, + ...options, + }), + }, + }, + }, + }, + }, + }, +})) + +function renderHeader({ + onRestart = vi.fn(), +}: { + onRestart?: () => void +} = {}) { + const queryClient = new QueryClient() + queryClient.setQueryData(['agent'], { + debug_conversation_id: 'debug-conversation-old', + name: 'Research Agent', + }) + + render( + + + , + ) + + return { + queryClient, + } +} + +describe('AgentPreviewHeader', () => { + beforeEach(() => { + vi.clearAllMocks() + mocks.refreshDebugConversation.mockResolvedValue({ + debug_conversation_id: 'debug-conversation-new', + }) + }) + + it('should refresh debug conversation before clearing preview chat', async () => { + const onRestart = vi.fn() + const { queryClient } = renderHeader({ onRestart }) + + fireEvent.click(screen.getByRole('button', { name: 'agentV2.agentDetail.configure.preview.restart' })) + + await waitFor(() => expect(mocks.refreshDebugConversation).toHaveBeenCalledWith({ + params: { + agent_id: 'agent-1', + }, + }, expect.any(Object))) + expect(queryClient.getQueryData(['agent'])).toEqual(expect.objectContaining({ + debug_conversation_id: 'debug-conversation-new', + })) + expect(onRestart).toHaveBeenCalledTimes(1) + }) +})