|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import { createAgentDeviceClient } from '../agent-device-client.ts'; |
| 4 | +import { createTransport } from './client-transport-fixture.ts'; |
| 5 | + |
| 6 | +test('apps.open resolves session device identifiers from open response', async () => { |
| 7 | + const setup = createTransport(async (req) => { |
| 8 | + if (req.command === 'open') { |
| 9 | + return { |
| 10 | + ok: true, |
| 11 | + data: { |
| 12 | + session: 'qa', |
| 13 | + sessionStateDir: '/tmp/agent-device/sessions/qa', |
| 14 | + runnerLogPath: '/tmp/agent-device/sessions/qa/runner.log', |
| 15 | + requestLogPath: '/tmp/agent-device/sessions/qa/requests/open.ndjson', |
| 16 | + eventLogPath: '/tmp/agent-device/sessions/qa/events.ndjson', |
| 17 | + appName: 'Settings', |
| 18 | + appBundleId: 'com.apple.Preferences', |
| 19 | + platform: 'ios', |
| 20 | + target: 'mobile', |
| 21 | + device: 'iPhone 16', |
| 22 | + id: 'SIM-001', |
| 23 | + kind: 'simulator', |
| 24 | + device_udid: 'SIM-001', |
| 25 | + ios_simulator_device_set: '/tmp/sim-set', |
| 26 | + warnings: ['Script publication was aborted by a second successful open.', 42], |
| 27 | + startup: { |
| 28 | + durationMs: 1234, |
| 29 | + measuredAt: '2026-03-13T10:00:00.000Z', |
| 30 | + method: 'open-command-roundtrip', |
| 31 | + }, |
| 32 | + }, |
| 33 | + }; |
| 34 | + } |
| 35 | + throw new Error(`Unexpected command: ${req.command}`); |
| 36 | + }); |
| 37 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 38 | + |
| 39 | + const result = await client.apps.open({ |
| 40 | + app: 'Settings', |
| 41 | + platform: 'ios', |
| 42 | + relaunch: true, |
| 43 | + }); |
| 44 | + |
| 45 | + assert.equal(setup.calls.length, 1); |
| 46 | + assert.equal(setup.calls[0]?.command, 'open'); |
| 47 | + assert.deepEqual(setup.calls[0]?.positionals, ['Settings']); |
| 48 | + assert.equal(result.identifiers.session, 'qa'); |
| 49 | + assert.equal(result.identifiers.deviceId, 'SIM-001'); |
| 50 | + assert.equal(result.identifiers.udid, 'SIM-001'); |
| 51 | + assert.equal(result.identifiers.appId, 'com.apple.Preferences'); |
| 52 | + assert.equal(result.sessionStateDir, '/tmp/agent-device/sessions/qa'); |
| 53 | + assert.equal(result.runnerLogPath, '/tmp/agent-device/sessions/qa/runner.log'); |
| 54 | + assert.equal(result.requestLogPath, '/tmp/agent-device/sessions/qa/requests/open.ndjson'); |
| 55 | + assert.equal(result.eventLogPath, '/tmp/agent-device/sessions/qa/events.ndjson'); |
| 56 | + assert.equal(result.device?.name, 'iPhone 16'); |
| 57 | + assert.equal(result.device?.ios?.simulatorSetPath, '/tmp/sim-set'); |
| 58 | + assert.deepEqual(result.warnings, [ |
| 59 | + 'Script publication was aborted by a second successful open.', |
| 60 | + ]); |
| 61 | +}); |
| 62 | + |
| 63 | +test('apps.open reports an answered launch confirmation only when the daemon answered one', async () => { |
| 64 | + let launchConfirmation: unknown = 'accepted'; |
| 65 | + const setup = createTransport(async (req) => { |
| 66 | + if (req.command === 'open') { |
| 67 | + return { |
| 68 | + ok: true, |
| 69 | + data: { session: 'qa', appBundleId: 'com.example.app', launchConfirmation }, |
| 70 | + }; |
| 71 | + } |
| 72 | + throw new Error(`Unexpected command: ${req.command}`); |
| 73 | + }); |
| 74 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 75 | + |
| 76 | + const answered = await client.apps.open({ app: 'com.example.app', platform: 'ios' }); |
| 77 | + launchConfirmation = undefined; |
| 78 | + const unanswered = await client.apps.open({ app: 'com.example.app', platform: 'ios' }); |
| 79 | + |
| 80 | + assert.equal(answered.launchConfirmation, 'accepted'); |
| 81 | + assert.equal('launchConfirmation' in unanswered, false); |
| 82 | +}); |
| 83 | + |
| 84 | +test('apps.open preserves the full initialSnapshotError shape through client normalization', async () => { |
| 85 | + // open --foreground: open succeeded, composed snapshot did not. The public |
| 86 | + // client result must carry the FULL daemon error — dropping the boundary |
| 87 | + // normalization (or truncating to code+message) must fail here. |
| 88 | + const initialSnapshotError = { |
| 89 | + code: 'COMMAND_FAILED', |
| 90 | + message: 'capture failed', |
| 91 | + hint: 'Run: agent-device snapshot -i', |
| 92 | + details: { reason: 'runner_capture_failed' }, |
| 93 | + diagnosticId: 'ms-diag-1234', |
| 94 | + logPath: '/tmp/agent-device/sessions/qa/requests/snap.ndjson', |
| 95 | + retriable: true, |
| 96 | + }; |
| 97 | + const setup = createTransport(async (req) => { |
| 98 | + if (req.command === 'open') { |
| 99 | + return { |
| 100 | + ok: true, |
| 101 | + data: { |
| 102 | + session: 'qa', |
| 103 | + appName: 'Settings', |
| 104 | + appBundleId: 'com.apple.Preferences', |
| 105 | + platform: 'ios', |
| 106 | + target: 'mobile', |
| 107 | + device: 'iPhone 16', |
| 108 | + id: 'SIM-001', |
| 109 | + kind: 'simulator', |
| 110 | + device_udid: 'SIM-001', |
| 111 | + warnings: ['The session is open, but the initial interactive snapshot failed.'], |
| 112 | + initialSnapshotError, |
| 113 | + }, |
| 114 | + }; |
| 115 | + } |
| 116 | + throw new Error(`Unexpected command: ${req.command}`); |
| 117 | + }); |
| 118 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 119 | + |
| 120 | + const result = await client.apps.open({ app: 'Settings', platform: 'ios', foreground: true }); |
| 121 | + |
| 122 | + assert.deepEqual(result.initialSnapshotError, initialSnapshotError); |
| 123 | + assert.equal(result.snapshot, undefined); |
| 124 | +}); |
| 125 | + |
| 126 | +test('apps.open drops a malformed initialSnapshotError instead of projecting garbage', async () => { |
| 127 | + const setup = createTransport(async () => ({ |
| 128 | + ok: true, |
| 129 | + data: { |
| 130 | + session: 'qa', |
| 131 | + appName: 'Settings', |
| 132 | + appBundleId: 'com.apple.Preferences', |
| 133 | + platform: 'ios', |
| 134 | + target: 'mobile', |
| 135 | + device: 'iPhone 16', |
| 136 | + id: 'SIM-001', |
| 137 | + kind: 'simulator', |
| 138 | + device_udid: 'SIM-001', |
| 139 | + initialSnapshotError: { code: 'COMMAND_FAILED' }, |
| 140 | + }, |
| 141 | + })); |
| 142 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 143 | + |
| 144 | + const result = await client.apps.open({ app: 'Settings', platform: 'ios' }); |
| 145 | + |
| 146 | + assert.equal(result.initialSnapshotError, undefined); |
| 147 | +}); |
| 148 | + |
| 149 | +test('apps.open forwards explicit runtime hints through the daemon request', async () => { |
| 150 | + const setup = createTransport(async () => ({ |
| 151 | + ok: true, |
| 152 | + data: { |
| 153 | + session: 'qa', |
| 154 | + appName: 'Demo', |
| 155 | + appBundleId: 'com.example.demo', |
| 156 | + runtime: { |
| 157 | + platform: 'ios', |
| 158 | + metroHost: '127.0.0.1', |
| 159 | + metroPort: 8081, |
| 160 | + }, |
| 161 | + }, |
| 162 | + })); |
| 163 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 164 | + |
| 165 | + const result = await client.apps.open({ |
| 166 | + app: 'Demo', |
| 167 | + platform: 'ios', |
| 168 | + runtime: { |
| 169 | + metroHost: '127.0.0.1', |
| 170 | + metroPort: 8081, |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + assert.equal(setup.calls.length, 1); |
| 175 | + assert.deepEqual(setup.calls[0]?.runtime, { |
| 176 | + metroHost: '127.0.0.1', |
| 177 | + metroPort: 8081, |
| 178 | + }); |
| 179 | + assert.deepEqual(result.runtime, { |
| 180 | + platform: 'ios', |
| 181 | + metroHost: '127.0.0.1', |
| 182 | + metroPort: 8081, |
| 183 | + bundleUrl: undefined, |
| 184 | + launchUrl: undefined, |
| 185 | + }); |
| 186 | +}); |
| 187 | + |
| 188 | +test('apps.installFromSource forwards source payload and normalizes launch identity', async () => { |
| 189 | + const setup = createTransport(async () => ({ |
| 190 | + ok: true, |
| 191 | + data: { |
| 192 | + packageName: 'com.example.demo', |
| 193 | + appName: 'Demo', |
| 194 | + launchTarget: 'com.example.demo', |
| 195 | + installablePath: '/tmp/materialized/installable/demo.apk', |
| 196 | + archivePath: '/tmp/materialized/archive/demo.zip', |
| 197 | + materializationId: 'materialized-123', |
| 198 | + materializationExpiresAt: '2026-03-13T12:00:00.000Z', |
| 199 | + }, |
| 200 | + })); |
| 201 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 202 | + |
| 203 | + const result = await client.apps.installFromSource({ |
| 204 | + platform: 'android', |
| 205 | + retainPaths: true, |
| 206 | + retentionMs: 60_000, |
| 207 | + source: { |
| 208 | + kind: 'url', |
| 209 | + url: 'https://example.com/demo.apk', |
| 210 | + headers: { authorization: 'Bearer token' }, |
| 211 | + }, |
| 212 | + }); |
| 213 | + |
| 214 | + assert.equal(setup.calls.length, 1); |
| 215 | + assert.equal(setup.calls[0]?.command, 'install_source'); |
| 216 | + assert.deepEqual(setup.calls[0]?.meta?.installSource, { |
| 217 | + kind: 'url', |
| 218 | + url: 'https://example.com/demo.apk', |
| 219 | + headers: { authorization: 'Bearer token' }, |
| 220 | + }); |
| 221 | + assert.equal(setup.calls[0]?.meta?.retainMaterializedPaths, true); |
| 222 | + assert.equal(setup.calls[0]?.meta?.materializedPathRetentionMs, 60_000); |
| 223 | + assert.deepEqual(result, { |
| 224 | + appName: 'Demo', |
| 225 | + appId: 'com.example.demo', |
| 226 | + bundleId: undefined, |
| 227 | + packageName: 'com.example.demo', |
| 228 | + launchTarget: 'com.example.demo', |
| 229 | + installablePath: '/tmp/materialized/installable/demo.apk', |
| 230 | + archivePath: '/tmp/materialized/archive/demo.zip', |
| 231 | + materializationId: 'materialized-123', |
| 232 | + materializationExpiresAt: '2026-03-13T12:00:00.000Z', |
| 233 | + identifiers: { |
| 234 | + session: 'qa', |
| 235 | + appId: 'com.example.demo', |
| 236 | + appBundleId: undefined, |
| 237 | + package: 'com.example.demo', |
| 238 | + }, |
| 239 | + }); |
| 240 | +}); |
| 241 | + |
| 242 | +test('apps.installFromSource derives Android launchTarget from packageName when daemon omits it', async () => { |
| 243 | + const setup = createTransport(async () => ({ |
| 244 | + ok: true, |
| 245 | + data: { |
| 246 | + packageName: 'com.example.package-name-only', |
| 247 | + appName: 'PackageNameOnly', |
| 248 | + }, |
| 249 | + })); |
| 250 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 251 | + |
| 252 | + const result = await client.apps.installFromSource({ |
| 253 | + platform: 'android', |
| 254 | + source: { |
| 255 | + kind: 'url', |
| 256 | + url: 'https://example.com/package-name-only.apk', |
| 257 | + headers: {}, |
| 258 | + }, |
| 259 | + }); |
| 260 | + |
| 261 | + assert.deepEqual(result, { |
| 262 | + appName: 'PackageNameOnly', |
| 263 | + appId: 'com.example.package-name-only', |
| 264 | + bundleId: undefined, |
| 265 | + packageName: 'com.example.package-name-only', |
| 266 | + launchTarget: 'com.example.package-name-only', |
| 267 | + installablePath: undefined, |
| 268 | + archivePath: undefined, |
| 269 | + materializationId: undefined, |
| 270 | + materializationExpiresAt: undefined, |
| 271 | + identifiers: { |
| 272 | + session: 'qa', |
| 273 | + appId: 'com.example.package-name-only', |
| 274 | + appBundleId: undefined, |
| 275 | + package: 'com.example.package-name-only', |
| 276 | + }, |
| 277 | + }); |
| 278 | +}); |
| 279 | + |
| 280 | +test('apps.installFromSource forwards GitHub Actions artifact sources unchanged', async () => { |
| 281 | + const setup = createTransport(async () => ({ |
| 282 | + ok: true, |
| 283 | + data: { |
| 284 | + packageName: 'com.example.ci', |
| 285 | + }, |
| 286 | + })); |
| 287 | + const client = createAgentDeviceClient(setup.config, { transport: setup.transport }); |
| 288 | + |
| 289 | + await client.apps.installFromSource({ |
| 290 | + platform: 'android', |
| 291 | + source: { |
| 292 | + kind: 'github-actions-artifact', |
| 293 | + owner: 'acme', |
| 294 | + repo: 'mobile', |
| 295 | + artifactId: 1234567890, |
| 296 | + }, |
| 297 | + }); |
| 298 | + |
| 299 | + assert.equal(setup.calls.length, 1); |
| 300 | + assert.deepEqual(setup.calls[0]?.meta?.installSource, { |
| 301 | + kind: 'github-actions-artifact', |
| 302 | + owner: 'acme', |
| 303 | + repo: 'mobile', |
| 304 | + artifactId: 1234567890, |
| 305 | + }); |
| 306 | +}); |
0 commit comments