Files
knowledge-governance/web/src/sourceCatalog.test.ts
T
姚章浩 fad8b10247 feat(web): annotation result copy matches policy-driven ingestion
Saved strategies now report intents/fetch/planned/blocked counts and point at
解析监控 → 待入库; the blocked note explains the 需下架 lane instead of the
deprecated snapshot-diff confirmation step.
2026-09-30 10:54:07 +08:00

75 lines
6.9 KiB
TypeScript

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('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')
})
})