|
| 1 | +import { AppError } from '@agent-device/kernel/errors'; |
| 2 | +import { createAwsDeviceFarmCommandRunner } from './aws-device-farm.ts'; |
| 3 | +import type { RunHostCommand } from './dependencies.ts'; |
| 4 | +import type { |
| 5 | + CloudWebDriverConnectionVerification, |
| 6 | + CloudWebDriverConnectionVerificationOptions, |
| 7 | +} from './connection-verification.ts'; |
| 8 | +import type { ProviderConnectionResource } from '@agent-device/contracts/remote'; |
| 9 | + |
| 10 | +type AwsOptions = Extract< |
| 11 | + CloudWebDriverConnectionVerificationOptions, |
| 12 | + { provider: 'aws-device-farm' } |
| 13 | +>; |
| 14 | + |
| 15 | +export function readAwsDeviceFarmRegionFromArn(arn: string): string | undefined { |
| 16 | + return /^arn:[^:]+:devicefarm:([^:]+):/.exec(arn)?.[1]; |
| 17 | +} |
| 18 | + |
| 19 | +export async function verifyAwsDeviceFarmConnection( |
| 20 | + options: AwsOptions, |
| 21 | + runHostCommand: RunHostCommand, |
| 22 | +): Promise<CloudWebDriverConnectionVerification> { |
| 23 | + const runAwsJson = createAwsDeviceFarmCommandRunner({ |
| 24 | + runHostCommand, |
| 25 | + region: options.region, |
| 26 | + }); |
| 27 | + const [projectResponse, deviceResponse, uploadResponse] = await Promise.all([ |
| 28 | + runAwsJson('get-project', ['--arn', options.projectArn]), |
| 29 | + runAwsJson('get-device', ['--arn', options.deviceArn]), |
| 30 | + options.appArn ? runAwsJson('get-upload', ['--arn', options.appArn]) : undefined, |
| 31 | + ]); |
| 32 | + const project = readAwsResource(projectResponse, 'project'); |
| 33 | + const device = readAwsResource(deviceResponse, 'device'); |
| 34 | + const devicePlatform = readAwsPlatform(device.platform); |
| 35 | + if (devicePlatform !== options.platform) { |
| 36 | + throw new AppError( |
| 37 | + 'INVALID_ARGS', |
| 38 | + `AWS Device Farm device "${readString(device.name) ?? options.deviceArn}" is ${devicePlatform}, not ${options.platform}.`, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + const app = options.appArn |
| 43 | + ? verifyAwsUpload(readAwsResource(uploadResponse, 'upload'), options) |
| 44 | + : { |
| 45 | + status: 'missing' as const, |
| 46 | + message: |
| 47 | + 'No app upload is attached; AWS Device Farm does not support install after allocation. Reconnect with --aws-app-arn <arn>.', |
| 48 | + }; |
| 49 | + return { |
| 50 | + provider: 'aws-device-farm', |
| 51 | + service: 'AWS Device Farm', |
| 52 | + verificationMessage: options.appArn |
| 53 | + ? 'Credentials, project, device, and app upload verified.' |
| 54 | + : 'Credentials, project, and device verified.', |
| 55 | + project: { name: readString(project.name), reference: options.projectArn }, |
| 56 | + device: { |
| 57 | + status: 'verified', |
| 58 | + name: readString(device.name) ?? options.deviceArn, |
| 59 | + reference: options.deviceArn, |
| 60 | + platform: devicePlatform, |
| 61 | + osVersion: readString(device.os), |
| 62 | + availability: readString(device.availability), |
| 63 | + }, |
| 64 | + app, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function verifyAwsUpload( |
| 69 | + upload: Record<string, unknown>, |
| 70 | + options: AwsOptions, |
| 71 | +): ProviderConnectionResource { |
| 72 | + const status = readString(upload.status); |
| 73 | + if (status !== 'SUCCEEDED') { |
| 74 | + throw new AppError( |
| 75 | + 'COMMAND_FAILED', |
| 76 | + `AWS Device Farm app upload is not ready (${status ?? 'unknown status'}).`, |
| 77 | + { |
| 78 | + status, |
| 79 | + hint: 'Wait for the upload to succeed or reconnect with a different --aws-app-arn.', |
| 80 | + }, |
| 81 | + ); |
| 82 | + } |
| 83 | + const expectedType = options.platform === 'android' ? 'ANDROID_APP' : 'IOS_APP'; |
| 84 | + const type = readString(upload.type); |
| 85 | + if (type !== expectedType) { |
| 86 | + throw new AppError( |
| 87 | + 'INVALID_ARGS', |
| 88 | + `AWS Device Farm app upload type is ${type ?? 'unknown'}, expected ${expectedType}.`, |
| 89 | + ); |
| 90 | + } |
| 91 | + return { |
| 92 | + status: 'verified', |
| 93 | + name: readString(upload.name), |
| 94 | + reference: options.appArn, |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +function readAwsResource(value: unknown, key: string): Record<string, unknown> { |
| 99 | + if (!value || typeof value !== 'object') { |
| 100 | + throw new AppError('COMMAND_FAILED', `AWS Device Farm ${key} response was not an object.`); |
| 101 | + } |
| 102 | + const resource = (value as Record<string, unknown>)[key]; |
| 103 | + if (!resource || typeof resource !== 'object' || Array.isArray(resource)) { |
| 104 | + throw new AppError('COMMAND_FAILED', `AWS Device Farm response missed ${key}.`); |
| 105 | + } |
| 106 | + return resource as Record<string, unknown>; |
| 107 | +} |
| 108 | + |
| 109 | +function readAwsPlatform(value: unknown): 'android' | 'ios' { |
| 110 | + if (value === 'ANDROID') return 'android'; |
| 111 | + if (value === 'IOS') return 'ios'; |
| 112 | + throw new AppError( |
| 113 | + 'UNSUPPORTED_PLATFORM', |
| 114 | + `AWS Device Farm device platform is ${String(value)}.`, |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +function readString(value: unknown): string | undefined { |
| 119 | + return typeof value === 'string' && value.length > 0 ? value : undefined; |
| 120 | +} |
0 commit comments