import { afterEach, describe, expect, it, vi } from 'vitest' import { fetchAllCatalogFolders, fetchCatalogNodes, fetchCatalogRoutingCapabilities, fetchCatalogSources, fetchLatestCatalogChangeSet, routeCatalogFiles, triggerCatalogRefresh, } from './sourceCatalog' afterEach(() => vi.unstubAllGlobals()) describe('source catalog API', () => { it('keeps a source that is still scanning, with its progress', async () => { const fetchMock = vi.fn().mockResolvedValue( new Response( JSON.stringify({ data: [ { connection_id: 'conn-scan', source_type: 'dingtalk', name: '公司级营销资料管理', root_node_id: '', folder_count: 1153, file_count: 1428, observed_at: '2026-10-10T08:51:44Z', snapshot_only: true, snapshot_status: 'scanning', last_refresh_duration_ms: 0, scan_progress: 36, scan_message: '正在扫描目录:已完成 470 个文件夹,待处理 683 个', }, { connection_id: 'conn-1', source_type: 'dingtalk', name: '公司级技术支持知识库', root_node_id: 'root-1', folder_count: 8, file_count: 3468, observed_at: '2026-09-08T00:00:00Z', snapshot_only: true, snapshot_status: 'ready', last_refresh_duration_ms: 12000, }, ], summary: { total: 2 }, }) ) ) vi.stubGlobal('fetch', fetchMock) const sources = await fetchCatalogSources() expect(sources).toHaveLength(2) expect(sources[0].snapshot_status).toBe('scanning') expect(sources[0].scan_progress).toBe(36) expect(sources[0].scan_message).toContain('470 个文件夹') expect(sources[1].snapshot_status).toBe('ready') expect(sources[1].scan_progress).toBeUndefined() }) it('rejects a scanning source without progress instead of rendering a fake 0%', async () => { const fetchMock = vi .fn() .mockResolvedValue( new Response( JSON.stringify({ data: [ { connection_id: 'conn-scan', source_type: 'dingtalk', name: 'X', root_node_id: '', folder_count: 0, file_count: 0, observed_at: '2026-10-10T08:51:44Z', snapshot_only: true, snapshot_status: 'scanning', last_refresh_duration_ms: 0, }, ], summary: { total: 1 }, }) ) ) vi.stubGlobal('fetch', fetchMock) await expect(fetchCatalogSources()).rejects.toThrow(/来源目录响应无效/) }) it('loads safe source roots', async () => { const fetchMock = vi .fn() .mockResolvedValue( new Response( JSON.stringify({ data: [ { connection_id: 'conn-1', source_type: 'dingtalk', name: '公司级技术支持知识库', root_node_id: 'root-1', folder_count: 8, file_count: 3468, observed_at: '2026-09-08T00:00:00Z', snapshot_only: true, snapshot_status: 'ready', last_refresh_duration_ms: 12000, secret: 'drop', }, ], summary: { total: 1 }, }) ) ) vi.stubGlobal('fetch', fetchMock) const sources = await fetchCatalogSources() expect(sources[0].name).toBe('公司级技术支持知识库') expect(sources[0].snapshot_status).toBe('ready') expect(sources[0].last_refresh_duration_ms).toBe(12000) expect(sources[0]).not.toHaveProperty('secret') expect(fetchMock).toHaveBeenCalledWith( '/api/v1/source-catalog/sources', expect.objectContaining({ headers: { Accept: 'application/json' } }) ) }) it('loads every page for a complete deduplicated folder tree level', async () => { const folderA = { node_id: 'folder-a', parent_node_id: 'root', node_type: 'folder', name: 'A', direct_child_count: 1, subtree_file_count: 1, subtree_bytes: 1, visibility: 'unknown', imported_count: 0, parsed_count: 0, active_count: 0, failed_count: 0, routable_count: 0, } const file = { ...folderA, node_id: 'file-a', node_type: 'file', name: 'a.pdf' } const folderB = { ...folderA, node_id: 'folder-b', name: 'B' } const response = (items: unknown[], offset: number) => new Response( JSON.stringify({ current: { ...folderA, node_id: 'root', name: 'Root' }, breadcrumbs: [{ node_id: 'root', name: 'Root' }], items, total: 3, limit: 2, offset, }) ) const fetchMock = vi .fn() .mockResolvedValueOnce(response([folderA, file], 0)) .mockResolvedValueOnce(response([folderB, folderA], 2)) vi.stubGlobal('fetch', fetchMock) await expect(fetchAllCatalogFolders('conn-1', 'root')).resolves.toEqual([folderA, folderB]) expect(fetchMock.mock.calls.map((call) => call[0])).toEqual([ '/api/v1/source-catalog/nodes?connection_id=conn-1&parent_id=root&limit=100&offset=0', '/api/v1/source-catalog/nodes?connection_id=conn-1&parent_id=root&limit=100&offset=2', ]) }) it('requests only one parent page at a time', async () => { const response = { current: { node_id: 'root-1', node_type: 'folder', name: '公司级技术支持知识库', direct_child_count: 1, subtree_file_count: 10, subtree_bytes: 20, visibility: 'unknown', imported_count: 0, parsed_count: 0, active_count: 0, failed_count: 0, routable_count: 0, }, breadcrumbs: [{ node_id: 'root-1', name: '公司级技术支持知识库' }], items: [ { node_id: 'folder-1', parent_node_id: 'root-1', node_type: 'folder', name: '01|产品资料', direct_child_count: 2, subtree_file_count: 10, subtree_bytes: 20, visibility: 'internal', imported_count: 8, parsed_count: 7, active_count: 1, failed_count: 0, routable_count: 8, }, ], total: 1, limit: 50, offset: 0, } const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify(response))) vi.stubGlobal('fetch', fetchMock) const page = await fetchCatalogNodes('conn-1', 'root-1', 50, 0) expect(page.items).toHaveLength(1) expect(fetchMock.mock.calls[0][0]).toBe( '/api/v1/source-catalog/nodes?connection_id=conn-1&parent_id=root-1&limit=50&offset=0' ) expect(JSON.stringify(page)).not.toContain('relative_path') }) it('routes selected files without exposing target knowledge-base IDs to the browser', async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: { internal_enabled: true, external_enabled: true } }))) .mockResolvedValueOnce( new Response( JSON.stringify({ data: { selected_files: 2, intents_recorded: 2, fetch_queued: 1, planned_publications: 4, blocked_items: 0, unbound_files: 0, }, }) ) ) vi.stubGlobal('fetch', fetchMock) expect((await fetchCatalogRoutingCapabilities()).internal_enabled).toBe(true) const result = await routeCatalogFiles({ connection_id: 'conn-1', node_ids: ['a', 'b'], policy: 'both', reason: '批量标注', }) expect(result.planned_publications).toBe(4) expect(result.unbound_files).toBe(0) const payload = JSON.parse(fetchMock.mock.calls[1][1].body) expect(payload.policy).toBe('both') expect(payload).not.toHaveProperty('visibility') expect(payload).not.toHaveProperty('target_knowledge_base_id') expect(fetchMock.mock.calls[1][1].headers).toMatchObject({ Accept: 'application/json', 'Content-Type': 'application/json', 'Idempotency-Key': expect.any(String), }) expect(fetchMock.mock.calls[1][1].headers['Idempotency-Key']).not.toBe('') }) it('reports blocked items without planning anything', async () => { const fetchMock = vi .fn() .mockResolvedValue( new Response( JSON.stringify({ data: { selected_files: 2, intents_recorded: 2, fetch_queued: 0, planned_publications: 0, blocked_items: 2, unbound_files: 0, }, }) ) ) vi.stubGlobal('fetch', fetchMock) const result = await routeCatalogFiles({ connection_id: 'conn-1', node_ids: ['a', 'b'], policy: 'blocked', reason: '禁止导入', }) expect(result.blocked_items).toBe(2) expect(result.planned_publications).toBe(0) }) it('starts a durable background refresh job with no source credential payload', async () => { const job = { id: 'job-refresh', type: 'source.catalog.refresh', payload: { connection_id: 'conn-1', stage: 'queued', progress: 0, message: '等待后台刷新' }, state: 'pending', priority: 20, available_at: '2026-09-09T00:00:00Z', attempt: 0, max_attempts: 3, idempotency_key: 'safe', created_at: '2026-09-09T00:00:00Z', updated_at: '2026-09-09T00:00:00Z', } const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: job }))) vi.stubGlobal('fetch', fetchMock) const result = await triggerCatalogRefresh('conn-1') expect(result.id).toBe('job-refresh') expect(fetchMock.mock.calls[0][1].method).toBe('POST') expect(fetchMock.mock.calls[0][1].headers).toMatchObject({ Accept: 'application/json', 'Idempotency-Key': expect.any(String), }) expect(fetchMock.mock.calls[0][1].headers['Idempotency-Key']).not.toBe('') expect(JSON.stringify(result.payload)).not.toContain('token') }) it('loads the latest persisted snapshot change set without internal hashes', async () => { const fetchMock = vi .fn() .mockResolvedValue( new Response( JSON.stringify({ data: { id: 'cs-1', connection_id: 'conn-1', source_name: '国内工业', state: 'planned', add_count: 2, update_count: 1, move_count: 0, move_scope_count: 1, delete_count: 1, no_change_count: 10, created_at: '2026-09-10T00:00:00Z', items: [ { action: 'delete', title: '旧手册.pdf', old_path: '旧目录/旧手册.pdf', reason_code: 'source_missing', seq: 7, }, ], total: 1, limit: 20, offset: 0, source_locator_hash: 'drop', }, }) ) ) vi.stubGlobal('fetch', fetchMock) const result = await fetchLatestCatalogChangeSet('conn-1', 'delete', 20, 0) expect(result.add_count).toBe(2) expect(result.items[0]).toEqual({ action: 'delete', title: '旧手册.pdf', old_path: '旧目录/旧手册.pdf', new_path: '', reason_code: 'source_missing', seq: 7, }) expect(result).not.toHaveProperty('source_locator_hash') expect(fetchMock.mock.calls[0][0]).toBe( '/api/v1/source-catalog/change-sets/latest?connection_id=conn-1&action=delete&limit=20&offset=0' ) }) })