|
| 1 | +import fs from "fs"; |
| 2 | +import axios from "axios"; |
| 3 | +import config from "../../config"; |
| 4 | +import FormData from "form-data"; |
| 5 | +import { customFuzzySearch } from "../../lib/fuzzy"; |
| 6 | + |
| 7 | +interface Device { |
| 8 | + device: string; |
| 9 | + display_name: string; |
| 10 | + os_version: string; |
| 11 | + real_mobile: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +interface UploadResponse { |
| 15 | + app_url: string; |
| 16 | + custom_id?: string; |
| 17 | + shareable_id?: string; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Finds devices that exactly match the provided display name. |
| 22 | + * Uses fuzzy search first, and then filters for exact case-insensitive match. |
| 23 | + */ |
| 24 | +export function findMatchingDevice( |
| 25 | + devices: Device[], |
| 26 | + deviceName: string, |
| 27 | +): Device[] { |
| 28 | + const matches = customFuzzySearch(devices, ["display_name"], deviceName, 5); |
| 29 | + |
| 30 | + if (matches.length === 0) { |
| 31 | + const availableDevices = [ |
| 32 | + ...new Set(devices.map((d) => d.display_name)), |
| 33 | + ].join(", "); |
| 34 | + throw new Error( |
| 35 | + `No devices found matching "${deviceName}". Available devices: ${availableDevices}`, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + const exactMatches = matches.filter( |
| 40 | + (m) => m.display_name.toLowerCase() === deviceName.toLowerCase(), |
| 41 | + ); |
| 42 | + |
| 43 | + if (exactMatches.length === 0) { |
| 44 | + const suggestions = [...new Set(matches.map((d) => d.display_name))].join( |
| 45 | + ", ", |
| 46 | + ); |
| 47 | + throw new Error( |
| 48 | + `Alternative devices found: ${suggestions}. Please select one of these exact device names.`, |
| 49 | + ); |
| 50 | + } |
| 51 | + return exactMatches; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Extracts all unique OS versions from a device list and sorts them. |
| 56 | + */ |
| 57 | +export function getDeviceVersions(devices: Device[]): string[] { |
| 58 | + return [...new Set(devices.map((d) => d.os_version))].sort(); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Resolves the requested platform version against available versions. |
| 63 | + * Supports 'latest' and 'oldest' as dynamic selectors. |
| 64 | + */ |
| 65 | +export function resolveVersion( |
| 66 | + versions: string[], |
| 67 | + requestedVersion: string, |
| 68 | +): string { |
| 69 | + if (requestedVersion === "latest") { |
| 70 | + return versions[versions.length - 1]; |
| 71 | + } |
| 72 | + |
| 73 | + if (requestedVersion === "oldest") { |
| 74 | + return versions[0]; |
| 75 | + } |
| 76 | + |
| 77 | + const match = versions.find((v) => v === requestedVersion); |
| 78 | + if (!match) { |
| 79 | + throw new Error( |
| 80 | + `Version "${requestedVersion}" not found. Available versions: ${versions.join(", ")}`, |
| 81 | + ); |
| 82 | + } |
| 83 | + return match; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Validates the input arguments for taking app screenshots. |
| 88 | + * Checks for presence and correctness of platform, device, and file types. |
| 89 | + */ |
| 90 | +export function validateArgs(args: { |
| 91 | + desiredPlatform: string; |
| 92 | + desiredPlatformVersion: string; |
| 93 | + appPath: string; |
| 94 | + desiredPhone: string; |
| 95 | +}): void { |
| 96 | + const { desiredPlatform, desiredPlatformVersion, appPath, desiredPhone } = |
| 97 | + args; |
| 98 | + |
| 99 | + if (!desiredPlatform || !desiredPhone) { |
| 100 | + throw new Error( |
| 101 | + "Missing required arguments: desiredPlatform and desiredPhone are required", |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + if (!desiredPlatformVersion) { |
| 106 | + throw new Error( |
| 107 | + "Missing required arguments: desiredPlatformVersion is required", |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + if (!appPath) { |
| 112 | + throw new Error("You must provide an appPath."); |
| 113 | + } |
| 114 | + |
| 115 | + if (desiredPlatform === "android" && !appPath.endsWith(".apk")) { |
| 116 | + throw new Error("You must provide a valid Android app path (.apk)."); |
| 117 | + } |
| 118 | + |
| 119 | + if (desiredPlatform === "ios" && !appPath.endsWith(".ipa")) { |
| 120 | + throw new Error("You must provide a valid iOS app path (.ipa)."); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Uploads an application file to Automate and returns the app URL |
| 126 | + */ |
| 127 | +export async function uploadApp(appPath: string): Promise<string> { |
| 128 | + const filePath = appPath; |
| 129 | + |
| 130 | + if (!fs.existsSync(filePath)) { |
| 131 | + throw new Error(`File not found at path: ${filePath}`); |
| 132 | + } |
| 133 | + |
| 134 | + const formData = new FormData(); |
| 135 | + formData.append("file", fs.createReadStream(filePath)); |
| 136 | + |
| 137 | + const response = await axios.post<UploadResponse>( |
| 138 | + "https://api-cloud.browserstack.com/app-automate/upload", |
| 139 | + formData, |
| 140 | + { |
| 141 | + headers: { |
| 142 | + ...formData.getHeaders(), |
| 143 | + }, |
| 144 | + auth: { |
| 145 | + username: config.browserstackUsername, |
| 146 | + password: config.browserstackAccessKey, |
| 147 | + }, |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + if (response.data.app_url) { |
| 152 | + return response.data.app_url; |
| 153 | + } else { |
| 154 | + throw new Error(`Failed to upload app: ${response.data}`); |
| 155 | + } |
| 156 | +} |
0 commit comments