| 1 | import { expect } from "chai"; |
| 2 | import { Key } from "vscode-extension-tester"; |
| 3 | |
| 4 | import { access, constants } from 'fs/promises'; |
| 5 | import * as path from "path"; |
| 6 | import { DEFAULT_TIMEOUT } from "./constants"; |
| 7 | |
| 8 | export class TestUtils { |
| 9 | public static CONTINUE_GLOBAL_DIR = process.env.CONTINUE_GLOBAL_DIR ?? ""; |
| 10 | |
| 11 | public static getGlobalContextFilePath(): string { |
| 12 | return path.join(TestUtils.CONTINUE_GLOBAL_DIR, "index", "globalContext.json"); |
| 13 | } |
| 14 | |
| 15 | public static async fileExists(path: string): Promise<boolean> { |
| 16 | try { |
| 17 | await access(path, constants.F_OK); |
| 18 | return true; |
| 19 | } catch { |
| 20 | return false; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * In many cases it might be more useful to use existing Selenium |
| 26 | * utilities. For example: |
| 27 | * |
| 28 | * await driver.wait(until.elementLocated(By.xpath(xpath)), 5000); |
| 29 | * |
| 30 | * There's also 'waitForAttributeValue'. |
| 31 | */ |
| 32 | public static async waitForSuccess<T>( |
| 33 | locatorFn: () => Promise<T>, |
| 34 | timeout: number = DEFAULT_TIMEOUT.MD, |
| 35 | interval: number = 500, |
| 36 | ): Promise<T> { |
| 37 | const startTime = Date.now(); |
| 38 | |
| 39 | while (Date.now() - startTime < timeout) { |
| 40 | try { |
| 41 | const result = await locatorFn(); |
| 42 | return result; |
| 43 | } catch (e) { |
| 44 | if (Date.now() - startTime >= timeout) { |
| 45 | throw new Error( |
| 46 | `Element not found after ${timeout}ms timeout: ${locatorFn}`, |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 | await new Promise((resolve) => setTimeout(resolve, interval)); |
| 51 | } |
| 52 | |
| 53 | throw new Error( |
| 54 | `Element not found after ${timeout}ms timeout: ${locatorFn}`, |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | public static async logFailure<T>(locatorFn: () => Promise<T>): Promise<T> { |
| 59 | try { |
| 60 | const result = await locatorFn(); |
| 61 | return result; |
| 62 | } catch (e) { |
| 63 | throw new Error(`Element not found: ${locatorFn}`); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public static async expectNoElement<T>( |
| 68 | locatorFn: () => Promise<T>, |
| 69 | timeout: number = 1000, |
| 70 | interval: number = 200, |
| 71 | ): Promise<void> { |
| 72 | const startTime = Date.now(); |
| 73 | let elementFound = false; |
| 74 | |
| 75 | while (Date.now() - startTime < timeout) { |
| 76 | try { |
| 77 | const element = await locatorFn(); |
| 78 | console.log("ELEMENT", element); |
| 79 | if (element) { |
| 80 | elementFound = true; |
| 81 | break; |
| 82 | } |
| 83 | } catch (e) { |
| 84 | // Continue if there's an error (element not found) |
| 85 | } |
| 86 | await new Promise((resolve) => setTimeout(resolve, interval)); |
| 87 | } |
| 88 | |
| 89 | expect(elementFound).to.be.false; |
| 90 | } |
| 91 | |
| 92 | public static generateTestMessagePair(id: number = 0): { |
| 93 | userMessage: string; |
| 94 | llmResponse: string; |
| 95 | } { |
| 96 | return { |
| 97 | userMessage: `TEST_USER_MESSAGE_${id}`, |
| 98 | llmResponse: `TEST_LLM_RESPONSE_${id}`, |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | public static waitForTimeout(ms: number): Promise<void> { |
| 103 | return new Promise((resolve) => setTimeout(resolve, ms)); |
| 104 | } |
| 105 | |
| 106 | public static get isMacOS(): boolean { |
| 107 | return process.platform === "darwin"; |
| 108 | } |
| 109 | |
| 110 | public static get osControlKey() { |
| 111 | return TestUtils.isMacOS ? Key.META : Key.CONTROL; |
| 112 | } |
| 113 | } |