Skip to content

test: add validation tests for Elasticsearch tool operations#229986

Open
nehaaprasad wants to merge 5 commits intoelastic:mainfrom
nehaaprasad:test/validate-es-operations
Open

test: add validation tests for Elasticsearch tool operations#229986
nehaaprasad wants to merge 5 commits intoelastic:mainfrom
nehaaprasad:test/validate-es-operations

Conversation

@nehaaprasad
Copy link
Copy Markdown
Contributor

CLOSES: #229975

Summary

This PR addresses the review comments from #229497 by improving the Elasticsearch tool implementation. It extracts the operation validation logic into a well-named helper function isOperationAllowed and adds comprehensive API tests to ensure only certain operations are allowed through the AI assistant.

@nehaaprasad nehaaprasad requested a review from a team as a code owner July 30, 2025 16:38
@botelastic botelastic bot added ci:project-deploy-observability Create an Observability project Team:Obs AI Assistant Observability AI Assistant labels Jul 30, 2025
@cla-checker-service
Copy link
Copy Markdown

cla-checker-service bot commented Jul 30, 2025

💚 CLA has been signed

@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/obs-ai-assistant (Team:Obs AI Assistant)

@github-actions
Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Expand to view the GitHub comments

Just comment with:

  • /oblt-deploy : Deploy a Kibana instance using the Observability test environments.
  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

@sorenlouv
Copy link
Copy Markdown
Contributor

@viduni94 Can you take a look?

Copy link
Copy Markdown
Contributor

@viduni94 viduni94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @naaa760
I've added some comments

@@ -0,0 +1,426 @@
import expect from '@kbn/expect';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing tests related to this function is located in x-pack/solutions/observability/test/api_integration_deployment_agnostic/apis/ai_assistant/complete/functions/elasticsearch.spec.ts
Would you be able to move the new tests there?

});

await observabilityAIAssistantAPIClient.editor({
endpoint: 'POST /internal/observability_ai_assistant/chat',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling the /chat endpoint, you could use the existing helper function to invoke a chat complete request with the function call. Would you be able to update all the tests to use the invokeChatCompleteWithFunctionRequest helper method?

Example:

const responseBody = await invokeChatCompleteWithFunctionRequest({
        connectorId,
        observabilityAIAssistantAPIClient,
        functionCall: {
          name: ELASTICSEARCH_FUNCTION_NAME,
          trigger: MessageRole.User,
          arguments: JSON.stringify({
            method: 'POST',
            path: 'traces*/_search',
            body: {
              size: 0,
              aggs: {
                services: {
                  terms: {
                    field: 'service.name',
                  },
                },
              },
            },
          }),
        },
      });

expect(requestBody.messages[0].content).to.eql(SYSTEM_MESSAGE);
});

it('should allow POST requests to _search endpoint', async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should allow POST requests to _search endpoint', async () => {
it('should allow POST requests to the _search endpoint', async () => {

});
});

describe('Disallowed Operations', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all tests in this block, would you be able to check whether the function call response has an error that states the request is not allowed?

});
});

describe('Edge Cases', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
describe('Edge Cases', () => {
describe('Request paths with query parameters', () => {

},
];

describe('/internal/observability_ai_assistant/chat - Elasticsearch Tool Restrictions', function () {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After these tests are moved to the tool: elasticsearch test suite, the describe block can be renamed.

Suggested change
describe('/internal/observability_ai_assistant/chat - Elasticsearch Tool Restrictions', function () {
describe('tool restrictions', function () {

…strictions

- Replace system message validation with actual function execution testing
- Use invokeChatCompleteWithFunctionRequest helper as suggested by reviewer
- Test that allowed operations (GET, POST to _search) return success responses
- Test that disallowed operations (PUT, DELETE, PATCH, POST to non-search) return proper error messages
- Verify actual security restrictions rather than just system configuration
- Addresses reviewer feedback about validating function call responses vs system messages
@nehaaprasad
Copy link
Copy Markdown
Contributor Author

@viduni94
Hi! I've updated the tests to use invokeChatCompleteWithFunctionRequest and now properly validate whether function calls succeed/fail with the right error messages instead of just checking system messages.

@viduni94
Copy link
Copy Markdown
Contributor

viduni94 commented Aug 8, 2025

@viduni94 Hi! I've updated the tests to use invokeChatCompleteWithFunctionRequest and now properly validate whether function calls succeed/fail with the right error messages instead of just checking system messages.

Thank you for the changes. Would you be able to address the other comments as well?

Would you also be able to add some screenshots or screen recordings of testing the helper method in the UI and it to the PR description?
For example: Ask a question that would invoke the Elasticsearch tool where the tool would succeed and also a scenario where the tool would fail and inform the user about the limitations.

@momovdg momovdg requested a review from a team as a code owner March 11, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:project-deploy-observability Create an Observability project 💝community Team:Obs AI Assistant Observability AI Assistant

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Obs AI Assistant] Improve the Elasticsearch tool

5 participants