|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, test, vi } from 'vitest'; |
| 5 | +import { runCli } from '../../cli.ts'; |
| 6 | +import { runCliCapture } from '../../__tests__/cli-capture.ts'; |
| 7 | +import { |
| 8 | + hashRemoteConfigFile, |
| 9 | + readRemoteConnectionState, |
| 10 | + writeRemoteConnectionState, |
| 11 | +} from '@agent-device/remote/remote-connection-state'; |
| 12 | +import { |
| 13 | + beginStdoutCapture, |
| 14 | + pluginHome, |
| 15 | + selectV2Plugin, |
| 16 | + v2RegistrationSource, |
| 17 | +} from '@agent-device/test-kit'; |
| 18 | + |
| 19 | +const provider = 'browserstack'; |
| 20 | +const packageName = '@agent-device/browserstack'; |
| 21 | +const capabilities = { |
| 22 | + leaseKind: 'direct-device-provider', |
| 23 | + requiresAppAttachment: false, |
| 24 | + requiresRemoteDaemon: false, |
| 25 | + supportsArtifacts: false, |
| 26 | + supportsDeferredAppSelection: false, |
| 27 | + supportsDirectPortReverse: false, |
| 28 | + usesCloudWebDriverLease: true, |
| 29 | +}; |
| 30 | + |
| 31 | +afterEach(() => vi.unstubAllEnvs()); |
| 32 | + |
| 33 | +function fixture(home: string, leaseProvider = provider) { |
| 34 | + const stateDir = path.join(home, 'state'); |
| 35 | + const remoteConfigPath = path.join(home, 'remote.json'); |
| 36 | + fs.writeFileSync( |
| 37 | + remoteConfigPath, |
| 38 | + JSON.stringify({ |
| 39 | + tenant: 'team', |
| 40 | + runId: 'run-1', |
| 41 | + leaseProvider, |
| 42 | + leaseBackend: 'android-instance', |
| 43 | + platform: 'android', |
| 44 | + ...(leaseProvider === 'ordinary-remote' |
| 45 | + ? { daemonBaseUrl: 'https://daemon.example.test' } |
| 46 | + : {}), |
| 47 | + }), |
| 48 | + ); |
| 49 | + return { stateDir, remoteConfigPath }; |
| 50 | +} |
| 51 | + |
| 52 | +function seedState(f: ReturnType<typeof fixture>, leaseProvider = provider) { |
| 53 | + const now = new Date().toISOString(); |
| 54 | + writeRemoteConnectionState({ |
| 55 | + stateDir: f.stateDir, |
| 56 | + state: { |
| 57 | + version: 1, |
| 58 | + session: 'default', |
| 59 | + remoteConfigPath: f.remoteConfigPath, |
| 60 | + remoteConfigHash: hashRemoteConfigFile(f.remoteConfigPath), |
| 61 | + tenant: 'team', |
| 62 | + runId: 'run-1', |
| 63 | + leaseProvider, |
| 64 | + leaseBackend: 'android-instance', |
| 65 | + platform: 'android', |
| 66 | + connectedAt: now, |
| 67 | + updatedAt: now, |
| 68 | + }, |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +test.each(['missing', 'failed'] as const)( |
| 73 | + 'routed %s vetted plugin fails before capability-based actions', |
| 74 | + async (kind) => { |
| 75 | + const { home, env } = pluginHome(); |
| 76 | + if (kind === 'failed') |
| 77 | + selectV2Plugin(home, packageName, 'throw new Error("registration failed")'); |
| 78 | + const f = fixture(home); |
| 79 | + const opts = { cwd: home, env: { ...env, AGENT_DEVICE_SESSION: 'default' } }; |
| 80 | + const expected = kind === 'missing' ? 'plugin-not-installed' : 'plugin-unavailable'; |
| 81 | + const connect = await runCliCapture( |
| 82 | + ['connect', '--remote-config', f.remoteConfigPath, '--state-dir', f.stateDir, '--json'], |
| 83 | + opts, |
| 84 | + ); |
| 85 | + seedState(f); |
| 86 | + const status = await runCliCapture( |
| 87 | + ['connection', 'status', '--state-dir', f.stateDir, '--json'], |
| 88 | + opts, |
| 89 | + ); |
| 90 | + const snapshot = await runCliCapture( |
| 91 | + ['snapshot', '--remote-config', f.remoteConfigPath, '--state-dir', f.stateDir, '--json'], |
| 92 | + opts, |
| 93 | + ); |
| 94 | + const disconnected = await runCliCapture( |
| 95 | + ['disconnect', '--state-dir', f.stateDir, '--json'], |
| 96 | + opts, |
| 97 | + ); |
| 98 | + assert.equal(disconnected.code, null); |
| 99 | + assert.equal(readRemoteConnectionState({ stateDir: f.stateDir, session: 'default' }), null); |
| 100 | + for (const result of [connect, status, snapshot]) { |
| 101 | + const parsed = JSON.parse(result.stdout); |
| 102 | + assert.equal(result.code, 1, result.stdout); |
| 103 | + assert.equal(parsed.error?.code, 'UNSUPPORTED_OPERATION', result.stdout); |
| 104 | + assert.equal(parsed.error?.details?.reason, expected, result.stdout); |
| 105 | + assert.deepEqual(result.calls, []); |
| 106 | + assert.doesNotMatch(result.stdout, /install-from-source|leaseId.*pending/); |
| 107 | + } |
| 108 | + }, |
| 109 | +); |
| 110 | + |
| 111 | +test('a generic remote profile remains connectable without a plugin', async () => { |
| 112 | + const { home, env } = pluginHome(); |
| 113 | + const f = fixture(home, 'ordinary-remote'); |
| 114 | + const result = await runCliCapture( |
| 115 | + ['connect', '--remote-config', f.remoteConfigPath, '--state-dir', f.stateDir, '--json'], |
| 116 | + { cwd: home, env: { ...env, AGENT_DEVICE_SESSION: 'default' } }, |
| 117 | + ); |
| 118 | + assert.equal(result.code, null); |
| 119 | + assert.equal( |
| 120 | + readRemoteConnectionState({ stateDir: f.stateDir, session: 'default' })?.leaseProvider, |
| 121 | + 'ordinary-remote', |
| 122 | + ); |
| 123 | +}); |
| 124 | + |
| 125 | +test('routed status uses capabilities from the supplied host store rather than the process store', async () => { |
| 126 | + const { home } = pluginHome(); |
| 127 | + const ambient = pluginHome(); |
| 128 | + vi.stubEnv('AGENT_DEVICE_HOME', ambient.home); |
| 129 | + selectV2Plugin( |
| 130 | + home, |
| 131 | + packageName, |
| 132 | + v2RegistrationSource(provider, { |
| 133 | + extra: `connection: { capabilities: ${JSON.stringify(capabilities)}, resolve() {}, verify() { return { provider: 'browserstack', service: 'BrowserStack', verificationMessage: 'verified' }; } },`, |
| 134 | + }), |
| 135 | + ); |
| 136 | + const f = fixture(home); |
| 137 | + const capture = beginStdoutCapture(); |
| 138 | + const sendToDaemon = vi.fn(async () => { |
| 139 | + throw new Error('unexpected daemon access'); |
| 140 | + }); |
| 141 | + try { |
| 142 | + const suppliedHost = { |
| 143 | + cwd: home, |
| 144 | + env: { |
| 145 | + AGENT_DEVICE_HOME: home, |
| 146 | + AGENT_DEVICE_NO_UPDATE_NOTIFIER: '1', |
| 147 | + AGENT_DEVICE_SESSION: 'default', |
| 148 | + }, |
| 149 | + }; |
| 150 | + await runCli( |
| 151 | + ['connect', '--remote-config', f.remoteConfigPath, '--state-dir', f.stateDir], |
| 152 | + { sendToDaemon }, |
| 153 | + suppliedHost, |
| 154 | + ); |
| 155 | + assert.equal( |
| 156 | + readRemoteConnectionState({ stateDir: f.stateDir, session: 'default' })?.leaseProvider, |
| 157 | + provider, |
| 158 | + ); |
| 159 | + const statusOutputStart = capture.read().length; |
| 160 | + await runCli( |
| 161 | + ['connection', 'status', '--state-dir', f.stateDir], |
| 162 | + { sendToDaemon }, |
| 163 | + suppliedHost, |
| 164 | + ); |
| 165 | + const statusOutput = capture.read().slice(statusOutputStart); |
| 166 | + assert.match(statusOutput, /The first device command shown below will allocate one/); |
| 167 | + assert.doesNotMatch(statusOutput, /install-from-source/); |
| 168 | + assert.equal(sendToDaemon.mock.calls.length, 0); |
| 169 | + } finally { |
| 170 | + capture.restore(); |
| 171 | + } |
| 172 | +}); |
0 commit comments