|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import { PluginInitializerContext, CoreSetup } from '@kbn/core/server'; |
| 11 | +import type { AIAssistantManagementSelectionPluginServerDependenciesSetup } from './types'; |
| 12 | +import { AIAssistantType } from '../common/ai_assistant_type'; |
| 13 | +import { PREFERRED_AI_ASSISTANT_TYPE_SETTING_KEY } from '../common/ui_setting_keys'; |
| 14 | +import { classicSetting } from './src/settings/classic_setting'; |
| 15 | +import { observabilitySolutionSetting } from './src/settings/observability_setting'; |
| 16 | +import { securitySolutionSetting } from './src/settings/security_setting'; |
| 17 | +import { AIAssistantManagementSelectionPlugin } from './plugin'; |
| 18 | + |
| 19 | +describe('plugin', () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
| 23 | + describe('stateless', () => { |
| 24 | + beforeEach(() => { |
| 25 | + jest.clearAllMocks(); |
| 26 | + }); |
| 27 | + const initializerContext = { |
| 28 | + env: { |
| 29 | + packageInfo: { |
| 30 | + buildFlavor: 'serverless', |
| 31 | + }, |
| 32 | + }, |
| 33 | + config: { |
| 34 | + get: jest.fn(), |
| 35 | + }, |
| 36 | + } as unknown as PluginInitializerContext; |
| 37 | + |
| 38 | + const coreSetup = { |
| 39 | + uiSettings: { |
| 40 | + register: jest.fn(), |
| 41 | + }, |
| 42 | + capabilities: { |
| 43 | + registerProvider: jest.fn(), |
| 44 | + }, |
| 45 | + } as unknown as CoreSetup; |
| 46 | + |
| 47 | + const setupDeps = { |
| 48 | + management: { |
| 49 | + sections: { |
| 50 | + getSection: jest.fn(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + serverless: { |
| 54 | + uiSettings: { |
| 55 | + register: jest.fn(), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + it('registers correct uiSettings for serverless oblt', () => { |
| 61 | + (initializerContext.config.get as jest.Mock).mockReturnValue({ |
| 62 | + preferredAIAssistantType: AIAssistantType.Observability, |
| 63 | + }); |
| 64 | + const aiAssistantManagementSelectionPlugin = new AIAssistantManagementSelectionPlugin( |
| 65 | + initializerContext |
| 66 | + ); |
| 67 | + aiAssistantManagementSelectionPlugin.setup(coreSetup, { |
| 68 | + ...setupDeps, |
| 69 | + cloud: { |
| 70 | + serverless: { |
| 71 | + projectType: 'observability', |
| 72 | + }, |
| 73 | + }, |
| 74 | + } as unknown as AIAssistantManagementSelectionPluginServerDependenciesSetup); |
| 75 | + |
| 76 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledTimes(1); |
| 77 | + |
| 78 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledWith({ |
| 79 | + [PREFERRED_AI_ASSISTANT_TYPE_SETTING_KEY]: { |
| 80 | + ...observabilitySolutionSetting, |
| 81 | + value: AIAssistantType.Observability, |
| 82 | + }, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('registers correct uiSettings for serverless security', () => { |
| 87 | + (initializerContext.config.get as jest.Mock).mockReturnValue({ |
| 88 | + preferredAIAssistantType: AIAssistantType.Security, |
| 89 | + }); |
| 90 | + const aiAssistantManagementSelectionPlugin = new AIAssistantManagementSelectionPlugin( |
| 91 | + initializerContext |
| 92 | + ); |
| 93 | + aiAssistantManagementSelectionPlugin.setup(coreSetup, { |
| 94 | + ...setupDeps, |
| 95 | + cloud: { |
| 96 | + serverless: { |
| 97 | + projectType: 'security', |
| 98 | + }, |
| 99 | + }, |
| 100 | + } as unknown as AIAssistantManagementSelectionPluginServerDependenciesSetup); |
| 101 | + |
| 102 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledTimes(1); |
| 103 | + |
| 104 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledWith({ |
| 105 | + [PREFERRED_AI_ASSISTANT_TYPE_SETTING_KEY]: { |
| 106 | + ...securitySolutionSetting, |
| 107 | + value: AIAssistantType.Security, |
| 108 | + }, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('registers correct uiSettings for serverless search', () => { |
| 113 | + (initializerContext.config.get as jest.Mock).mockReturnValue({ |
| 114 | + preferredAIAssistantType: undefined, |
| 115 | + }); |
| 116 | + const aiAssistantManagementSelectionPlugin = new AIAssistantManagementSelectionPlugin( |
| 117 | + initializerContext |
| 118 | + ); |
| 119 | + aiAssistantManagementSelectionPlugin.setup(coreSetup, { |
| 120 | + ...setupDeps, |
| 121 | + cloud: { |
| 122 | + serverless: { |
| 123 | + projectType: 'search', |
| 124 | + }, |
| 125 | + }, |
| 126 | + } as unknown as AIAssistantManagementSelectionPluginServerDependenciesSetup); |
| 127 | + |
| 128 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledTimes(1); |
| 129 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledWith({ |
| 130 | + [PREFERRED_AI_ASSISTANT_TYPE_SETTING_KEY]: { |
| 131 | + ...classicSetting, |
| 132 | + value: AIAssistantType.Default, |
| 133 | + }, |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('stateful', () => { |
| 139 | + it('uses the correct setting key to get the correct value from uiSettings', async () => { |
| 140 | + const initializerContext = { |
| 141 | + env: { |
| 142 | + packageInfo: { |
| 143 | + buildFlavor: 'classic', |
| 144 | + }, |
| 145 | + }, |
| 146 | + config: { |
| 147 | + get: jest.fn().mockReturnValue({ |
| 148 | + preferredAIAssistantType: AIAssistantType.Observability, |
| 149 | + }), |
| 150 | + }, |
| 151 | + } as unknown as PluginInitializerContext; |
| 152 | + const aiAssistantManagementSelectionPlugin = new AIAssistantManagementSelectionPlugin( |
| 153 | + initializerContext |
| 154 | + ); |
| 155 | + |
| 156 | + const coreSetup = { |
| 157 | + uiSettings: { |
| 158 | + register: jest.fn(), |
| 159 | + }, |
| 160 | + capabilities: { |
| 161 | + registerProvider: jest.fn(), |
| 162 | + }, |
| 163 | + } as unknown as CoreSetup; |
| 164 | + |
| 165 | + const setupDeps = { |
| 166 | + management: { |
| 167 | + sections: { |
| 168 | + getSection: jest.fn(), |
| 169 | + }, |
| 170 | + }, |
| 171 | + serverless: { |
| 172 | + uiSettings: { |
| 173 | + register: jest.fn(), |
| 174 | + }, |
| 175 | + }, |
| 176 | + } as unknown as AIAssistantManagementSelectionPluginServerDependenciesSetup; |
| 177 | + |
| 178 | + aiAssistantManagementSelectionPlugin.setup(coreSetup, setupDeps); |
| 179 | + |
| 180 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledTimes(1); |
| 181 | + expect(coreSetup.uiSettings.register).toHaveBeenCalledWith({ |
| 182 | + [PREFERRED_AI_ASSISTANT_TYPE_SETTING_KEY]: { |
| 183 | + ...classicSetting, |
| 184 | + value: AIAssistantType.Observability, |
| 185 | + }, |
| 186 | + }); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments