-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-cli.test.ts
More file actions
386 lines (318 loc) · 13.3 KB
/
web-cli.test.ts
File metadata and controls
386 lines (318 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import {
test,
expect,
getTestUrl,
log,
reloadPageWithRateLimit,
} from "./helpers/base-test";
import { authenticateWebCli } from "./auth-helper.js";
import { waitForRateLimitLock } from "./rate-limit-lock";
import { waitForTerminalReady, waitForTerminalStable } from "./wait-helpers.js";
import { getTerminalServerUrl } from "./helpers/ci-auth.js";
// Type for browser context in evaluate() calls
type _BrowserContext = {
localStorage: Storage;
innerHeight: number;
};
// Constants
const DRAWER_OPEN_KEY = "ablyCliDrawerOpen";
const TERMINAL_SERVER_URL = getTerminalServerUrl();
/**
* Wait for the terminal prompt to appear, indicating the terminal is ready
* @param page Playwright Page object
* @param terminalSelector Selector for the terminal element
* @param timeout Maximum time to wait in milliseconds
*/
// Removed _waitForPrompt - using wait helpers instead
// --- Test Suite ---
test.describe("Web CLI E2E Tests", () => {
test.setTimeout(120_000); // Overall test timeout
test.beforeAll(async () => {
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(
`[WebCLI Test Suite] beforeAll hook started at ${new Date().toISOString()}`,
);
console.log(`[WebCLI Test Suite] Process ID: ${process.pid}`);
console.log(
`[WebCLI Test Suite] Total tests in suite: ${test.describe.name}`,
);
}
});
test.afterAll(async () => {
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(
`[WebCLI Test Suite] afterAll hook started at ${new Date().toISOString()}`,
);
console.log(`[WebCLI Test Suite] Process ID: ${process.pid}`);
}
});
test.beforeEach(async ({ page: _page }, testInfo) => {
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(
`[WebCLI Test] beforeEach hook for "${testInfo.title}" at ${new Date().toISOString()}`,
);
console.log(`[WebCLI Test] Test status: ${testInfo.status}`);
console.log(`[WebCLI Test] Test retry: ${testInfo.retry}`);
}
});
test.afterEach(async ({ page: _page }, testInfo) => {
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(
`[WebCLI Test] afterEach hook for "${testInfo.title}" at ${new Date().toISOString()}`,
);
console.log(`[WebCLI Test] Test status: ${testInfo.status}`);
console.log(`[WebCLI Test] Test duration: ${testInfo.duration}ms`);
}
});
test("should load the terminal, connect to public server, and run basic commands", async ({
page,
}, testInfo) => {
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(
`[WebCLI Test] Test body started: "${testInfo.title}" at ${new Date().toISOString()}`,
);
}
// Wait for any ongoing rate limit pause
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(
`[WebCLI Test] Waiting for rate limit lock before test execution...`,
);
}
await waitForRateLimitLock();
if (!process.env.CI || process.env.VERBOSE_TESTS) {
console.log(`[WebCLI Test] Rate limit lock check complete`);
}
// Wait for test stability
log("Waiting for test stability...");
await waitForTerminalStable(page, 2000);
// Use the public terminal server
const pageUrl = `${getTestUrl()}?serverUrl=${encodeURIComponent(TERMINAL_SERVER_URL)}`;
log(`Navigating to: ${pageUrl}`);
await page.goto(pageUrl);
// Handle authentication if needed
await authenticateWebCli(page);
// Wait for the terminal element to be present
const terminalSelector = ".xterm"; // Adjust if the selector changes in the React component
const _terminalElement = await page.waitForSelector(terminalSelector, {
timeout: 15000,
});
log("Terminal element found.");
// Wait for React to mount and expose the state function
await waitForTerminalStable(page, 1000);
// Check if the React state function is available
const hasStateFunction = await page.evaluate(() => {
return typeof (window as any).getAblyCliTerminalReactState === "function";
});
log("React state function available:", hasStateFunction);
// Wait for the terminal to be ready
await waitForTerminalReady(page);
// --- Run 'help' ---
log("Executing: help");
await page.locator(terminalSelector).focus(); // Explicitly focus terminal
await page.keyboard.type("help");
await page.keyboard.press("Enter");
// Wait for specific output from 'help' using toContainText
await expect(page.locator(terminalSelector)).toContainText("COMMANDS", {
timeout: 15000,
});
log("'help' output verified.");
// --- Run '--version' ---
log("Executing: --version");
await page.locator(terminalSelector).focus();
await page.keyboard.type("--version");
await page.keyboard.press("Enter");
// Wait for specific output from '--version'
const versionOutputText = "browser-based interactive CLI"; // substring expected from version output
await expect(page.locator(terminalSelector)).toContainText(
versionOutputText,
{ timeout: 15000 },
);
log("'--version' output verified.");
// Wait for output to be fully rendered
await waitForTerminalStable(page, 500);
// Check if the scrollbar appears when needed
const scrollbarInfo = await page.evaluate((selector) => {
const terminal = document.querySelector(selector) as HTMLElement;
const scrollContainer = terminal?.querySelector(
".xterm-screen",
) as HTMLElement;
return {
terminalHeight: terminal?.clientHeight || 0,
scrollHeight: scrollContainer?.scrollHeight || 0,
hasScrollbar:
(scrollContainer?.scrollHeight || 0) > (terminal?.clientHeight || 0),
};
}, terminalSelector);
log("Scrollbar info:", scrollbarInfo);
// If content exceeds terminal height, verify scrollbar functionality
if (scrollbarInfo.hasScrollbar) {
// Scroll down
await page.locator(terminalSelector).focus();
await page.keyboard.press("End");
await waitForTerminalStable(page, 500);
// Scroll up
await page.keyboard.press("Home");
await waitForTerminalStable(page, 500);
log("Scrollbar functionality verified.");
}
});
test("side drawer persists state across page reloads", async ({ page }) => {
// Wait for any ongoing rate limit pause
await waitForRateLimitLock();
// Navigate to the Web CLI app
await page.goto(getTestUrl());
// Authenticate
await authenticateWebCli(page);
// Wait for terminal to be ready
const terminalSelector = ".xterm";
await page.waitForSelector(terminalSelector, { timeout: 15000 });
await waitForTerminalReady(page);
// Switch to drawer mode by clicking the Drawer button
const drawerButton = page.locator('button:has-text("Drawer")');
await drawerButton.click();
// In drawer mode, there's a tab button at the bottom to open the drawer
// The button has class "fixed bottom-0 left-4" and contains "Ably CLI" text
const drawerTab = page.locator(
'button.fixed.bottom-0.left-4:has-text("Ably CLI")',
);
await expect(drawerTab).toBeVisible({ timeout: 5000 });
await drawerTab.click();
// Wait for drawer to be visible - the drawer is a fixed bottom panel
const drawer = page.locator(
"div.fixed.bottom-0.left-0.right-0.z-50.bg-zinc-900",
);
await expect(drawer).toBeVisible({ timeout: 5000 });
// Verify localStorage has the drawer state saved
const drawerState = await page.evaluate((key) => {
return localStorage.getItem(key);
}, DRAWER_OPEN_KEY);
expect(drawerState).toBe("true");
// Reload the page with rate limiting
await reloadPageWithRateLimit(page);
// Wait for terminal element to exist (don't need full ready state for drawer test)
await page.waitForSelector(terminalSelector, { timeout: 15000 });
// Just wait for the component to be mounted
await page.waitForFunction(
() => {
const state = (window as any).getAblyCliTerminalReactState?.();
return state && state.componentConnectionStatus !== "initial";
},
{ timeout: 10000 },
);
// Verify drawer is still open after reload
await expect(drawer).toBeVisible({ timeout: 5000 });
// Close the drawer by clicking the X button in the drawer
const closeButton = drawer.locator('button[aria-label="Close drawer"]');
await closeButton.click();
await expect(drawer).not.toBeVisible({ timeout: 5000 });
// Verify localStorage state is updated
const drawerStateClosed = await page.evaluate((key) => {
return localStorage.getItem(key);
}, DRAWER_OPEN_KEY);
expect(drawerStateClosed).toBe("false");
// Reload again and verify drawer remains closed
await reloadPageWithRateLimit(page);
// In drawer mode with drawer closed, terminal is not visible
// Just verify the drawer remains closed
await expect(drawer).not.toBeVisible({ timeout: 5000 });
// Verify we can see the drawer tab button (indicating drawer mode is active but drawer is closed)
await expect(drawerTab).toBeVisible({ timeout: 5000 });
});
test("bottom drawer adapts to different screen sizes", async ({ page }) => {
// Wait for any ongoing rate limit pause
await waitForRateLimitLock();
// Navigate to the Web CLI app
await page.goto(getTestUrl());
// Authenticate
await authenticateWebCli(page);
// Wait for terminal to be ready
const terminalSelector = ".xterm";
await page.waitForSelector(terminalSelector, { timeout: 15000 });
await waitForTerminalReady(page);
// Test desktop size
await page.setViewportSize({ width: 1200, height: 800 });
// Switch to drawer mode
const drawerModeButton = page.locator('button:has-text("Drawer")');
await drawerModeButton.click();
// Open the drawer tab
const drawerTab = page.locator(
'button.fixed.bottom-0.left-4:has-text("Ably CLI")',
);
await expect(drawerTab).toBeVisible({ timeout: 5000 });
await drawerTab.click();
const drawer = page.locator(
"div.fixed.bottom-0.left-0.right-0.z-50.bg-zinc-900",
);
await expect(drawer).toBeVisible({ timeout: 5000 });
// Verify drawer height on desktop
const desktopDrawerHeight = await drawer.evaluate((el) => el.clientHeight);
expect(desktopDrawerHeight).toBeGreaterThan(200); // Drawer should have reasonable height
expect(desktopDrawerHeight).toBeLessThan(600); // But not take full screen
// Test mobile size
await page.setViewportSize({ width: 600, height: 800 });
await waitForTerminalStable(page, 500); // Wait for resize transition
// Drawer should still be visible
await expect(drawer).toBeVisible();
// Verify drawer adapts to mobile viewport
const mobileDrawerHeight = await drawer.evaluate((el) => el.clientHeight);
expect(mobileDrawerHeight).toBeGreaterThan(200); // Still reasonable height
expect(mobileDrawerHeight).toBeLessThan(600); // Not full screen
// Close drawer by clicking the X button
const closeButton = drawer.locator('button[aria-label="Close drawer"]');
await closeButton.click();
await expect(drawer).not.toBeVisible({ timeout: 5000 });
});
test("terminal maintains functionality with drawer interactions", async ({
page,
}) => {
// Wait for any ongoing rate limit pause
await waitForRateLimitLock();
// Navigate to the Web CLI app
await page.goto(getTestUrl());
// Authenticate
await authenticateWebCli(page);
// Wait for terminal to be ready
const terminalSelector = ".xterm";
await page.waitForSelector(terminalSelector, { timeout: 15000 });
await waitForTerminalReady(page);
// Run a command
await page.locator(terminalSelector).focus();
await page.keyboard.type("--version");
await page.keyboard.press("Enter");
await expect(page.locator(terminalSelector)).toContainText(
"browser-based interactive CLI",
{ timeout: 5000 },
);
// Switch to drawer mode by clicking the Drawer button
const drawerModeButton = page.locator('button:has-text("Drawer")');
await drawerModeButton.click();
// Click the drawer tab to open it
const drawerTab = page.locator(
'button.fixed.bottom-0.left-4:has-text("Ably CLI")',
);
await expect(drawerTab).toBeVisible({ timeout: 5000 });
await drawerTab.click();
const drawer = page.locator(
"div.fixed.bottom-0.left-0.right-0.z-50.bg-zinc-900",
);
await expect(drawer).toBeVisible({ timeout: 5000 });
// Terminal should still be functional with drawer open
await page.locator(terminalSelector).focus();
await page.keyboard.type("help");
await page.keyboard.press("Enter");
await expect(page.locator(terminalSelector)).toContainText(
"Show help for any command",
{ timeout: 5000 },
);
// Close the drawer by clicking the X button
const closeButton = drawer.locator('button[aria-label="Close drawer"]');
await closeButton.click();
await expect(drawer).not.toBeVisible({ timeout: 5000 });
// Terminal should still be functional after drawer closes
// But in drawer mode with drawer closed, terminal is not visible
// We need to verify the drawer tab is visible instead
await expect(drawerTab).toBeVisible({ timeout: 5000 });
});
});
// Re-export window declaration to ensure TypeScript compatibility
declare const window: any;