import { afterEach, describe, expect, it, vi } from 'vitest' import { discoveryCandidates, driveSpaceURL, fetchAvailableDingtalkSources } from './dingtalkSources' const payload = { data: { knowledge_bases: [ { name: 'ATOM知识库', space_id: '9JOGOQneRgkneX4Q', space_type: 'orgWikiSpace', url: 'https://alidocs.dingtalk.com/i/spaces/9JOGOQneRgkneX4Q/overview', description: '' }, { name: '视觉文档', space_id: '9JOGO9EEOMLMWX4Q', space_type: 'orgWikiSpace', url: '', description: '2d/2.5D/3D 视觉' }, { name: '我的文档', space_id: '9JOGOxeo90ybnz4Q', space_type: 'myWikiSpace', url: 'https://alidocs.dingtalk.com/i/spaces/9JOGOxeo90ybnz4Q/overview', description: '' }, ], drive_spaces: [ { name: '我的文件', space_id: '28122692916', space_type: 'mySpace', root_folder_id: '65Y1OQX0' }, { name: '全员文件夹', space_id: '102571304', space_type: 'orgSpace', root_folder_id: 'g1YGl6Pm' }, ], knowledge_notice: '', drive_notice: '', fetched_at: '2026-10-10T08:00:00Z', }, } afterEach(() => { vi.unstubAllGlobals() }) describe('fetchAvailableDingtalkSources', () => { it('parses knowledge bases and drive spaces', async () => { vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(payload), { status: 200 }))) const list = await fetchAvailableDingtalkSources() expect(list.knowledge_bases).toHaveLength(3) expect(list.knowledge_bases[1].url).toBe('https://alidocs.dingtalk.com/i/spaces/9JOGO9EEOMLMWX4Q/overview') expect(list.drive_spaces).toHaveLength(2) expect(list.drive_spaces[1].space_type).toBe('orgSpace') expect(list.knowledge_notice).toBe('') expect(list.drive_notice).toBe('') }) it('explains environment problems instead of pretending the list is empty', async () => { vi.stubGlobal('fetch', vi.fn(async () => new Response('{"error":"dingtalk_discovery_unavailable"}', { status: 503 }))) await expect(fetchAvailableDingtalkSources()).rejects.toThrow(/暂时列不出来/) vi.stubGlobal('fetch', vi.fn(async () => new Response('{}', { status: 501 }))) await expect(fetchAvailableDingtalkSources()).rejects.toThrow(/后端未接线/) vi.stubGlobal('fetch', vi.fn(async () => new Response('{}', { status: 403 }))) await expect(fetchAvailableDingtalkSources()).rejects.toThrow(/需要管理员身份/) }) it('carries the drive notice so the page can explain a missing 钉盘 list', async () => { vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify({ data: { knowledge_bases: [], drive_spaces: [], drive_notice: 'list orgSpace: permission denied' } }), { status: 200 }))) const list = await fetchAvailableDingtalkSources() expect(list.drive_notice).toContain('permission denied') }) }) describe('discoveryCandidates', () => { it('flattens both groups and marks the ones already registered as sources', () => { const candidates = discoveryCandidates(payload.data as never, ['ATOM知识库', ' (空名字) ']) expect(candidates.map((item) => item.key)).toEqual([ 'kb:9JOGOQneRgkneX4Q', 'kb:9JOGO9EEOMLMWX4Q', 'kb:9JOGOxeo90ybnz4Q', 'drive:28122692916', 'drive:102571304', ]) expect(candidates[0].alreadyRegistered).toBe(true) expect(candidates[1].alreadyRegistered).toBe(false) expect(candidates[0].detail).toBe('钉钉知识库 · 组织知识库') expect(candidates[2].detail).toBe('钉钉知识库 · 我的知识库') expect(candidates[3].detail).toBe('钉盘 · 我的文件') expect(candidates[4].detail).toBe('钉盘 · 组织空间') expect(candidates[4].url).toBe(driveSpaceURL('102571304')) }) it('returns nothing for an empty list', () => { expect(discoveryCandidates(null, ['X'])).toEqual([]) }) })