45 lines
1.9 KiB
TypeScript
45 lines
1.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react'
|
|
import { AttachmentId } from '@deepseek-ai/dsh-attachment'
|
|
import { MessageImage } from '../src/client/chat/MessageImage.tsx'
|
|
|
|
afterEach(cleanup)
|
|
|
|
const attachment = {
|
|
attachmentId: AttachmentId(`sha256:${'a'.repeat(64)}`),
|
|
mediaType: 'image/png' as const,
|
|
bytes: 68,
|
|
width: 640,
|
|
height: 320,
|
|
name: 'history.png',
|
|
}
|
|
|
|
describe('MessageImage', () => {
|
|
it('loads a session-authorized URL, bounds the thumbnail, and double-clicks into the original', async () => {
|
|
const load = vi.fn().mockResolvedValue('blob:history')
|
|
const view = render(<MessageImage attachment={attachment} load={load} />)
|
|
const frame = view.getByRole('button', { name: 'history.png,双击查看原图' })
|
|
expect(frame.getAttribute('style')).toContain('width: 240px')
|
|
expect(frame.getAttribute('style')).toContain('height: 120px')
|
|
await waitFor(() => { expect(view.getByAltText('history.png')).toBeTruthy() })
|
|
expect(load).toHaveBeenCalledWith(attachment)
|
|
fireEvent.doubleClick(frame)
|
|
expect(view.getByRole('dialog', { name: '原图预览' })).toBeTruthy()
|
|
fireEvent.click(view.getByRole('button', { name: '关闭原图预览' }))
|
|
expect(view.queryByRole('dialog', { name: '原图预览' })).toBeNull()
|
|
})
|
|
|
|
it('surfaces a retry control when durable bytes cannot be read', async () => {
|
|
const load = vi.fn()
|
|
.mockRejectedValueOnce(new Error('offline'))
|
|
.mockResolvedValueOnce('blob:retry')
|
|
const view = render(<MessageImage attachment={attachment} load={load} />)
|
|
const retry = await view.findByRole('button', { name: '图片加载失败,点击重试' })
|
|
fireEvent.click(retry)
|
|
await waitFor(() => { expect(view.getByAltText('history.png')).toBeTruthy() })
|
|
expect(load).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|