|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import { createRoot, type Root } from 'react-dom/client' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('@/lib/auth/auth-client', () => ({ useSession: () => ({ data: null, isPending: false }) })) |
| 7 | +vi.mock('@/app/workspace/[workspaceId]/home/components/chat-surface-context', () => ({ |
| 8 | + useChatSurface: () => ({ chatId: 'chat' }), |
| 9 | +})) |
| 10 | +vi.mock('@/hooks/use-smooth-text', () => ({ useSmoothText: (text: string) => text })) |
| 11 | + |
| 12 | +import { |
| 13 | + inlineChatImageUrl, |
| 14 | + isInlineFileReference, |
| 15 | + normalizeInlineFileReference, |
| 16 | +} from '@/lib/mothership/chat/inline-image-reference' |
| 17 | +import { collectMarkdownImageSources } from '@/lib/mothership/chat/markdown-images' |
| 18 | +import { ChatContent } from '@/app/workspace/[workspaceId]/home/components/message-content/components/chat-content/chat-content' |
| 19 | + |
| 20 | +let root: Root |
| 21 | +let container: HTMLDivElement |
| 22 | +beforeEach(() => { |
| 23 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 24 | + vi.stubGlobal( |
| 25 | + 'matchMedia', |
| 26 | + vi |
| 27 | + .fn() |
| 28 | + .mockReturnValue({ matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() }) |
| 29 | + ) |
| 30 | + container = document.createElement('div') |
| 31 | + document.body.appendChild(container) |
| 32 | + root = createRoot(container) |
| 33 | +}) |
| 34 | +afterEach(() => { |
| 35 | + act(() => root.unmount()) |
| 36 | + container.remove() |
| 37 | + vi.unstubAllGlobals() |
| 38 | +}) |
| 39 | +async function render( |
| 40 | + content: string, |
| 41 | + imageRequestId: string | undefined = 'request', |
| 42 | + isStreaming = false |
| 43 | +) { |
| 44 | + await act(async () => |
| 45 | + root.render( |
| 46 | + <ChatContent |
| 47 | + content={content} |
| 48 | + messageId='unstable-message-id' |
| 49 | + imageRequestId={imageRequestId} |
| 50 | + isStreaming={isStreaming} |
| 51 | + /> |
| 52 | + ) |
| 53 | + ) |
| 54 | +} |
| 55 | +describe('standard Markdown private chat images', () => { |
| 56 | + it.each([ |
| 57 | + 'files/chart.png', |
| 58 | + '/tmp/chart.png', |
| 59 | + 'uploads/chart.png', |
| 60 | + 'files/my image.png', |
| 61 | + 'files/échantillon.png', |
| 62 | + 'files/price%.png', |
| 63 | + 'files/literal%2520.png', |
| 64 | + 'files/chart(1).png', |
| 65 | + '11111111-1111-4111-8111-111111111111', |
| 66 | + 'wf_V1StGXR8z5jdHi6BmyT91', |
| 67 | + ])( |
| 68 | + 'renders %s through the canonical authenticated endpoint without changing source syntax', |
| 69 | + async (reference) => { |
| 70 | + const markdown = `` |
| 71 | + const astSource = collectMarkdownImageSources(markdown)[0] |
| 72 | + await render(markdown) |
| 73 | + const img = container.querySelector('img') |
| 74 | + expect(img?.getAttribute('src')).toBe(inlineChatImageUrl('chat', 'request', astSource)) |
| 75 | + expect(img?.getAttribute('alt')).toBe('Chart') |
| 76 | + const path = new URL(img!.getAttribute('src')!, 'http://localhost').searchParams.get('path')! |
| 77 | + expect(normalizeInlineFileReference(path)).toBe(normalizeInlineFileReference(astSource)) |
| 78 | + } |
| 79 | + ) |
| 80 | + it.each(['files/' + 'x'.repeat(2100), 'files/\uD800.png', 'files/\u0000.png'])( |
| 81 | + 'keeps prose when an image source is malformed', |
| 82 | + async (reference) => { |
| 83 | + await render(`Before  after.`) |
| 84 | + expect(container.textContent).toContain('Before') |
| 85 | + expect(container.textContent).toContain('after.') |
| 86 | + expect(isInlineFileReference(reference)).toBe(false) |
| 87 | + const src = container.querySelector('img')?.getAttribute('src') |
| 88 | + if (src?.startsWith('/api/mothership/chats/')) { |
| 89 | + // CommonMark may replace invalid Unicode before the renderer sees it. |
| 90 | + const path = new URL(src, 'http://localhost').searchParams.get('path')! |
| 91 | + expect(isInlineFileReference(path)).toBe(true) |
| 92 | + expect(normalizeInlineFileReference(path)).toBe(path) |
| 93 | + } |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + it('isolates cached Markdown processors across distinct assistant turns', async () => { |
| 98 | + await act(async () => |
| 99 | + root.render( |
| 100 | + <> |
| 101 | + <ChatContent content='' imageRequestId='first-turn' /> |
| 102 | + <ChatContent content='' imageRequestId='second-turn' /> |
| 103 | + </> |
| 104 | + ) |
| 105 | + ) |
| 106 | + expect( |
| 107 | + [...container.querySelectorAll('img')].map((image) => image.getAttribute('src')) |
| 108 | + ).toEqual([ |
| 109 | + inlineChatImageUrl('chat', 'first-turn', 'files/same.png'), |
| 110 | + inlineChatImageUrl('chat', 'second-turn', 'files/same.png'), |
| 111 | + ]) |
| 112 | + }) |
| 113 | + it('keeps existing remote image rendering', async () => { |
| 114 | + await render('') |
| 115 | + expect(container.querySelector('img')?.getAttribute('src')).toBe( |
| 116 | + 'https://example.com/chart.png' |
| 117 | + ) |
| 118 | + }) |
| 119 | + it('never displays image syntax inside fenced or inline code', async () => { |
| 120 | + await render('``\n\n```markdown\n\n```') |
| 121 | + expect(container.querySelector('img')).toBeNull() |
| 122 | + }) |
| 123 | + it('does not request an incomplete streaming destination', async () => { |
| 124 | + await render('Before  |
| 125 | + expect(container.querySelector('img')).toBeNull() |
| 126 | + await render('Before ', 'request', true) |
| 127 | + expect(container.querySelector('img')?.getAttribute('src')).toBe( |
| 128 | + inlineChatImageUrl('chat', 'request', 'files/chart.png') |
| 129 | + ) |
| 130 | + }) |
| 131 | + it('keeps the same snapshot URL when live and persisted message IDs differ', async () => { |
| 132 | + await render('') |
| 133 | + const src = container.querySelector('img')?.getAttribute('src') |
| 134 | + await act(async () => |
| 135 | + root.render( |
| 136 | + <ChatContent |
| 137 | + content='' |
| 138 | + messageId='new-persisted-id' |
| 139 | + imageRequestId='request' |
| 140 | + /> |
| 141 | + ) |
| 142 | + ) |
| 143 | + expect(container.querySelector('img')?.getAttribute('src')).toBe(src) |
| 144 | + }) |
| 145 | +}) |
0 commit comments