Skip to content

Commit 5f57459

Browse files
committed
chore: changes from feedback
1 parent 1010161 commit 5f57459

File tree

1 file changed

+65
-95
lines changed

1 file changed

+65
-95
lines changed

packages/compass-e2e-tests/tests/assistant.test.ts

Lines changed: 65 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('MongoDB Assistant', function () {
3030
response?: MockAssistantResponse;
3131
}
3232
) => Promise<void>;
33+
let setAIOptIn: (newValue: boolean) => Promise<void>;
3334

3435
const testMessage = 'What is MongoDB?';
3536
const testResponse = 'MongoDB is a database.';
@@ -74,6 +75,19 @@ describe('MongoDB Assistant', function () {
7475
await submitButton.click();
7576
};
7677

78+
setAIOptIn = async (newValue: boolean) => {
79+
if (
80+
(await browser.getFeature('optInGenAIFeatures')) === true &&
81+
newValue === false
82+
) {
83+
// Reseting the opt-in to false can be tricky so it's best to start over in this case.
84+
compass = await init(this.test?.fullTitle(), { firstRun: false });
85+
return;
86+
}
87+
88+
await browser.setFeature('optInGenAIFeatures', newValue);
89+
};
90+
7791
await browser.setupDefaultConnections();
7892
await browser.connectToDefaults();
7993
await browser.selectConnectionMenuItem(
@@ -151,8 +165,8 @@ describe('MongoDB Assistant', function () {
151165
});
152166

153167
describe('before opt-in', function () {
154-
beforeEach(async function () {
155-
await setAIOptIn(browser, false);
168+
before(async function () {
169+
await setAIOptIn(false);
156170
});
157171

158172
it('does not send the message if the user declines the opt-in', async function () {
@@ -176,16 +190,14 @@ describe('MongoDB Assistant', function () {
176190
});
177191

178192
describe('entry points', function () {
179-
beforeEach(async function () {
180-
await setAIOptIn(browser, false);
181-
});
182-
183193
it('should display opt-in modal for connection error entry point', async function () {
184194
await browser.connectWithConnectionString(
185195
'mongodb-invalid://localhost:27017',
186196
{ connectionStatus: 'failure' }
187197
);
188-
await useErrorViewEntryPoint(browser);
198+
await browser.clickVisible(
199+
browser.$(Selectors.ConnectionToastErrorDebugButton)
200+
);
189201

190202
const optInModal = browser.$(Selectors.AIOptInModal);
191203
await optInModal.waitForDisplayed();
@@ -216,38 +228,40 @@ describe('MongoDB Assistant', function () {
216228
expect(await getDisplayedMessages(browser)).to.deep.equal([]);
217229
});
218230
});
231+
});
219232

220-
describe('opting in', function () {
221-
it('sends the message if the user opts in', async function () {
222-
await openAssistantDrawer(browser);
233+
describe('opting in', function () {
234+
before(async function () {
235+
await setAIOptIn(false);
236+
await openAssistantDrawer(browser);
237+
});
223238

224-
await sendMessage(testMessage);
239+
it('sends the message if the user opts in', async function () {
240+
await sendMessage(testMessage);
225241

226-
const optInModal = browser.$(Selectors.AIOptInModal);
227-
await optInModal.waitForDisplayed();
228-
expect(await optInModal.isDisplayed()).to.be.true;
242+
const optInModal = browser.$(Selectors.AIOptInModal);
243+
await optInModal.waitForDisplayed();
244+
expect(await optInModal.isDisplayed()).to.be.true;
229245

230-
const acceptButton = browser.$(Selectors.AIOptInModalAcceptButton);
231-
await acceptButton.waitForClickable();
232-
await acceptButton.click();
246+
const acceptButton = browser.$(Selectors.AIOptInModalAcceptButton);
247+
await acceptButton.waitForClickable();
248+
await acceptButton.click();
233249

234-
await optInModal.waitForDisplayed({ reverse: true });
250+
await optInModal.waitForDisplayed({ reverse: true });
235251

236-
const chatInput = browser.$(Selectors.AssistantChatInputTextArea);
237-
expect(await chatInput.getValue()).to.equal('');
252+
const chatInput = browser.$(Selectors.AssistantChatInputTextArea);
253+
expect(await chatInput.getValue()).to.equal('');
238254

239-
expect(await getDisplayedMessages(browser)).to.deep.equal([
240-
{ text: testMessage, role: 'user' },
241-
{ text: testResponse, role: 'assistant' },
242-
]);
243-
});
255+
expect(await getDisplayedMessages(browser)).to.deep.equal([
256+
{ text: testMessage, role: 'user' },
257+
{ text: testResponse, role: 'assistant' },
258+
]);
244259
});
245260
});
246261

247262
describe('after opt-in', function () {
248-
beforeEach(async function () {
249-
await setAIOptIn(browser, true);
250-
263+
before(async function () {
264+
await setAIOptIn(true);
251265
await openAssistantDrawer(browser);
252266
});
253267

@@ -275,7 +289,12 @@ describe('MongoDB Assistant', function () {
275289
});
276290

277291
it('displays multiple messages correctly', async function () {
278-
await sendMessage(testMessage);
292+
await sendMessage(testMessage, {
293+
response: {
294+
status: 200,
295+
body: testResponse,
296+
},
297+
});
279298

280299
await sendMessage('This is a different message', {
281300
response: {
@@ -331,17 +350,13 @@ describe('MongoDB Assistant', function () {
331350
const thumbsDownButton = assistantMessage.$(
332351
'[aria-label="Dislike this message"]'
333352
);
334-
await thumbsDownButton.waitForDisplayed();
335-
await thumbsDownButton.click();
353+
await browser.clickVisible(thumbsDownButton);
336354

337355
const feedbackTextarea = assistantMessage.$('textarea');
338356
await feedbackTextarea.waitForDisplayed();
339-
340357
await feedbackTextarea.setValue('This is a test feedback');
341358

342-
const submitButton = browser.$('button*=Submit');
343-
await submitButton.waitForClickable();
344-
await submitButton.click();
359+
await browser.clickVisible(browser.$('button*=Submit'));
345360

346361
await feedbackTextarea.waitForDisplayed({ reverse: true });
347362

@@ -356,7 +371,7 @@ describe('MongoDB Assistant', function () {
356371
describe('entry points', function () {
357372
describe('explain plan entry point', function () {
358373
before(async function () {
359-
await setAIOptIn(browser, true);
374+
await setAIOptIn(true);
360375
await setAIFeatures(browser, true);
361376

362377
mockAssistantServer.setResponse({
@@ -417,9 +432,7 @@ describe('MongoDB Assistant', function () {
417432
});
418433

419434
describe('error message entry point', function () {
420-
before(async function () {
421-
await setAIOptIn(browser, true);
422-
435+
before(function () {
423436
mockAssistantServer.setResponse({
424437
status: 200,
425438
body: 'You should review the connection string.',
@@ -431,7 +444,9 @@ describe('MongoDB Assistant', function () {
431444
'mongodb-invalid://localhost:27017',
432445
{ connectionStatus: 'failure' }
433446
);
434-
await useErrorViewEntryPoint(browser);
447+
await browser.clickVisible(
448+
browser.$(Selectors.ConnectionToastErrorDebugButton)
449+
);
435450

436451
const messages = await getDisplayedMessages(browser);
437452
expect(messages).deep.equal([
@@ -455,11 +470,9 @@ describe('MongoDB Assistant', function () {
455470
async function setAIFeatures(browser: CompassBrowser, newValue: boolean) {
456471
await browser.openSettingsModal('ai');
457472

458-
// Wait for AI settings content to be visible
459-
const aiSettingsContent = browser.$(
460-
Selectors.ArtificialIntelligenceSettingsContent
461-
);
462-
await aiSettingsContent.waitForDisplayed();
473+
await browser
474+
.$(Selectors.ArtificialIntelligenceSettingsContent)
475+
.waitForDisplayed();
463476

464477
const currentValue =
465478
(await browser
@@ -481,19 +494,8 @@ async function setAIFeatures(browser: CompassBrowser, newValue: boolean) {
481494
});
482495
}
483496

484-
async function setAIOptIn(browser: CompassBrowser, enabled: boolean) {
485-
// Reset the opt-in preference by using the execute command
486-
await browser.setFeature('optInGenAIFeatures', enabled);
487-
488-
// Wait for the IPC to be processed
489-
await browser.pause(500);
490-
}
491-
492497
async function openAssistantDrawer(browser: CompassBrowser) {
493-
const drawerButton = browser.$(Selectors.AssistantDrawerButton);
494-
await drawerButton.waitForDisplayed();
495-
await drawerButton.waitForClickable();
496-
await drawerButton.click();
498+
await browser.clickVisible(Selectors.AssistantDrawerButton);
497499
}
498500

499501
async function clearChat(browser: CompassBrowser) {
@@ -509,11 +511,8 @@ async function clearChat(browser: CompassBrowser) {
509511
}
510512

511513
async function getDisplayedMessages(browser: CompassBrowser) {
512-
// Wait for the messages container to be visible
513-
const chatMessages = browser.$(Selectors.AssistantChatMessages);
514-
await chatMessages.waitForDisplayed();
514+
await browser.$(Selectors.AssistantChatMessages).waitForDisplayed();
515515

516-
// Get all individual message elements
517516
const messageElements = await browser
518517
.$$(Selectors.AssistantChatMessage)
519518
.getElements();
@@ -540,42 +539,13 @@ async function getDisplayedMessages(browser: CompassBrowser) {
540539
}
541540

542541
async function useExplainPlanEntryPoint(browser: CompassBrowser) {
543-
// Open explain plan modal by clicking the explain button
544-
const explainButton = browser.$(Selectors.AggregationExplainButton);
545-
await explainButton.waitForDisplayed();
546-
await explainButton.click();
547-
548-
// Wait for the explain plan modal to open and finish loading
549-
const explainModal = browser.$(Selectors.AggregationExplainModal);
550-
await explainModal.waitForDisplayed();
551-
552-
// Wait for the explain plan to be ready (loader should disappear)
553-
const explainLoader = browser.$(Selectors.ExplainLoader);
554-
await explainLoader.waitForDisplayed({
555-
reverse: true,
556-
timeout: 10000,
557-
});
558-
559-
// Click the "Interpret for me" button
560-
const interpretButton = browser.$(Selectors.ExplainPlanInterpretButton);
561-
await interpretButton.waitForDisplayed();
562-
await interpretButton.click();
542+
await browser.clickVisible(Selectors.AggregationExplainButton);
563543

564-
// The modal should close
565-
await explainModal.waitForDisplayed({ reverse: true });
544+
await browser.clickVisible(Selectors.ExplainPlanInterpretButton);
566545

567-
// The assistant drawer should open
568-
const assistantDrawer = browser.$(Selectors.SideDrawer);
569-
await assistantDrawer.waitForDisplayed();
570-
}
571-
572-
async function useErrorViewEntryPoint(browser: CompassBrowser) {
573-
const connectionToastErrorDebugButton = browser.$(
574-
Selectors.ConnectionToastErrorDebugButton
575-
);
576-
await connectionToastErrorDebugButton.waitForDisplayed();
577-
await connectionToastErrorDebugButton.click();
578-
await connectionToastErrorDebugButton.waitForDisplayed({
546+
await browser.$(Selectors.AggregationExplainModal).waitForDisplayed({
579547
reverse: true,
580548
});
549+
550+
await browser.$(Selectors.AssistantChatMessages).waitForDisplayed();
581551
}

0 commit comments

Comments
 (0)