Files
knowledge-governance/web/src/knowledgeBaseBindings.test.ts
T
狐友 GXXP & Miku 7d91343e5f
build / build (push) Successful in 1m45s
feat(binding): 连接四态验证、密钥尾号与私网 http 放行
在既有 kbbinding(迁移 000081)上补齐任务要求,不另建表、不另起路由:
- 测试连接区分 401/403(密钥无效 / 无权限)与不可达、成功四态;
- 视图回显密钥尾 4 位(服务端不存、不回读明文);
- key_action=clear 作为独立动作,清除后解析失败关闭、不回落文件旧 Key;
- 内外库留空 = 取消绑定,回报回退来源(测试侧部署默认库 / 生产侧同 ID 语义);
- 生产绑定回报在飞执行的冻结/仍可改指向条数,已冻结目标不被改写;
- 新增 WEKNORA_ALLOW_PRIVATE_HTTP:客户端/探活/连接页共用 HTTPAllowed,
  只放行 RFC1918/回环/链路本地 http,生产 18093 同网段反代因此可用。
2026-11-22 15:46:13 +08:00

76 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
duplicateSelection,
keyActionFor,
knowledgeBaseLabel,
parseKBBindingSnapshot,
shortKnowledgeBaseID,
} from './knowledgeBaseBindings'
import type { KBEnvironmentBinding } from './knowledgeBaseBindings'
const binding = (environment: 'test' | 'production'): KBEnvironmentBinding => ({
environment,
revision: 1,
base_url: `https://${environment}.example`,
key_configured: true,
key_source: 'sealed',
internal_kb_id: `${environment}-in`,
internal_kb_name: '内部资料',
external_kb_id: `${environment}-out`,
external_kb_name: '对外资料',
verification: 'verified',
checked_at: null,
updated_by: 'admin',
updated_at: null,
source: 'web',
storage_ready: true,
replanned: 0,
retired_duplicates: 0,
key_tail: '9f2c',
internal_fallback: '',
external_fallback: '',
frozen: 0,
retargetable: 0,
})
describe('knowledgeBaseBindings', () => {
it('parses both environments and never projects a key field', () => {
const parsed = parseKBBindingSnapshot({
data: {
environments: [binding('test'), binding('production')],
storage_ready: true,
},
})
expect(parsed.test.base_url).toBe('https://test.example')
expect(parsed.production.internal_kb_name).toBe('内部资料')
expect(parsed.storage_ready).toBe(true)
const asRecord = parsed.test as unknown as Record<string, unknown>
expect(asRecord.api_key).toBeUndefined()
expect(asRecord.key_ciphertext).toBeUndefined()
})
it('rejects a snapshot missing an environment instead of inventing one', () => {
expect(() => parseKBBindingSnapshot({ data: { environments: [binding('test')], storage_ready: true } })).toThrow()
})
it('shows a short id next to the name so same-name bases are distinguishable', () => {
expect(shortKnowledgeBaseID('0b217e8a-1234-5678')).toBe('0b217e8a…')
expect(knowledgeBaseLabel('0b217e8a-1234-5678', '客户帮助中心')).toBe('客户帮助中心(0b217e8a…)')
expect(knowledgeBaseLabel('', '')).toBe('未绑定')
})
it('rejects using one base for both sides', () => {
expect(duplicateSelection('kb-1', 'kb-1')).toBe(true)
expect(duplicateSelection('kb-1', 'kb-2')).toBe(false)
expect(duplicateSelection('', '')).toBe(false)
})
it('forces a new key for any address other than the saved one', () => {
const saved = binding('test')
expect(keyActionFor(saved, 'https://test.example', false)).toBe('keep')
expect(keyActionFor(saved, 'https://other.example', false)).toBe('replace')
expect(keyActionFor({ ...saved, key_configured: false }, 'https://test.example', false)).toBe('replace')
expect(keyActionFor(saved, 'https://test.example', true)).toBe('replace')
})
})