|
| 1 | +import { test, vi } from 'vitest'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import { AppError } from '@agent-device/kernel/errors'; |
| 5 | +import { |
| 6 | + connectionWorkspace, |
| 7 | + createTestClient, |
| 8 | + type LeaseAllocateRequest, |
| 9 | + seedConnectionState, |
| 10 | +} from './remote-connection.fixtures.ts'; |
| 11 | + |
| 12 | +vi.mock('../metro/client-metro-companion.ts', () => ({ |
| 13 | + stopMetroCompanion: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock('../client/client-react-devtools-companion.ts', () => ({ |
| 17 | + stopReactDevtoolsCompanion: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +import { materializeRemoteConnectionForCommand } from '../cli/commands/connection-runtime.ts'; |
| 21 | +import { readRemoteConnectionState } from '../remote/remote-connection-state.ts'; |
| 22 | + |
| 23 | +const DAEMON_LEASE_ID = 'abc123abc123abc1'; |
| 24 | + |
| 25 | +function recordingAllocate(requests: LeaseAllocateRequest[]) { |
| 26 | + return async (request: LeaseAllocateRequest) => { |
| 27 | + requests.push(request); |
| 28 | + // The daemon's own id, as an older daemon that ignores the requested one answers. |
| 29 | + return { |
| 30 | + leaseId: DAEMON_LEASE_ID, |
| 31 | + tenantId: request.tenant, |
| 32 | + runId: request.runId, |
| 33 | + backend: request.leaseBackend ?? 'android-instance', |
| 34 | + leaseProvider: request.leaseProvider, |
| 35 | + clientId: request.clientId, |
| 36 | + deviceKey: request.deviceKey, |
| 37 | + }; |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +test('proxy allocation sends a fresh lease id on every attempt and keeps the id the daemon returns', async () => { |
| 42 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 43 | + 'agent-device-proxy-lease-id-', |
| 44 | + ); |
| 45 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 46 | + seedConnectionState({ |
| 47 | + stateDir, |
| 48 | + state: { |
| 49 | + session: 'adc-proxy', |
| 50 | + remoteConfigPath, |
| 51 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 52 | + tenant: 'proxy', |
| 53 | + runId: 'proxy-client-1', |
| 54 | + leaseProvider: 'proxy', |
| 55 | + clientId: 'client-1', |
| 56 | + }, |
| 57 | + }); |
| 58 | + const requests: LeaseAllocateRequest[] = []; |
| 59 | + const client = createTestClient({ |
| 60 | + allocate: recordingAllocate(requests), |
| 61 | + heartbeat: async () => { |
| 62 | + throw new AppError('UNAUTHORIZED', 'Lease expired', { reason: 'LEASE_EXPIRED' }); |
| 63 | + }, |
| 64 | + }); |
| 65 | + const flags = { |
| 66 | + json: true, |
| 67 | + help: false, |
| 68 | + version: false, |
| 69 | + stateDir, |
| 70 | + remoteConfig: remoteConfigPath, |
| 71 | + daemonBaseUrl: 'https://daemon.example', |
| 72 | + tenant: 'proxy', |
| 73 | + runId: 'proxy-client-1', |
| 74 | + session: 'adc-proxy', |
| 75 | + platform: 'android', |
| 76 | + } as const; |
| 77 | + |
| 78 | + const first = await materializeRemoteConnectionForCommand({ |
| 79 | + command: 'install', |
| 80 | + flags: { ...flags }, |
| 81 | + client, |
| 82 | + }); |
| 83 | + const second = await materializeRemoteConnectionForCommand({ |
| 84 | + command: 'install', |
| 85 | + flags: { ...flags }, |
| 86 | + client, |
| 87 | + }); |
| 88 | + |
| 89 | + assert.equal(requests.length, 2); |
| 90 | + assert.match(requests[0]?.leaseId ?? '', /^[0-9a-f]{32}$/); |
| 91 | + assert.match(requests[1]?.leaseId ?? '', /^[0-9a-f]{32}$/); |
| 92 | + assert.notEqual(requests[0]?.leaseId, requests[1]?.leaseId); |
| 93 | + assert.equal(first.flags.leaseId, DAEMON_LEASE_ID); |
| 94 | + assert.equal(second.flags.leaseId, DAEMON_LEASE_ID); |
| 95 | + assert.equal( |
| 96 | + readRemoteConnectionState({ stateDir, session: 'adc-proxy' })?.leaseId, |
| 97 | + DAEMON_LEASE_ID, |
| 98 | + ); |
| 99 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 100 | +}); |
| 101 | + |
| 102 | +test('a non-proxy remote connection leaves the lease id to the daemon', async () => { |
| 103 | + const { tempRoot, stateDir, remoteConfigPath } = connectionWorkspace( |
| 104 | + 'agent-device-cloud-lease-id-', |
| 105 | + ); |
| 106 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({})); |
| 107 | + seedConnectionState({ |
| 108 | + stateDir, |
| 109 | + state: { |
| 110 | + session: 'adc-cloud', |
| 111 | + remoteConfigPath, |
| 112 | + tenant: 'acme', |
| 113 | + runId: 'run-123', |
| 114 | + leaseProvider: 'aws-device-farm', |
| 115 | + platform: 'ios', |
| 116 | + }, |
| 117 | + }); |
| 118 | + const requests: LeaseAllocateRequest[] = []; |
| 119 | + |
| 120 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 121 | + command: 'screenshot', |
| 122 | + flags: { |
| 123 | + json: true, |
| 124 | + help: false, |
| 125 | + version: false, |
| 126 | + stateDir, |
| 127 | + remoteConfig: remoteConfigPath, |
| 128 | + tenant: 'acme', |
| 129 | + runId: 'run-123', |
| 130 | + session: 'adc-cloud', |
| 131 | + platform: 'ios', |
| 132 | + }, |
| 133 | + client: createTestClient({ allocate: recordingAllocate(requests) }), |
| 134 | + }); |
| 135 | + |
| 136 | + assert.equal(requests.length, 1); |
| 137 | + assert.equal(requests[0]?.leaseId, undefined); |
| 138 | + assert.equal(materialized.flags.leaseId, DAEMON_LEASE_ID); |
| 139 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 140 | +}); |
0 commit comments