Files
knowledge-governance/web/src/SourceCatalogBrowser.focus.test.tsx
T
姚章浩 5bf4aae30e
build / build (push) Successful in 1m42s
fix(ui): 接通取件后自动解析与一次性定位
F17:MarkArtifactFetched 与已验证内容复用都在同一事务内经过策略、来源启用、
过滤、superseded 和当前绑定的复核后,自动把 planned 转 pending 并入队唯一
test.upload;确认与作业幂等,失败整笔回滚。356 已卡记录可对同一文件再次保存
当前策略,经同一路径恢复,不做全库补扫或手工补队列。来源页提示改为自动接续。

F14:来源→监控定位清空未结束/动作/阶段/时间筛选;来源目录定位改为一次性请求
并由父消费,revision/sources 重载不再重放旧请求,手工换目录保持。

F13:抽出 readiness.Resolver 作为端点/绑定/凭据唯一来源;worker 上传/轮询/
校验/移除改用动态客户端,每次调用重读有效配置,不再固化启动 env 客户端。

验证:隔离 PG 的 F17 正负例与恢复用例、前端挂载定位回归、全量 Go/前端测试
及 make verify 17 项通过。未部署、未做 8088 UI 验收。
2026-11-05 11:46:42 +08:00

156 lines
5.7 KiB
TypeScript

// @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(<SourceCatalogBrowser focus={focus} onFocusApplied={applied} />))
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(<SourceCatalogBrowser focus={focus} onFocusApplied={applied} />))
await settle()
expect(requested.at(-1)).toBe('folder-2')
// 新的显式定位请求必须仍然有效。
const next = { connectionID: 'conn-atom', nodeID: 'folder-1' }
await act(async () => root.render(<SourceCatalogBrowser focus={next} onFocusApplied={applied} />))
await settle()
expect(requested.at(-1)).toBe('folder-1')
expect(applied).toHaveBeenCalledTimes(2)
})
})