Elffuss BraidHub

kikocis/continue public

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