// @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 { WeKnoraProgress } from './ReconciliationFindings'
let container: HTMLDivElement
let root: Root
beforeEach(() => {
;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
})
afterEach(() => {
act(() => root.unmount())
container.remove()
vi.unstubAllGlobals()
})
describe('WeKnora progress drawer block', () => {
it('degrades to a read-only key message on 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')
})
it('shows stages and failures when available', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
data: {
document_id: 'doc-1',
parse_status: 'failed',
ingestion_status: 'failed',
wiki_status: 'none',
stages: [{ name: 'parse', status: 'failed', error_code: 'E1', finished_at: '2026-12-01T00:00:00Z' }],
failures: [
{ task_type: 'parse', fail_count: 2, failed_at: '2026-12-01T00:00:00Z', last_error: '解析失败' },
],
has_more_failures: false,
observed_at: '2026-12-01T00:00:00Z',
},
}),
{ status: 200 }
)
)
)
await act(async () => root.render())
await act(async () => await Promise.resolve())
expect(container.textContent).toContain('parse')
expect(container.textContent).toContain('解析失败')
expect(container.textContent).toContain('failed')
})
})