|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'vitest'; |
| 3 | +import type { AgentDeviceDaemonTransport } from '@agent-device/contracts/client'; |
| 4 | +import type { CommandFlags } from '@agent-device/contracts/command'; |
| 5 | +import { runCommand, type DevicePin } from '@agent-device/command-surface'; |
| 6 | +import { createAgentDeviceClient } from '../index.ts'; |
| 7 | + |
| 8 | +const PIN: DevicePin = { |
| 9 | + selector: { platform: 'ios', device: 'iPad Pro' }, |
| 10 | + source: { kind: 'project', path: '/repo/agent-device.json' }, |
| 11 | +}; |
| 12 | + |
| 13 | +function recordingClient(devicePin: DevicePin | undefined) { |
| 14 | + const requests: Array<Parameters<AgentDeviceDaemonTransport>[0]> = []; |
| 15 | + const transport: AgentDeviceDaemonTransport = async (request) => { |
| 16 | + requests.push(request); |
| 17 | + return { ok: true, data: { results: [], executed: 0, total: 0 } }; |
| 18 | + }; |
| 19 | + return { requests, client: createAgentDeviceClient({}, { transport, devicePin }) }; |
| 20 | +} |
| 21 | + |
| 22 | +function failsWithPinConflict(expected: { message: RegExp; conflicts: unknown[] }) { |
| 23 | + return (error: unknown): boolean => { |
| 24 | + assert.ok(error instanceof Error && 'code' in error && 'details' in error); |
| 25 | + assert.equal(error.code, 'INVALID_ARGS'); |
| 26 | + assert.match(error.message, expected.message); |
| 27 | + const details = error.details as Record<string, unknown>; |
| 28 | + assert.equal(details.reason, 'device_pin_conflict'); |
| 29 | + assert.equal(details.pinSource, 'project config file /repo/agent-device.json'); |
| 30 | + assert.deepEqual(details.conflicts, expected.conflicts); |
| 31 | + return true; |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +test('a batch step naming another device fails before the request leaves the client', async () => { |
| 36 | + const { client, requests } = recordingClient(PIN); |
| 37 | + |
| 38 | + await assert.rejects( |
| 39 | + runCommand(client, 'batch', { |
| 40 | + steps: [ |
| 41 | + { command: 'snapshot', input: {} }, |
| 42 | + { command: 'open', input: { app: 'com.example.app', device: 'iPhone 16' } }, |
| 43 | + ], |
| 44 | + }), |
| 45 | + failsWithPinConflict({ |
| 46 | + message: |
| 47 | + /^Batch step 2: device "iPhone 16" conflicts with the device pin from project config file/, |
| 48 | + conflicts: [{ key: 'device', pinned: 'iPad Pro', requested: 'iPhone 16', step: 2 }], |
| 49 | + }), |
| 50 | + ); |
| 51 | + assert.equal(requests.length, 0); |
| 52 | +}); |
| 53 | + |
| 54 | +test('a top-level selector from any layer that contradicts the pin fails', async () => { |
| 55 | + const { client, requests } = recordingClient(PIN); |
| 56 | + |
| 57 | + await assert.rejects( |
| 58 | + runCommand(client, 'snapshot', { platform: 'android' }), |
| 59 | + failsWithPinConflict({ |
| 60 | + message: /^platform "android" conflicts with the device pin/, |
| 61 | + conflicts: [{ key: 'platform', pinned: 'ios', requested: 'android' }], |
| 62 | + }), |
| 63 | + ); |
| 64 | + assert.equal(requests.length, 0); |
| 65 | +}); |
| 66 | + |
| 67 | +test('a batch whose steps repeat, refine, or omit the pin is sent', async () => { |
| 68 | + const { client, requests } = recordingClient(PIN); |
| 69 | + |
| 70 | + await runCommand(client, 'batch', { |
| 71 | + platform: 'ios', |
| 72 | + device: 'iPad Pro', |
| 73 | + steps: [ |
| 74 | + { command: 'snapshot', input: {} }, |
| 75 | + { command: 'open', input: { app: 'com.example.app', device: 'iPad Pro' } }, |
| 76 | + // The daemon checks that this identity still matches the pinned name. |
| 77 | + { command: 'snapshot', input: { udid: 'ABCDEF' } }, |
| 78 | + ], |
| 79 | + }); |
| 80 | + |
| 81 | + assert.equal(requests.length, 1); |
| 82 | + const flags = requests[0]?.flags as CommandFlags | undefined; |
| 83 | + assert.ok(requests[0]?.meta && 'devicePin' in requests[0].meta); |
| 84 | + assert.deepEqual(requests[0].meta.devicePin, PIN); |
| 85 | + assert.equal(flags?.platform, 'ios'); |
| 86 | + assert.equal(flags?.device, 'iPad Pro'); |
| 87 | + const steps = flags?.batchSteps; |
| 88 | + assert.equal(steps?.length, 3); |
| 89 | + assert.equal(steps?.[0]?.flags?.device, undefined); |
| 90 | + assert.equal(steps?.[1]?.flags?.device, 'iPad Pro'); |
| 91 | + assert.equal(steps?.[2]?.flags?.udid, 'ABCDEF'); |
| 92 | +}); |
| 93 | + |
| 94 | +test('without a pin the client sends a batch step that names another device', async () => { |
| 95 | + const { client, requests } = recordingClient(undefined); |
| 96 | + |
| 97 | + await runCommand(client, 'batch', { |
| 98 | + steps: [{ command: 'open', input: { app: 'com.example.app', device: 'iPhone 16' } }], |
| 99 | + }); |
| 100 | + |
| 101 | + const steps = (requests[0]?.flags as CommandFlags | undefined)?.batchSteps; |
| 102 | + assert.equal(steps?.[0]?.flags?.device, 'iPhone 16'); |
| 103 | +}); |
| 104 | + |
| 105 | +test('the client supplies pin selectors even when request options contain undefined defaults', async () => { |
| 106 | + const { client, requests } = recordingClient(PIN); |
| 107 | + await runCommand(client, 'snapshot', { platform: undefined, device: undefined }); |
| 108 | + assert.equal(requests[0]?.flags?.platform, 'ios'); |
| 109 | + assert.equal(requests[0]?.flags?.device, 'iPad Pro'); |
| 110 | + assert.ok(requests[0]?.meta && 'devicePin' in requests[0].meta); |
| 111 | + assert.deepEqual(requests[0].meta.devicePin, PIN); |
| 112 | +}); |
| 113 | + |
| 114 | +test('a name alias and overlapping Apple platform selectors are accepted', async () => { |
| 115 | + const { client, requests } = recordingClient(PIN); |
| 116 | + await runCommand(client, 'snapshot', { device: 'ipad_pro', platform: 'apple' }); |
| 117 | + assert.equal(requests.length, 1); |
| 118 | + assert.equal(requests[0]?.flags?.platform, 'ios'); |
| 119 | +}); |
| 120 | + |
| 121 | +test('an Apple-family pin forwards a requested leaf platform', async () => { |
| 122 | + const { client, requests } = recordingClient({ ...PIN, selector: { platform: 'apple' } }); |
| 123 | + await runCommand(client, 'snapshot', { platform: 'macos' }); |
| 124 | + assert.equal(requests[0]?.flags?.platform, 'macos'); |
| 125 | +}); |
| 126 | + |
| 127 | +for (const [selector, input, conflict] of [ |
| 128 | + [ |
| 129 | + { platform: 'ios' }, |
| 130 | + { serial: 'emulator-5554' }, |
| 131 | + { key: 'serial', pinnedKey: 'platform', pinned: 'ios', requested: 'emulator-5554' }, |
| 132 | + ], |
| 133 | + [ |
| 134 | + { platform: 'android' }, |
| 135 | + { udid: 'IPAD-001' }, |
| 136 | + { key: 'udid', pinnedKey: 'platform', pinned: 'android', requested: 'IPAD-001' }, |
| 137 | + ], |
| 138 | + [ |
| 139 | + { udid: 'IPAD-001' }, |
| 140 | + { serial: 'emulator-5554' }, |
| 141 | + { key: 'serial', pinnedKey: 'udid', pinned: 'IPAD-001', requested: 'emulator-5554' }, |
| 142 | + ], |
| 143 | + [ |
| 144 | + { serial: 'emulator-5554' }, |
| 145 | + { udid: 'IPAD-001' }, |
| 146 | + { key: 'udid', pinnedKey: 'serial', pinned: 'emulator-5554', requested: 'IPAD-001' }, |
| 147 | + ], |
| 148 | + [ |
| 149 | + { udid: 'IPAD-001' }, |
| 150 | + { platform: 'android' }, |
| 151 | + { key: 'platform', pinnedKey: 'udid', pinned: 'IPAD-001', requested: 'android' }, |
| 152 | + ], |
| 153 | + [ |
| 154 | + { serial: 'emulator-5554' }, |
| 155 | + { platform: 'ios' }, |
| 156 | + { key: 'platform', pinnedKey: 'serial', pinned: 'emulator-5554', requested: 'ios' }, |
| 157 | + ], |
| 158 | + [ |
| 159 | + { platform: 'linux' }, |
| 160 | + { udid: 'IPAD-001' }, |
| 161 | + { key: 'udid', pinnedKey: 'platform', pinned: 'linux', requested: 'IPAD-001' }, |
| 162 | + ], |
| 163 | + [ |
| 164 | + { platform: 'web' }, |
| 165 | + { serial: 'emulator-5554' }, |
| 166 | + { key: 'serial', pinnedKey: 'platform', pinned: 'web', requested: 'emulator-5554' }, |
| 167 | + ], |
| 168 | +] as const) { |
| 169 | + test(`a pin ${JSON.stringify(selector)} rejects incompatible selector ${JSON.stringify(input)} before transport`, async () => { |
| 170 | + const { client, requests } = recordingClient({ ...PIN, selector }); |
| 171 | + await assert.rejects( |
| 172 | + runCommand(client, 'snapshot', input), |
| 173 | + failsWithPinConflict({ message: /conflicts with the device pin/, conflicts: [conflict] }), |
| 174 | + ); |
| 175 | + assert.equal(requests.length, 0); |
| 176 | + }); |
| 177 | +} |
| 178 | + |
| 179 | +test('an incompatible selector in a batch step is rejected before any request is sent', async () => { |
| 180 | + const { client, requests } = recordingClient(PIN); |
| 181 | + await assert.rejects( |
| 182 | + runCommand(client, 'batch', { |
| 183 | + steps: [{ command: 'snapshot', input: { serial: 'emulator-5554' } }], |
| 184 | + }), |
| 185 | + failsWithPinConflict({ |
| 186 | + message: /^Batch step 1: serial/, |
| 187 | + conflicts: [ |
| 188 | + { |
| 189 | + key: 'serial', |
| 190 | + pinnedKey: 'platform', |
| 191 | + pinned: 'ios', |
| 192 | + requested: 'emulator-5554', |
| 193 | + step: 1, |
| 194 | + }, |
| 195 | + ], |
| 196 | + }), |
| 197 | + ); |
| 198 | + assert.equal(requests.length, 0); |
| 199 | +}); |
| 200 | + |
| 201 | +for (const [platform, identity] of [ |
| 202 | + ['ios', { udid: 'IPAD-001' }], |
| 203 | + ['macos', { udid: 'MAC-001' }], |
| 204 | + ['android', { serial: 'emulator-5554' }], |
| 205 | + ['harmonyos', { serial: 'HARMONY-001' }], |
| 206 | + ['vega', { serial: 'VEGA-001' }], |
| 207 | +] as const) { |
| 208 | + test(`a ${platform} pin accepts a compatible identity selector`, async () => { |
| 209 | + const { client, requests } = recordingClient({ ...PIN, selector: { platform } }); |
| 210 | + await runCommand(client, 'snapshot', identity); |
| 211 | + assert.equal(requests.length, 1); |
| 212 | + if ('udid' in identity) { |
| 213 | + assert.equal(requests[0]?.flags?.udid, identity.udid); |
| 214 | + } else { |
| 215 | + assert.equal(requests[0]?.flags?.serial, identity.serial); |
| 216 | + } |
| 217 | + }); |
| 218 | +} |
0 commit comments