import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import {
SystemSettingsView,
ppSourceConnections,
refreshDraftText,
refreshSettingsDirty,
refreshSettingsInput,
parseRefreshDrafts,
serviceSettingsInput,
visibleSaveMessage,
} from './SystemSettings'
import { AuthContext } from './auth'
import type { MonitorOverview } from './monitor'
import type { CatalogSource } from './sourceCatalog'
import type { UISettings } from './uiSettings'
import type { SourceConnectionListItem } from './sourceConnections'
const monitor: MonitorOverview = {
mode: 'simulation',
simulated: true,
observed_at: '2026-09-08T12:00:00Z',
host: {
cpu_percent: 42,
memory_used_bytes: 1,
memory_total_bytes: 2,
disk_used_bytes: 1,
disk_total_bytes: 2,
gpu_state: 'unknown',
gpu_detail: '未接入真实GPU监控',
simulated: true,
},
services: [
{
key: 'mineru',
name: 'MinerU',
state: 'healthy',
detail: '模拟',
latency_ms: 1,
observed_at: '2026-09-08T12:00:00Z',
simulated: true,
},
],
knowledge_bases: [],
health: [
{
key: 'mineru',
name: 'MinerU',
liveness: 'up',
readiness: 'blocked',
reason: '服务可达但容量已满(429),暂时不可接单',
latency_ms: 30,
observed_at: '2026-09-08T12:00:00Z',
last_success_at: '2026-09-08T11:59:00Z',
consecutive_failures: 2,
configured: true,
applicable: true,
},
],
}
const sources: CatalogSource[] = [
{
connection_id: 'ding-1',
source_type: 'dingtalk',
name: '公司级技术支持知识库',
root_node_id: 'root-1',
folder_count: 1079,
file_count: 3444,
observed_at: '2026-09-09T09:30:32Z',
snapshot_only: true,
snapshot_status: 'ready',
last_refresh_duration_ms: 0,
},
]
const connections: SourceConnectionListItem[] = [
{
id: 'conn-ding',
source_type: 'dingtalk',
name: '公司级技术支持知识库',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 3444,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: '2026-09-12T10:00:00Z',
},
{
id: 'conn-man',
source_type: 'manual',
name: '本地手工验收',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 5,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
]
const settings: UISettings = {
id: 'settings-default',
state: 'active',
document_refresh_seconds: 15,
resource_refresh_seconds: 30,
stalled_after_seconds: 1800,
finalizing_after_seconds: 7200,
ingestion_concurrency: 2,
test_weknora_base_url: '',
production_weknora_base_url: '',
production_tenant_id: '',
production_knowledge_base_id: '',
updated_by: 'system:migration',
created_at: '2026-09-10T08:00:00Z',
activated_at: '2026-09-10T08:00:00Z',
retired_at: null,
error_code: '',
}
const authValue = (role: 'admin' | 'engineer') => ({
identity: { id: `${role}-1`, username: role, display_name: role, role, must_change_password: false },
ready: true,
login: async () => null,
logout: async () => {},
changePassword: async () => null,
signedOut: 0,
notifySignedOut: () => {},
})
describe('SystemSettingsView AI 模型连接', () => {
const view = (role: 'admin' | 'engineer') =>
renderToStaticMarkup(
)
it('keeps the model connection alone in AI 模型连接 and points WeKnora to 知识库绑定', () => {
const adminHtml = view('admin')
for (const text of [
'AI 模型连接',
'正在读取已保存的模型连接',
'问答四字段分析与中文问题翻译共用此连接',
'WeKnora 的知识库连接已移到「知识库绑定」',
]) {
expect(adminHtml).toContain(text)
}
// 地址类配置已迁走:这里不再出现 WeKnora 连接表单,也不出现旧的“服务配置”分区名。
for (const removed of ['保存并校验连接', '保存并启用服务配置', '生产租户 ID', '服务配置'])
expect(adminHtml).not.toContain(removed)
const engineerHtml = view('engineer')
expect(engineerHtml).toContain('仅管理员可修改模型连接')
// 工程师看不到模型编辑表单(QAModelSettings 只在管理员下挂载)。
expect(engineerHtml).not.toContain('正在读取已保存的模型连接')
})
})
describe('SystemSettingsView', () => {
it('separates document-engineer settings from locked administrator and production controls', () => {
const html = renderToStaticMarkup(
)
for (const text of ['系统设置', '数据源配置', 'AI 模型连接', '知识库绑定', '存储与备份', '参数设置'])
expect(html).toContain(text)
// 主人 2026-10-26 要求去掉的噪声:页头副标题、刷新状态、权限条、活动版本横幅。
for (const removed of [
'查看内容源、服务与备份状态',
'刷新状态',
'文档工程师可调整',
'管理员设置已锁定',
'设置服务已启用',
])
expect(html).not.toContain(removed)
// 需求(2026-10-14 收敛):数据源配置只剩两个入口 —— dws 导入(按当前钉钉账号发现)
// 与 PP 导入;AList/静态钉钉知识库等来源类型不再出现在这里(也没有折叠开关兜着)。
expect(html).toContain('公司级技术支持知识库')
expect(html).not.toContain('dws 共享授权')
for (const hidden of ['专用浏览器扫码会话', '显示 AList/钉钉知识库来源']) expect(html).not.toContain(hidden)
// 统一入口后不再按类型分标题:PP 与钉钉共用同一张清单与同一个添加向导。
expect(html).not.toContain('PP 导入')
// 需求(2026-10-10):数据源配置不再做文件格式筛选(格式由目录级标注决定)。
for (const removed of ['文件格式筛选', '未勾选的一律不取件', '保存保留格式', '恢复继承上级规则'])
expect(html).not.toContain(removed)
// 需求(2026-10-10):新增入口只剩「可加入的来源」勾选,粘贴链接的按钮已下线。
for (const removed of ['+ 新增钉钉知识库', '新增钉钉知识库']) expect(html).not.toContain(removed)
// WeKnora 连接已迁到「知识库绑定」:主表单保存连接+绑定,诊断折叠保留。
const mapHtml = renderToStaticMarkup(
)
for (const text of [
'知识库绑定',
'测试 090',
'生产 090',
'正在读取知识库绑定…',
'连接与绑定诊断',
'测试 WeKnora',
'生产 WeKnora',
'整库发布目标',
])
expect(mapHtml).toContain(text)
// 旧连接表单已被单一保存入口取代;SSR 尚未读到绑定时不渲染密钥输入。
for (const removed of ['保存并校验连接', '测试地址', '生产租户 ID', '生产核验参数'])
expect(mapHtml).not.toContain(removed)
expect(mapHtml).not.toContain('API Key')
const storageHtml = renderToStaticMarkup(
)
// 存储与备份分区是只读摘要页:SSR 先给加载态,静态占位文案不再存在。
expect(storageHtml).toContain('存储与备份')
expect(storageHtml).toContain('正在读取存储与备份…')
expect(storageHtml).not.toContain('仅保存凭据引用,不可回读')
const refreshHtml = renderToStaticMarkup(
)
for (const text of [
'参数设置',
'来源与目录',
'解析监控',
'文档状态刷新间隔',
'解析监控按此周期刷新文档状态;调小更及时、请求更频繁,调大更省请求、更新更慢',
'资源刷新间隔',
'解析监控按此周期刷新资源与服务状态;调小更及时、请求更频繁,调大更省请求、更新更慢',
'解析停滞阈值',
'超过此时长没有进展就标记停滞;调大更容忍长解析,调小更快标记停滞',
'入库并发上限',
'调大入库更快、写入压力更高;调小更稳,排队更久',
'保存参数设置',
'当前:文档 15 秒、资源 30 秒、停滞 30 分钟、入库并发 2。',
])
expect(refreshHtml).toContain(text)
for (const removed of ['收尾超时告警', 'finalizing', '刷新与入库', '保存刷新与告警参数'])
expect(refreshHtml).not.toContain(removed)
const sourcesGroup = refreshHtml.slice(
refreshHtml.indexOf('data-refresh-group="sources"'),
refreshHtml.indexOf('data-refresh-group="parsing"')
)
const parsingGroup = refreshHtml.slice(refreshHtml.indexOf('data-refresh-group="parsing"'))
expect(sourcesGroup).toContain('文档状态刷新间隔')
expect(sourcesGroup).toContain('资源刷新间隔')
expect(sourcesGroup).not.toContain('解析停滞阈值')
expect(sourcesGroup).not.toContain('入库并发上限')
expect(parsingGroup).toContain('解析停滞阈值')
expect(parsingGroup).toContain('入库并发上限')
expect(parsingGroup).not.toContain('文档状态刷新间隔')
expect(parsingGroup).not.toContain('资源刷新间隔')
expect(refreshHtml).toContain('aria-invalid="false"')
expect(refreshHtml).toContain('inputMode="numeric"')
expect(visibleSaveMessage('已保存', true)).toBeNull()
expect(visibleSaveMessage('已保存', false)).toBe('已保存')
expect(html).not.toContain('本地模拟')
expect(html).not.toContain('保存模拟设置')
for (const forbidden of ['DATABASE_URL', 'RELEASE_CONFIG_FILE', 'API Key明文', 'secret_ref'])
expect(html).not.toContain(forbidden)
// 两个环境卡仍由真实探测驱动(依赖矩阵搬到总览页,本页不再渲染)。
expect(mapHtml).toContain('解析队列 对内 已配置 · 对外 已配置')
expect(mapHtml).not.toContain('PostgreSQL')
expect(mapHtml).toContain('端点探测:暂无真实探测结果')
// V1: left-nav section controls, per-source schedule editing, danger modal & history affordances.
expect(html).toContain('settings-nav')
for (const kept of ['更多', '自动刷新中']) expect(html).toContain(kept)
for (const hidden of ['查看修订历史', '保存并启用调度', '停用来源']) expect(html).not.toContain(hidden)
expect(html).not.toContain('本页只列 PP 来源')
expect(html).not.toContain('本地手工导入来源不支持定时刷新')
// dead buttons removed
expect(html).not.toContain('添加内容源')
expect(html).not.toContain('管理员编辑')
})
it('lets an administrator bind the internal/external import knowledge bases from 系统设置', () => {
// 组件一直在(RoutingBindingsSection),但从未挂到任何页面 —— 导入目标库只能靠
// 部署 env 钉死,换库必须重建容器。
const view = (section: 'sources' | 'bindings' | 'production-bindings') =>
renderToStaticMarkup(
)
// 旧入口(bindings / production-bindings)都落到「知识库绑定」:新主表单 + 折叠诊断。
expect(view('sources')).toContain('知识库绑定')
const bound = view('bindings')
expect(bound).toContain('id="settings-kb-bindings"')
expect(bound).toContain('知识库绑定')
expect(bound).toContain('正在读取知识库绑定…')
expect(bound).toContain('连接与绑定诊断')
// 诊断区保留原链路:测试入库绑定与生产目标入口仍在,但不再提供第二个保存路径。
expect(bound).toContain('id="settings-routing-bindings"')
expect(bound).toContain('分发到对应的测试 WeKnora 知识库')
const target = view('production-bindings')
expect(target).toContain('id="settings-production-bindings"')
expect(target).toContain('整库发布目标')
expect(target).toContain('文档级绑定')
})
it('does not show healthy simulation when the settings data failed to load', () => {
const html = renderToStaticMarkup(
)
expect(html).toContain('加载失败')
expect(html).not.toContain('模拟运行中')
})
it('renders the WeKnora cards and production channel from live probe and the real production gate', () => {
const probed: MonitorOverview = {
...monitor,
health: [
{
key: 'weknora',
name: 'WeKnora API',
liveness: 'up',
readiness: 'ready',
reason: '',
latency_ms: 86,
observed_at: '2026-09-08T12:00:00Z',
last_success_at: '2026-09-08T11:59:00Z',
consecutive_failures: 0,
configured: true,
applicable: true,
},
{
key: 'weknora-production',
name: '生产 WeKnora',
liveness: 'up',
readiness: 'ready',
reason: '',
latency_ms: 41,
observed_at: '2026-09-08T12:00:00Z',
last_success_at: '2026-09-08T11:59:00Z',
consecutive_failures: 0,
configured: true,
applicable: true,
},
{
key: 'embedding',
name: 'Embedding',
liveness: 'up',
readiness: 'not_applicable',
reason: '未部署:本平台不使用该依赖',
latency_ms: 0,
observed_at: '2026-09-08T12:00:00Z',
last_success_at: null,
consecutive_failures: 0,
configured: false,
applicable: false,
},
],
}
const view = (enabled: boolean, verified: boolean) =>
renderToStaticMarkup(
)
const on = view(true, true)
expect(on).toContain('测试 WeKnora')
expect(on).toContain('接口可达 · 执行器未验证')
expect(on).toContain('延迟 86ms')
expect(on).toContain('已启用 · 可提交生产请求')
expect(on).toContain('生产写入:开启')
expect(on).toContain('凭据:服务器文件/加密存储,页面不回读')
const off = view(false, false)
expect(off).toContain('未启用')
expect(off).toContain('生产写入:关闭')
// 卡片状态由真实探测/门禁驱动,写死的“已连接 / 未连接 / 生产映射:未配置”必须消失。
for (const removed of ['本地真实连接', '生产映射:未配置']) expect(off).not.toContain(removed)
// 生产端点可达性来自真实探活,且只说真话:探活不等于验证过接单能力。
expect(on).toContain('端点探测:端点可达 · 未做接单验证')
const blocked = renderToStaticMarkup(
)
expect(blocked).toContain('已启用 · 被阻断')
// 未配置生产端点时必须显式说「未配置」,不得靠文案含糊过去。
expect(blocked).toContain('端点探测:暂无真实探测结果')
const unconfigured = renderToStaticMarkup(
)
expect(unconfigured).toContain('端点探测:未配置')
expect(unconfigured).not.toContain('端点可达')
})
})
describe('SystemSettingsView 数据源配置的默认清单', () => {
const withPP: SourceConnectionListItem[] = [
{
id: 'conn-pp',
source_type: 'pp',
name: 'PP资料下载中心',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 613,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'conn-alist',
source_type: 'alist',
name: '国内工业',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 3027,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'conn-ding2',
source_type: 'dingtalk',
name: 'ATOM知识库',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 109,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
]
const view = (role: 'admin' | 'engineer' = 'admin') =>
renderToStaticMarkup(
)
it('统一来源清单显示已接入的 PP 和钉钉来源,不依赖目录快照', () => {
const html = view()
expect(html).toContain('PP资料下载中心')
expect(html).toContain('PP 资料下载中心')
for (const name of ['国内工业', 'ATOM知识库']) expect(html).toContain(name)
expect(html).not.toContain('显示 AList')
// 统一入口:一个「+ 添加数据源」向导,不再有按类型分家的 dws 导入面板标题。
expect(html).toContain('+ 添加数据源')
expect(html).not.toContain('PP 导入')
expect(html).not.toContain('去掉来源')
for (const removed of ['端点与凭据', '认证方式', '查看修订历史', '保存并启用调度', '停用来源', '自动刷新周期'])
expect(html).not.toContain(removed)
expect(html).toContain('更多')
expect(html).toContain('aria-expanded="false"')
expect(html).toContain('仅手动刷新')
expect(html).toContain('数据源设置审计')
expect((html.match(/aria-label="钉钉账号(DWS)"/g) ?? []).length).toBe(1)
expect(html).toContain('登录授权')
expect(html).toContain('检查连接')
for (const removed of ['+ 新增钉钉知识库', '文件格式筛选']) expect(html).not.toContain(removed)
// 文档工程师看不到添加向导与「去掉来源」:两者都是管理员动作。
const engineer = view('engineer')
expect(engineer).not.toContain('+ 添加数据源')
expect(engineer).not.toContain('去掉来源')
// PP 与钉钉共用同一张清单:工程师看得到卡,只是没有添加与归档。
expect(engineer).toContain('PP资料下载中心')
})
})
describe('ppSourceConnections(数据源配置的清单规则)', () => {
const rows: SourceConnectionListItem[] = [
{
id: 'a',
source_type: 'pp',
name: 'PP资料下载中心',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 1,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'b',
source_type: 'alist',
name: '国内工业',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 2,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'c',
source_type: 'dingtalk',
name: 'ATOM知识库',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 3,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'd',
source_type: 'manual',
name: '本地手工验收',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 4,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'e',
source_type: 'pp',
name: 'PP legacy',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 5,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
]
it('只放行 PP 来源,顺序不变,其余类型一条都不放行', () => {
expect(ppSourceConnections(rows).map((item) => item.id)).toEqual(['a', 'e'])
expect(ppSourceConnections([])).toEqual([])
})
})
// F03 回归:分区保存不能夹带另一分区未保存的草稿。
describe('保存边界(F03)', () => {
const server = {
document_refresh_seconds: 15,
resource_refresh_seconds: 30,
stalled_after_seconds: 1800,
finalizing_after_seconds: 7200,
ingestion_concurrency: 2,
test_weknora_base_url: 'https://server.test',
production_weknora_base_url: 'https://server.prod',
production_tenant_id: '10000',
production_knowledge_base_id: 'kb-server',
} as unknown as UISettings
it('保存参数设置时忽略服务分区里未保存的地址,并原样回传 finalizing', () => {
const input = refreshSettingsInput(server, {
documentRefresh: 60,
resourceRefresh: 60,
stalled: 3600,
concurrency: 7,
})
expect(input.test_weknora_base_url).toBe('https://server.test')
expect(input.production_weknora_base_url).toBe('https://server.prod')
expect(input.production_tenant_id).toBe('10000')
expect(input.production_knowledge_base_id).toBe('kb-server')
expect(input.finalizing_after_seconds).toBe(server.finalizing_after_seconds)
expect(input.document_refresh_seconds).toBe(60)
expect(input.stalled_after_seconds).toBe(3600)
expect(input.ingestion_concurrency).toBe(7)
})
it('保存服务配置时忽略参数分区里未保存的参数', () => {
const input = serviceSettingsInput(server, {
testEndpoint: 'https://draft.test',
endpoint: 'https://draft.prod',
productionTenantID: '20000',
productionKnowledgeBaseID: 'kb-draft',
})
expect(input.document_refresh_seconds).toBe(15)
expect(input.ingestion_concurrency).toBe(2)
expect(input.test_weknora_base_url).toBe('https://draft.test')
})
})
describe('参数设置的文本草稿解析', () => {
const drafts = (patch: Record = {}) => ({ ...refreshDraftText(settings), ...patch })
it('把活动设置转成带单位的草稿(停滞用分钟)', () => {
expect(refreshDraftText(settings)).toEqual({
documentRefresh: '15',
resourceRefresh: '30',
stalled: '30',
concurrency: '2',
})
})
it('接受白名单与 1..16 的整数,停滞分钟换算成秒', () => {
const parsed = parseRefreshDrafts({
documentRefresh: '60',
resourceRefresh: '60',
stalled: '60',
concurrency: '16',
})
expect(parsed.errors).toEqual({
documentRefresh: null,
resourceRefresh: null,
stalled: null,
concurrency: null,
})
expect(parsed.values).toEqual({
documentRefresh: 60,
resourceRefresh: 60,
stalled: 3600,
concurrency: 16,
})
})
it('接受并发 5/7/9(后端范围 1..16,而不是旧下拉六档)', () => {
for (const value of ['5', '7', '9']) {
expect(parseRefreshDrafts(drafts({ concurrency: value })).values?.concurrency).toBe(Number(value))
}
})
it('拒绝空值、非整数、越界与全角数字,并给出字段级提示', () => {
const cases: [Record, string, string][] = [
[{ documentRefresh: '' }, 'documentRefresh', '不能为空'],
[{ documentRefresh: '1' }, 'documentRefresh', '15/30/60'],
[{ documentRefresh: '2.5' }, 'documentRefresh', '必须是整数'],
[{ documentRefresh: '+15' }, 'documentRefresh', '必须是整数'],
[{ documentRefresh: '1e1' }, 'documentRefresh', '必须是整数'],
[{ documentRefresh: '15' }, 'documentRefresh', '必须是整数'],
[{ documentRefresh: '15abc' }, 'documentRefresh', '必须是整数'],
[{ resourceRefresh: '15' }, 'resourceRefresh', '30/60'],
[{ stalled: '45' }, 'stalled', '30/60'],
[{ stalled: '0.5' }, 'stalled', '必须是整数'],
[{ concurrency: '0' }, 'concurrency', '1~16'],
[{ concurrency: '99' }, 'concurrency', '1~16'],
[{ concurrency: '2.5' }, 'concurrency', '必须是整数'],
]
for (const [patch, field, message] of cases) {
const parsed = parseRefreshDrafts(drafts(patch))
expect(parsed.values, `${field} ${patch[field]}`).toBeNull()
expect(parsed.errors[field as keyof typeof parsed.errors]).toContain(message)
}
})
it('错误文案使用完整字段名', () => {
expect(parseRefreshDrafts(drafts({ documentRefresh: '1' })).errors.documentRefresh).toContain(
'文档状态刷新间隔只能是 15/30/60 秒'
)
expect(parseRefreshDrafts(drafts({ stalled: '45' })).errors.stalled).toContain('解析停滞阈值只能是 30/60 分钟')
})
it('任一字段非法时整体 values 为 null,避免夹带默认值保存', () => {
const parsed = parseRefreshDrafts(drafts({ concurrency: '99' }))
expect(parsed.values).toBeNull()
expect(parsed.errors.documentRefresh).toBeNull()
})
it('脏值按解析后的数值比较:015 等价 15,改值或非法值才变脏', () => {
expect(refreshSettingsDirty(drafts(), settings)).toBe(false)
expect(refreshSettingsDirty(drafts({ documentRefresh: '015' }), settings)).toBe(false)
expect(refreshSettingsDirty(drafts({ concurrency: '7' }), settings)).toBe(true)
expect(refreshSettingsDirty(drafts({ documentRefresh: '1' }), settings)).toBe(true)
})
})