|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 4 | +import { createRoot } from 'react-dom/client' |
| 5 | +import { expect, it, vi } from 'vitest' |
| 6 | +import type { PersistedMessage } from '@/lib/mothership/chat/persisted-message' |
| 7 | +import { compactRetrievalCitations } from '@/lib/mothership/chat/retrieval-citations' |
| 8 | +import { SourcesResourceContent } from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/components/sources-resource-content' |
| 9 | +import { type MothershipChatHistory, mothershipChatKeys } from '@/hooks/queries/mothership-chats' |
| 10 | + |
| 11 | +it('renders cited cached evidence, follows the latest answer, and never reruns provider searches', async () => { |
| 12 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 13 | + const fetch = vi.fn() |
| 14 | + vi.stubGlobal('fetch', fetch) |
| 15 | + const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 16 | + const container = document.createElement('div') |
| 17 | + const root = createRoot(container) |
| 18 | + const citationId = `document:live:${'x'.repeat(700)}` |
| 19 | + const message: PersistedMessage = { |
| 20 | + id: 'persisted-answer', |
| 21 | + requestId: 'run-1', |
| 22 | + role: 'assistant', |
| 23 | + requestMode: 'assistant', |
| 24 | + timestamp: '2026-09-22T00:00:00Z', |
| 25 | + content: `Answer <source>{"id":"${citationId}"}</source>`, |
| 26 | + contentBlocks: [ |
| 27 | + { |
| 28 | + type: 'tool', |
| 29 | + toolCall: { |
| 30 | + id: 'search', |
| 31 | + name: 'search_workspace', |
| 32 | + state: 'success', |
| 33 | + result: { |
| 34 | + success: true, |
| 35 | + output: compactRetrievalCitations('search_workspace', { |
| 36 | + success: true, |
| 37 | + data: { |
| 38 | + results: [ |
| 39 | + { |
| 40 | + citationId, |
| 41 | + citationUrl: 'https://sim.slack.com/archives/G1/p123', |
| 42 | + documentName: 'Group DM · Sid, Waleed', |
| 43 | + connectorType: 'slack', |
| 44 | + author: 'Vikhyath Mondreti', |
| 45 | + content: '@Vikhyath Mondreti Please review the release notes.', |
| 46 | + }, |
| 47 | + { |
| 48 | + citationId: 'unused', |
| 49 | + citationUrl: 'https://unused.test', |
| 50 | + documentName: 'Unused result', |
| 51 | + }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + }), |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + ], |
| 59 | + } |
| 60 | + client.setQueryData<MothershipChatHistory>(mothershipChatKeys.detail('chat'), { |
| 61 | + id: 'chat', |
| 62 | + title: 'Chat', |
| 63 | + messages: [ |
| 64 | + message, |
| 65 | + { |
| 66 | + ...message, |
| 67 | + id: 'second-answer', |
| 68 | + requestId: 'run-2', |
| 69 | + content: '<source>{"id":"unused"}</source>', |
| 70 | + }, |
| 71 | + ], |
| 72 | + activeStreamId: null, |
| 73 | + resources: [], |
| 74 | + }) |
| 75 | + const render = (requestId: string) => |
| 76 | + root.render( |
| 77 | + <QueryClientProvider client={client}> |
| 78 | + <SourcesResourceContent |
| 79 | + chatId='chat' |
| 80 | + resource={{ |
| 81 | + type: 'sources', |
| 82 | + id: 'cited-sources', |
| 83 | + title: 'Sources', |
| 84 | + sources: { messageId: 'optimistic-answer', requestId }, |
| 85 | + }} |
| 86 | + /> |
| 87 | + </QueryClientProvider> |
| 88 | + ) |
| 89 | + try { |
| 90 | + await act(async () => render('run-1')) |
| 91 | + expect(container.textContent).toContain('Sources · 1') |
| 92 | + expect(container.textContent).toContain('Group DM · Sid, Waleed') |
| 93 | + expect(container.textContent).not.toContain('Unused result') |
| 94 | + expect(container.querySelector('a')?.href).toBe('https://sim.slack.com/archives/G1/p123') |
| 95 | + await act(async () => render('run-2')) |
| 96 | + expect(container.textContent).toContain('Unused result') |
| 97 | + expect(container.textContent).not.toContain('Group DM') |
| 98 | + expect(fetch).not.toHaveBeenCalled() |
| 99 | + } finally { |
| 100 | + await act(async () => root.unmount()) |
| 101 | + client.clear() |
| 102 | + vi.unstubAllGlobals() |
| 103 | + } |
| 104 | +}) |
0 commit comments