Files
knowledge-governance/web/src/dingtalkSources.test.ts
T
姚章浩 e00cab2f70 feat(settings): 数据源配置改成「列出来、勾选、变成来源」,并去掉文件格式筛选
用户对 系统设置 → 数据源配置 的四条要求:
① 这里不要做文件格式筛选(格式归目录级标注决定);
② 列出我账号下能看到的知识库;③ 列出钉盘能看到哪些;④ 勾选那些作为来源。

① 来源卡片不再渲染 FilenameFilterEditor(目录级筛选仍在「来源与目录」里,按目录就近覆盖)。
②③ 新端点 GET /api/v1/dingtalk-spaces/available(管理员,只读):用 dws 列举
   - 知识库:wiki +space-list --type orgWikiSpace/myWikiSpace(实测 17 + 1 个)
   - 钉盘:wiki space list --type mySpace/orgSpace(实测 我的文件 + 6 个组织空间)
   实测约束写进命令形态:--limit 上限是 50;翻页必须 --page-all(--max-items 兜底),
   否则超过一页的知识库会被静默截断(被截断时页面显示"可能不全"而不是假装列全)。
   列不出来 ≠ 空列表:dws 未登录/权限不足 → 503 + 原因;某一类失败只丢那一类。
④ 面板勾选后走既有登记路径(/api/v1/dingtalk-spaces),首次目录扫描自动排队;
   已登记的按空间名标「已是来源」并禁用勾选;扫码会话失效时直接弹二维码,扫完自动重试。

基础设施:api 与 worker 共享同一份 dws 状态(bind mount 同一份,refresh token 是轮换制,
不能各留一份)。子进程 HOME 必须指向 DWS_HOME —— 这条是实测踩出来的:漏了会报"未登录",
已有回归测试钉住。dws 子进程沿用白名单环境,库口令等变量不进子进程。

测试:新包 internal/dingtalkdiscovery(fixture 用真实 dws 输出形状):paging 约束、
截断提示、一类失败不连带、全部失败→ErrUnavailable、子进程环境;httpapi 侧 401/501/503/500
与不泄漏内部错误;OpenAPI 契约(400/503 组件)通过。前端 189 条 + tsc 干净。

面板实测:知识库(18)/钉盘(7)都列出来,已有三条钉钉来源自动标「已是来源」;
勾选「视觉文档」→ 加入为来源 → 新来源 sc-8a4be5b8293d1c9a048c3c7e9b194155(dingtalk,enabled)
+ source.catalog.refresh 已完成。
2026-10-10 16:46:39 +08:00

72 lines
3.7 KiB
TypeScript

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([])
})
})