Skip to content

Commit

Permalink
Add chatbot test cases for default data source change (#1386) (#1407)
Browse files Browse the repository at this point in the history
* Add test cases for default data source change

Signed-off-by: Lin Wang <[email protected]>

* Fix get title from undefined

Signed-off-by: Lin Wang <[email protected]>

* Go to data source list first

Signed-off-by: Lin Wang <[email protected]>

* Wait two seconds

Signed-off-by: Lin Wang <[email protected]>

* Delete all data sources every time

Signed-off-by: Lin Wang <[email protected]>

* Delete all data sources before each case

Signed-off-by: Lin Wang <[email protected]>

* Wait for 3s

Signed-off-by: Lin Wang <[email protected]>

---------

Signed-off-by: Lin Wang <[email protected]>
(cherry picked from commit c46263e)

Co-authored-by: Lin Wang <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and wanglam authored Jun 13, 2024
1 parent ff0a607 commit 57f48b4
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 57f48b4

Please sign in to comment.