From e20997748e33baae8a5103fa4c51cd3553b940e5 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Fri, 13 Dec 2024 09:54:16 -0800 Subject: [PATCH] test: fix flaky tests --- extensions/vscode/e2e/TestUtils.ts | 8 +-- .../e2e/actions/Autocomplete.actions.ts | 4 +- extensions/vscode/e2e/actions/GUI.actions.ts | 2 +- extensions/vscode/e2e/tests/GUI.test.ts | 24 ++++---- extensions/vscode/e2e/tests/SSH.test.ts | 58 ++++++++----------- 5 files changed, 43 insertions(+), 53 deletions(-) diff --git a/extensions/vscode/e2e/TestUtils.ts b/extensions/vscode/e2e/TestUtils.ts index 3afe83df4..4fb70a31a 100644 --- a/extensions/vscode/e2e/TestUtils.ts +++ b/extensions/vscode/e2e/TestUtils.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import { WebElement } from "vscode-extension-tester"; export class TestUtils { - public static async waitForElement( + public static async waitForSuccess( locatorFn: () => Promise, timeout: number = 5000, interval: number = 500, @@ -11,10 +11,8 @@ export class TestUtils { while (Date.now() - startTime < timeout) { try { - const element = await locatorFn(); - if (element) { - return element; - } + const result = await locatorFn(); + return result; } catch (e) { if (Date.now() - startTime >= timeout) { throw new Error(`Element not found after ${timeout}ms timeout`); diff --git a/extensions/vscode/e2e/actions/Autocomplete.actions.ts b/extensions/vscode/e2e/actions/Autocomplete.actions.ts index 7d879cd57..df1e06c1b 100644 --- a/extensions/vscode/e2e/actions/Autocomplete.actions.ts +++ b/extensions/vscode/e2e/actions/Autocomplete.actions.ts @@ -12,7 +12,7 @@ export class AutocompleteActions { const messagePair0 = TestUtils.generateTestMessagePair(0); await editor.typeTextAt(1, 1, messagePair0.userMessage); await editor.typeTextAt(1, messagePair0.userMessage.length + 1, " "); - const ghostText0 = await TestUtils.waitForElement( + const ghostText0 = await TestUtils.waitForSuccess( () => AutocompleteSelectors.getGhostTextContent(driver), // The first completion takes longer because Continue needs to load DEFAULT_TIMEOUT, @@ -24,7 +24,7 @@ export class AutocompleteActions { const messagePair1 = TestUtils.generateTestMessagePair(1); await editor.typeTextAt(1, 1, messagePair1.userMessage); await editor.typeTextAt(1, messagePair1.userMessage.length + 1, " "); - const ghostText1 = await TestUtils.waitForElement(() => + const ghostText1 = await TestUtils.waitForSuccess(() => AutocompleteSelectors.getGhostTextContent(driver), ); expect(ghostText1).to.equal(messagePair1.llmResponse); diff --git a/extensions/vscode/e2e/actions/GUI.actions.ts b/extensions/vscode/e2e/actions/GUI.actions.ts index b4afcd1c9..68e8e03d2 100644 --- a/extensions/vscode/e2e/actions/GUI.actions.ts +++ b/extensions/vscode/e2e/actions/GUI.actions.ts @@ -51,7 +51,7 @@ export class GUIActions { const dropdownButton = await GUISelectors.getModelDropdownButton(view); await dropdownButton.click(); - const dropdownOption = await TestUtils.waitForElement(() => { + const dropdownOption = await TestUtils.waitForSuccess(() => { return GUISelectors.getModelDropdownOption(view, option); }); diff --git a/extensions/vscode/e2e/tests/GUI.test.ts b/extensions/vscode/e2e/tests/GUI.test.ts index 013c47509..4acad2779 100644 --- a/extensions/vscode/e2e/tests/GUI.test.ts +++ b/extensions/vscode/e2e/tests/GUI.test.ts @@ -54,7 +54,7 @@ describe("GUI Test", () => { const messagePair = TestUtils.generateTestMessagePair(); await messageInput.sendKeys(messagePair.userMessage); await messageInput.sendKeys(Key.ENTER); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, messagePair.llmResponse), ); }); @@ -64,7 +64,7 @@ describe("GUI Test", () => { const messagePair = TestUtils.generateTestMessagePair(); await messageInput.sendKeys(messagePair.userMessage); (await GUISelectors.getSubmitInputButton(view)).click(); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, messagePair.llmResponse), ); }); @@ -77,7 +77,7 @@ describe("GUI Test", () => { message: userMessage0, inputFieldIndex: 0, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse0), ); @@ -88,7 +88,7 @@ describe("GUI Test", () => { message: userMessage1, inputFieldIndex: 1, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse1), ); @@ -99,7 +99,7 @@ describe("GUI Test", () => { message: userMessage2, inputFieldIndex: 2, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse2), ); @@ -130,7 +130,7 @@ describe("GUI Test", () => { message: userMessage0, inputFieldIndex: 0, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse0), ); @@ -141,7 +141,7 @@ describe("GUI Test", () => { message: userMessage1, inputFieldIndex: 1, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse1), ); @@ -152,7 +152,7 @@ describe("GUI Test", () => { message: userMessage2, inputFieldIndex: 2, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse2), ); @@ -172,7 +172,7 @@ describe("GUI Test", () => { }); await GUISelectors.getThreadMessageByText(view, llmResponse0); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, llmResponse3), ); await Promise.all([ @@ -194,7 +194,7 @@ describe("GUI Test", () => { message: messagePair1.userMessage, inputFieldIndex: 0, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, messagePair1.llmResponse), ); @@ -204,7 +204,7 @@ describe("GUI Test", () => { message: messagePair2.userMessage, inputFieldIndex: 1, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, messagePair2.llmResponse), ); @@ -238,7 +238,7 @@ describe("GUI Test", () => { message: messagePair3.userMessage, inputFieldIndex: 2, }); - await TestUtils.waitForElement(() => + await TestUtils.waitForSuccess(() => GUISelectors.getThreadMessageByText(view, messagePair3.llmResponse), ); }).timeout(DEFAULT_TIMEOUT); diff --git a/extensions/vscode/e2e/tests/SSH.test.ts b/extensions/vscode/e2e/tests/SSH.test.ts index 52c465861..0d4666c9b 100644 --- a/extensions/vscode/e2e/tests/SSH.test.ts +++ b/extensions/vscode/e2e/tests/SSH.test.ts @@ -3,12 +3,15 @@ import { TextEditor, InputBox, Workbench, + VSBrowser, + Key, } from "vscode-extension-tester"; import { DEFAULT_TIMEOUT } from "../constants"; import { TestUtils } from "../TestUtils"; import { SSHSelectors } from "../selectors/SSH.selectors"; import { AutocompleteActions } from "../actions/Autocomplete.actions"; +import * as path from "path"; describe("SSH", function () { this.retries(2); // Retries failed tests up to 2 times @@ -19,49 +22,38 @@ describe("SSH", function () { await new Workbench().executeCommand( "Remote-SSH: Connect Current Window to Host...", ); - await (await InputBox.create()).selectQuickPick("ssh-test-container"); + await ( + await InputBox.create(DEFAULT_TIMEOUT) + ).selectQuickPick("ssh-test-container"); - await TestUtils.waitForElement( + await TestUtils.waitForSuccess( () => SSHSelectors.connectedToRemoteConfirmationMessage(), DEFAULT_TIMEOUT, ); - await new Promise((res) => { - setTimeout(res, 1000); - }); + await TestUtils.waitForSuccess(async () => { + await new Workbench().executeCommand("File: Open Folder..."); + const inputBox = await InputBox.create(DEFAULT_TIMEOUT); + await inputBox.setText("/home/ec2-user/test-folder"); + await inputBox.confirm(); + }, DEFAULT_TIMEOUT); - await new Workbench().executeCommand("File: Open Folder..."); - await new Promise((res) => { - setTimeout(res, 4000); - }); - - const inputBox = await TestUtils.waitForElement( - () => InputBox.create(), - DEFAULT_TIMEOUT, - ); - await inputBox.selectQuickPick("test-folder"); - await new Promise((res) => { - setTimeout(res, 4000); - }); - await inputBox.confirm(); - - await new Promise((res) => { - setTimeout(res, 4000); - }); - - await new Workbench().executeCommand("File: Open File..."); - await (await InputBox.create(5000)).selectQuickPick("main.py"); - await new Promise((res) => { - setTimeout(res, 4000); - }); + await TestUtils.waitForSuccess(async () => { + await new Workbench().executeCommand("File: Open File..."); + const inputBox = await InputBox.create(DEFAULT_TIMEOUT); + await inputBox.setText("/home/ec2-user/test-folder/main.py"); + await inputBox.selectQuickPick("main.py"); + await inputBox.sendKeys(Key.ENTER); + }, DEFAULT_TIMEOUT); }); it("Should display completions", async () => { - const editor = (await new EditorView().openEditor("main.py")) as TextEditor; + const editor = await TestUtils.waitForSuccess( + async () => (await new EditorView().openEditor("main.py")) as TextEditor, + DEFAULT_TIMEOUT, + ); + await editor.clearText(); await AutocompleteActions.testCompletions(editor); - // await new Promise((res) => { - // setTimeout(res, DEFAULT_TIMEOUT); - // }); }).timeout(DEFAULT_TIMEOUT); });