import { fireEvent, render, screen, waitFor } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { beforeEach, describe, expect, it, vi } from 'vitest' const requestImage = vi.fn() vi.mock('../../src/renderer/src/components/image-loader', () => ({ getCachedLoadedImage: vi.fn(() => undefined), requestImage: (...args: unknown[]) => requestImage(...args) })) import { ImageBubble } from '../../src/renderer/src/components/ImageBubble' import { RichMessageBubble } from '../../src/renderer/src/components/RichMessageBubble' const thumbnail = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9ZQmcAAAAASUVORK5CYII=' const original = `${thumbnail}original` describe('ImageBubble', () => { beforeEach(() => { requestImage.mockReset() window.api = { copyImage: vi.fn().mockResolvedValue({ success: true }) } as typeof window.api }) it('loads a thumbnail lazily, then requests the original when opened', async () => { requestImage .mockResolvedValueOnce({ data: thumbnail, isThumbnail: true }) .mockResolvedValueOnce({ data: original, isThumbnail: false }) const onImageClick = vi.fn() render( ) const image = await screen.findByAltText('图片') expect(image).toHaveAttribute('src', thumbnail) await userEvent.click(image) await waitFor(() => expect(onImageClick).toHaveBeenCalledWith(original)) expect(requestImage.mock.calls[1][3]).toMatchObject({ force: true }) onImageClick.mockClear() requestImage.mockResolvedValueOnce({ data: original, isThumbnail: false }) const trigger = screen.getByRole('button', { name: '查看图片' }) trigger.focus() await userEvent.keyboard('{Enter}') await waitFor(() => expect(onImageClick).toHaveBeenCalledWith(original)) }) it('shows an explicit error and allows retry', async () => { requestImage.mockRejectedValueOnce(new Error('不支持的 DAT 版本')) render() expect(await screen.findByText('不支持的 DAT 版本')).toBeVisible() requestImage.mockResolvedValueOnce({ data: thumbnail, isThumbnail: true }) await userEvent.click(screen.getByText('加载失败')) expect(await screen.findByAltText('图片')).toBeVisible() }) it('copies the raw message xml when right clicked', async () => { const writeText = vi.fn().mockResolvedValue(undefined) Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }) const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => undefined) const getImageMessageXml = vi .fn() .mockResolvedValue({ success: true, xml: '' }) window.api = { copyImage: vi.fn(), getImageMessageXml } as unknown as typeof window.api requestImage.mockResolvedValueOnce({ data: thumbnail, isThumbnail: true }) render( ) const image = await screen.findByAltText('图片') fireEvent.contextMenu(image) await waitFor(() => expect(writeText).toHaveBeenCalledWith('') ) expect(getImageMessageXml).toHaveBeenCalledWith({ sessionId: 'fixture-session', localId: 42, serverId: undefined, createTime: 1700000000 }) expect(alertSpy).toHaveBeenCalledWith('图片消息 XML 已复制') alertSpy.mockRestore() }) it('surfaces a failure instead of copying anything when the xml is unavailable', async () => { const writeText = vi.fn().mockResolvedValue(undefined) Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }) const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => undefined) window.api = { copyImage: vi.fn(), getImageMessageXml: vi.fn().mockResolvedValue({ success: false, error: '数据库尚未连接' }) } as unknown as typeof window.api requestImage.mockResolvedValueOnce({ data: thumbnail, isThumbnail: true }) render() fireEvent.contextMenu(await screen.findByAltText('图片')) await waitFor(() => expect(alertSpy).toHaveBeenCalledWith('XML 获取失败:数据库尚未连接')) expect(writeText).not.toHaveBeenCalled() alertSpy.mockRestore() }) it('places a quoted image on the line below the quoted sender', async () => { requestImage.mockResolvedValueOnce({ data: thumbnail, isThumbnail: true }) const { container } = render( ) expect(screen.getByText('测试群成员')).toBeInTheDocument() expect(container.querySelector('.quoted-message')).toHaveClass('quoted-message-image') expect(await screen.findByAltText('图片')).toBeVisible() }) }) describe('RichMessageBubble', () => { it('renders every article in a public-account bundle', async () => { const open = vi.spyOn(window, 'open').mockImplementation(() => null) render( ) expect(screen.getByText('Fixture Publisher')).toBeVisible() expect(screen.getByRole('button', { name: 'Article one' })).toBeVisible() expect(screen.getByRole('button', { name: 'Article two' })).toBeVisible() expect(screen.getByRole('button', { name: 'Article three' })).toBeVisible() await userEvent.click(screen.getByRole('button', { name: 'Article three' })) expect(open).toHaveBeenCalledWith('https://example.test/three', '_blank') open.mockRestore() }) })