|
| 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 | +// #2161: the client enforces the workspace device pin on the finished request, so every surface |
| 9 | +// (CLI argv, CLI batch, MCP flat and nested input, remote config) is held to it in one place. |
| 10 | + |
| 11 | +const PIN: DevicePin = { |
| 12 | + selector: { platform: 'ios', device: 'iPad Pro' }, |
| 13 | + policySource: { kind: 'project', label: 'project config file /repo/agent-device.json' }, |
| 14 | +}; |
| 15 | + |
| 16 | +function recordingClient(devicePin: DevicePin | undefined) { |
| 17 | + const requests: Array<Parameters<AgentDeviceDaemonTransport>[0]> = []; |
| 18 | + const transport: AgentDeviceDaemonTransport = async (request) => { |
| 19 | + requests.push(request); |
| 20 | + return { ok: true, data: { results: [], executed: 0, total: 0 } }; |
| 21 | + }; |
| 22 | + return { requests, client: createAgentDeviceClient({}, { transport, devicePin }) }; |
| 23 | +} |
| 24 | + |
| 25 | +function failsWithPinConflict(expected: { message: RegExp; conflicts: unknown[] }) { |
| 26 | + return (error: unknown): boolean => { |
| 27 | + assert.ok(error instanceof Error && 'code' in error && 'details' in error); |
| 28 | + assert.equal(error.code, 'INVALID_ARGS'); |
| 29 | + assert.match(error.message, expected.message); |
| 30 | + const details = error.details as Record<string, unknown>; |
| 31 | + assert.equal(details.reason, 'device_pin_conflict'); |
| 32 | + assert.equal(details.pinSource, 'project config file /repo/agent-device.json'); |
| 33 | + assert.deepEqual(details.conflicts, expected.conflicts); |
| 34 | + return true; |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +test('a batch step naming another device fails before the request leaves the client', async () => { |
| 39 | + const { client, requests } = recordingClient(PIN); |
| 40 | + |
| 41 | + await assert.rejects( |
| 42 | + runCommand(client, 'batch', { |
| 43 | + steps: [ |
| 44 | + { command: 'snapshot', input: {} }, |
| 45 | + { command: 'open', input: { app: 'com.example.app', device: 'iPhone 16' } }, |
| 46 | + ], |
| 47 | + }), |
| 48 | + failsWithPinConflict({ |
| 49 | + message: |
| 50 | + /^Batch step 2: device "iPhone 16" conflicts with the device pin from project config file/, |
| 51 | + conflicts: [{ key: 'device', pinned: 'iPad Pro', requested: 'iPhone 16', step: 2 }], |
| 52 | + }), |
| 53 | + ); |
| 54 | + assert.equal(requests.length, 0); |
| 55 | +}); |
| 56 | + |
| 57 | +test('a top-level selector from any layer that contradicts the pin fails', async () => { |
| 58 | + const { client, requests } = recordingClient(PIN); |
| 59 | + |
| 60 | + await assert.rejects( |
| 61 | + runCommand(client, 'snapshot', { platform: 'android' }), |
| 62 | + failsWithPinConflict({ |
| 63 | + message: /^platform "android" conflicts with the device pin/, |
| 64 | + conflicts: [{ key: 'platform', pinned: 'ios', requested: 'android' }], |
| 65 | + }), |
| 66 | + ); |
| 67 | + assert.equal(requests.length, 0); |
| 68 | +}); |
| 69 | + |
| 70 | +test('a batch whose steps repeat, refine, or omit the pin is sent', async () => { |
| 71 | + const { client, requests } = recordingClient(PIN); |
| 72 | + |
| 73 | + await runCommand(client, 'batch', { |
| 74 | + platform: 'ios', |
| 75 | + device: 'iPad Pro', |
| 76 | + steps: [ |
| 77 | + { command: 'snapshot', input: {} }, |
| 78 | + { command: 'open', input: { app: 'com.example.app', device: 'iPad Pro' } }, |
| 79 | + // A UDID picks one device among several that share the pinned name. |
| 80 | + { command: 'snapshot', input: { udid: 'ABCDEF' } }, |
| 81 | + ], |
| 82 | + }); |
| 83 | + |
| 84 | + assert.equal(requests.length, 1); |
| 85 | + assert.equal((requests[0]?.flags as CommandFlags | undefined)?.batchSteps?.length, 3); |
| 86 | +}); |
| 87 | + |
| 88 | +test('without a pin the client sends a batch step that names another device', async () => { |
| 89 | + const { client, requests } = recordingClient(undefined); |
| 90 | + |
| 91 | + await runCommand(client, 'batch', { |
| 92 | + steps: [{ command: 'open', input: { app: 'com.example.app', device: 'iPhone 16' } }], |
| 93 | + }); |
| 94 | + |
| 95 | + const steps = (requests[0]?.flags as CommandFlags | undefined)?.batchSteps; |
| 96 | + assert.equal(steps?.[0]?.flags?.device, 'iPhone 16'); |
| 97 | +}); |
0 commit comments