Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions x-pack/platform/plugins/shared/elastic_console/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../../../..',
roots: [
'<rootDir>/x-pack/platform/plugins/shared/elastic_console/common',
'<rootDir>/x-pack/platform/plugins/shared/elastic_console/public',
'<rootDir>/x-pack/platform/plugins/shared/elastic_console/server',
],
};
3 changes: 3 additions & 0 deletions x-pack/platform/plugins/shared/elastic_console/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ tags:
- prod
- group-platform
- shared
- jest-unit-tests
fileGroups:
src:
- common/**/*
- public/**/*
- server/**/*
- '!target/**/*'
jest-config:
- jest.config.js
tasks: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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 type { CoreSetup } from '@kbn/core/server';
import { httpServerMock, httpServiceMock, loggingSystemMock } from '@kbn/core/server/mocks';
import { type InferenceConnector, InferenceConnectorType } from '@kbn/inference-common';
import { inferenceMock } from '@kbn/inference-plugin/server/mocks';
import type { ElasticConsolePluginStart, ElasticConsoleStartDependencies } from '../types';
import { isElasticConsoleEnabled } from './is_enabled';
import { registerModelsRoute } from './models';

jest.mock('./is_enabled', () => ({
isElasticConsoleEnabled: jest.fn(),
}));

const isElasticConsoleEnabledMock = isElasticConsoleEnabled as jest.MockedFunction<
typeof isElasticConsoleEnabled
>;

const createConnector = (parts: Partial<InferenceConnector> = {}): InferenceConnector => ({
connectorId: 'connector-1',
name: 'Connector 1',
type: InferenceConnectorType.OpenAI,
config: {},
capabilities: {},
isInferenceEndpoint: false,
isPreconfigured: false,
...parts,
});

describe('registerModelsRoute', () => {
let coreSetup: jest.Mocked<
Pick<CoreSetup<ElasticConsoleStartDependencies, ElasticConsolePluginStart>, 'getStartServices'>
>;
let inference: ReturnType<typeof inferenceMock.createStartContract>;
let router: ReturnType<typeof httpServiceMock.createRouter>;
const logger = loggingSystemMock.create().get();

beforeEach(() => {
jest.spyOn(Date, 'now').mockReturnValue(1700000000000);

inference = inferenceMock.createStartContract();
router = httpServiceMock.createRouter();
coreSetup = {
getStartServices: jest.fn().mockResolvedValue([{}, { inference }]),
};
isElasticConsoleEnabledMock.mockResolvedValue(true);
});

afterEach(() => {
jest.restoreAllMocks();
});

it('registers the OpenAI-compatible models route', () => {
registerModelsRoute({ router, coreSetup: coreSetup as CoreSetup, logger });

const [config] = router.get.mock.calls[0];
expect(config.path).toBe('/internal/elastic_ramen/v1/models');
expect(config.options?.access).toBe('internal');
});

it('returns connector context window sizes when available', async () => {
const request = httpServerMock.createKibanaRequest();
const response = httpServerMock.createResponseFactory();
inference.getConnectorList.mockResolvedValue([
createConnector({
connectorId: 'sonnet-4',
type: InferenceConnectorType.Bedrock,
capabilities: { contextWindowSize: 1000000 },
}),
createConnector({ connectorId: 'unknown-model' }),
]);

registerModelsRoute({ router, coreSetup: coreSetup as CoreSetup, logger });

const [, handler] = router.get.mock.calls[0];
await handler({}, request, response);

expect(inference.getConnectorList).toHaveBeenCalledWith(request);
expect(response.ok).toHaveBeenCalledWith({
body: {
object: 'list',
data: [
{
id: 'sonnet-4',
object: 'model',
created: 1700000000,
owned_by: InferenceConnectorType.Bedrock,
permission: [],
root: 'sonnet-4',
parent: null,
context_window_size: 1000000,
},
{
id: 'unknown-model',
object: 'model',
created: 1700000000,
owned_by: InferenceConnectorType.OpenAI,
permission: [],
root: 'unknown-model',
parent: null,
context_window_size: undefined,
},
],
},
});

const [{ body }] = response.ok.mock.calls[0];
const serializedBody = JSON.parse(JSON.stringify(body));
expect(serializedBody.data[0].context_window_size).toBe(1000000);
expect(serializedBody.data[1]).not.toHaveProperty('context_window_size');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const registerModelsRoute = ({
permission: [],
root: connector.connectorId,
parent: null,
context_window_size: connector.capabilities.contextWindowSize,
}));

return response.ok({
Expand Down
Loading