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 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') }) })