diff --git a/extensions/vscode/e2e/TestUtils.ts b/extensions/vscode/e2e/TestUtils.ts index 92986e498..e1ae22227 100644 --- a/extensions/vscode/e2e/TestUtils.ts +++ b/extensions/vscode/e2e/TestUtils.ts @@ -37,8 +37,8 @@ export class TestUtils { throw new Error(`Element not found after ${timeout}ms timeout`); } - public static async expectNoElement( - locatorFn: () => Promise, + public static async expectNoElement( + locatorFn: () => Promise, timeout: number = 1000, interval: number = 200, ): Promise { @@ -48,6 +48,7 @@ export class TestUtils { while (Date.now() - startTime < timeout) { try { const element = await locatorFn(); + console.log("ELEMENT", element); if (element) { elementFound = true; break; @@ -71,6 +72,10 @@ export class TestUtils { }; } + public static waitForTimeout(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); + } + public static get isMacOS(): boolean { return process.platform === "darwin"; } diff --git a/extensions/vscode/e2e/actions/GUI.actions.ts b/extensions/vscode/e2e/actions/GUI.actions.ts index 402176b20..567a2f572 100644 --- a/extensions/vscode/e2e/actions/GUI.actions.ts +++ b/extensions/vscode/e2e/actions/GUI.actions.ts @@ -1,8 +1,33 @@ -import { Key, WebElement, WebView, Workbench } from "vscode-extension-tester"; +import { + InputBox, + Key, + WebDriver, + WebElement, + WebView, + Workbench, +} from "vscode-extension-tester"; import { GUISelectors } from "../selectors/GUI.selectors"; import { TestUtils } from "../TestUtils"; +import { DEFAULT_TIMEOUT } from "../constants"; export class GUIActions { + public static moveContinueToSidebar = async (driver: WebDriver) => { + await GUIActions.toggleGui(); + await TestUtils.waitForSuccess(async () => { + await new Workbench().executeCommand("View: Move View"); + await (await InputBox.create(DEFAULT_TIMEOUT.MD)).selectQuickPick(4); + await (await InputBox.create(DEFAULT_TIMEOUT.MD)).selectQuickPick(14); + }); + + // first call focuses the input + await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); + await GUIActions.executeFocusContinueInputShortcut(driver); + + // second call closes the gui + await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); + await GUIActions.executeFocusContinueInputShortcut(driver); + }; + public static switchToReactIframe = async () => { const view = new WebView(); const driver = view.getDriver(); @@ -19,7 +44,7 @@ export class GUIActions { } if (!continueIFrame) { - throw new Error("Could not find continue iframe"); + throw new Error("Could not find Continue iframe"); } await driver.switchTo().frame(continueIFrame); @@ -41,7 +66,7 @@ export class GUIActions { }; }; - public static openGui = async () => { + public static toggleGui = async () => { return TestUtils.waitForSuccess(() => new Workbench().executeCommand("continue.focusContinueInput"), ); @@ -77,4 +102,13 @@ export class GUIActions { await editor.sendKeys(message); await editor.sendKeys(Key.ENTER); } + + public static async executeFocusContinueInputShortcut(driver: WebDriver) { + return driver + .actions() + .keyDown(TestUtils.osControlKey) + .sendKeys("l") + .keyUp(TestUtils.osControlKey) + .perform(); + } } diff --git a/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts b/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts index dfa6dee15..0a4cc51ad 100644 --- a/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts +++ b/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts @@ -1,14 +1,15 @@ -import { WebDriver } from "vscode-extension-tester"; +import { TextEditor, WebDriver, WebView } 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(); + /** + * For some reason Selenium-simulated keyboard shortcuts don't perfectly + * mimic the behavior of real shortcuts unless some text is highlighted first. + */ + public static async HACK__typeWithSelect(editor: TextEditor, text: string) { + await editor.typeText(text); + await editor.selectText(text); + await editor.typeText(text); } } diff --git a/extensions/vscode/e2e/tests/GUI.test.ts b/extensions/vscode/e2e/tests/GUI.test.ts index 9b3eca3e0..60b6723b8 100644 --- a/extensions/vscode/e2e/tests/GUI.test.ts +++ b/extensions/vscode/e2e/tests/GUI.test.ts @@ -18,7 +18,7 @@ describe("GUI Test", () => { beforeEach(async function () { this.timeout(DEFAULT_TIMEOUT.XL); - await GUIActions.openGui(); + await GUIActions.toggleGui(); ({ view, driver } = await GUIActions.switchToReactIframe()); await GUIActions.selectModelFromDropdown(view, "TEST LLM"); diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index b15d4ae81..837372bab 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -6,19 +6,24 @@ import { TextEditor, Workbench, WebView, + WebElement, } from "vscode-extension-tester"; import { DEFAULT_TIMEOUT } from "../constants"; import { GUISelectors } from "../selectors/GUI.selectors"; import { GUIActions } from "../actions/GUI.actions"; import { expect } from "chai"; import { TestUtils } from "../TestUtils"; -import { KeyboardShortcutsActions } from "../actions/KeyboardShortcuts.actions"; describe("Cmd+L Shortcut Test", () => { let driver: WebDriver; let editor: TextEditor; let view: WebView; + before(async function () { + this.timeout(DEFAULT_TIMEOUT.XL); + await GUIActions.moveContinueToSidebar(VSBrowser.instance.driver); + }); + beforeEach(async function () { this.timeout(DEFAULT_TIMEOUT.XL); @@ -38,11 +43,6 @@ describe("Cmd+L Shortcut Test", () => { 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(); }); @@ -51,13 +51,30 @@ describe("Cmd+L Shortcut Test", () => { await editor.setText(text); - await KeyboardShortcutsActions.executeFocusContinueInput(driver); + await GUIActions.executeFocusContinueInputShortcut(driver); ({ view } = await GUIActions.switchToReactIframe()); await TestUtils.expectNoElement(async () => { return GUISelectors.getInputBoxCodeBlockAtIndex(view, 0); }, DEFAULT_TIMEOUT.XS); + await GUIActions.executeFocusContinueInputShortcut(driver); + }).timeout(DEFAULT_TIMEOUT.XL); + + it("Fresh VS Code window → sidebar closed → cmd+L with no code highlighted → opens sidebar and focuses input → cmd+L closes sidebar", async () => { + await GUIActions.executeFocusContinueInputShortcut(driver); + ({ view } = await GUIActions.switchToReactIframe()); + const textInput = await TestUtils.waitForSuccess(() => + GUISelectors.getMessageInputFieldAtIndex(view, 0), + ); + const activeElement: WebElement = await driver.switchTo().activeElement(); + const textInputHtml = await textInput.getAttribute("outerHTML"); + const activeElementHtml = await activeElement.getAttribute("outerHTML"); + expect(textInputHtml).to.equal(activeElementHtml); + expect(await textInput.isDisplayed()).to.equal(true); + + await GUIActions.executeFocusContinueInputShortcut(driver); + expect(await textInput.isDisplayed()).to.equal(false); }).timeout(DEFAULT_TIMEOUT.XL); it("Should create a code block when Cmd+L is pressed with text highlighted", async () => { @@ -66,7 +83,7 @@ describe("Cmd+L Shortcut Test", () => { await editor.setText(text); await editor.selectText(text); - await KeyboardShortcutsActions.executeFocusContinueInput(driver); + await GUIActions.executeFocusContinueInputShortcut(driver); ({ view } = await GUIActions.switchToReactIframe()); @@ -78,5 +95,7 @@ describe("Cmd+L Shortcut Test", () => { ); expect(codeblockContent).to.equal(text); + + await GUIActions.executeFocusContinueInputShortcut(driver); }).timeout(DEFAULT_TIMEOUT.XL); }); diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index ffb0b400d..690063628 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -432,6 +432,12 @@ const getCommandsMap: ( core.invoke("context/indexDocs", { reIndex: true }); }, "continue.focusContinueInput": async () => { + const isContinueInputFocused = await sidebar.webviewProtocol.request( + "isContinueInputFocused", + undefined, + false, + ); + // This is a temporary fix—sidebar.webviewProtocol.request is blocking // when the GUI hasn't yet been setup and we should instead be // immediately throwing an error, or returning a Result object @@ -448,11 +454,6 @@ const getCommandsMap: ( undefined, false, ); - const isContinueInputFocused = await sidebar.webviewProtocol.request( - "isContinueInputFocused", - undefined, - false, - ); if (isContinueInputFocused) { if (historyLength === 0) { @@ -475,19 +476,23 @@ const getCommandsMap: ( } }, "continue.focusContinueInputWithoutClear": async () => { - // This is a temporary fix—sidebar.webviewProtocol.request is blocking - // when the GUI hasn't yet been setup and we should instead be - // immediately throwing an error, or returning a Result object - if (!sidebar.isReady) { - focusGUI(); - return; - } - const isContinueInputFocused = await sidebar.webviewProtocol.request( "isContinueInputFocused", undefined, + false, ); + // This is a temporary fix—sidebar.webviewProtocol.request is blocking + // when the GUI hasn't yet been setup and we should instead be + // immediately throwing an error, or returning a Result object + focusGUI(); + if (!sidebar.isReady) { + const isReady = await waitForSidebarReady(sidebar, 5000, 100); + if (!isReady) { + return; + } + } + if (isContinueInputFocused) { hideGUI(); } else { @@ -721,7 +726,10 @@ const getCommandsMap: ( "continue.toggleFullScreen": async () => { focusGUI(); - const sessionId = await sidebar.webviewProtocol.request("getCurrentSessionId", undefined); + const sessionId = await sidebar.webviewProtocol.request( + "getCurrentSessionId", + undefined, + ); // Check if full screen is already open by checking open tabs const fullScreenTab = getFullScreenTab(); @@ -755,11 +763,14 @@ const getCommandsMap: ( undefined, true, ); - + panel.onDidChangeViewState(() => { vscode.commands.executeCommand("continue.newSession"); - if(sessionId){ - vscode.commands.executeCommand("continue.focusContinueSessionId", sessionId); + if (sessionId) { + vscode.commands.executeCommand( + "continue.focusContinueSessionId", + sessionId, + ); } });