|
1 | 1 | import childProcess from "child_process";
|
2 | 2 | import logger from "../../logger";
|
| 3 | +import { getAppLiveData } from "./device-cache"; |
| 4 | +import { fuzzySearchDevices } from "./fuzzy-search"; |
3 | 5 | import { sanitizeUrlParam } from "../../lib/utils";
|
| 6 | +import { uploadApp } from "./upload-app"; |
| 7 | + |
| 8 | +export interface DeviceEntry { |
| 9 | + device: string; |
| 10 | + display_name: string; |
| 11 | + os: string; |
| 12 | + os_version: string; |
| 13 | + real_mobile: boolean; |
| 14 | +} |
4 | 15 |
|
5 | 16 | interface StartSessionArgs {
|
6 |
| - appUrl: string; |
| 17 | + appPath: string; |
7 | 18 | desiredPlatform: "android" | "ios";
|
8 | 19 | desiredPhone: string;
|
9 | 20 | desiredPlatformVersion: string;
|
10 | 21 | }
|
11 | 22 |
|
| 23 | +/** |
| 24 | + * Starts an App Live session after filtering, fuzzy matching, and launching. |
| 25 | + * @param args - The arguments for starting the session. |
| 26 | + * @returns The launch URL for the session. |
| 27 | + * @throws Will throw an error if no devices are found or if the app URL is invalid. |
| 28 | + */ |
12 | 29 | export async function startSession(args: StartSessionArgs): Promise<string> {
|
13 |
| - // Sanitize all input parameters |
14 |
| - const sanitizedArgs = { |
15 |
| - appUrl: sanitizeUrlParam(args.appUrl), |
16 |
| - desiredPlatform: sanitizeUrlParam(args.desiredPlatform), |
17 |
| - desiredPhone: sanitizeUrlParam(args.desiredPhone), |
18 |
| - desiredPlatformVersion: sanitizeUrlParam(args.desiredPlatformVersion), |
19 |
| - }; |
20 |
| - |
21 |
| - // Get app hash ID and format phone name |
22 |
| - const appHashedId = sanitizedArgs.appUrl.split("bs://").pop(); |
23 |
| - const desiredPhoneWithSpaces = sanitizedArgs.desiredPhone.replace( |
24 |
| - /\s+/g, |
25 |
| - "+", |
| 30 | + const { appPath, desiredPlatform, desiredPhone } = args; |
| 31 | + let { desiredPlatformVersion } = args; |
| 32 | + |
| 33 | + const data = await getAppLiveData(); |
| 34 | + const allDevices: DeviceEntry[] = data.mobile.flatMap((group: any) => |
| 35 | + group.devices.map((dev: any) => ({ ...dev, os: group.os })), |
| 36 | + ); |
| 37 | + |
| 38 | + desiredPlatformVersion = resolvePlatformVersion( |
| 39 | + allDevices, |
| 40 | + desiredPlatform, |
| 41 | + desiredPlatformVersion, |
| 42 | + ); |
| 43 | + |
| 44 | + const filteredDevices = filterDevicesByPlatformAndVersion( |
| 45 | + allDevices, |
| 46 | + desiredPlatform, |
| 47 | + desiredPlatformVersion, |
| 48 | + ); |
| 49 | + |
| 50 | + const matches = await fuzzySearchDevices(filteredDevices, desiredPhone); |
| 51 | + |
| 52 | + const selectedDevice = validateAndSelectDevice( |
| 53 | + matches, |
| 54 | + desiredPhone, |
| 55 | + desiredPlatform, |
| 56 | + desiredPlatformVersion, |
| 57 | + ); |
| 58 | + |
| 59 | + const { app_url } = await uploadApp(appPath); |
| 60 | + |
| 61 | + validateAppUrl(app_url); |
| 62 | + |
| 63 | + const launchUrl = constructLaunchUrl( |
| 64 | + app_url, |
| 65 | + selectedDevice, |
| 66 | + desiredPlatform, |
| 67 | + desiredPlatformVersion, |
| 68 | + ); |
| 69 | + |
| 70 | + openBrowser(launchUrl); |
| 71 | + |
| 72 | + return launchUrl; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Resolves the platform version based on the desired platform and version. |
| 77 | + * @param allDevices - The list of all devices. |
| 78 | + * @param desiredPlatform - The desired platform (android or ios). |
| 79 | + * @param desiredPlatformVersion - The desired platform version. |
| 80 | + * @returns The resolved platform version. |
| 81 | + * @throws Will throw an error if the platform version is not valid. |
| 82 | + */ |
| 83 | +function resolvePlatformVersion( |
| 84 | + allDevices: DeviceEntry[], |
| 85 | + desiredPlatform: string, |
| 86 | + desiredPlatformVersion: string, |
| 87 | +): string { |
| 88 | + if ( |
| 89 | + desiredPlatformVersion === "latest" || |
| 90 | + desiredPlatformVersion === "oldest" |
| 91 | + ) { |
| 92 | + const filtered = allDevices.filter((d) => d.os === desiredPlatform); |
| 93 | + filtered.sort((a, b) => { |
| 94 | + const versionA = parseFloat(a.os_version); |
| 95 | + const versionB = parseFloat(b.os_version); |
| 96 | + return desiredPlatformVersion === "latest" |
| 97 | + ? versionB - versionA |
| 98 | + : versionA - versionB; |
| 99 | + }); |
| 100 | + |
| 101 | + return filtered[0].os_version; |
| 102 | + } |
| 103 | + return desiredPlatformVersion; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Filters devices based on the desired platform and version. |
| 108 | + * @param allDevices - The list of all devices. |
| 109 | + * @param desiredPlatform - The desired platform (android or ios). |
| 110 | + * @param desiredPlatformVersion - The desired platform version. |
| 111 | + * @returns The filtered list of devices. |
| 112 | + * @throws Will throw an error if the platform version is not valid. |
| 113 | + */ |
| 114 | +function filterDevicesByPlatformAndVersion( |
| 115 | + allDevices: DeviceEntry[], |
| 116 | + desiredPlatform: string, |
| 117 | + desiredPlatformVersion: string, |
| 118 | +): DeviceEntry[] { |
| 119 | + return allDevices.filter((d) => { |
| 120 | + if (d.os !== desiredPlatform) return false; |
| 121 | + |
| 122 | + try { |
| 123 | + const versionA = parseFloat(d.os_version); |
| 124 | + const versionB = parseFloat(desiredPlatformVersion); |
| 125 | + return versionA === versionB; |
| 126 | + } catch { |
| 127 | + return d.os_version === desiredPlatformVersion; |
| 128 | + } |
| 129 | + }); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Validates the selected device and handles multiple matches. |
| 134 | + * @param matches - The list of device matches. |
| 135 | + * @param desiredPhone - The desired phone name. |
| 136 | + * @param desiredPlatform - The desired platform (android or ios). |
| 137 | + * @param desiredPlatformVersion - The desired platform version. |
| 138 | + * @returns The selected device entry. |
| 139 | + */ |
| 140 | +function validateAndSelectDevice( |
| 141 | + matches: DeviceEntry[], |
| 142 | + desiredPhone: string, |
| 143 | + desiredPlatform: string, |
| 144 | + desiredPlatformVersion: string, |
| 145 | +): DeviceEntry { |
| 146 | + if (matches.length === 0) { |
| 147 | + throw new Error( |
| 148 | + `No devices found matching "${desiredPhone}" for ${desiredPlatform} ${desiredPlatformVersion}`, |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + const exactMatch = matches.find( |
| 153 | + (d) => d.display_name.toLowerCase() === desiredPhone.toLowerCase(), |
| 154 | + ); |
| 155 | + |
| 156 | + if (exactMatch) { |
| 157 | + return exactMatch; |
| 158 | + } else if (matches.length >= 1) { |
| 159 | + const names = matches.map((d) => d.display_name).join(", "); |
| 160 | + const error_message = |
| 161 | + matches.length === 1 |
| 162 | + ? `Alternative device found: ${names}. Would you like to use it?` |
| 163 | + : `Multiple devices found: ${names}. Please select one.`; |
| 164 | + throw new Error(`${error_message}`); |
| 165 | + } |
| 166 | + |
| 167 | + return matches[0]; |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * Validates the app URL. |
| 172 | + * @param appUrl - The app URL to validate. |
| 173 | + * @throws Will throw an error if the app URL is not valid. |
| 174 | + */ |
| 175 | +function validateAppUrl(appUrl: string): void { |
| 176 | + if (!appUrl.match("bs://")) { |
| 177 | + throw new Error("The app path is not a valid BrowserStack app URL."); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * Constructs the launch URL for the App Live session. |
| 183 | + * @param appUrl - The app URL. |
| 184 | + * @param device - The selected device entry. |
| 185 | + * @param desiredPlatform - The desired platform (android or ios). |
| 186 | + * @param desiredPlatformVersion - The desired platform version. |
| 187 | + * @returns The constructed launch URL. |
| 188 | + */ |
| 189 | +function constructLaunchUrl( |
| 190 | + appUrl: string, |
| 191 | + device: DeviceEntry, |
| 192 | + desiredPlatform: string, |
| 193 | + desiredPlatformVersion: string, |
| 194 | +): string { |
| 195 | + const deviceParam = sanitizeUrlParam( |
| 196 | + device.display_name.replace(/\s+/g, "+"), |
26 | 197 | );
|
27 | 198 |
|
28 |
| - // Construct URL with encoded parameters |
29 | 199 | const params = new URLSearchParams({
|
30 |
| - os: sanitizedArgs.desiredPlatform, |
31 |
| - os_version: sanitizedArgs.desiredPlatformVersion, |
32 |
| - app_hashed_id: appHashedId || "", |
| 200 | + os: desiredPlatform, |
| 201 | + os_version: desiredPlatformVersion, |
| 202 | + app_hashed_id: appUrl.split("bs://").pop() || "", |
33 | 203 | scale_to_fit: "true",
|
34 | 204 | speed: "1",
|
35 | 205 | start: "true",
|
36 | 206 | });
|
37 | 207 |
|
38 |
| - const launchUrl = `https://app-live.browserstack.com/dashboard#${params.toString()}&device=${desiredPhoneWithSpaces}`; |
| 208 | + return `https://app-live.browserstack.com/dashboard#${params.toString()}&device=${deviceParam}`; |
| 209 | +} |
39 | 210 |
|
| 211 | +/** |
| 212 | + * Opens the launch URL in the default browser. |
| 213 | + * @param launchUrl - The URL to open. |
| 214 | + * @throws Will throw an error if the browser fails to open. |
| 215 | + */ |
| 216 | +function openBrowser(launchUrl: string): void { |
40 | 217 | try {
|
41 |
| - // Use platform-specific commands with proper escaping |
42 | 218 | const command =
|
43 | 219 | process.platform === "darwin"
|
44 | 220 | ? ["open", launchUrl]
|
45 | 221 | : process.platform === "win32"
|
46 | 222 | ? ["cmd", "/c", "start", launchUrl]
|
47 | 223 | : ["xdg-open", launchUrl];
|
48 |
| - |
| 224 | + |
49 | 225 | // nosemgrep:javascript.lang.security.detect-child-process.detect-child-process
|
50 | 226 | const child = childProcess.spawn(command[0], command.slice(1), {
|
51 | 227 | stdio: "ignore",
|
52 | 228 | detached: true,
|
53 | 229 | });
|
54 | 230 |
|
55 |
| - // Handle process errors |
56 | 231 | child.on("error", (error) => {
|
57 | 232 | logger.error(
|
58 | 233 | `Failed to open browser automatically: ${error}. Please open this URL manually: ${launchUrl}`,
|
59 | 234 | );
|
60 | 235 | });
|
61 | 236 |
|
62 |
| - // Unref the child process to allow the parent to exit |
63 | 237 | child.unref();
|
64 |
| - |
65 |
| - return launchUrl; |
66 | 238 | } catch (error) {
|
67 | 239 | logger.error(
|
68 | 240 | `Failed to open browser automatically: ${error}. Please open this URL manually: ${launchUrl}`,
|
69 | 241 | );
|
70 |
| - return launchUrl; |
71 | 242 | }
|
72 | 243 | }
|
0 commit comments