From 57f48b4657bde0be66f5642cdf9a063db533a5fa Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 22:35:31 -0400 Subject: [PATCH] Add chatbot test cases for default data source change (#1386) (#1407) * Add test cases for default data source change Signed-off-by: Lin Wang * Fix get title from undefined Signed-off-by: Lin Wang * Go to data source list first Signed-off-by: Lin Wang * Wait two seconds Signed-off-by: Lin Wang * Delete all data sources every time Signed-off-by: Lin Wang * Delete all data sources before each case Signed-off-by: Lin Wang * Wait for 3s Signed-off-by: Lin Wang --------- Signed-off-by: Lin Wang (cherry picked from commit c46263e5e6616400afb08640873bdcfbf3e2c74b) Co-authored-by: Lin Wang --- .../mds_chatbot_default_data_source_change.js | 129 ++++++++++++++++++ .../commands.js | 43 +++--- 2 files changed, 152 insertions(+), 20 deletions(-) create mode 100644 cypress/integration/plugins/dashboards-assistant/mds_chatbot_default_data_source_change.js diff --git a/cypress/integration/plugins/dashboards-assistant/mds_chatbot_default_data_source_change.js b/cypress/integration/plugins/dashboards-assistant/mds_chatbot_default_data_source_change.js new file mode 100644 index 000000000..c491df2a4 --- /dev/null +++ b/cypress/integration/plugins/dashboards-assistant/mds_chatbot_default_data_source_change.js @@ -0,0 +1,129 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +const manualSetDefaultDataSource = (dataSourceTitle) => { + // Go to data source list + cy.getElementByTestId('dataSources').click(); + + cy.wait(1000); + + // Goto data source detail page + cy.contains(dataSourceTitle).click(); + + if ( + cy.getElementByTestId('editSetDefaultDataSource').contains('Set as default') + ) { + cy.wait(2000); + cy.getElementByTestId('editSetDefaultDataSource').click(); + } + // Back to data source list + cy.getElementByTestId('dataSources').click(); +}; + +const openChatBotAndSendMessage = () => { + // Common text to wait for to confirm page loaded, give up to 120 seconds for initial load + cy.get(`input[placeholder="Ask question"]`, { timeout: 120000 }).as( + 'chatInput' + ); + cy.get('@chatInput').should('be.length', 1); + + cy.wait(1000); + + cy.get('@chatInput').click(); + + cy.get('@chatInput').type('What are the indices in my cluster?{enter}'); + + // should have a LLM Response + cy.contains( + 'The indices in your cluster are the names listed in the response obtained from using a tool to get information about the OpenSearch indices.' + ).should('be.visible'); + + // Should have three bubbles + cy.get('[aria-label="chat message bubble"]').should('have.length', 3); +}; + +if ( + Cypress.env('DASHBOARDS_ASSISTANT_ENABLED') && + Cypress.env('DATASOURCE_MANAGEMENT_ENABLED') +) { + describe('Assistant basic spec', () => { + beforeEach(function () { + cy.deleteAllDataSources(); + // create default data source + cy.createDataSourceNoAuth({ title: 'DefaultDataSource' }).then( + ([dataSourceId]) => cy.setDefaultDataSource(dataSourceId) + ); + // create new data source + cy.createDataSourceNoAuth({ title: 'NewDataSource' }).as('newDataSource'); + + // Wait 3s for data source created + cy.wait(3000); + + cy.visit('/app/management/opensearch-dashboards/dataSources'); + cy.waitForLoader(); + }); + + it('should reload history with new data source id', function () { + // The header may render multiple times, wait for UI to be stable + cy.wait(5000); + // enable to toggle and show Chatbot + cy.get(`img[aria-label="toggle chat flyout icon"]`).click(); + cy.get('.llm-chat-flyout button[aria-label="history"]') + .should('be.visible') + .click(); + + cy.intercept('GET', '/api/assistant/conversations**').as( + 'loadConversationsRequest' + ); + + manualSetDefaultDataSource(this.newDataSource[1]); + + cy.wait('@loadConversationsRequest').then(({ request }) => { + expect(request.url).contains(this.newDataSource[0]); + }); + }); + + it('should not reset to chat tab after data source change in history page', function () { + openChatBotAndSendMessage(); + + cy.get('.llm-chat-flyout button[aria-label="history"]') + .should('be.visible') + .click(); + + manualSetDefaultDataSource(this.newDataSource[1]); + + // Should reset conversation and stay history page + cy.get('h3').contains('OpenSearch Assistant').should('be.visible'); + cy.get('h3').contains('Conversations').should('be.visible'); + }); + + it('should reset chat conversation after data source changed', function () { + openChatBotAndSendMessage(); + + manualSetDefaultDataSource(this.newDataSource[1]); + + // Should reset chat + cy.get('h3').contains('OpenSearch Assistant').should('be.visible'); + cy.get('[aria-label="chat message bubble"]').should('have.length', 1); + }); + + it('should reset chat tab after data source changed in trace page', function () { + openChatBotAndSendMessage(); + // click view trace button + cy.get(`[aria-label="How was this generated?"]`).click(); + cy.contains('How was this generated').should('be.visible'); + + manualSetDefaultDataSource(this.newDataSource[1]); + + // Should reset chat tab + cy.get('#how-was-this-generated').should('not.exist'); + cy.get('h3').contains('OpenSearch Assistant').should('be.visible'); + }); + + after(() => { + cy.deleteAllDataSources(); + }); + }); +} diff --git a/cypress/utils/dashboards/datasource-management-dashboards-plugin/commands.js b/cypress/utils/dashboards/datasource-management-dashboards-plugin/commands.js index 9982232f1..4cee061de 100644 --- a/cypress/utils/dashboards/datasource-management-dashboards-plugin/commands.js +++ b/cypress/utils/dashboards/datasource-management-dashboards-plugin/commands.js @@ -35,28 +35,31 @@ Cypress.Commands.add('deleteAllDataSources', () => { ); }); -Cypress.Commands.add('createDataSourceNoAuth', () => { - cy.request({ - method: 'POST', - url: `${BASE_PATH}/api/saved_objects/data-source`, - headers: { - 'osd-xsrf': true, - }, - body: { - attributes: { - title: 'RemoteDataSourceNoAuth', - endpoint: Cypress.env('remoteDataSourceNoAuthUrl'), - auth: { - type: 'no_auth', +Cypress.Commands.add( + 'createDataSourceNoAuth', + ({ title = 'RemoteDataSourceNoAuth' } = {}) => { + cy.request({ + method: 'POST', + url: `${BASE_PATH}/api/saved_objects/data-source`, + headers: { + 'osd-xsrf': true, + }, + body: { + attributes: { + title, + endpoint: Cypress.env('remoteDataSourceNoAuthUrl'), + auth: { + type: 'no_auth', + }, }, }, - }, - }).then((resp) => { - if (resp && resp.body && resp.body.id) { - return [resp.body.id, 'RemoteDataSourceNoAuth']; - } - }); -}); + }).then((resp) => { + if (resp && resp.body && resp.body.id) { + return [resp.body.id, title]; + } + }); + } +); Cypress.Commands.add('createDataSourceBasicAuth', () => { cy.request({