From 12b3ed9291b4f46e920f8417f1e6585e45b7edd9 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Mon, 14 Apr 2025 22:55:24 -0700 Subject: [PATCH 1/4] feat: apply e2e tests --- .../vscode/e2e/actions/Apply.actions.ts | 16 ++++ .../vscode/e2e/actions/Global.actions.ts | 50 +++++++++- .../e2e/actions/KeyboardShortcuts.actions.ts | 25 ++++- .../vscode/e2e/selectors/Apply.selectors.ts | 15 +++ extensions/vscode/e2e/tests/Apply.test.ts | 92 +++++++++++++++++++ .../vscode/e2e/tests/Autocomplete.test.ts | 7 +- extensions/vscode/e2e/tests/Edit.test.ts | 6 +- .../e2e/tests/KeyboardShortcuts.test.ts | 21 +++-- .../StepContainerPreToolbar/ApplyActions.tsx | 3 + .../CreateFileButton.tsx | 1 + .../ToolbarButtonWithTooltip.tsx | 3 + .../StepContainerPreToolbar/index.tsx | 2 +- 12 files changed, 219 insertions(+), 22 deletions(-) create mode 100644 extensions/vscode/e2e/actions/Apply.actions.ts create mode 100644 extensions/vscode/e2e/selectors/Apply.selectors.ts create mode 100644 extensions/vscode/e2e/tests/Apply.test.ts diff --git a/extensions/vscode/e2e/actions/Apply.actions.ts b/extensions/vscode/e2e/actions/Apply.actions.ts new file mode 100644 index 000000000..44aa87e85 --- /dev/null +++ b/extensions/vscode/e2e/actions/Apply.actions.ts @@ -0,0 +1,16 @@ +import { WebView } from "vscode-extension-tester"; + +import { ApplySelectors } from "../selectors/Apply.selectors"; +import { TestUtils } from "../TestUtils"; + +export class ApplyActions { + static async performAction( + view: WebView, + action: "apply" | "accept" | "reject" | "create", + ) { + let applyButton = await TestUtils.waitForSuccess( + async () => await ApplySelectors.getCodeblockToolbarAction(view, action), + ); + applyButton.click(); + } +} diff --git a/extensions/vscode/e2e/actions/Global.actions.ts b/extensions/vscode/e2e/actions/Global.actions.ts index aafd1040a..d418fece3 100644 --- a/extensions/vscode/e2e/actions/Global.actions.ts +++ b/extensions/vscode/e2e/actions/Global.actions.ts @@ -7,12 +7,18 @@ import { } from "vscode-extension-tester"; import { DEFAULT_TIMEOUT } from "../constants"; +import { TestUtils } from "../TestUtils"; export class GlobalActions { public static async openTestWorkspace() { - return VSBrowser.instance.openResources("e2e/test-continue"); + await VSBrowser.instance.openResources("e2e/test-continue"); + await new Workbench().executeCommand( + "Notifications: Clear All Notifications", + ); } + public static defaultNewFilename = "test.py"; + public static async createAndOpenNewTextFile(): Promise<{ editor: TextEditor; }> { @@ -26,4 +32,46 @@ export class GlobalActions { return { editor }; } + + public static async createAndSaveNewFile( + filename = GlobalActions.defaultNewFilename, + ): Promise<{ + editor: TextEditor; + }> { + let { editor } = await GlobalActions.createAndOpenNewTextFile(); + + await new Workbench().executeCommand("File: Save As..."); + const inputBox = await InputBox.create(DEFAULT_TIMEOUT.MD); + + // Get current path and replace filename + const currentPath = await inputBox.getText(); + const pathParts = currentPath.split(/[\/\\]/); + pathParts[pathParts.length - 1] = filename; + const newPath = pathParts.join("/"); + + await inputBox.setText(newPath); + + await inputBox.confirm(); + await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); + + editor = (await new EditorView().openEditor(filename)) as TextEditor; + + return { editor }; + } + + public static async deleteFile( + filename = GlobalActions.defaultNewFilename, + ): Promise { + const fs = require("fs"); + const path = require("path"); + + const workspacePath = path.join(process.cwd(), "e2e", "test-continue"); + const filePath = path.join(workspacePath, filename); + + try { + await fs.promises.unlink(filePath); + } catch (error) { + console.warn(`Failed to delete file ${filePath}:`, error); + } + } } diff --git a/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts b/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts index 358d883bd..cac1097c7 100644 --- a/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts +++ b/extensions/vscode/e2e/actions/KeyboardShortcuts.actions.ts @@ -1,4 +1,4 @@ -import { TextEditor } from "vscode-extension-tester"; +import { Key, TextEditor, WebElement } from "vscode-extension-tester"; export class KeyboardShortcutsActions { /** @@ -10,4 +10,27 @@ export class KeyboardShortcutsActions { await editor.selectText(text); await editor.typeText(text); } + + /** + * Types each string in the array with a newline (Shift+Enter) between them. + * Optionally adds a final Enter key press to submit. + */ + public static async typeWithNewlines({ + input, + lines, + submit, + }: { + input: TextEditor | WebElement; + lines: string[]; + submit?: boolean; + }) { + for (let i = 0; i < lines.length - 1; i++) { + await input.sendKeys(lines[i]); + await input.sendKeys(Key.chord(Key.SHIFT, Key.ENTER)); + } + await input.sendKeys(lines[lines.length - 1]); + if (submit) { + await input.sendKeys(Key.ENTER); + } + } } diff --git a/extensions/vscode/e2e/selectors/Apply.selectors.ts b/extensions/vscode/e2e/selectors/Apply.selectors.ts new file mode 100644 index 000000000..4d14f7ff5 --- /dev/null +++ b/extensions/vscode/e2e/selectors/Apply.selectors.ts @@ -0,0 +1,15 @@ +import { WebView } from "vscode-extension-tester"; + +import { SelectorUtils } from "./SelectorUtils"; + +export class ApplySelectors { + static getCodeblockToolbarAction( + view: WebView, + action: "apply" | "accept" | "reject" | "create", + ) { + return SelectorUtils.getElementByDataTestId( + view, + `codeblock-toolbar-${action}`, + ); + } +} diff --git a/extensions/vscode/e2e/tests/Apply.test.ts b/extensions/vscode/e2e/tests/Apply.test.ts new file mode 100644 index 000000000..6d893c835 --- /dev/null +++ b/extensions/vscode/e2e/tests/Apply.test.ts @@ -0,0 +1,92 @@ +import { TextEditor, VSBrowser, WebView } from "vscode-extension-tester"; + +import { ApplyActions } from "../actions/Apply.actions"; +import { GlobalActions } from "../actions/Global.actions"; +import { GUIActions } from "../actions/GUI.actions"; +import { KeyboardShortcutsActions } from "../actions/KeyboardShortcuts.actions"; +import { DEFAULT_TIMEOUT } from "../constants"; +import { GUISelectors } from "../selectors/GUI.selectors"; +import { TestUtils } from "../TestUtils"; + +describe("Apply Test", () => { + let view: WebView; + let editor: TextEditor; + let fileContent = "hello = 'world'"; + + before(async function () { + this.timeout(DEFAULT_TIMEOUT.XL); + + await GUIActions.moveContinueToSidebar(VSBrowser.instance.driver); + await GlobalActions.openTestWorkspace(); + + ({ editor } = await GlobalActions.createAndSaveNewFile()); + }); + + beforeEach(async function () { + this.timeout(DEFAULT_TIMEOUT.XL); + + await editor.typeTextAt(1, 1, fileContent); + await GUIActions.toggleGui(); + + ({ view } = await GUIActions.switchToReactIframe()); + await GUIActions.selectModelFromDropdown(view, "TEST LLM"); + }); + + afterEach(async function () { + this.timeout(DEFAULT_TIMEOUT.XL); + + await editor.clearText(); + + ({ view } = await GUIActions.switchToReactIframe()); + const tipTapEditor = await GUISelectors.getMessageInputFieldAtIndex( + view, + 0, + ); + await tipTapEditor.clear(); + }); + + after(async function () { + this.timeout(DEFAULT_TIMEOUT.XL); + await GlobalActions.deleteFile(); + }); + + it("Can reject and apply changes from sidebar", async () => { + const newFileContent = fileContent + "!"; + + const [messageInput] = await GUISelectors.getMessageInputFields(view); + + await KeyboardShortcutsActions.typeWithNewlines({ + submit: true, + input: messageInput, + lines: [ + "```py " + GlobalActions.defaultNewFilename, + newFileContent, + "```", + "_", // To avoid extra lines getting included in the block + ], + }); + + await ApplyActions.performAction(view, "apply"); + await ApplyActions.performAction(view, "reject"); + await view.switchBack(); + + let editorText = await editor.getText(); + + await TestUtils.waitForSuccess(async () => editorText === fileContent); + // expect(editorText).to.equal(fileContent); + + ({ view } = await GUIActions.switchToReactIframe()); + + await ApplyActions.performAction(view, "apply"); + await ApplyActions.performAction(view, "accept"); + await view.switchBack(); + + // Note that the editor after accpeting contains additional text + // from the default output of the test LLM, so instead of checking + // for an exact text match on the new content we just do a `.includes()` + editorText = await editor.getText(); + await TestUtils.waitForSuccess(async () => + editorText.includes(newFileContent), + ); + }).timeout(DEFAULT_TIMEOUT.XL); +}); diff --git a/extensions/vscode/e2e/tests/Autocomplete.test.ts b/extensions/vscode/e2e/tests/Autocomplete.test.ts index e6444c80c..836c2c60e 100644 --- a/extensions/vscode/e2e/tests/Autocomplete.test.ts +++ b/extensions/vscode/e2e/tests/Autocomplete.test.ts @@ -1,9 +1,4 @@ -import { - EditorView, - InputBox, - TextEditor, - Workbench, -} from "vscode-extension-tester"; +import { EditorView, TextEditor } from "vscode-extension-tester"; import { AutocompleteActions } from "../actions/Autocomplete.actions"; import { GlobalActions } from "../actions/Global.actions"; diff --git a/extensions/vscode/e2e/tests/Edit.test.ts b/extensions/vscode/e2e/tests/Edit.test.ts index 66b82aaf3..6d8812ff1 100644 --- a/extensions/vscode/e2e/tests/Edit.test.ts +++ b/extensions/vscode/e2e/tests/Edit.test.ts @@ -1,9 +1,9 @@ import { expect } from "chai"; import { - WebView, - VSBrowser, - TextEditor, CodeLens, + TextEditor, + VSBrowser, + WebView, } from "vscode-extension-tester"; import { EditActions } from "../actions/Edit.actions"; diff --git a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts index 0191d1d63..e9b79472f 100644 --- a/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts +++ b/extensions/vscode/e2e/tests/KeyboardShortcuts.test.ts @@ -1,21 +1,22 @@ +import { expect } from "chai"; import { - WebDriver, - VSBrowser, EditorView, InputBox, - TextEditor, - Workbench, - WebView, - WebElement, - until, Key, + TextEditor, + VSBrowser, + WebDriver, + WebElement, + WebView, + Workbench, + until, } from "vscode-extension-tester"; + +import { GUIActions } from "../actions/GUI.actions"; +import { KeyboardShortcutsActions } from "../actions/KeyboardShortcuts.actions"; 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("Keyboard Shortcuts", () => { let driver: WebDriver; diff --git a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx index e645e68c9..7131e6ad9 100644 --- a/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx +++ b/gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/ApplyActions.tsx @@ -36,6 +36,7 @@ export function ApplyActions(props: ApplyActionsProps) { const applyButton = (text: string) => (