import { describe, expect, it, vi } from 'vitest' import { fetchRoutingBindingOptions, fetchRoutingBindings, saveRoutingBindings } from './routingBindings' const binding = { internal_kb_id: '1b52c140-2e18-4676-afc5-4c7d8d190a8c', external_kb_id: 'fd7db6aa-763a-46f1-84d2-ecc7c952a5ea', internal_env_fallback: true, external_env_fallback: false, internal_bound_by_ui: false, external_bound_by_ui: true, internal_kb_name: '', external_kb_name: 'governance-external-local', updated_by: 'yao', weknora_connected: true, } describe('routing bindings API', () => { it('reads the active binding projection', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: { ...binding, secret: 'drop' } }), { status: 200 }))) const result = await fetchRoutingBindings() expect(result.internal_env_fallback).toBe(true) expect(result.external_bound_by_ui).toBe(true) expect(result).not.toHaveProperty('secret') vi.unstubAllGlobals() }) it('lists knowledge base options for the picker', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [{ id: 'kb-1', name: '对内', knowledge_count: 4 }, { id: '', name: '坏行' }] }), { status: 200 }))) expect(await fetchRoutingBindingOptions()).toEqual([{ id: 'kb-1', name: '对内', knowledge_count: 4 }]) vi.unstubAllGlobals() }) it('saves bindings with an idempotency key and surfaces the server reason', async () => { const fetchMock = vi.fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: binding }), { status: 200 })) .mockResolvedValueOnce(new Response(JSON.stringify({ error: '对内与对外必须绑定不同的知识库' }), { status: 400 })) vi.stubGlobal('fetch', fetchMock) await expect(saveRoutingBindings({ internal_kb_id: 'kb-1', external_kb_id: '' })).resolves.toMatchObject({ internal_bound_by_ui: false }) const [, init] = fetchMock.mock.calls[0] expect(init.method).toBe('PUT') expect(init.headers['Idempotency-Key']).toBeTruthy() await expect(saveRoutingBindings({ internal_kb_id: 'kb-1', external_kb_id: 'kb-1' })).rejects.toThrow('对内与对外必须绑定不同的知识库') vi.unstubAllGlobals() }) })