|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { tryRunClientBackedCommand } from '../cli-client-commands.ts'; |
| 4 | +import type { AgentDeviceClient, AppInstallFromSourceOptions } from '../client.ts'; |
| 5 | +import { AppError } from '../utils/errors.ts'; |
| 6 | + |
| 7 | +test('install-from-source forwards URL and repeated headers to client.apps.installFromSource', async () => { |
| 8 | + let observed: AppInstallFromSourceOptions | undefined; |
| 9 | + const client = createStubClient({ |
| 10 | + installFromSource: async (options) => { |
| 11 | + observed = options; |
| 12 | + return { |
| 13 | + launchTarget: 'com.example.demo', |
| 14 | + packageName: 'com.example.demo', |
| 15 | + identifiers: { appId: 'com.example.demo', package: 'com.example.demo' }, |
| 16 | + }; |
| 17 | + }, |
| 18 | + }); |
| 19 | + |
| 20 | + const handled = await tryRunClientBackedCommand({ |
| 21 | + command: 'install-from-source', |
| 22 | + positionals: ['https://example.com/app.apk'], |
| 23 | + flags: { |
| 24 | + json: false, |
| 25 | + help: false, |
| 26 | + version: false, |
| 27 | + platform: 'android', |
| 28 | + header: ['authorization: Bearer token', 'x-build-id: 42'], |
| 29 | + retainPaths: true, |
| 30 | + retentionMs: 60_000, |
| 31 | + }, |
| 32 | + client, |
| 33 | + }); |
| 34 | + |
| 35 | + assert.equal(handled, true); |
| 36 | + assert.equal(observed?.platform, 'android'); |
| 37 | + assert.equal(observed?.retainPaths, true); |
| 38 | + assert.equal(observed?.retentionMs, 60_000); |
| 39 | + assert.deepEqual(observed?.source, { |
| 40 | + kind: 'url', |
| 41 | + url: 'https://example.com/app.apk', |
| 42 | + headers: { |
| 43 | + authorization: 'Bearer token', |
| 44 | + 'x-build-id': '42', |
| 45 | + }, |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +test('install-from-source rejects malformed header syntax', async () => { |
| 50 | + const client = createStubClient({ |
| 51 | + installFromSource: async () => { |
| 52 | + throw new Error('unexpected call'); |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + await assert.rejects( |
| 57 | + () => |
| 58 | + tryRunClientBackedCommand({ |
| 59 | + command: 'install-from-source', |
| 60 | + positionals: ['https://example.com/app.apk'], |
| 61 | + flags: { |
| 62 | + json: false, |
| 63 | + help: false, |
| 64 | + version: false, |
| 65 | + header: ['authorization'], |
| 66 | + }, |
| 67 | + client, |
| 68 | + }), |
| 69 | + (error) => |
| 70 | + error instanceof AppError && |
| 71 | + error.code === 'INVALID_ARGS' && |
| 72 | + error.message.includes('Expected "name:value"'), |
| 73 | + ); |
| 74 | +}); |
| 75 | + |
| 76 | +function createStubClient(params: { |
| 77 | + installFromSource: AgentDeviceClient['apps']['installFromSource']; |
| 78 | +}): AgentDeviceClient { |
| 79 | + return { |
| 80 | + devices: { |
| 81 | + list: async () => [], |
| 82 | + }, |
| 83 | + sessions: { |
| 84 | + list: async () => [], |
| 85 | + close: async () => ({ session: 'default', identifiers: { session: 'default' } }), |
| 86 | + }, |
| 87 | + simulators: { |
| 88 | + ensure: async () => ({ |
| 89 | + udid: 'sim-1', |
| 90 | + device: 'iPhone 16', |
| 91 | + runtime: 'iOS-18-0', |
| 92 | + created: false, |
| 93 | + booted: true, |
| 94 | + identifiers: { |
| 95 | + deviceId: 'sim-1', |
| 96 | + deviceName: 'iPhone 16', |
| 97 | + udid: 'sim-1', |
| 98 | + }, |
| 99 | + }), |
| 100 | + }, |
| 101 | + apps: { |
| 102 | + install: async () => ({ |
| 103 | + app: 'Demo', |
| 104 | + appPath: '/tmp/Demo.app', |
| 105 | + platform: 'ios', |
| 106 | + identifiers: { appId: 'com.example.demo' }, |
| 107 | + }), |
| 108 | + reinstall: async () => ({ |
| 109 | + app: 'Demo', |
| 110 | + appPath: '/tmp/Demo.app', |
| 111 | + platform: 'ios', |
| 112 | + identifiers: { appId: 'com.example.demo' }, |
| 113 | + }), |
| 114 | + installFromSource: params.installFromSource, |
| 115 | + open: async () => ({ |
| 116 | + session: 'default', |
| 117 | + identifiers: { session: 'default' }, |
| 118 | + }), |
| 119 | + close: async () => ({ |
| 120 | + session: 'default', |
| 121 | + identifiers: { session: 'default' }, |
| 122 | + }), |
| 123 | + }, |
| 124 | + materializations: { |
| 125 | + release: async (options) => ({ |
| 126 | + released: true, |
| 127 | + materializationId: options.materializationId, |
| 128 | + identifiers: { session: options.session ?? 'default' }, |
| 129 | + }), |
| 130 | + }, |
| 131 | + runtime: { |
| 132 | + set: async () => ({ |
| 133 | + session: 'default', |
| 134 | + configured: true, |
| 135 | + identifiers: { session: 'default' }, |
| 136 | + }), |
| 137 | + show: async () => ({ |
| 138 | + session: 'default', |
| 139 | + configured: false, |
| 140 | + identifiers: { session: 'default' }, |
| 141 | + }), |
| 142 | + }, |
| 143 | + capture: { |
| 144 | + snapshot: async () => ({ |
| 145 | + nodes: [], |
| 146 | + truncated: false, |
| 147 | + identifiers: { session: 'default' }, |
| 148 | + }), |
| 149 | + screenshot: async () => ({ |
| 150 | + path: '/tmp/screenshot.png', |
| 151 | + identifiers: { session: 'default' }, |
| 152 | + }), |
| 153 | + }, |
| 154 | + }; |
| 155 | +} |
0 commit comments