forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.test.ts
More file actions
218 lines (181 loc) · 8.18 KB
/
Copy pathplugin.test.ts
File metadata and controls
218 lines (181 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { DEFAULT_APP_CATEGORIES } from '@kbn/core/server';
import { actionsMock } from '@kbn/actions-plugin/server/mocks';
import { SavedObjectsErrorHelpers } from '@kbn/core/server';
import { coreMock, httpServerMock, savedObjectsClientMock } from '@kbn/core/server/mocks';
import { featuresPluginMock } from '@kbn/features-plugin/server/mocks';
import { inferenceMock } from '@kbn/inference-plugin/server/mocks';
import { SearchInferenceEndpointsPlugin } from './plugin';
import {
ELASTIC_INFERENCE_SERVICE_APP_ID,
INFERENCE_ENDPOINTS_APP_ID,
INFERENCE_SETTINGS_SO_TYPE,
MODEL_SETTINGS_APP_ID,
PLUGIN_ID,
PLUGIN_NAME,
} from '../common/constants';
import type { SearchInferenceEndpointsConfig } from './config';
describe('SearchInferenceEndpointsPlugin', () => {
let plugin: SearchInferenceEndpointsPlugin;
let coreSetup: ReturnType<typeof coreMock.createSetup>;
let features: ReturnType<typeof featuresPluginMock.createSetup>;
beforeEach(() => {
const context = coreMock.createPluginInitializerContext<SearchInferenceEndpointsConfig>({
enabled: true,
ui: { enabled: true },
dynamicConnectors: { enabled: false, pollingIntervalMins: 15 },
});
plugin = new SearchInferenceEndpointsPlugin(context);
coreSetup = coreMock.createSetup();
features = featuresPluginMock.createSetup();
});
describe('setup()', () => {
it('registers routes', () => {
plugin.setup(coreSetup, { features });
expect(coreSetup.http.createRouter).toHaveBeenCalledTimes(1);
});
it('registers the kibana feature with correct properties', () => {
plugin.setup(coreSetup, { features });
expect(features.registerKibanaFeature).toHaveBeenCalledTimes(1);
const feature = features.registerKibanaFeature.mock.calls[0][0];
expect(feature).toMatchObject({
id: PLUGIN_ID,
name: PLUGIN_NAME,
minimumLicense: 'enterprise',
category: DEFAULT_APP_CATEGORIES.management,
management: {
modelManagement: [
ELASTIC_INFERENCE_SERVICE_APP_ID,
INFERENCE_ENDPOINTS_APP_ID,
MODEL_SETTINGS_APP_ID,
],
},
});
});
it('registers feature with all privilege granting management access', () => {
plugin.setup(coreSetup, { features });
const feature = features.registerKibanaFeature.mock.calls[0][0];
expect(feature.privileges?.all).toMatchObject({
management: {
modelManagement: [
ELASTIC_INFERENCE_SERVICE_APP_ID,
INFERENCE_ENDPOINTS_APP_ID,
MODEL_SETTINGS_APP_ID,
],
},
});
});
it('registers feature with read privilege disabled', () => {
plugin.setup(coreSetup, { features });
const feature = features.registerKibanaFeature.mock.calls[0][0];
expect(feature.privileges?.read).toMatchObject({
disabled: true,
});
});
});
describe('start()', () => {
let coreStart: ReturnType<typeof coreMock.createStart>;
let startContract: ReturnType<SearchInferenceEndpointsPlugin['start']>;
beforeEach(() => {
coreStart = coreMock.createStart();
plugin.setup(coreSetup, { features });
const inference = inferenceMock.createStartContract();
inference.getConnectorList.mockResolvedValue([]);
inference.getConnectorById.mockRejectedValue(new Error('not found'));
startContract = plugin.start(coreStart, {
actions: actionsMock.createStart(),
inference,
});
});
it('endpoints.getForFeature reads inference settings with the internal client', async () => {
const request = httpServerMock.createKibanaRequest();
await startContract.endpoints.getForFeature('any_feature', request);
expect(coreStart.savedObjects.getUnsafeInternalClient).toHaveBeenCalledWith({
includedHiddenTypes: [INFERENCE_SETTINGS_SO_TYPE],
});
});
it('scopes the internal settings client to the namespace of the request', async () => {
const request = httpServerMock.createKibanaRequest();
const scopedClient = savedObjectsClientMock.create();
scopedClient.getCurrentNamespace.mockReturnValue('applications');
coreStart.savedObjects.getScopedClient.mockReturnValue(scopedClient);
const internalClient = savedObjectsClientMock.create();
const spaceScopedClient = savedObjectsClientMock.create();
internalClient.asScopedToNamespace.mockReturnValue(spaceScopedClient);
coreStart.savedObjects.getUnsafeInternalClient.mockReturnValue(internalClient);
await startContract.endpoints.getForFeature('any_feature', request);
expect(internalClient.asScopedToNamespace).toHaveBeenCalledWith('applications');
expect(spaceScopedClient.get).toHaveBeenCalledWith(INFERENCE_SETTINGS_SO_TYPE, 'default');
expect(internalClient.get).not.toHaveBeenCalled();
});
it('does not scope the internal settings client when the request is in the default space', async () => {
const request = httpServerMock.createKibanaRequest();
const internalClient = savedObjectsClientMock.create();
coreStart.savedObjects.getUnsafeInternalClient.mockReturnValue(internalClient);
await startContract.endpoints.getForFeature('any_feature', request);
expect(internalClient.asScopedToNamespace).not.toHaveBeenCalled();
expect(internalClient.get).toHaveBeenCalledWith(INFERENCE_SETTINGS_SO_TYPE, 'default');
});
it('applies the admin-configured model list to users who cannot read the settings saved object', async () => {
const request = httpServerMock.createKibanaRequest();
const scopedClient = savedObjectsClientMock.create();
scopedClient.get.mockRejectedValue(
SavedObjectsErrorHelpers.decorateForbiddenError(
new Error(`Unable to get ${INFERENCE_SETTINGS_SO_TYPE}`)
)
);
coreStart.savedObjects.getScopedClient.mockReturnValue(scopedClient);
const internalClient = savedObjectsClientMock.create();
internalClient.get.mockResolvedValue({
id: 'default',
type: INFERENCE_SETTINGS_SO_TYPE,
references: [],
attributes: { features: [{ feature_id: 'any_feature', endpoints: [{ id: 'allowed' }] }] },
});
coreStart.savedObjects.getUnsafeInternalClient.mockReturnValue(internalClient);
const createConnector = (connectorId: string) => ({
connectorId,
name: connectorId,
type: '.gen-ai',
config: {},
capabilities: {},
isPreconfigured: false,
isInferenceEndpoint: false,
});
const inference = inferenceMock.createStartContract();
inference.getConnectorList.mockResolvedValue([
createConnector('allowed'),
createConnector('hidden'),
] as any);
inference.getConnectorById.mockImplementation(
async (id: string) => createConnector(id) as any
);
const contract = plugin.start(coreStart, { actions: actionsMock.createStart(), inference });
contract.features.register({
featureId: 'any_feature',
featureName: 'Any feature',
featureDescription: 'Any feature',
taskType: 'chat_completion',
recommendedEndpoints: [],
});
const result = await contract.endpoints.getForFeature('any_feature', request);
expect(result.soEntryFound).toBe(true);
expect(result.endpoints.map((e) => e.connectorId)).toEqual(['allowed']);
});
it('creates a separate scoped SO client per request, ensuring space isolation', async () => {
const requestA = httpServerMock.createKibanaRequest();
const requestB = httpServerMock.createKibanaRequest();
await startContract.endpoints.getForFeature('any_feature', requestA);
await startContract.endpoints.getForFeature('any_feature', requestB);
const calls = coreStart.savedObjects.getScopedClient.mock.calls;
const requestsUsed = calls.map(([req]) => req);
expect(requestsUsed).toContain(requestA);
expect(requestsUsed).toContain(requestB);
});
});
});