// @vitest-environment jsdom import { act } from 'react' import { createRoot } from 'react-dom/client' import type { Root } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { TaskDetail } from './WeKnoraMonitor' import type { MonitorTask } from './monitorTasks' const item: MonitorTask = { id: 'tp-1', title: '维护手册.pdf', path: '产品/维护', node_id: 'node-1', parent_node_id: 'folder-1', source_id: 's1', source_name: '技术资料', target_scope: 'internal', target_kb: 'kb-1', document_id: 'doc-1', weknora_url: 'https://weknora.test/platform/knowledge-bases/kb-1', action: 'update', stage: 'parsing', status: 'running', detail: '正在解析处理', updated_at: '2026-12-01T02:00:00Z', retry_kind: '', failure_id: '', retry_count: 0, version_id: 'v1', } let container: HTMLDivElement let root: Root beforeEach(() => { // React's test-only global; the test environment exposes it without a type. const reactTestGlobal: { IS_REACT_ACT_ENVIRONMENT: boolean } = globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean } reactTestGlobal.IS_REACT_ACT_ENVIRONMENT = true // jsdom does not implement the dialog methods the drawer calls on mount. HTMLDialogElement.prototype.showModal = function showModal(this: HTMLDialogElement) { this.open = true } container = document.createElement('div') document.body.appendChild(container) root = createRoot(container) }) afterEach(() => { act(() => root.unmount()) container.remove() vi.unstubAllGlobals() }) describe('platform task drawer', () => { it('offers the WeKnora jump link when progress answers 503', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( new Response(JSON.stringify({ error: 'runtime_read_unavailable', code: 'runtime_read_unavailable' }), { status: 503, }) ) ) await act(async () => root.render()) await act(async () => await Promise.resolve()) expect(container.textContent).toContain('未配置 WeKnora 只读 Key') expect(container.querySelector('a[href="https://weknora.test/platform/knowledge-bases/kb-1"]')).not.toBeNull() }) })