From d24af6627f6b28be2f951df8f1d3d7d000936819 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Mon, 16 Dec 2024 16:07:59 -0800 Subject: [PATCH 1/5] test: keyboard shortcuts --- extensions/vscode/e2e/TestUtils.ts | 10 ++- extensions/vscode/e2e/actions/GUI.actions.ts | 17 ++--- .../vscode/e2e/selectors/GUI.selectors.ts | 17 ++++- extensions/vscode/e2e/tests/GUI.test.ts | 9 +-- .../e2e/tests/KeyboardShortcuts.test.ts | 71 +++++++++++++++++++ extensions/vscode/package.json | 2 +- 6 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts diff --git a/extensions/vscode/e2e/TestUtils.ts b/extensions/vscode/e2e/TestUtils.ts index 653314462..97ea2fc8a 100644 --- a/extensions/vscode/e2e/TestUtils.ts +++ b/extensions/vscode/e2e/TestUtils.ts @@ -1,8 +1,16 @@ import { expect } from "chai"; -import { WebElement } from "vscode-extension-tester"; +import { WebElement, waitForAttributeValue } from "vscode-extension-tester"; import { DEFAULT_TIMEOUT } from "./constants"; export class TestUtils { + /** + * In many cases it might be more useful to use existing Selenium + * utilities. For example: + * + * await driver.wait(until.elementLocated(By.xpath(xpath)), 5000); + * + * There's also 'waitForAttributeValue'. + */ public static async waitForSuccess( locatorFn: () => Promise, timeout: number = DEFAULT_TIMEOUT.MD, diff --git a/extensions/vscode/e2e/actions/GUI.actions.ts b/extensions/vscode/e2e/actions/GUI.actions.ts index 5c5a006c1..402176b20 100644 --- a/extensions/vscode/e2e/actions/GUI.actions.ts +++ b/extensions/vscode/e2e/actions/GUI.actions.ts @@ -1,15 +1,12 @@ -import { - Key, - WebDriver, - WebElement, - WebView, - Workbench, -} from "vscode-extension-tester"; +import { Key, WebElement, WebView, Workbench } from "vscode-extension-tester"; import { GUISelectors } from "../selectors/GUI.selectors"; import { TestUtils } from "../TestUtils"; export class GUIActions { - public static switchToReactIframe = async (driver: WebDriver) => { + public static switchToReactIframe = async () => { + const view = new WebView(); + const driver = view.getDriver(); + const iframes = await GUISelectors.getAllIframes(driver); let continueIFrame: WebElement | undefined = undefined; for (let i = 0; i < iframes.length; i++) { @@ -38,6 +35,10 @@ export class GUIActions { } await driver.switchTo().frame(reactIFrame); + return { + view, + driver, + }; }; public static openGui = async () => { diff --git a/extensions/vscode/e2e/selectors/GUI.selectors.ts b/extensions/vscode/e2e/selectors/GUI.selectors.ts index c73b2c739..611bb1503 100644 --- a/extensions/vscode/e2e/selectors/GUI.selectors.ts +++ b/extensions/vscode/e2e/selectors/GUI.selectors.ts @@ -1,4 +1,4 @@ -import { By, WebDriver, WebView } from "vscode-extension-tester"; +import { By, until, WebDriver, WebView } from "vscode-extension-tester"; import { SelectorUtils } from "./SelectorUtils"; export class GUISelectors { @@ -14,7 +14,7 @@ export class GUISelectors { view: WebView, index: number, ) { - const elements = await view.findWebElements(By.className("tiptap")); + const elements = await this.getMessageInputFields(view); return elements[index]; } @@ -66,4 +66,17 @@ export class GUISelectors { public static getNewSessionNavButton(view: WebView) { return SelectorUtils.getElementByAriaLabel(view, "New Session"); } + + public static async getInputBoxCodeBlockAtIndex( + view: WebView, + index: number, + ) { + const firstInputField = await this.getMessageInputFieldAtIndex(view, index); + + const codeBlockElement = await firstInputField.findElement( + By.xpath(".//code"), + ); + + return codeBlockElement; + } } diff --git a/extensions/vscode/e2e/tests/GUI.test.ts b/extensions/vscode/e2e/tests/GUI.test.ts index 8e84d4fb1..d53f3ad73 100644 --- a/extensions/vscode/e2e/tests/GUI.test.ts +++ b/extensions/vscode/e2e/tests/GUI.test.ts @@ -20,10 +20,7 @@ describe("GUI Test", () => { await GUIActions.openGui(); - view = new WebView(); - driver = view.getDriver(); - - await GUIActions.switchToReactIframe(driver); + ({ view, driver } = await GUIActions.switchToReactIframe()); await GUIActions.selectModelFromDropdown(view, "TEST LLM"); }); @@ -212,7 +209,7 @@ describe("GUI Test", () => { */ await view.switchBack(); await (await GUISelectors.getHistoryNavButton(view)).click(); - await GUIActions.switchToReactIframe(driver); + await GUIActions.switchToReactIframe(); await (await GUISelectors.getNthHistoryTableRow(view, 0)).click(); @@ -222,7 +219,7 @@ describe("GUI Test", () => { * END OF SWITCHING BACK AND FORTH */ - await GUIActions.switchToReactIframe(driver); + await GUIActions.switchToReactIframe(); await (await GUISelectors.getNthHistoryTableRow(view, 0)).click(); await GUISelectors.getThreadMessageByText(view, messagePair1.llmResponse); diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts new file mode 100644 index 000000000..99bba376e --- /dev/null +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -0,0 +1,71 @@ +import { + WebDriver, + Key, + VSBrowser, + EditorView, + InputBox, + TextEditor, + Workbench, + waitForAttributeValue, + until, +} from "vscode-extension-tester"; +import { DEFAULT_TIMEOUT } from "../constants"; +import { GlobalActions } from "../actions/Global.actions"; +import { GUISelectors } from "../selectors/GUI.selectors"; +import { GUIActions } from "../actions/GUI.actions"; +import { expect } from "chai"; +import { TestUtils } from "../TestUtils"; +import { Test } from "mocha"; + +describe("Cmd+L Shortcut Test", () => { + let driver: WebDriver; + + beforeEach(async function () { + this.timeout(DEFAULT_TIMEOUT.XL); + + await GlobalActions.openTestWorkspace(); + + await TestUtils.waitForSuccess(async () => { + await new Workbench().executeCommand("Create: New File..."); + await ( + await InputBox.create(DEFAULT_TIMEOUT.MD) + ).selectQuickPick("Text File"); + }); + + driver = VSBrowser.instance.driver; + }); + + it("should select the current line with Cmd+L", async () => { + const text = "Hello, world!"; + + const editor = (await new EditorView().openEditor( + "Untitled-1", + )) as TextEditor; + await editor.setText(text); + await editor.selectText(text); + + // await driver.sleep(DEFAULT_TIMEOUT.XS); + + // Simulate Cmd+L + await driver + .actions() + .keyDown(Key.META) // Use Key.CONTROL for Windows/Linux + .sendKeys("l") + .keyUp(Key.META) + .perform(); + + const { view } = await GUIActions.switchToReactIframe(); + + // await GUIActions.selectModelFromDropdown(view, "TEST LLM"); + // await driver.sleep(DEFAULT_TIMEOUT.SM); + + const codeBlock = await TestUtils.waitForSuccess(() => + GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), + ); + const codeblockContent = await codeBlock.getAttribute( + "data-codeblockcontent", + ); + + expect(codeblockContent).to.equal(text); + }).timeout(DEFAULT_TIMEOUT.XL); +}); diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 9aa7c22ae..bbe239798 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -629,7 +629,7 @@ "e2e:copy-vsix": "chmod +x ./e2e/get-latest-vsix.sh && bash ./e2e/get-latest-vsix.sh", "e2e:install-vsix": "extest install-vsix -f ./e2e/vsix/continue.vsix --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage", "e2e:install-extensions": "extest install-from-marketplace ms-vscode-remote.remote-ssh --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage && extest install-from-marketplace ms-vscode-remote.remote-containers --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage && extest install-from-marketplace ms-vscode-remote.remote-wsl --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage", - "e2e:test": "extest run-tests './e2e/_output/tests/*.test.js' --code_settings settings.json --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage", + "e2e:test": "extest run-tests './e2e/_output/tests/KeyboardShortcuts.test.js' --code_settings settings.json --extensions_dir ./e2e/.test-extensions --storage ./e2e/storage", "e2e:clean": "rm -rf ./e2e/_output", "e2e:all": "npm run e2e:build && npm run e2e:compile && npm run e2e:create-storage && npm run e2e:get-chromedriver && npm run e2e:get-vscode && npm run e2e:sign-vscode && npm run e2e:copy-vsix && npm run e2e:install-vsix && npm run e2e:install-extensions && CONTINUE_GLOBAL_DIR=e2e/test-continue npm run e2e:test && npm run e2e:clean", "e2e:quick": "npm run e2e:compile && CONTINUE_GLOBAL_DIR=e2e/test-continue npm run e2e:test && npm run e2e:clean", From 1194c6791f06e4cf1fed9dc0965e2b86b8e8d0bf Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 17 Dec 2024 11:17:31 -0800 Subject: [PATCH 2/5] cleanup --- extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index 99bba376e..89ec37d33 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -44,8 +44,6 @@ describe("Cmd+L Shortcut Test", () => { await editor.setText(text); await editor.selectText(text); - // await driver.sleep(DEFAULT_TIMEOUT.XS); - // Simulate Cmd+L await driver .actions() @@ -56,9 +54,6 @@ describe("Cmd+L Shortcut Test", () => { const { view } = await GUIActions.switchToReactIframe(); - // await GUIActions.selectModelFromDropdown(view, "TEST LLM"); - // await driver.sleep(DEFAULT_TIMEOUT.SM); - const codeBlock = await TestUtils.waitForSuccess(() => GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), ); From 9618327cf45687f18aacdc04d62da86ce2d1e520 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 17 Dec 2024 11:22:59 -0800 Subject: [PATCH 3/5] fix: linux key --- extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index 89ec37d33..b49be7a0e 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -47,7 +47,7 @@ describe("Cmd+L Shortcut Test", () => { // Simulate Cmd+L await driver .actions() - .keyDown(Key.META) // Use Key.CONTROL for Windows/Linux + .keyDown(Key.CONTROL) // Use Key.CONTROL for Windows/Linux .sendKeys("l") .keyUp(Key.META) .perform(); From 1ace921ee7b6ce04a8ba8dba25e3b9099c808fb1 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 17 Dec 2024 11:28:37 -0800 Subject: [PATCH 4/5] test: linux keyboard shortcuts --- extensions/vscode/e2e/TestUtils.ts | 4 ++++ extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/extensions/vscode/e2e/TestUtils.ts b/extensions/vscode/e2e/TestUtils.ts index 97ea2fc8a..b566f3711 100644 --- a/extensions/vscode/e2e/TestUtils.ts +++ b/extensions/vscode/e2e/TestUtils.ts @@ -66,4 +66,8 @@ export class TestUtils { llmResponse: `TEST_LLM_RESPONSE_${id}`, }; } + + public static get isMacOS(): boolean { + return process.platform === "darwin"; + } } diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index b49be7a0e..1165fe047 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -47,7 +47,7 @@ describe("Cmd+L Shortcut Test", () => { // Simulate Cmd+L await driver .actions() - .keyDown(Key.CONTROL) // Use Key.CONTROL for Windows/Linux + .keyDown(TestUtils.isMacOS ? Key.META : Key.CONTROL) .sendKeys("l") .keyUp(Key.META) .perform(); From ccd3bb74da7f02275d2ba5efba24bda5010de757 Mon Sep 17 00:00:00 2001 From: tomasz-io Date: Tue, 17 Dec 2024 11:31:20 -0800 Subject: [PATCH 5/5] test: cmd + l edge cases --- extensions/vscode/e2e/TestUtils.ts | 10 +++- .../e2e/actions/KeyboardShortcuts.actions.ts | 14 +++++ .../vscode/e2e/selectors/GUI.selectors.ts | 4 ++ .../e2e/tests/KeyboardShortcuts.test.ts | 54 ++++++++++++------- 4 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts diff --git a/extensions/vscode/e2e/TestUtils.ts b/extensions/vscode/e2e/TestUtils.ts index b566f3711..92986e498 100644 --- a/extensions/vscode/e2e/TestUtils.ts +++ b/extensions/vscode/e2e/TestUtils.ts @@ -1,5 +1,9 @@ import { expect } from "chai"; -import { WebElement, waitForAttributeValue } from "vscode-extension-tester"; +import { + Key, + WebElement, + waitForAttributeValue, +} from "vscode-extension-tester"; import { DEFAULT_TIMEOUT } from "./constants"; export class TestUtils { @@ -70,4 +74,8 @@ export class TestUtils { public static get isMacOS(): boolean { return process.platform === "darwin"; } + + public static get osControlKey() { + return TestUtils.isMacOS ? Key.META : Key.CONTROL; + } } diff --git a/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts b/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts new file mode 100644 index 000000000..dfa6dee15 --- /dev/null +++ b/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts @@ -0,0 +1,14 @@ +import { WebDriver } from "vscode-extension-tester"; + +import { TestUtils } from "../TestUtils"; + +export class KeyboardShortcutsActions { + public static async executeFocusContinueInput(driver: WebDriver) { + return await driver + .actions() + .keyDown(TestUtils.osControlKey) + .sendKeys("l") + .keyUp(TestUtils.osControlKey) + .perform(); + } +} diff --git a/extensions/vscode/e2e/selectors/GUI.selectors.ts b/extensions/vscode/e2e/selectors/GUI.selectors.ts index 611bb1503..82c0292b4 100644 --- a/extensions/vscode/e2e/selectors/GUI.selectors.ts +++ b/extensions/vscode/e2e/selectors/GUI.selectors.ts @@ -79,4 +79,8 @@ export class GUISelectors { return codeBlockElement; } + + public static getContinueExtensionBadge(view: WebView) { + return SelectorUtils.getElementByAriaLabel(view, "Continue"); + } } diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index 1165fe047..b15d4ae81 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -1,30 +1,27 @@ import { WebDriver, - Key, VSBrowser, EditorView, InputBox, TextEditor, Workbench, - waitForAttributeValue, - until, + WebView, } from "vscode-extension-tester"; import { DEFAULT_TIMEOUT } from "../constants"; -import { GlobalActions } from "../actions/Global.actions"; import { GUISelectors } from "../selectors/GUI.selectors"; import { GUIActions } from "../actions/GUI.actions"; import { expect } from "chai"; import { TestUtils } from "../TestUtils"; -import { Test } from "mocha"; +import { KeyboardShortcutsActions } from "../actions/KeyboardShortcuts.actions"; describe("Cmd+L Shortcut Test", () => { let driver: WebDriver; + let editor: TextEditor; + let view: WebView; beforeEach(async function () { this.timeout(DEFAULT_TIMEOUT.XL); - await GlobalActions.openTestWorkspace(); - await TestUtils.waitForSuccess(async () => { await new Workbench().executeCommand("Create: New File..."); await ( @@ -33,26 +30,45 @@ describe("Cmd+L Shortcut Test", () => { }); driver = VSBrowser.instance.driver; + + editor = (await new EditorView().openEditor("Untitled-1")) as TextEditor; }); - it("should select the current line with Cmd+L", async () => { + afterEach(async function () { + this.timeout(DEFAULT_TIMEOUT.XL * 1000); + await view.switchBack(); + await editor.clearText(); + await TestUtils.waitForSuccess( + async () => (await GUISelectors.getContinueExtensionBadge(view)).click(), + DEFAULT_TIMEOUT.XS, + ); + + await new EditorView().closeAllEditors(); + }); + + it("Should not create a code block when Cmd+L is pressed without text highlighted", async () => { + const text = "Hello, world!"; + + await editor.setText(text); + + await KeyboardShortcutsActions.executeFocusContinueInput(driver); + + ({ view } = await GUIActions.switchToReactIframe()); + + await TestUtils.expectNoElement(async () => { + return GUISelectors.getInputBoxCodeBlockAtIndex(view, 0); + }, DEFAULT_TIMEOUT.XS); + }).timeout(DEFAULT_TIMEOUT.XL); + + it("Should create a code block when Cmd+L is pressed with text highlighted", async () => { const text = "Hello, world!"; - const editor = (await new EditorView().openEditor( - "Untitled-1", - )) as TextEditor; await editor.setText(text); await editor.selectText(text); - // Simulate Cmd+L - await driver - .actions() - .keyDown(TestUtils.isMacOS ? Key.META : Key.CONTROL) - .sendKeys("l") - .keyUp(Key.META) - .perform(); + await KeyboardShortcutsActions.executeFocusContinueInput(driver); - const { view } = await GUIActions.switchToReactIframe(); + ({ view } = await GUIActions.switchToReactIframe()); const codeBlock = await TestUtils.waitForSuccess(() => GUISelectors.getInputBoxCodeBlockAtIndex(view, 0),