Files
knowledge-governance/web/src/SourceCatalogBrowser.test.tsx
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

536 lines
18 KiB
TypeScript

import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import { SourceCatalogBrowserView } from './SourceCatalogBrowser'
import type { CatalogNode, CatalogNodePage, CatalogSource } from './sourceCatalog'
import type { Job } from './jobs'
const source: CatalogSource = {
connection_id: 'conn-1',
source_type: 'dingtalk',
name: '公司级技术支持知识库',
root_node_id: 'root-1',
folder_count: 1080,
file_count: 3468,
observed_at: '2026-09-08T00:00:00Z',
snapshot_only: true,
snapshot_status: 'ready',
last_refresh_duration_ms: 12000,
}
const node = (name: string, type: 'folder' | 'file', id: string) => ({
node_id: id,
parent_node_id: 'root-1',
node_type: type,
name,
direct_child_count: type === 'folder' ? 2 : 0,
subtree_file_count: type === 'folder' ? 10 : 1,
subtree_bytes: 1024,
size_bytes: type === 'file' ? 1024 : undefined,
visibility: type === 'folder' ? ('internal' as const) : ('unknown' as const),
imported_count: type === 'folder' ? 8 : 0,
parsed_count: type === 'folder' ? 7 : 0,
active_count: 1,
failed_count: 0,
routable_count: type === 'file' ? 1 : 8,
})
const page: CatalogNodePage = {
current: { ...node('公司级技术支持知识库', 'folder', 'root-1'), parent_node_id: undefined },
breadcrumbs: [{ node_id: 'root-1', name: '公司级技术支持知识库' }],
items: [node('01|产品总览与定位', 'folder', 'folder-1'), node('根目录说明.pdf', 'file', 'file-1')],
total: 2,
limit: 50,
offset: 0,
}
describe('SourceCatalogBrowserView', () => {
it('shows a source that is still doing its first scan, with progress', () => {
const scanning: CatalogSource = {
...source,
connection_id: 'conn-scan',
name: '公司级营销资料管理',
root_node_id: '',
folder_count: 1153,
file_count: 1428,
snapshot_status: 'scanning',
scan_progress: 36,
scan_message: '正在扫描目录:已完成 470 个文件夹,待处理 683 个',
last_refresh_duration_ms: 0,
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[scanning, source]}
selectedSource={scanning}
page={null}
loading={false}
error={null}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
// 新来源不必等快照落地就出现在列表里,并且带百分比。
for (const text of [
'公司级营销资料管理',
'首次扫描中 36%',
'首次扫描中 · 36%',
'正在扫描目录:已完成 470 个文件夹,待处理 683 个',
'已发现约 1428 份文件',
])
expect(html).toContain(text)
// 还没有目录:不渲染空目录、也不提供标注/格式筛选入口。
expect(html).not.toContain('当前目录为空。')
expect(html).not.toContain('禁止导入')
})
it('uses a Windows-style path bar instead of a directory tree', () => {
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('aria-label="当前目录路径"')
expect(html).toContain('aria-label="目录面包屑"')
expect(html).toContain('aria-label="返回上一级"')
expect(html).not.toContain('role="tree"')
expect(html).not.toContain('目录树')
for (const text of ['禁止导入即生效', '<th>大小</th>', '大小未提供', '总大小', '已知合计'])
expect(html).not.toContain(text)
expect(html).toContain('<details class="catalog-format-filter">')
expect(html).toContain('<summary>文件格式筛选</summary>')
expect(html).not.toMatch(/<details[^>]*class="catalog-format-filter"[^>]*open/)
})
it('looks like a folder browser and shows only direct children', () => {
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
for (const text of [
'各个知识源头的资料总览与标注',
'公司级技术支持知识库',
'01|产品总览与定位',
'文件夹',
'已解析 7 / 已导入 8',
'本地只读快照',
'含所有子目录文件',
])
expect(html).toContain(text)
expect(html).not.toContain('真实目录')
expect(html).toContain('根目录说明.pdf')
expect(html).not.toContain('显示根目录文件')
expect(html).toContain('aria-label="当前目录路径"')
expect(html).not.toContain('第 1 / 1 页')
expect(html).not.toContain('PP分类 · 不代表完整原始目录')
expect(html).toContain('快照时间')
expect(html).toContain('快照就绪')
expect(html).toContain('上次刷新耗时 12秒')
expect(html).toContain('实时刷新目录')
expect(html).not.toContain('aria-label="处理策略01|产品总览与定位"')
expect(html).not.toContain('递归作用于全部子目录')
})
it('shows current directory, discovered file counts and the estimate marker during refresh', () => {
const job: Job = {
id: 'job-scan',
type: 'source.catalog.refresh',
payload: {
connection_id: 'conn-1',
stage: 'scanning',
progress: 43,
message: '正在扫描目录:已完成 125 个文件夹,待处理 55 个',
processed: 125,
total: 180,
current_directory: '产品资料/CR系列',
scanned_files: 2438,
estimated_total_files: 5662,
total_is_estimate: true,
},
state: 'running',
priority: 20,
available_at: '2026-09-09T00:00:00Z',
attempt: 1,
max_attempts: 3,
idempotency_key: 'safe',
created_at: '2026-09-09T00:00:00Z',
updated_at: '2026-09-09T00:00:00Z',
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
refreshJob={job}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('当前目录:产品资料/CR系列')
expect(html).toContain('已发现 2438 份文件')
expect(html).toContain('(估算)')
})
it('hides the running refresh to a compact pill that expands back on click without blocking browsing', () => {
const job: Job = {
id: 'job-hide',
type: 'source.catalog.refresh',
payload: {
connection_id: 'conn-1',
stage: 'scanning',
progress: 60,
message: '正在扫描目录:已完成 1000 个文件夹',
processed: 1000,
total: 1339,
current_directory: '产品资料/CR系列',
scanned_files: 2438,
},
state: 'running',
priority: 20,
available_at: '2026-09-09T00:00:00Z',
attempt: 1,
max_attempts: 3,
idempotency_key: 'safe',
created_at: '2026-09-09T00:00:00Z',
updated_at: '2026-09-09T00:00:00Z',
}
const common = {
sources: [source],
selectedSource: source,
page,
loading: false,
error: null,
refreshJob: job,
onSelectSource: vi.fn(),
onOpenFolder: vi.fn(),
onNavigateTo: vi.fn(),
onBack: vi.fn(),
onPageChange: vi.fn(),
onRefresh: vi.fn(),
}
const full = renderToStaticMarkup(
<SourceCatalogBrowserView {...common} refreshHidden={false} onToggleRefreshHidden={vi.fn()} />
)
expect(full).toContain('隐藏到后台')
expect(full).toContain('实时快照进度')
expect(full).toContain('产品资料/CR系列')
const hidden = renderToStaticMarkup(
<SourceCatalogBrowserView {...common} refreshHidden onToggleRefreshHidden={vi.fn()} />
)
expect(hidden).not.toContain('实时快照进度')
expect(hidden).toContain('catalog-refresh-pill')
expect(hidden).toContain('60%')
expect(hidden).toContain('公司级技术支持知识库') // pill still names the source
// browsing and annotation stay interactive in hidden mode (only a duplicate refresh is blocked)
expect(hidden).toContain('aria-label="选择01|产品总览与定位"')
expect(hidden).toContain('aria-label="选择根目录说明.pdf"')
expect(hidden).toContain('全选当前页')
expect(hidden).toContain('实时刷新中…')
})
it('announces failure safely and keeps old snapshot usable', () => {
const job: Job = {
id: 'job-fail',
type: 'source.catalog.refresh',
payload: {
connection_id: 'conn-1',
stage: 'failed',
progress: 0,
message: 'AList API Token 未配置,请联系管理员更新来源凭据;旧快照仍然保留',
processed: 0,
total: 0,
},
state: 'failed',
priority: 20,
available_at: '2026-09-09T00:00:00Z',
attempt: 3,
max_attempts: 3,
idempotency_key: 'safe',
created_at: '2026-09-09T00:00:00Z',
updated_at: '2026-09-09T00:00:00Z',
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
refreshJob={job}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('实时刷新失败')
expect(html).toContain('刷新失败;旧快照仍然保留,目录浏览与标注不受影响。')
expect(html).toContain('快照就绪')
expect(html).toContain('01|产品总览与定位')
})
it('keeps the progress bar free of directory details when the scanner reports none', () => {
const job: Job = {
id: 'job-plain',
type: 'source.catalog.refresh',
payload: {
connection_id: 'conn-1',
stage: 'building',
progress: 80,
message: '正在校验目录结构',
processed: 10,
total: 10,
},
state: 'running',
priority: 20,
available_at: '2026-09-09T00:00:00Z',
attempt: 1,
max_attempts: 3,
idempotency_key: 'safe',
created_at: '2026-09-09T00:00:00Z',
updated_at: '2026-09-09T00:00:00Z',
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
refreshJob={job}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('正在校验目录结构')
expect(html).not.toContain('刷新明细')
})
it('uses durable background refresh progress and the current stage message', () => {
const job: Job = {
id: 'job-refresh',
type: 'source.catalog.refresh',
payload: {
connection_id: 'conn-1',
stage: 'scanning',
progress: 50,
message: '正在扫描目录:已完成 50 个',
processed: 50,
total: 100,
},
state: 'running',
priority: 20,
available_at: '2026-09-09T00:00:00Z',
attempt: 1,
max_attempts: 3,
idempotency_key: 'safe',
created_at: '2026-09-09T00:00:00Z',
updated_at: '2026-09-09T00:00:00Z',
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
refreshJob={job}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('实时刷新中')
expect(html).toContain('正在扫描目录:已完成 50 个')
expect(html).toContain('value="50"')
})
it('uses clickable breadcrumbs and an explicit back action below the source root', () => {
const nested = {
...page,
current: { ...page.current, node_id: 'folder-1', parent_node_id: 'root-1', name: '01|产品总览与定位' },
breadcrumbs: [...page.breadcrumbs, { node_id: 'folder-1', name: '01|产品总览与定位' }],
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={nested}
loading={false}
error={null}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('aria-label="返回上一级"')
expect(html).toContain('公司级技术支持知识库')
expect(html).toContain('01|产品总览与定位')
})
it('shows policy as a read-only table state and offers top-level policy buttons', () => {
const nested = {
...page,
current: { ...page.current, node_id: 'folder-1', parent_node_id: 'root-1', name: '01|产品总览与定位' },
breadcrumbs: [...page.breadcrumbs, { node_id: 'folder-1', name: '01|产品总览与定位' }],
items: [
node('可入队.pdf', 'file', 'file-ready'),
node('已知目录', 'folder', 'folder-known'),
{ ...node('大小未知目录', 'folder', 'folder-unknown'), subtree_bytes: 0 },
{ ...node('只有元数据.pdf', 'file', 'file-meta'), size_bytes: undefined, routable_count: 0 },
],
total: 4,
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={nested}
loading={false}
error={null}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
onRoute={vi.fn()}
/>
)
for (const text of ['名称', '处理策略', '全选当前页', '对内', '对外', '禁止导入', 'aria-label="选择可入队.pdf"'])
expect(html).toContain(text)
for (const text of [
'class="catalog-policy-actions"',
'class="internal-policy-button"',
'class="external-policy-button"',
'class="blocked-policy-button"',
'class="selected-count"',
])
expect(html).toContain(text)
for (const text of [
'操作人',
'判断原因',
'名称与处理策略',
'当前策略',
'选择策略',
'批量处理策略',
'应用策略',
'对内 + 对外',
'<select',
])
expect(html).not.toContain(text)
expect(html).not.toContain('批量标注对内并入队')
expect(html).not.toContain('已请求移除')
expect(html).not.toContain('新增待确认计划')
expect(html).not.toContain('批量标注对外并入队')
expect(html).not.toContain('递归全选当前目录')
})
it('freezes the S6 batch annotation contract: exactly three policy buttons and current-page selection only', () => {
const nested = {
...page,
current: { ...page.current, node_id: 'folder-1' },
breadcrumbs: [...page.breadcrumbs, { node_id: 'folder-1', name: '01|产品总览与定位' }],
items: [node('可入队.pdf', 'file', 'file-ready'), node('已知目录', 'folder', 'folder-known')],
total: 2,
}
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={nested}
loading={false}
error={null}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
onRoute={vi.fn()}
/>
)
// exactly three top-level policy buttons: 对内 / 对外 / 禁止导入 (docs/SOURCE_SCOPE_POLICY.md rules 4-6)
expect((html.match(/class="[a-z-]+-policy-button"/g) || []).length).toBe(3)
// rule 2: only current-page select-all exists
expect(html).toContain('全选当前页')
expect(html).not.toContain('递归全选')
// rule 1: both file and folder rows expose selection checkboxes
expect(html).toContain('aria-label="选择可入队.pdf"')
expect(html).toContain('aria-label="选择已知目录"')
// rules 5-6: no batch entry for historical 对内 + 对外 or for unset state
expect(html).not.toContain('对内 + 对外')
expect(html).not.toContain('批量未选择')
})
it('lets an engineer select a directory without opening it', () => {
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[source]}
selectedSource={source}
page={page}
loading={false}
error={null}
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('aria-label="选择01|产品总览与定位"')
})
it('does not claim an empty source loaded when the request failed', () => {
const html = renderToStaticMarkup(
<SourceCatalogBrowserView
sources={[]}
selectedSource={null}
page={null}
loading={false}
error="加载失败"
onSelectSource={vi.fn()}
onOpenFolder={vi.fn()}
onNavigateTo={vi.fn()}
onBack={vi.fn()}
onPageChange={vi.fn()}
onRefresh={vi.fn()}
/>
)
expect(html).toContain('加载失败')
expect(html).not.toContain('尚无来源目录')
})
})