Files
knowledge-governance/web/src/dingtalkSources.test.ts
姚章浩 3825859da6
build / build (push) Successful in 2m20s
feat(settings): 打通 PP 认证配置并统一 DWS 来源授权
PP 增加受控凭据更新、真实登录与目录检查,待验证修改不会替换生效配置;扫描与取件共享生效修订。
钉钉来源向导、校验与登记共用 DWS 授权,支持中途恢复与换账号保护,取消旧扫码自动回落。
认证审计复用已有来源审计页,配置响应不回传密码;补齐运行镜像脚本与权限。

验证:make verify 退出 0;PP/DWS/httpapi 目标 race 回归通过;隔离 HTTP 服务验证 PP 协议及扫描/取件消费者。
尚未部署;真实 PP 凭据与目录可用性在测试站部署后验证。
2026-11-17 19:08:46 +08:00

119 lines
4.0 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { discoveryCandidates, driveSpaceURL, fetchAvailableDingtalkSources } from './dingtalkSources'
const payload = {
data: {
knowledge_bases: [
{
name: 'ATOM知识库',
registered: true,
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([])
})
})