Skip to content

Commit 23ed452

Browse files
[8.19] add test: visualize_query (#215808) (#220223)
# Backport This will backport the following commits from `main` to `8.19`: - [add test: visualize_query (#215808)](#215808) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Arturo Lidueña","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-05-06T10:27:06Z","message":"add test: visualize_query (#215808)\n\nRelated: https://github.com/elastic/kibana/issues/180787\n\n- Add test for `visualize_query` function","sha":"7541c1ac065484054ab422e8b682eac702bfb6a5","branchLabelMapping":{"^v9.1.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Obs AI Assistant","backport:version","v9.1.0","v8.19.0"],"title":"add test: visualize_query","number":215808,"url":"https://github.com/elastic/kibana/pull/215808","mergeCommit":{"message":"add test: visualize_query (#215808)\n\nRelated: https://github.com/elastic/kibana/issues/180787\n\n- Add test for `visualize_query` function","sha":"7541c1ac065484054ab422e8b682eac702bfb6a5"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.19"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/215808","number":215808,"mergeCommit":{"message":"add test: visualize_query (#215808)\n\nRelated: https://github.com/elastic/kibana/issues/180787\n\n- Add test for `visualize_query` function","sha":"7541c1ac065484054ab422e8b682eac702bfb6a5"}},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Arturo Lidueña <[email protected]>
1 parent 4b8b5cf commit 23ed452

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 expect from '@kbn/expect';
9+
import { MessageAddEvent, MessageRole } from '@kbn/observability-ai-assistant-plugin/common';
10+
import { VisualizeESQLUserIntention } from '@kbn/observability-ai-assistant-plugin/common/functions/visualize_esql';
11+
import {
12+
LlmProxy,
13+
createLlmProxy,
14+
} from '../../../../../../../observability_ai_assistant_api_integration/common/create_llm_proxy';
15+
import {
16+
getMessageAddedEvents,
17+
invokeChatCompleteWithFunctionRequest,
18+
} from '../../utils/conversation';
19+
import type { DeploymentAgnosticFtrProviderContext } from '../../../../../ftr_provider_context';
20+
21+
export default function ApiTest({ getService }: DeploymentAgnosticFtrProviderContext) {
22+
const es = getService('es');
23+
const log = getService('log');
24+
const observabilityAIAssistantAPIClient = getService('observabilityAIAssistantApi');
25+
26+
describe('visualize_query', function () {
27+
this.tags(['skipCloud']);
28+
let llmProxy: LlmProxy;
29+
let connectorId: string;
30+
let events: MessageAddEvent[];
31+
const query = `FROM test_index`;
32+
33+
before(async () => {
34+
llmProxy = await createLlmProxy(log);
35+
connectorId = await observabilityAIAssistantAPIClient.createProxyActionConnector({
36+
port: llmProxy.getPort(),
37+
});
38+
await es.index({
39+
index: 'test_index',
40+
refresh: true,
41+
id: 'index_id',
42+
document: { bar: 'foo' },
43+
});
44+
void llmProxy.interceptWithResponse('Hello from LLM Proxy');
45+
const responseBody = await invokeChatCompleteWithFunctionRequest({
46+
connectorId,
47+
observabilityAIAssistantAPIClient,
48+
functionCall: {
49+
name: 'visualize_query',
50+
trigger: MessageRole.Assistant,
51+
arguments: JSON.stringify({
52+
query,
53+
intention: VisualizeESQLUserIntention.visualizeAuto,
54+
}),
55+
},
56+
});
57+
58+
await llmProxy.waitForAllInterceptorsToHaveBeenCalled();
59+
60+
events = getMessageAddedEvents(responseBody);
61+
});
62+
63+
after(async () => {
64+
await es.indices.delete({
65+
index: 'test_index',
66+
});
67+
llmProxy.close();
68+
await observabilityAIAssistantAPIClient.deleteActionConnector({
69+
actionId: connectorId,
70+
});
71+
});
72+
73+
it('should execute the visualize_query function and return expected messages', async () => {
74+
const functionResponse = events[0];
75+
expect(functionResponse.message.message.name).to.be('visualize_query');
76+
77+
const parsedResponse = JSON.parse(functionResponse.message.message.content!);
78+
expect(parsedResponse.message).to.contain(query);
79+
});
80+
81+
it('should contain expected document data in response', async () => {
82+
const functionResponse = events[0];
83+
const parsedData = JSON.parse(functionResponse.message.message.data!);
84+
85+
expect(parsedData.columns[0].id).to.be('bar');
86+
expect(parsedData.rows[0][0]).to.be('foo');
87+
expect(parsedData).to.have.property('correctedQuery', query);
88+
});
89+
});
90+
}

x-pack/test/api_integration/deployment_agnostic/apis/observability/ai_assistant/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function aiAssistantApiIntegrationTests({
2222
loadTestFile(require.resolve('./complete/functions/retrieve_elastic_doc.spec.ts'));
2323
loadTestFile(require.resolve('./complete/functions/summarize.spec.ts'));
2424
loadTestFile(require.resolve('./complete/functions/title_conversation.spec.ts'));
25+
loadTestFile(require.resolve('./complete/functions/visualize_query.spec.ts'));
2526

2627
// knowledge base
2728
loadTestFile(require.resolve('./knowledge_base/knowledge_base_8.10_upgrade_test.spec.ts'));

0 commit comments

Comments
 (0)