|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { HttpSetup } from '@kbn/core-http-browser'; |
| 9 | +import type { SettingsStart } from '@kbn/core-ui-settings-browser'; |
| 10 | +import { InferenceConnectorType, type InferenceConnector } from '@kbn/inference-common'; |
| 11 | +import { |
| 12 | + GEN_AI_SETTINGS_DEFAULT_AI_CONNECTOR, |
| 13 | + GEN_AI_SETTINGS_DEFAULT_AI_CONNECTOR_DEFAULT_ONLY, |
| 14 | +} from '@kbn/management-settings-ids'; |
| 15 | +import { loadConnectors, toAIConnector, applyConnectorSettings } from './load_connectors'; |
| 16 | +import { fetchConnectorsForFeature } from './fetch_connectors_for_feature'; |
| 17 | + |
| 18 | +jest.mock('./fetch_connectors_for_feature'); |
| 19 | +const fetchConnectorsForFeatureMock = fetchConnectorsForFeature as jest.MockedFn< |
| 20 | + typeof fetchConnectorsForFeature |
| 21 | +>; |
| 22 | + |
| 23 | +const createInferenceConnector = ( |
| 24 | + overrides: Partial<InferenceConnector> = {} |
| 25 | +): InferenceConnector => ({ |
| 26 | + type: InferenceConnectorType.OpenAI, |
| 27 | + name: 'Test Connector', |
| 28 | + connectorId: 'test-connector-id', |
| 29 | + config: {}, |
| 30 | + capabilities: {}, |
| 31 | + isInferenceEndpoint: true, |
| 32 | + isPreconfigured: false, |
| 33 | + ...overrides, |
| 34 | +}); |
| 35 | + |
| 36 | +const createSettings = (defaultConnectorId?: string, defaultOnly = false): SettingsStart => |
| 37 | + ({ |
| 38 | + client: { |
| 39 | + get: jest.fn((key: string) => { |
| 40 | + if (key === GEN_AI_SETTINGS_DEFAULT_AI_CONNECTOR) return defaultConnectorId; |
| 41 | + if (key === GEN_AI_SETTINGS_DEFAULT_AI_CONNECTOR_DEFAULT_ONLY) return defaultOnly; |
| 42 | + return undefined; |
| 43 | + }), |
| 44 | + }, |
| 45 | + } as unknown as SettingsStart); |
| 46 | + |
| 47 | +describe('toAIConnector', () => { |
| 48 | + it('should map InferenceConnector fields to AIConnector shape', () => { |
| 49 | + const connector = createInferenceConnector({ |
| 50 | + connectorId: 'my-endpoint', |
| 51 | + name: 'My Connector', |
| 52 | + type: InferenceConnectorType.Bedrock, |
| 53 | + config: { region: 'us-east-1' }, |
| 54 | + isPreconfigured: true, |
| 55 | + isDeprecated: true, |
| 56 | + isConnectorTypeDeprecated: true, |
| 57 | + isMissingSecrets: true, |
| 58 | + }); |
| 59 | + |
| 60 | + const result = toAIConnector(connector); |
| 61 | + |
| 62 | + expect(result).toEqual({ |
| 63 | + id: 'my-endpoint', |
| 64 | + name: 'My Connector', |
| 65 | + actionTypeId: InferenceConnectorType.Bedrock, |
| 66 | + config: { region: 'us-east-1' }, |
| 67 | + secrets: {}, |
| 68 | + isPreconfigured: true, |
| 69 | + isSystemAction: false, |
| 70 | + isDeprecated: true, |
| 71 | + isConnectorTypeDeprecated: true, |
| 72 | + isMissingSecrets: true, |
| 73 | + isRecommended: undefined, |
| 74 | + apiProvider: undefined, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should default optional boolean fields to false when undefined', () => { |
| 79 | + const connector = createInferenceConnector({ |
| 80 | + isDeprecated: undefined, |
| 81 | + isConnectorTypeDeprecated: undefined, |
| 82 | + isMissingSecrets: undefined, |
| 83 | + }); |
| 84 | + |
| 85 | + const result = toAIConnector(connector); |
| 86 | + |
| 87 | + expect(result.isDeprecated).toBe(false); |
| 88 | + expect(result.isConnectorTypeDeprecated).toBe(false); |
| 89 | + expect(result.isMissingSecrets).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should set apiProvider for non-preconfigured OpenAI connectors', () => { |
| 93 | + const connector = createInferenceConnector({ |
| 94 | + isPreconfigured: false, |
| 95 | + config: { apiProvider: 'OpenAI' }, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(toAIConnector(connector).apiProvider).toBe('OpenAI'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should not set apiProvider for preconfigured connectors', () => { |
| 102 | + const connector = createInferenceConnector({ |
| 103 | + isPreconfigured: true, |
| 104 | + config: { apiProvider: 'OpenAI' }, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(toAIConnector(connector).apiProvider).toBeUndefined(); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe('applyConnectorSettings', () => { |
| 112 | + it('should return all connectors when defaultOnly is false', () => { |
| 113 | + const connectors = [{ id: 'a' }, { id: 'b' }]; |
| 114 | + const settings = createSettings('a', false); |
| 115 | + |
| 116 | + expect(applyConnectorSettings(connectors, settings)).toEqual(connectors); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should filter to default connector when defaultOnly is true', () => { |
| 120 | + const connectors = [{ id: 'a' }, { id: 'b' }]; |
| 121 | + const settings = createSettings('a', true); |
| 122 | + |
| 123 | + expect(applyConnectorSettings(connectors, settings)).toEqual([{ id: 'a' }]); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should return all connectors when defaultOnly is true but default not found', () => { |
| 127 | + const connectors = [{ id: 'a' }, { id: 'b' }]; |
| 128 | + const settings = createSettings('missing', true); |
| 129 | + |
| 130 | + expect(applyConnectorSettings(connectors, settings)).toEqual(connectors); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should return all connectors when no default connector is configured', () => { |
| 134 | + const connectors = [{ id: 'a' }, { id: 'b' }]; |
| 135 | + const settings = createSettings(undefined, true); |
| 136 | + |
| 137 | + expect(applyConnectorSettings(connectors, settings)).toEqual(connectors); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +describe('loadConnectors', () => { |
| 142 | + const http = {} as HttpSetup; |
| 143 | + |
| 144 | + beforeEach(() => { |
| 145 | + jest.clearAllMocks(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should fetch connectors, map to AIConnector, and apply settings', async () => { |
| 149 | + const connector1 = createInferenceConnector({ connectorId: 'c1', name: 'Connector 1' }); |
| 150 | + const connector2 = createInferenceConnector({ connectorId: 'c2', name: 'Connector 2' }); |
| 151 | + fetchConnectorsForFeatureMock.mockResolvedValue({ |
| 152 | + connectors: [connector1, connector2], |
| 153 | + soEntryFound: false, |
| 154 | + }); |
| 155 | + const settings = createSettings(); |
| 156 | + |
| 157 | + const result = await loadConnectors({ http, featureId: 'siem_migrations', settings }); |
| 158 | + |
| 159 | + expect(fetchConnectorsForFeatureMock).toHaveBeenCalledWith(http, 'siem_migrations'); |
| 160 | + expect(result).toHaveLength(2); |
| 161 | + expect(result[0].id).toBe('c1'); |
| 162 | + expect(result[1].id).toBe('c2'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should apply default connector settings filter', async () => { |
| 166 | + const connector1 = createInferenceConnector({ connectorId: 'c1', name: 'Connector 1' }); |
| 167 | + const connector2 = createInferenceConnector({ connectorId: 'c2', name: 'Connector 2' }); |
| 168 | + fetchConnectorsForFeatureMock.mockResolvedValue({ |
| 169 | + connectors: [connector1, connector2], |
| 170 | + soEntryFound: false, |
| 171 | + }); |
| 172 | + const settings = createSettings('c1', true); |
| 173 | + |
| 174 | + const result = await loadConnectors({ http, featureId: 'test', settings }); |
| 175 | + |
| 176 | + expect(result).toHaveLength(1); |
| 177 | + expect(result[0].id).toBe('c1'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should return empty array when no connectors are available', async () => { |
| 181 | + fetchConnectorsForFeatureMock.mockResolvedValue({ |
| 182 | + connectors: [], |
| 183 | + soEntryFound: false, |
| 184 | + }); |
| 185 | + const settings = createSettings(); |
| 186 | + |
| 187 | + const result = await loadConnectors({ http, featureId: 'test', settings }); |
| 188 | + |
| 189 | + expect(result).toEqual([]); |
| 190 | + }); |
| 191 | +}); |
0 commit comments