import { describe, expect, it, vi } from 'vitest' import { fetchProductionBindingOptions, fetchProductionBindings, saveProductionBindings } from './productionBindings' const binding = { internal_kb_id: '0b217e8a-2a65-40a3-ab8f-34ac5c7a6f05', external_kb_id: '', internal_kb_name: '对内知识库-wiki', external_kb_name: '', updated_by: 'admin', production_base_url: 'https://weknora-test.dobot.cn/', production_connected: true, production_message: '', } describe('production bindings API', () => { it('reads the 源库 → 目标库 projection without leaking extra fields', async () => { vi.stubGlobal( 'fetch', vi .fn() .mockResolvedValue(new Response(JSON.stringify({ data: { ...binding, api_key: 'drop' } }), { status: 200 })) ) const result = await fetchProductionBindings() expect(result.internal_kb_name).toBe('对内知识库-wiki') expect(result.production_connected).toBe(true) expect(result).not.toHaveProperty('api_key') vi.unstubAllGlobals() }) it('lists production knowledge base options for the picker', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( new Response( JSON.stringify({ data: [ { id: 'kb-1', name: '对内知识库-wiki', knowledge_count: 2069 }, { id: '', name: '坏行' }, ], }), { status: 200 } ) ) ) expect(await fetchProductionBindingOptions()).toEqual([ { id: 'kb-1', name: '对内知识库-wiki', knowledge_count: 2069 }, ]) vi.unstubAllGlobals() }) it('saves with an idempotency key and surfaces a rejected base', async () => { const fetchMock = vi .fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: binding }), { status: 200 })) .mockResolvedValueOnce( new Response(JSON.stringify({ error: '对内绑定的生产知识库在该 WeKnora 实例中不存在' }), { status: 400 }) ) vi.stubGlobal('fetch', fetchMock) await expect(saveProductionBindings({ internal_kb_id: 'kb-1', external_kb_id: '' })).resolves.toMatchObject({ internal_kb_name: '对内知识库-wiki', }) const [, init] = fetchMock.mock.calls[0] expect(init.method).toBe('PUT') expect(init.headers['Idempotency-Key']).toBeTruthy() await expect(saveProductionBindings({ internal_kb_id: 'kb-ghost', external_kb_id: '' })).rejects.toThrow('不存在') vi.unstubAllGlobals() }) it('reports an unverified save honestly instead of pretending it was checked', async () => { vi.stubGlobal( 'fetch', vi .fn() .mockResolvedValue( new Response( JSON.stringify({ data: { ...binding, production_connected: false, production_message: '生产 WeKnora 不可用:dial tcp: timeout', }, }), { status: 200 } ) ) ) const result = await saveProductionBindings({ internal_kb_id: 'kb-1', external_kb_id: '' }) expect(result.production_connected).toBe(false) expect(result.production_message).toContain('不可用') vi.unstubAllGlobals() }) })