| 1 | import { expect } from "chai"; |
| 2 | import { |
| 3 | EditorView, |
| 4 | InputBox, |
| 5 | Key, |
| 6 | TextEditor, |
| 7 | until, |
| 8 | VSBrowser, |
| 9 | WebDriver, |
| 10 | WebElement, |
| 11 | WebView, |
| 12 | Workbench, |
| 13 | } from "vscode-extension-tester"; |
| 14 | |
| 15 | import { GlobalActions } from "../actions/Global.actions"; |
| 16 | import { GUIActions } from "../actions/GUI.actions"; |
| 17 | import { KeyboardShortcutsActions } from "../actions/KeyboardShortcuts.actions"; |
| 18 | import { DEFAULT_TIMEOUT } from "../constants"; |
| 19 | import { GUISelectors } from "../selectors/GUI.selectors"; |
| 20 | import { TestUtils } from "../TestUtils"; |
| 21 | |
| 22 | describe("Keyboard Shortcuts", () => { |
| 23 | let driver: WebDriver; |
| 24 | let editor: TextEditor; |
| 25 | let view: WebView; |
| 26 | |
| 27 | before(async function () { |
| 28 | this.timeout(DEFAULT_TIMEOUT.XL); |
| 29 | await GUIActions.moveContinueToSidebar(VSBrowser.instance.driver); |
| 30 | await GlobalActions.disableNextEdit(); |
| 31 | }); |
| 32 | |
| 33 | beforeEach(async function () { |
| 34 | this.timeout(DEFAULT_TIMEOUT.XL); |
| 35 | |
| 36 | await TestUtils.waitForSuccess(async () => { |
| 37 | await new Workbench().executeCommand("Create: New File..."); |
| 38 | await ( |
| 39 | await InputBox.create(DEFAULT_TIMEOUT.MD) |
| 40 | ).selectQuickPick("Text File"); |
| 41 | }); |
| 42 | |
| 43 | driver = VSBrowser.instance.driver; |
| 44 | |
| 45 | editor = (await new EditorView().openEditor("Untitled-1")) as TextEditor; |
| 46 | }); |
| 47 | |
| 48 | afterEach(async function () { |
| 49 | this.timeout(DEFAULT_TIMEOUT.XL * 1000); |
| 50 | await cleanupChat(); |
| 51 | await view.switchBack(); |
| 52 | await editor.clearText(); |
| 53 | await new EditorView().closeAllEditors(); |
| 54 | }); |
| 55 | |
| 56 | async function cleanupChat(chatInput?: WebElement) { |
| 57 | try { |
| 58 | if (!chatInput) { |
| 59 | chatInput = await TestUtils.waitForSuccess(async () => { |
| 60 | return GUISelectors.getMessageInputFieldAtIndex(view, 0); |
| 61 | }); |
| 62 | } |
| 63 | if ( |
| 64 | chatInput && |
| 65 | (await chatInput.isDisplayed()) && |
| 66 | (await chatInput.isEnabled()) |
| 67 | ) { |
| 68 | await chatInput.clear(); |
| 69 | } |
| 70 | } catch (e) { |
| 71 | console.error(`Failed to clear chat: ${e}`); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | it("Should correctly undo and redo using keyboard shortcuts when writing a chat message", async () => { |
| 76 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 77 | ({ view } = await GUIActions.switchToReactIframe()); |
| 78 | const chatInput = await TestUtils.waitForSuccess(async () => { |
| 79 | return GUISelectors.getMessageInputFieldAtIndex(view, 0); |
| 80 | }); |
| 81 | |
| 82 | await chatInput.sendKeys("HELLO "); |
| 83 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 84 | |
| 85 | await chatInput.sendKeys("WORLD "); |
| 86 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 87 | |
| 88 | await chatInput.sendKeys("HELLO "); |
| 89 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 90 | |
| 91 | await chatInput.sendKeys("CONTINUE"); |
| 92 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 93 | |
| 94 | await chatInput.sendKeys(TestUtils.osControlKey + "z"); |
| 95 | await driver.wait( |
| 96 | until.elementTextIs(chatInput, "HELLO WORLD"), |
| 97 | DEFAULT_TIMEOUT.SM, |
| 98 | ); |
| 99 | |
| 100 | await chatInput.sendKeys(TestUtils.osControlKey + "z"); |
| 101 | await driver.wait(until.elementTextIs(chatInput, ""), DEFAULT_TIMEOUT.SM); |
| 102 | |
| 103 | await chatInput.sendKeys(Key.SHIFT + TestUtils.osControlKey + "z"); |
| 104 | await driver.wait( |
| 105 | until.elementTextIs(chatInput, "HELLO"), |
| 106 | DEFAULT_TIMEOUT.SM, |
| 107 | ); |
| 108 | |
| 109 | await chatInput.sendKeys(Key.SHIFT + TestUtils.osControlKey + "z"); |
| 110 | await driver.wait( |
| 111 | until.elementTextIs(chatInput, "HELLO WORLD"), |
| 112 | DEFAULT_TIMEOUT.SM, |
| 113 | ); |
| 114 | |
| 115 | await chatInput.sendKeys(Key.SHIFT + TestUtils.osControlKey + "z"); |
| 116 | await driver.wait( |
| 117 | until.elementTextIs(chatInput, "HELLO WORLD HELLO"), |
| 118 | DEFAULT_TIMEOUT.SM, |
| 119 | ); |
| 120 | |
| 121 | await chatInput.sendKeys(Key.SHIFT + TestUtils.osControlKey + "z"); |
| 122 | await driver.wait( |
| 123 | until.elementTextIs(chatInput, "HELLO WORLD HELLO CONTINUE"), |
| 124 | DEFAULT_TIMEOUT.SM, |
| 125 | ); |
| 126 | }).timeout(DEFAULT_TIMEOUT.XL); |
| 127 | |
| 128 | it("Should not create a code block when Cmd+L is pressed on an empty document", async () => { |
| 129 | expect((await editor.getText()).trim()).to.equal(""); |
| 130 | |
| 131 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 132 | |
| 133 | ({ view } = await GUIActions.switchToReactIframe()); |
| 134 | |
| 135 | await TestUtils.expectNoElement(async () => { |
| 136 | return GUISelectors.getInputBoxCodeBlockAtIndex(view, 0); |
| 137 | }, DEFAULT_TIMEOUT.XS); |
| 138 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 139 | }).timeout(DEFAULT_TIMEOUT.XL); |
| 140 | |
| 141 | it("Fresh VS Code window → sidebar closed → cmd+L with no code highlighted → opens sidebar and focuses input → cmd+L closes sidebar", async () => { |
| 142 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 143 | ({ view } = await GUIActions.switchToReactIframe()); |
| 144 | const textInput = await TestUtils.waitForSuccess(() => |
| 145 | GUISelectors.getMessageInputFieldAtIndex(view, 0), |
| 146 | ); |
| 147 | const activeElement: WebElement = await driver.switchTo().activeElement(); |
| 148 | const textInputHtml = await textInput.getAttribute("outerHTML"); |
| 149 | const activeElementHtml = await activeElement.getAttribute("outerHTML"); |
| 150 | expect(textInputHtml).to.equal(activeElementHtml); |
| 151 | expect(await textInput.isDisplayed()).to.equal(true); |
| 152 | |
| 153 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 154 | |
| 155 | await driver.wait(until.elementIsNotVisible(textInput), DEFAULT_TIMEOUT.XS); |
| 156 | expect(await textInput.isDisplayed()).to.equal(false); |
| 157 | }).timeout(DEFAULT_TIMEOUT.XL); |
| 158 | |
| 159 | it("Send a message → focus code editor (not sidebar) → cmd+L → should focus sidebar and start a new session", async () => { |
| 160 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 161 | ({ view } = await GUIActions.switchToReactIframe()); |
| 162 | |
| 163 | const { userMessage: userMessage0 } = TestUtils.generateTestMessagePair(0); |
| 164 | |
| 165 | await GUIActions.sendMessage({ |
| 166 | view, |
| 167 | message: userMessage0, |
| 168 | inputFieldIndex: 0, |
| 169 | }); |
| 170 | |
| 171 | await view.switchBack(); |
| 172 | |
| 173 | KeyboardShortcutsActions.HACK__typeWithSelect(editor, "hello"); |
| 174 | |
| 175 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 176 | |
| 177 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 178 | |
| 179 | ({ view } = await GUIActions.switchToReactIframe()); |
| 180 | |
| 181 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 182 | |
| 183 | const textInput = await TestUtils.waitForSuccess(() => |
| 184 | GUISelectors.getMessageInputFieldAtIndex(view, 0), |
| 185 | ); |
| 186 | const activeElement: WebElement = await driver.switchTo().activeElement(); |
| 187 | const textInputHtml = await textInput.getAttribute("outerHTML"); |
| 188 | const activeElementHtml = await activeElement.getAttribute("outerHTML"); |
| 189 | expect(textInputHtml).to.equal(activeElementHtml); |
| 190 | |
| 191 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 192 | |
| 193 | await driver.wait(until.elementIsNotVisible(textInput), DEFAULT_TIMEOUT.XS); |
| 194 | expect(await textInput.isDisplayed()).to.equal(false); |
| 195 | |
| 196 | // Make sure the view is visible again, so it can be cleared in afterEach() |
| 197 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 198 | }).timeout(DEFAULT_TIMEOUT.XL); |
| 199 | |
| 200 | it("Should create a code block when Cmd+L is pressed with text highlighted", async () => { |
| 201 | const text = "Hello, world!"; |
| 202 | |
| 203 | await editor.setText(text); |
| 204 | await editor.selectText(text); |
| 205 | |
| 206 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 207 | |
| 208 | ({ view } = await GUIActions.switchToReactIframe()); |
| 209 | |
| 210 | const codeBlock = await TestUtils.waitForSuccess(() => |
| 211 | GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), |
| 212 | ); |
| 213 | const codeblockContent = await codeBlock.getAttribute( |
| 214 | "data-codeblockcontent", |
| 215 | ); |
| 216 | |
| 217 | expect(codeblockContent).to.equal(text); |
| 218 | |
| 219 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 220 | }).timeout(DEFAULT_TIMEOUT.XL); |
| 221 | |
| 222 | it("Should create a code block with the whole file when Cmd+L is pressed on an empty line", async () => { |
| 223 | const text = "Hello,\n\n\nworld!"; |
| 224 | |
| 225 | await editor.setText(text); |
| 226 | await editor.moveCursor(2, 1); //Move cursor to an empty line |
| 227 | |
| 228 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 229 | |
| 230 | ({ view } = await GUIActions.switchToReactIframe()); |
| 231 | |
| 232 | const codeBlock = await TestUtils.waitForSuccess(() => |
| 233 | GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), |
| 234 | ); |
| 235 | const codeblockContent = await codeBlock.getAttribute( |
| 236 | "data-codeblockcontent", |
| 237 | ); |
| 238 | |
| 239 | expect(codeblockContent).to.equal(text); |
| 240 | }); |
| 241 | |
| 242 | it("Should create a code block when Cmd+L is pressed on a non-empty line", async () => { |
| 243 | const text = "Hello, world!"; |
| 244 | |
| 245 | await editor.setText(text); |
| 246 | await editor.moveCursor(1, 7); //Move cursor to the 1st space |
| 247 | |
| 248 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 249 | |
| 250 | ({ view } = await GUIActions.switchToReactIframe()); |
| 251 | |
| 252 | const codeBlock = await TestUtils.waitForSuccess(() => |
| 253 | GUISelectors.getInputBoxCodeBlockAtIndex(view, 0), |
| 254 | ); |
| 255 | const codeblockContent = await codeBlock.getAttribute( |
| 256 | "data-codeblockcontent", |
| 257 | ); |
| 258 | |
| 259 | expect(codeblockContent).to.equal(text); |
| 260 | }).timeout(DEFAULT_TIMEOUT.XL); |
| 261 | }); |