|
| 1 | +// The platform axis a remote connection is named on (#2962). |
| 2 | +// |
| 3 | +// A `DeviceInfo` carries the INTERNAL `apple` platform while everything a connection records or |
| 4 | +// sends — `--platform`, the connection state, the proxy device key, the lease request — speaks the |
| 5 | +// PUBLIC leaf (`ios`/`macos`, ADR 0009). Comparing the two axes by string equality refused every |
| 6 | +// iOS install and open on a proxy lease and demanded `connect --force` for a connection that had |
| 7 | +// not changed. These pin both directions: the family and leaf selectors name the same devices, and |
| 8 | +// a genuinely different platform is still refused. |
| 9 | +// |
| 10 | +// Kept out of `remote-connection.test.ts`, which is already over the test-file size tripwire and |
| 11 | +// may not grow (docs/agents/testing.md). |
| 12 | + |
| 13 | +import { afterEach, test, vi } from 'vitest'; |
| 14 | +import assert from 'node:assert/strict'; |
| 15 | +import fs from 'node:fs'; |
| 16 | +import { |
| 17 | + connectionWorkspace, |
| 18 | + createTestClient, |
| 19 | + recordedLeaseAllocate, |
| 20 | + seedConnectionState, |
| 21 | +} from './remote-connection.fixtures.ts'; |
| 22 | +import { materializeRemoteConnectionForCommand } from '../cli/commands/connection-runtime.ts'; |
| 23 | +import { AppError } from '@agent-device/kernel/errors'; |
| 24 | +import { readRemoteConnectionState } from '../remote/remote-connection-state.ts'; |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + vi.restoreAllMocks(); |
| 29 | + vi.unstubAllEnvs(); |
| 30 | +}); |
| 31 | + |
| 32 | +test('proxy install against an iOS-bound connection is not refused as a platform change', async () => { |
| 33 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 34 | + 'agent-device-connect-proxy-ios-bound-', |
| 35 | + ); |
| 36 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 37 | + seedConnectionState({ |
| 38 | + stateDir, |
| 39 | + state: { |
| 40 | + session: 'adc-proxy', |
| 41 | + remoteConfigPath, |
| 42 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 43 | + tenant: 'proxy', |
| 44 | + runId: 'proxy-client-1', |
| 45 | + leaseProvider: 'proxy', |
| 46 | + clientId: 'client-1', |
| 47 | + platform: 'ios', |
| 48 | + leaseBackend: 'ios-instance', |
| 49 | + }, |
| 50 | + }); |
| 51 | + const allocate = recordedLeaseAllocate({ leaseId: 'ios-lease-1', backend: 'ios-instance' }); |
| 52 | + |
| 53 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 54 | + command: 'install', |
| 55 | + flags: { |
| 56 | + json: true, |
| 57 | + help: false, |
| 58 | + version: false, |
| 59 | + stateDir, |
| 60 | + remoteConfig: remoteConfigPath, |
| 61 | + daemonBaseUrl: 'https://daemon.example', |
| 62 | + tenant: 'proxy', |
| 63 | + runId: 'proxy-client-1', |
| 64 | + session: 'adc-proxy', |
| 65 | + platform: 'ios', |
| 66 | + }, |
| 67 | + client: createTestClient({ |
| 68 | + listDevices: async () => [ |
| 69 | + { |
| 70 | + platform: 'ios', |
| 71 | + target: 'mobile', |
| 72 | + kind: 'simulator', |
| 73 | + id: 'SIM-001', |
| 74 | + name: 'iPhone 16', |
| 75 | + booted: true, |
| 76 | + identifiers: { udid: 'SIM-001' }, |
| 77 | + ios: { udid: 'SIM-001' }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + allocate: allocate.stub, |
| 81 | + }), |
| 82 | + }); |
| 83 | + |
| 84 | + assert.equal(materialized.flags.leaseId, 'ios-lease-1'); |
| 85 | + assert.equal(allocate.request?.deviceKey, 'ios:mobile:SIM-001'); |
| 86 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 87 | +}); |
| 88 | + |
| 89 | +// The connection records a leaf; `--platform apple` names the same devices. Comparing the two by |
| 90 | +// string equality refused the request and demanded `connect --force` for a connection that had not |
| 91 | +// changed at all (#2962). |
| 92 | +test('proxy install with the apple family selector matches an ios-bound connection', async () => { |
| 93 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 94 | + 'agent-device-connect-proxy-apple-selector-', |
| 95 | + ); |
| 96 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 97 | + seedConnectionState({ |
| 98 | + stateDir, |
| 99 | + state: { |
| 100 | + session: 'adc-proxy', |
| 101 | + remoteConfigPath, |
| 102 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 103 | + tenant: 'proxy', |
| 104 | + runId: 'proxy-client-1', |
| 105 | + leaseProvider: 'proxy', |
| 106 | + clientId: 'client-1', |
| 107 | + platform: 'ios', |
| 108 | + leaseBackend: 'ios-instance', |
| 109 | + }, |
| 110 | + }); |
| 111 | + const allocate = recordedLeaseAllocate({ leaseId: 'ios-lease-1', backend: 'ios-instance' }); |
| 112 | + |
| 113 | + await materializeRemoteConnectionForCommand({ |
| 114 | + command: 'install', |
| 115 | + flags: { |
| 116 | + json: true, |
| 117 | + help: false, |
| 118 | + version: false, |
| 119 | + stateDir, |
| 120 | + remoteConfig: remoteConfigPath, |
| 121 | + daemonBaseUrl: 'https://daemon.example', |
| 122 | + tenant: 'proxy', |
| 123 | + runId: 'proxy-client-1', |
| 124 | + session: 'adc-proxy', |
| 125 | + platform: 'apple', |
| 126 | + }, |
| 127 | + client: createTestClient({ |
| 128 | + listDevices: async () => [ |
| 129 | + { |
| 130 | + platform: 'ios', |
| 131 | + target: 'mobile', |
| 132 | + kind: 'simulator', |
| 133 | + id: 'SIM-001', |
| 134 | + name: 'iPhone 16', |
| 135 | + booted: true, |
| 136 | + identifiers: { udid: 'SIM-001' }, |
| 137 | + ios: { udid: 'SIM-001' }, |
| 138 | + }, |
| 139 | + ], |
| 140 | + allocate: allocate.stub, |
| 141 | + }), |
| 142 | + }); |
| 143 | + |
| 144 | + assert.equal(allocate.request?.deviceKey, 'ios:mobile:SIM-001'); |
| 145 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 146 | +}); |
| 147 | + |
| 148 | +// The comparison itself, without a device resolution in the way: the connection bound `ios` from |
| 149 | +// `connect`, and `--platform apple` names those same devices. String inequality refused the command |
| 150 | +// and told the user to reconnect (#2962), while `apple` versus a non-Apple platform must still |
| 151 | +// refuse. |
| 152 | +test('remote command with the apple family selector matches an ios-bound connection', async () => { |
| 153 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 154 | + 'agent-device-connect-apple-selector-scope-', |
| 155 | + ); |
| 156 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 157 | + seedConnectionState({ |
| 158 | + stateDir, |
| 159 | + state: { |
| 160 | + session: 'adc-apple', |
| 161 | + remoteConfigPath, |
| 162 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 163 | + tenant: 'acme', |
| 164 | + runId: 'run-123', |
| 165 | + leaseId: 'apple-lease-1', |
| 166 | + leaseBackend: 'ios-instance', |
| 167 | + platform: 'ios', |
| 168 | + }, |
| 169 | + }); |
| 170 | + const heartbeats: string[] = []; |
| 171 | + |
| 172 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 173 | + command: 'snapshot', |
| 174 | + flags: { |
| 175 | + json: true, |
| 176 | + help: false, |
| 177 | + version: false, |
| 178 | + stateDir, |
| 179 | + remoteConfig: remoteConfigPath, |
| 180 | + daemonBaseUrl: 'https://daemon.example', |
| 181 | + tenant: 'acme', |
| 182 | + runId: 'run-123', |
| 183 | + session: 'adc-apple', |
| 184 | + platform: 'apple', |
| 185 | + }, |
| 186 | + client: createTestClient({ |
| 187 | + heartbeat: async (request) => { |
| 188 | + heartbeats.push(request.leaseId); |
| 189 | + return { |
| 190 | + leaseId: request.leaseId, |
| 191 | + tenantId: 'acme', |
| 192 | + runId: 'run-123', |
| 193 | + backend: 'ios-instance', |
| 194 | + }; |
| 195 | + }, |
| 196 | + }), |
| 197 | + }); |
| 198 | + |
| 199 | + assert.deepEqual(heartbeats, ['apple-lease-1']); |
| 200 | + assert.equal(materialized.flags.leaseId, 'apple-lease-1'); |
| 201 | + assert.equal(materialized.flags.platform, 'ios'); |
| 202 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 203 | +}); |
| 204 | + |
| 205 | +test('proxy install against a differently-bound platform is still refused', async () => { |
| 206 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 207 | + 'agent-device-connect-proxy-platform-conflict-', |
| 208 | + ); |
| 209 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 210 | + seedConnectionState({ |
| 211 | + stateDir, |
| 212 | + state: { |
| 213 | + session: 'adc-proxy', |
| 214 | + remoteConfigPath, |
| 215 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 216 | + tenant: 'proxy', |
| 217 | + runId: 'proxy-client-1', |
| 218 | + leaseProvider: 'proxy', |
| 219 | + clientId: 'client-1', |
| 220 | + platform: 'ios', |
| 221 | + leaseBackend: 'ios-instance', |
| 222 | + }, |
| 223 | + }); |
| 224 | + |
| 225 | + await assert.rejects( |
| 226 | + async () => |
| 227 | + await materializeRemoteConnectionForCommand({ |
| 228 | + command: 'install', |
| 229 | + flags: { |
| 230 | + json: true, |
| 231 | + help: false, |
| 232 | + version: false, |
| 233 | + stateDir, |
| 234 | + remoteConfig: remoteConfigPath, |
| 235 | + daemonBaseUrl: 'https://daemon.example', |
| 236 | + tenant: 'proxy', |
| 237 | + runId: 'proxy-client-1', |
| 238 | + session: 'adc-proxy', |
| 239 | + platform: 'android', |
| 240 | + }, |
| 241 | + client: createTestClient(), |
| 242 | + }), |
| 243 | + (error: unknown) => |
| 244 | + error instanceof AppError && |
| 245 | + error.code === 'INVALID_ARGS' && |
| 246 | + /different platform/.test(error.message), |
| 247 | + ); |
| 248 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 249 | +}); |
| 250 | + |
| 251 | +// The resolved device's internal platform is the collapsed `apple`, while the lease request, the |
| 252 | +// connection state, and `--platform` all speak the public leaf `ios`. Writing the internal value |
| 253 | +// into the connection state is what #2962 reported: it refused the next command of the same |
| 254 | +// session, whose bound platform was read back out of that state. |
| 255 | +test('proxy install records the public platform and the next command reuses that scope', async () => { |
| 256 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 257 | + 'agent-device-connect-proxy-ios-install-', |
| 258 | + ); |
| 259 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 260 | + seedConnectionState({ |
| 261 | + stateDir, |
| 262 | + state: { |
| 263 | + session: 'adc-proxy', |
| 264 | + remoteConfigPath, |
| 265 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 266 | + tenant: 'proxy', |
| 267 | + runId: 'proxy-client-1', |
| 268 | + leaseProvider: 'proxy', |
| 269 | + clientId: 'client-1', |
| 270 | + }, |
| 271 | + }); |
| 272 | + const allocate = recordedLeaseAllocate({ leaseId: 'ios-lease-1', backend: 'ios-instance' }); |
| 273 | + const client = createTestClient({ |
| 274 | + listDevices: async () => [ |
| 275 | + { |
| 276 | + platform: 'ios', |
| 277 | + target: 'mobile', |
| 278 | + kind: 'simulator', |
| 279 | + id: 'SIM-001', |
| 280 | + name: 'iPhone 16', |
| 281 | + booted: true, |
| 282 | + identifiers: { udid: 'SIM-001' }, |
| 283 | + ios: { udid: 'SIM-001' }, |
| 284 | + }, |
| 285 | + ], |
| 286 | + allocate: allocate.stub, |
| 287 | + }); |
| 288 | + const install = () => |
| 289 | + materializeRemoteConnectionForCommand({ |
| 290 | + command: 'install', |
| 291 | + flags: { |
| 292 | + json: true, |
| 293 | + help: false, |
| 294 | + version: false, |
| 295 | + stateDir, |
| 296 | + remoteConfig: remoteConfigPath, |
| 297 | + daemonBaseUrl: 'https://daemon.example', |
| 298 | + tenant: 'proxy', |
| 299 | + runId: 'proxy-client-1', |
| 300 | + session: 'adc-proxy', |
| 301 | + platform: 'ios', |
| 302 | + }, |
| 303 | + client, |
| 304 | + }); |
| 305 | + |
| 306 | + const materialized = await install(); |
| 307 | + assert.equal(materialized.flags.leaseId, 'ios-lease-1'); |
| 308 | + assert.equal(materialized.flags.platform, 'ios'); |
| 309 | + assert.equal(materialized.flags.udid, 'SIM-001'); |
| 310 | + assert.equal(allocate.request?.platform, 'ios'); |
| 311 | + assert.equal(allocate.request?.deviceKey, 'ios:mobile:SIM-001'); |
| 312 | + |
| 313 | + const state = readRemoteConnectionState({ stateDir, session: 'adc-proxy' }); |
| 314 | + assert.equal(state?.platform, 'ios'); |
| 315 | + assert.equal(state?.deviceKey, 'ios:mobile:SIM-001'); |
| 316 | + |
| 317 | + // The second command reads its bound platform back out of that state. |
| 318 | + const reused = await install(); |
| 319 | + assert.equal(reused.flags.platform, 'ios'); |
| 320 | + assert.equal(reused.flags.leaseId, 'ios-lease-1'); |
| 321 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 322 | +}); |
0 commit comments