/* @vitest-environment jsdom */ import { fireEvent, render, screen, waitFor, within, } from '@testing-library/react'; import { afterEach, describe, expect, test, vi } from 'vitest'; import * as clipboardService from '../../services/clipboard'; import { PlatformErrorDialog } from './PlatformErrorDialog'; import { PlatformTaskCompletionDialog } from './PlatformTaskCompletionDialog'; vi.mock('../../services/clipboard', () => ({ copyTextToClipboard: vi.fn(), })); afterEach(() => { vi.clearAllMocks(); }); describe('PlatformErrorDialog', () => { test('shows source, message, and copies the full error report', async () => { vi.mocked(clipboardService.copyTextToClipboard).mockResolvedValue(true); render( {}} />, ); const dialog = screen.getByRole('dialog', { name: '发生错误' }); expect(within(dialog).getByText('拼图草稿 puzzle-session-123')).toBeTruthy(); expect(within(dialog).getByText('图片生成失败,请稍后再试。')).toBeTruthy(); fireEvent.click(within(dialog).getByRole('button', { name: '复制报错' })); expect(clipboardService.copyTextToClipboard).toHaveBeenCalledWith( ['来源:拼图草稿 puzzle-session-123', '错误:图片生成失败,请稍后再试。'].join( '\n', ), ); await waitFor(() => { expect( within(dialog).getByRole('button', { name: '已复制' }), ).toBeTruthy(); }); }); test('does not render when there is no active error', () => { render( {}} />); expect(screen.queryByRole('dialog', { name: '发生错误' })).toBeNull(); }); test('does not render creation entry disabled errors', () => { render( {}} />, ); expect(screen.queryByRole('dialog', { name: '发生错误' })).toBeNull(); expect(screen.queryByText(/creation_entry_disabled/u)).toBeNull(); }); }); describe('PlatformTaskCompletionDialog', () => { test('shows source, message, and copies the full completion report', async () => { vi.mocked(clipboardService.copyTextToClipboard).mockResolvedValue(true); render( {}} />, ); const dialog = screen.getByRole('dialog', { name: '生成完成' }); expect( within(dialog).getByText('抓大鹅草稿 match3d-notice-session-1'), ).toBeTruthy(); expect( within(dialog).getByText('生成任务已完成,可以继续查看草稿。'), ).toBeTruthy(); fireEvent.click(within(dialog).getByRole('button', { name: '复制内容' })); expect(clipboardService.copyTextToClipboard).toHaveBeenCalledWith( [ '来源:抓大鹅草稿 match3d-notice-session-1', '状态:生成任务已完成,可以继续查看草稿。', ].join('\n'), ); await waitFor(() => { expect( within(dialog).getByRole('button', { name: '已复制' }), ).toBeTruthy(); }); }); test('does not render when there is no active completion', () => { render( {}} />, ); expect(screen.queryByRole('dialog', { name: '生成完成' })).toBeNull(); }); });