61c0811228
- 系统设置页新增服务配置表单(生产/测试地址、租户 ID、知识库 ID),保存时回显校验结果与失败原因 - 同步前端类型定义与接口调用,补齐设置相关前端用例
115 lines
8.4 KiB
TypeScript
115 lines
8.4 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: '',
|
|
})
|
|
})
|
|
})
|