| 1 | import { |
| 2 | InputBox, |
| 3 | Key, |
| 4 | WebDriver, |
| 5 | WebElement, |
| 6 | WebView, |
| 7 | Workbench, |
| 8 | } from "vscode-extension-tester"; |
| 9 | |
| 10 | import { DEFAULT_TIMEOUT } from "../constants"; |
| 11 | import { GUISelectors } from "../selectors/GUI.selectors"; |
| 12 | import { TestUtils } from "../TestUtils"; |
| 13 | |
| 14 | export class GUIActions { |
| 15 | public static moveContinueToSidebar = async (driver: WebDriver) => { |
| 16 | await GUIActions.toggleGui(); |
| 17 | await TestUtils.waitForSuccess(async () => { |
| 18 | await new Workbench().executeCommand("View: Move View"); |
| 19 | await ( |
| 20 | await InputBox.create(DEFAULT_TIMEOUT.MD) |
| 21 | ).selectQuickPick("Continue"); |
| 22 | await ( |
| 23 | await InputBox.create(DEFAULT_TIMEOUT.MD) |
| 24 | ).selectQuickPick("New Secondary Side Bar Entry"); |
| 25 | }); |
| 26 | |
| 27 | // first call focuses the input |
| 28 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 29 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 30 | |
| 31 | // second call closes the gui |
| 32 | await TestUtils.waitForTimeout(DEFAULT_TIMEOUT.XS); |
| 33 | await GUIActions.executeFocusContinueInputShortcut(driver); |
| 34 | }; |
| 35 | |
| 36 | public static switchToReactIframe = async () => { |
| 37 | const view = new WebView(); |
| 38 | const driver = view.getDriver(); |
| 39 | |
| 40 | const iframes = await GUISelectors.getAllIframes(driver); |
| 41 | let continueIFrame: WebElement | undefined = undefined; |
| 42 | for (let i = 0; i < iframes.length; i++) { |
| 43 | const iframe = iframes[i]; |
| 44 | const src = await iframe.getAttribute("src"); |
| 45 | if (src.includes("extensionId=Continue.continue")) { |
| 46 | continueIFrame = iframe; |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | if (!continueIFrame) { |
| 52 | throw new Error("Could not find Continue iframe"); |
| 53 | } |
| 54 | |
| 55 | await driver.switchTo().frame(continueIFrame); |
| 56 | |
| 57 | await new Promise((res) => { |
| 58 | setTimeout(res, 500); |
| 59 | }); |
| 60 | |
| 61 | const reactIFrame = await GUISelectors.getReactIframe(driver); |
| 62 | |
| 63 | if (!reactIFrame) { |
| 64 | throw new Error("Could not find React iframe"); |
| 65 | } |
| 66 | |
| 67 | await driver.switchTo().frame(reactIFrame); |
| 68 | return { |
| 69 | view, |
| 70 | driver, |
| 71 | }; |
| 72 | }; |
| 73 | |
| 74 | public static toggleGui = async () => { |
| 75 | return TestUtils.waitForSuccess(() => |
| 76 | new Workbench().executeCommand("continue.focusContinueInput"), |
| 77 | ); |
| 78 | }; |
| 79 | |
| 80 | public static selectModelFromDropdown = async ( |
| 81 | view: WebView, |
| 82 | option: string, |
| 83 | ) => { |
| 84 | const dropdownButton = await GUISelectors.getModelDropdownButton(view); |
| 85 | await dropdownButton.click(); |
| 86 | |
| 87 | const dropdownOption = await TestUtils.waitForSuccess(() => { |
| 88 | return GUISelectors.getModelDropdownOption(view, option); |
| 89 | }); |
| 90 | |
| 91 | await dropdownOption.click(); |
| 92 | }; |
| 93 | |
| 94 | public static selectModeFromDropdown = async ( |
| 95 | view: WebView, |
| 96 | option: string, |
| 97 | ) => { |
| 98 | const dropdownButton = await GUISelectors.getModeDropdownButton(view); |
| 99 | await dropdownButton.click(); |
| 100 | |
| 101 | const dropdownOption = await TestUtils.waitForSuccess(() => { |
| 102 | return GUISelectors.getModeDropdownOption(view, option); |
| 103 | }); |
| 104 | |
| 105 | await dropdownOption.click(); |
| 106 | }; |
| 107 | |
| 108 | public static async sendMessage({ |
| 109 | view, |
| 110 | message, |
| 111 | inputFieldIndex, |
| 112 | }: { |
| 113 | view: WebView; |
| 114 | message: string; |
| 115 | inputFieldIndex: number; |
| 116 | }) { |
| 117 | const editor = await GUISelectors.getMessageInputFieldAtIndex( |
| 118 | view, |
| 119 | inputFieldIndex, |
| 120 | ); |
| 121 | await editor.sendKeys(message); |
| 122 | await editor.sendKeys(Key.ENTER); |
| 123 | } |
| 124 | |
| 125 | public static async executeFocusContinueInputShortcut(driver: WebDriver) { |
| 126 | return driver |
| 127 | .actions() |
| 128 | .keyDown(TestUtils.osControlKey) |
| 129 | .sendKeys("l") |
| 130 | .keyUp(TestUtils.osControlKey) |
| 131 | .perform(); |
| 132 | } |
| 133 | |
| 134 | public static async toggleToolPolicy( |
| 135 | view: WebView, |
| 136 | toolName: string, |
| 137 | desiredState: number | string, |
| 138 | ) { |
| 139 | // Navigate to config page |
| 140 | |
| 141 | const settingsButton = await TestUtils.waitForSuccess(() => |
| 142 | GUISelectors.getSettingsNavButton(view), |
| 143 | ); |
| 144 | await settingsButton.click(); |
| 145 | await TestUtils.waitForTimeout(500); |
| 146 | |
| 147 | // Click on tools tab |
| 148 | const toolsTab = await TestUtils.waitForSuccess(() => |
| 149 | GUISelectors.getToolsTab(view), |
| 150 | ); |
| 151 | await toolsTab.click(); |
| 152 | await TestUtils.waitForTimeout(500); |
| 153 | |
| 154 | // Find and click the tool policy button |
| 155 | const toolPolicyButton = await TestUtils.waitForSuccess(() => |
| 156 | GUISelectors.getToolPolicyButton(view, toolName), |
| 157 | ); |
| 158 | await TestUtils.waitForTimeout(500); |
| 159 | |
| 160 | // Map old number format to state names for backward compatibility |
| 161 | // 0 = Automatic/Auto, 1 = Excluded/Off, 2 = Ask First/Ask |
| 162 | let targetStates: string[]; |
| 163 | if (typeof desiredState === "number") { |
| 164 | const stateMap = [ |
| 165 | ["Automatic", "Auto"], // 0 |
| 166 | ["Excluded", "Off"], // 1 |
| 167 | ["Ask First", "Ask"], // 2 |
| 168 | ]; |
| 169 | targetStates = stateMap[desiredState] || ["Ask First", "Ask"]; |
| 170 | } else { |
| 171 | targetStates = [desiredState]; |
| 172 | } |
| 173 | |
| 174 | // Keep clicking until we reach the desired state |
| 175 | let maxAttempts = 5; // Safety limit to prevent infinite loops |
| 176 | while (maxAttempts > 0) { |
| 177 | const currentText = await toolPolicyButton.getText(); |
| 178 | |
| 179 | // Check if we've reached any of the target states |
| 180 | if (targetStates.some((state) => currentText.includes(state))) { |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | // Click to move to next state |
| 185 | await toolPolicyButton.click(); |
| 186 | await TestUtils.waitForTimeout(200); // Small delay for UI update |
| 187 | |
| 188 | maxAttempts--; |
| 189 | } |
| 190 | |
| 191 | if (maxAttempts === 0) { |
| 192 | throw new Error( |
| 193 | `Failed to set tool policy to ${targetStates.join(" or ")} after 5 attempts`, |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | // Navigate back to chat |
| 198 | const backButton = await TestUtils.waitForSuccess(() => |
| 199 | GUISelectors.getBackButton(view), |
| 200 | ); |
| 201 | await backButton.click(); |
| 202 | await TestUtils.waitForTimeout(500); |
| 203 | } |
| 204 | } |