// @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 { SourceCatalogBrowser } from './SourceCatalogBrowser' import type { CatalogNode, CatalogSource } from './sourceCatalog' // F14:定位是“一次性导航请求”。旧请求不得在目录刷新/来源重载时重放, // 手工换目录后必须留在原地;新的显式请求仍要能定位。 const source: CatalogSource = { connection_id: 'conn-atom', source_type: 'dingtalk', name: 'ATOM知识库', root_node_id: 'root-1', folder_count: 2, file_count: 2, observed_at: '2026-11-05T00:00:00Z', snapshot_only: true, snapshot_status: 'ready', last_refresh_duration_ms: 0, } const node = (id: string, name: string, type: 'folder' | 'file', parent: string): CatalogNode => ({ node_id: id, parent_node_id: parent, node_type: type, name, direct_child_count: type === 'folder' ? 1 : 0, subtree_file_count: 1, subtree_bytes: 1024, size_bytes: type === 'file' ? 1024 : undefined, visibility: 'internal', imported_count: type === 'file' ? 1 : 0, parsed_count: type === 'file' ? 1 : 0, active_count: 0, failed_count: 0, routable_count: 1, }) const page = (parent: string) => { const crumbs = parent === 'root-1' ? [{ node_id: 'root-1', name: 'ATOM知识库' }] : [ { node_id: 'root-1', name: 'ATOM知识库' }, { node_id: parent, name: parent === 'folder-1' ? '售后机器出厂问题整改' : '常见问题汇总' }, ] return { current: node(parent, crumbs[crumbs.length - 1].name, 'folder', parent === 'root-1' ? '' : 'root-1'), breadcrumbs: crumbs, items: parent === 'root-1' ? [ node('folder-1', '售后机器出厂问题整改', 'folder', 'root-1'), node('folder-2', '常见问题汇总', 'folder', 'root-1'), ] : [node(parent === 'folder-1' ? 'file-1' : 'file-2', '人形常见问题.adoc', 'file', parent)], total: 1, limit: 50, offset: 0, } } let container: HTMLDivElement let root: Root let requested: string[] const json = (body: unknown) => new Response(JSON.stringify(body), { status: 200 }) 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) requested = [] vi.stubGlobal( 'fetch', vi.fn(async (input: RequestInfo | URL) => { const url = new URL(String(input), 'http://localhost') if (url.pathname === '/api/v1/source-catalog/sources') return json({ data: [source] }) if (url.pathname === '/api/v1/source-catalog/routing-capabilities') return json({ data: { internal_enabled: true, external_enabled: true } }) if (url.pathname === '/api/v1/source-catalog/nodes') { const parent = url.searchParams.get('parent_id') || '' requested.push(parent) return json(page(parent)) } if (url.pathname === '/api/v1/jobs') return json({ data: [] }) if (url.pathname === '/api/v1/source-catalog/route') return json({ data: { selected_files: 1, intents_recorded: 1, fetch_queued: 1, planned_publications: 1, blocked_items: 0, unbound_files: 0, }, }) return new Response('{}', { status: 404 }) }) ) }) afterEach(() => { act(() => root.unmount()) container.remove() vi.unstubAllGlobals() }) const settle = async () => { await act(async () => { await Promise.resolve() await Promise.resolve() }) } describe('one-shot source location', () => { it('keeps the manually chosen directory when the old request is re-rendered', async () => { const focus = { connectionID: 'conn-atom', nodeID: 'folder-1' } const applied = vi.fn() await act(async () => root.render()) await settle() expect(applied).toHaveBeenCalledTimes(1) expect(requested.at(-1)).toBe('folder-1') // 用户手工返回根目录并进入另一个目录(不经过新旧定位请求)。 const back = [...container.querySelectorAll('button')].find((b) => b.getAttribute('aria-label') === '返回上一级') await act(async () => back?.click()) await settle() const other = [...container.querySelectorAll('button')].find((b) => b.textContent?.includes('常见问题汇总')) await act(async () => other?.click()) await settle() expect(requested.at(-1)).toBe('folder-2') // 在当前目录保存一次策略:revision 会让 sources 重新加载,旧 focus 不得重放。 const checkbox = container.querySelector('input[aria-label="选择人形常见问题.adoc"]') as HTMLInputElement | null await act(async () => checkbox?.click()) const route = [...container.querySelectorAll('button')].find((b) => b.textContent === '对内') await act(async () => route?.click()) await settle() expect(requested.at(-1)).toBe('folder-2') // 同一旧请求对象因父重渲染再次传入(模拟 revision/sources 刷新):不得拽回 folder-1。 await act(async () => root.render()) await settle() expect(requested.at(-1)).toBe('folder-2') // 新的显式定位请求必须仍然有效。 const next = { connectionID: 'conn-atom', nodeID: 'folder-1' } await act(async () => root.render()) await settle() expect(requested.at(-1)).toBe('folder-1') expect(applied).toHaveBeenCalledTimes(2) }) })