Files
knowledge-governance/web/src/uiSettings.test.ts
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

244 lines
9.1 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import {
activateUISettings,
createUISettingsCandidate,
fetchActiveUISettings,
parseServiceTargetVerification,
parseUISettingsResponse,
settingsRefreshDelays,
UISettingsError,
} from './uiSettings'
const candidateInput = {
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: '',
} as const
const active = {
id: 'settings-default',
state: 'active',
document_refresh_seconds: 15,
resource_refresh_seconds: 30,
stalled_after_seconds: 1800,
finalizing_after_seconds: 7200,
ingestion_concurrency: 2,
production_weknora_base_url: '',
test_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: '',
}
describe('UI settings API', () => {
it('strictly rebuilds the safe revision projection', () => {
expect(parseUISettingsResponse({ data: { ...active, secret: 'drop' } })).toEqual(active)
expect(() => parseUISettingsResponse({ data: { ...active, document_refresh_seconds: 31 } })).toThrow(
'invalid UI settings response'
)
})
it('accepts only the four service fields with their own rules', () => {
const parsed = parseUISettingsResponse({
data: {
...active,
test_weknora_base_url: 'https://weknora.test.example:8443/kb',
production_tenant_id: '10000',
production_knowledge_base_id: 'kb-abc_123',
},
})
expect(parsed.test_weknora_base_url).toBe('https://weknora.test.example:8443/kb')
expect(parsed.production_tenant_id).toBe('10000')
expect(parsed.production_knowledge_base_id).toBe('kb-abc_123')
for (const bad of [
'weknora.test.example',
'https://user:pass@weknora.test.example',
'https://weknora.test.example?token=abc',
])
expect(() => parseUISettingsResponse({ data: { ...active, test_weknora_base_url: bad } })).toThrow(
'invalid UI settings response'
)
for (const bad of ['abc', '10 000', '-1', '0', '10000000000000000000000', '10000.5'])
expect(() => parseUISettingsResponse({ data: { ...active, production_tenant_id: bad } })).toThrow(
'invalid UI settings response'
)
for (const bad of ['kb/abc', 'kb abc', 'kb-abc?x', 'https://weknora.prod.example/kb'])
expect(() => parseUISettingsResponse({ data: { ...active, production_knowledge_base_id: bad } })).toThrow(
'invalid UI settings response'
)
})
it('keeps a web-configured production endpoint only when it is a bare http(s) origin', () => {
expect(
parseUISettingsResponse({
data: { ...active, production_weknora_base_url: 'https://weknora.prod.example:8443/kb' },
}).production_weknora_base_url
).toBe('https://weknora.prod.example:8443/kb')
for (const bad of [
'weknora.prod.example',
'https://user:pass@weknora.prod.example',
'https://weknora.prod.example?token=abc',
'https://weknora.prod.example#frag',
' https://weknora.prod.example',
])
expect(() => parseUISettingsResponse({ data: { ...active, production_weknora_base_url: bad } })).toThrow(
'invalid UI settings response'
)
})
it('converts the active revision into independent monitor delays', () => {
expect(settingsRefreshDelays({ ...active, document_refresh_seconds: 30, resource_refresh_seconds: 60 })).toEqual({
documentMs: 30000,
resourceMs: 60000,
})
})
it('creates a validated candidate then activates it through fixed endpoints', async () => {
const candidate = {
...active,
id: 'uis-candidate',
state: 'validated',
updated_by: 'engineer-1',
activated_at: null,
}
const fetchMock = vi
.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ data: active }), { status: 200 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ data: candidate }), { status: 201 }))
.mockResolvedValueOnce(
new Response(
JSON.stringify({ data: { ...candidate, state: 'active', activated_at: '2026-09-10T08:01:00Z' } }),
{ status: 200 }
)
)
vi.stubGlobal('fetch', fetchMock)
await fetchActiveUISettings()
await createUISettingsCandidate({
document_refresh_seconds: 30,
resource_refresh_seconds: 60,
stalled_after_seconds: 3600,
finalizing_after_seconds: 14400,
ingestion_concurrency: 3,
test_weknora_base_url: '',
production_weknora_base_url: '',
production_tenant_id: '',
production_knowledge_base_id: '',
})
await activateUISettings('uis-candidate')
expect(fetchMock.mock.calls[0][0]).toBe('/api/v1/ui-settings')
expect(fetchMock.mock.calls[1][0]).toBe('/api/v1/ui-settings/candidates')
expect(fetchMock.mock.calls[1][1]).toMatchObject({
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
document_refresh_seconds: 30,
resource_refresh_seconds: 60,
stalled_after_seconds: 3600,
finalizing_after_seconds: 14400,
ingestion_concurrency: 3,
test_weknora_base_url: '',
production_weknora_base_url: '',
production_tenant_id: '',
production_knowledge_base_id: '',
}),
})
expect(fetchMock.mock.calls[2][0]).toBe('/api/v1/ui-settings/uis-candidate/activate')
expect(fetchMock.mock.calls[2][1]).toMatchObject({
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{}',
})
vi.unstubAllGlobals()
})
// 保存的返回值带上真实核验结果:界面要能显示"用的是哪个知识库、属于哪个租户"。
it('returns the real verification block produced while saving', async () => {
const candidate = { ...active, id: 'uis-candidate', state: 'validated', activated_at: null }
const verification = {
status: 'verified',
message: '已核验:生产地址上存在知识库「生产知识库」,租户 10000 一致',
blocked: false,
knowledge_base_name: '生产知识库',
observed_tenant_id: '10000',
test_endpoint: { status: 'reachable', message: '测试地址可达(可列出 7 个知识库)' },
production_endpoint: { status: 'reachable', message: '生产地址可达(可列出 7 个知识库)' },
checked_at: '2026-09-18T09:00:00Z',
}
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(
new Response(JSON.stringify({ data: candidate, operation: 'created', verification }), { status: 201 })
)
)
const saved = await createUISettingsCandidate(candidateInput)
expect(saved.settings.id).toBe('uis-candidate')
expect(saved.verification).toEqual(verification)
vi.unstubAllGlobals()
})
// 被拒绝时保留服务器那句话,并带上核验详情,而不是退化成 "request failed (400)"。
it('keeps a refusal sentence and its detail instead of a status code', async () => {
const verification = {
status: 'missing',
message: '未通过:生产地址上找不到知识库 ID「kb-prod-1」;该地址现有 1 个知识库:生产知识库(kb-prod-1)',
blocked: true,
knowledge_base_name: '',
observed_tenant_id: '',
test_endpoint: { status: 'reachable', message: '测试地址可达' },
production_endpoint: { status: 'reachable', message: '生产地址可达' },
checked_at: '2026-09-18T09:00:00Z',
}
vi.stubGlobal(
'fetch',
vi
.fn()
.mockImplementation(() =>
Promise.resolve(
new Response(JSON.stringify({ error: '未通过:生产地址上找不到知识库 ID「kb-prod-1」', verification }), {
status: 400,
})
)
)
)
await expect(createUISettingsCandidate(candidateInput)).rejects.toThrow('找不到知识库 ID')
const failure = await createUISettingsCandidate(candidateInput).catch((cause: unknown) => cause)
expect(failure).toBeInstanceOf(UISettingsError)
expect((failure as UISettingsError).verification?.status).toBe('missing')
expect((failure as UISettingsError).verification?.blocked).toBe(true)
vi.unstubAllGlobals()
})
// 没有核验块(未配置核验器的开发 socket)或格式不对时,一律当作"没有核验",绝不编造"已核验"。
it('treats a missing or malformed verification block as no verification', () => {
expect(parseServiceTargetVerification(undefined)).toBeNull()
expect(parseServiceTargetVerification({ status: 'verified' })).toBeNull()
expect(
parseServiceTargetVerification({ status: 'verified', message: 'x', blocked: false, test_endpoint: 'nope' })
).toEqual({
status: 'verified',
message: 'x',
blocked: false,
knowledge_base_name: '',
observed_tenant_id: '',
test_endpoint: { status: '', message: '' },
production_endpoint: { status: '', message: '' },
checked_at: '',
})
})
})