Skip to content

Commit 23732ff

Browse files
committed
refactor: reduce duplicate code for terminal server URL and add ci auth helpers
1 parent b5d0771 commit 23732ff

9 files changed

Lines changed: 42 additions & 36 deletions

test/e2e/web-cli/helpers/base-test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
markTestAsFailing,
66
} from "./test-helpers";
77
import { setupCIAuth } from "./setup-ci-auth";
8+
import { isRemoteServer } from "./ci-auth";
89

910
// Extend the base test with our helpers
1011
export const test = base.extend({
@@ -159,11 +160,8 @@ export const test = base.extend({
159160
// Dump console on failure
160161
dumpConsoleOnFailure();
161162

162-
// Add a small delay between tests when running against production
163-
const isProduction =
164-
!process.env.TERMINAL_SERVER_URL ||
165-
process.env.TERMINAL_SERVER_URL.includes("web-cli-terminal.ably-dev.com");
166-
if (isProduction) {
163+
// Add a small delay between tests when running against the remote server
164+
if (isRemoteServer()) {
167165
await page.waitForTimeout(1000);
168166
}
169167

test/e2e/web-cli/helpers/ci-auth.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,29 @@ export function shouldUseTerminalServerSigningSecret(): boolean {
4949
}
5050

5151
/**
52-
* Get the WebSocket URL to use (production or local)
53-
* @returns WebSocket URL from environment or default production URL
52+
* Default terminal server URL used when no environment override is set.
5453
*/
55-
export function getCIWebSocketUrl(): string {
54+
const DEFAULT_TERMINAL_SERVER_URL = "wss://web-cli-terminal.ably-dev.com";
55+
56+
/**
57+
* Get the WebSocket URL to use for the terminal server.
58+
* Checks TERMINAL_SERVER_URL first, then ABLY_CLI_WEBSOCKET_URL, and falls
59+
* back to the default dev server.
60+
*/
61+
export function getTerminalServerUrl(): string {
5662
return (
57-
process.env.TERMINAL_SERVER_URL || "wss://web-cli-terminal.ably-dev.com"
63+
process.env.TERMINAL_SERVER_URL ||
64+
process.env.ABLY_CLI_WEBSOCKET_URL ||
65+
DEFAULT_TERMINAL_SERVER_URL
5866
);
5967
}
68+
69+
/**
70+
* Returns true when the terminal server URL points at the hosted dev/prod
71+
* endpoint (or is unset, which defaults to the hosted endpoint). Use this
72+
* to gate behaviours that should only run against the remote server (e.g.
73+
* extra stabilisation delays).
74+
*/
75+
export function isRemoteServer(): boolean {
76+
return getTerminalServerUrl().includes("web-cli-terminal.ably-dev.com");
77+
}

test/e2e/web-cli/helpers/setup-ci-auth.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Page } from "playwright/test";
22
import {
33
generateCIAuthToken,
44
shouldUseTerminalServerSigningSecret,
5-
getCIWebSocketUrl,
5+
getTerminalServerUrl,
66
} from "./ci-auth.js";
77

88
/**
@@ -92,7 +92,7 @@ export async function setupCIAuth(page: Page): Promise<void> {
9292
ciAuthToken,
9393
testGroup,
9494
runId,
95-
websocketUrl: getCIWebSocketUrl(),
95+
websocketUrl: getTerminalServerUrl(),
9696
verboseTests: process.env.VERBOSE_TESTS || "",
9797
},
9898
);
@@ -101,7 +101,7 @@ export async function setupCIAuth(page: Page): Promise<void> {
101101
console.log("[CI Auth] Setup completed", {
102102
testGroup,
103103
runId,
104-
websocketUrl: getCIWebSocketUrl(),
104+
websocketUrl: getTerminalServerUrl(),
105105
});
106106
}
107107
}
@@ -132,5 +132,5 @@ export async function disableCIAuth(page: Page): Promise<void> {
132132
* This respects the CI configuration or falls back to the public URL
133133
*/
134134
export function getTestWebSocketUrl(): string {
135-
return getCIWebSocketUrl();
135+
return getTerminalServerUrl();
136136
}

test/e2e/web-cli/prompt-integrity.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import {
1818
getTerminalContent,
1919
} from "./wait-helpers";
2020
import { createSignedConfig } from "./helpers/signing-helper";
21+
import { getTerminalServerUrl } from "./helpers/ci-auth.js";
2122

22-
// Public terminal server endpoint
23-
const TERMINAL_SERVER_URL =
24-
process.env.TERMINAL_SERVER_URL || "wss://web-cli-terminal.ably-dev.com";
23+
const TERMINAL_SERVER_URL = getTerminalServerUrl();
2524

2625
test.describe("Web CLI Prompt Integrity E2E Tests", () => {
2726
test.setTimeout(120_000);

test/e2e/web-cli/reconnection-diagnostic.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import { test, expect, getTestUrl } from "./helpers/base-test";
1111
const log = console.log.bind(console);
1212
import { authenticateWebCli } from "./auth-helper.js";
1313
import { waitForRateLimitLock } from "./rate-limit-lock";
14+
import { getTerminalServerUrl } from "./helpers/ci-auth.js";
1415

15-
// Public terminal server endpoint
16-
const TERMINAL_SERVER_URL =
17-
process.env.TERMINAL_SERVER_URL || "wss://web-cli-terminal.ably-dev.com";
16+
const TERMINAL_SERVER_URL = getTerminalServerUrl();
1817

1918
test.describe("Web CLI Reconnection Diagnostic E2E Tests", () => {
2019
// Increase timeout significantly for CI environments

test/e2e/web-cli/reconnection.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import {
1919
} from "./wait-helpers.js";
2020
import { waitForRateLimitLock } from "./rate-limit-lock";
2121
import { createSignedConfig } from "./helpers/signing-helper";
22+
import { getTerminalServerUrl } from "./helpers/ci-auth.js";
2223

23-
// Terminal server endpoint - use environment variable or default to public server
24-
const TERMINAL_SERVER_URL =
25-
process.env.TERMINAL_SERVER_URL || "wss://web-cli-terminal.ably-dev.com";
24+
const TERMINAL_SERVER_URL = getTerminalServerUrl();
2625

2726
async function _waitForPrompt(
2827
page: any,

test/e2e/web-cli/session-resume.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ import {
1313
executeCommandWithRetry,
1414
} from "./wait-helpers.js";
1515
import { waitForRateLimitLock } from "./rate-limit-lock";
16+
import { getTerminalServerUrl } from "./helpers/ci-auth.js";
1617

17-
// Terminal server endpoint - use environment variable or default to public server
18-
const TERMINAL_SERVER_URL =
19-
process.env.TERMINAL_SERVER_URL ||
20-
process.env.ABLY_CLI_WEBSOCKET_URL ||
21-
"wss://web-cli-terminal.ably-dev.com";
18+
const TERMINAL_SERVER_URL = getTerminalServerUrl();
2219

2320
// Removed _waitForPrompt - using wait helpers instead
2421

test/e2e/web-cli/wait-helpers.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Page } from "playwright/test";
2+
import { isRemoteServer } from "./helpers/ci-auth";
23

34
/**
45
* Wait for the terminal to be ready for interaction
@@ -12,12 +13,9 @@ export async function waitForTerminalReady(
1213
console.log("Waiting for terminal to be ready...");
1314
}
1415

15-
// Increase timeout for CI environments or when using production server
16-
const isProduction =
17-
!process.env.TERMINAL_SERVER_URL ||
18-
process.env.TERMINAL_SERVER_URL.includes("web-cli-terminal.ably-dev.com");
16+
// Increase timeout for CI environments or when using the remote server
1917
const effectiveTimeout =
20-
process.env.CI || isProduction ? timeout * 2 : timeout;
18+
process.env.CI || isRemoteServer() ? timeout * 2 : timeout;
2119
const startTime = Date.now();
2220
let manualReconnectAttempts = 0;
2321
const maxManualReconnects = process.env.CI ? 5 : 3;

test/e2e/web-cli/web-cli.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { authenticateWebCli } from "./auth-helper.js";
99
import { waitForRateLimitLock } from "./rate-limit-lock";
1010
import { waitForTerminalReady, waitForTerminalStable } from "./wait-helpers.js";
11+
import { getTerminalServerUrl } from "./helpers/ci-auth.js";
1112

1213
// Type for browser context in evaluate() calls
1314
type _BrowserContext = {
@@ -17,10 +18,7 @@ type _BrowserContext = {
1718

1819
// Constants
1920
const DRAWER_OPEN_KEY = "ablyCliDrawerOpen";
20-
21-
// Terminal server endpoint - use environment variable or default to public server
22-
const TERMINAL_SERVER_URL =
23-
process.env.TERMINAL_SERVER_URL || "wss://web-cli-terminal.ably-dev.com";
21+
const TERMINAL_SERVER_URL = getTerminalServerUrl();
2422

2523
/**
2624
* Wait for the terminal prompt to appear, indicating the terminal is ready

0 commit comments

Comments
 (0)