Elffuss BraidHub

kikocis/continue public

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