Skip to content

Commit eef3345

Browse files
thymikeeclaude
andcommitted
refactor(host): pass the run's host to CLI command handlers and fill the lease cwd from the host
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent ddafe9e commit eef3345

32 files changed

Lines changed: 311 additions & 115 deletions

‎src/__tests__/cli-client-commands.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
import type { SettingsUpdateOptions } from '@agent-device/contracts/client';
1616
import { AppError } from '@agent-device/kernel/errors';
1717
import { resolveCliOptions } from '../cli/resolve-cli-options.ts';
18-
import { mkdtempForTestSync } from '@agent-device/test-kit';
18+
import { mkdtempForTestSync, createTestHostEnvironment } from '@agent-device/test-kit';
1919
import { createStubClientLeases } from './test-utils/client-lease-fixtures.ts';
2020
import { withCapturedOutput, withCapturedStdoutAsync } from './test-utils/stream-capture.ts';
2121

@@ -42,6 +42,7 @@ test('install-from-source forwards URL and repeated headers to client.apps.insta
4242
});
4343

4444
const handled = await tryRunClientBackedCommand({
45+
host: createTestHostEnvironment(),
4546
command: 'install-from-source',
4647
positionals: ['https://example.com/app.apk'],
4748
flags: {
@@ -84,6 +85,7 @@ test('install-from-source forwards GitHub Actions artifact flag to client.apps.i
8485
});
8586

8687
const handled = await tryRunClientBackedCommand({
88+
host: createTestHostEnvironment(),
8789
command: 'install-from-source',
8890
positionals: [],
8991
flags: {
@@ -120,6 +122,7 @@ test('install-from-source preserves colons in GitHub Actions artifact names', as
120122
});
121123

122124
await tryRunClientBackedCommand({
125+
host: createTestHostEnvironment(),
123126
command: 'install-from-source',
124127
positionals: [],
125128
flags: {
@@ -153,6 +156,7 @@ test('install-from-source forwards configured GitHub Actions artifact name to cl
153156
});
154157

155158
const handled = await tryRunClientBackedCommand({
159+
host: createTestHostEnvironment(),
156160
command: 'install-from-source',
157161
positionals: [],
158162
flags: {
@@ -189,6 +193,7 @@ test('install-from-source rejects malformed header syntax', async () => {
189193
await assert.rejects(
190194
() =>
191195
tryRunClientBackedCommand({
196+
host: createTestHostEnvironment(),
192197
command: 'install-from-source',
193198
positionals: ['https://example.com/app.apk'],
194199
flags: {
@@ -216,6 +221,7 @@ test('install-from-source rejects headers with GitHub Actions artifact sources',
216221
await assert.rejects(
217222
() =>
218223
tryRunClientBackedCommand({
224+
host: createTestHostEnvironment(),
219225
command: 'install-from-source',
220226
positionals: [],
221227
flags: {
@@ -268,6 +274,7 @@ test('metro prepare forwards normalized options to client.metro.prepare', async
268274

269275
const stdout = await withCapturedStdoutAsync(async () => {
270276
const handled = await tryRunClientBackedCommand({
277+
host: createTestHostEnvironment(),
271278
command: 'metro',
272279
positionals: ['prepare'],
273280
flags: {
@@ -330,6 +337,7 @@ test('metro prepare rejects when no public or proxy base URL is provided', async
330337
await assert.rejects(
331338
() =>
332339
tryRunClientBackedCommand({
340+
host: createTestHostEnvironment(),
333341
command: 'metro',
334342
positionals: ['prepare'],
335343
flags: {
@@ -366,6 +374,7 @@ test('metro reload forwards host, port, bundle URL, and timeout to client.metro.
366374

367375
const stdout = await withCapturedStdoutAsync(async () => {
368376
const handled = await tryRunClientBackedCommand({
377+
host: createTestHostEnvironment(),
369378
command: 'metro',
370379
positionals: ['reload'],
371380
flags: {
@@ -420,6 +429,7 @@ test('events prints a parsed session timeline from the generic CLI route', async
420429

421430
const output = await withCapturedOutput(async () => {
422431
const handled = await tryRunClientBackedCommand({
432+
host: createTestHostEnvironment(),
423433
command: 'events',
424434
positionals: [],
425435
flags: {
@@ -461,6 +471,7 @@ test('screenshot forwards --overlay-refs to the client capture API', async () =>
461471
});
462472

463473
const handled = await tryRunClientBackedCommand({
474+
host: createTestHostEnvironment(),
464475
command: 'screenshot',
465476
positionals: ['/tmp/screenshot.png'],
466477
flags: {
@@ -510,6 +521,7 @@ test('diff screenshot forwards --surface to live client screenshot capture', asy
510521

511522
await withCapturedStdoutAsync(async () => {
512523
const handled = await tryRunClientBackedCommand({
524+
host: createTestHostEnvironment(),
513525
command: 'diff',
514526
positionals: ['screenshot'],
515527
flags: {
@@ -554,6 +566,7 @@ test('open forwards macOS surface to the client apps API', async () => {
554566
});
555567

556568
const handled = await tryRunClientBackedCommand({
569+
host: createTestHostEnvironment(),
557570
command: 'open',
558571
positionals: ['MenuBarApp'],
559572
flags: {
@@ -589,6 +602,7 @@ test('open forwards launch console option to the client apps API', async () => {
589602
});
590603

591604
const handled = await tryRunClientBackedCommand({
605+
host: createTestHostEnvironment(),
592606
command: 'open',
593607
positionals: ['Agent Device Tester'],
594608
flags: {
@@ -622,6 +636,7 @@ test('apps command defaults to user-installed and prints discovery hint', async
622636

623637
const output = await withCapturedOutput(async () => {
624638
const handled = await tryRunClientBackedCommand({
639+
host: createTestHostEnvironment(),
625640
command: 'apps',
626641
positionals: [],
627642
flags: {
@@ -669,6 +684,7 @@ test('screenshot reports annotated ref count in non-json mode', async () => {
669684

670685
const stdout = await withCapturedStdoutAsync(async () => {
671686
const handled = await tryRunClientBackedCommand({
687+
host: createTestHostEnvironment(),
672688
command: 'screenshot',
673689
positionals: ['/tmp/screenshot.png'],
674690
flags: {
@@ -703,6 +719,7 @@ click text="Continue"
703719

704720
const stdout = await withCapturedStdoutAsync(async () => {
705721
const handled = await tryRunClientBackedCommand({
722+
host: createTestHostEnvironment(),
706723
command: 'replay',
707724
positionals: ['export', sourcePath],
708725
flags: {
@@ -784,6 +801,7 @@ test('replay rejects extra plain replay paths before daemon dispatch', async ()
784801
await assert.rejects(
785802
async () =>
786803
await tryRunClientBackedCommand({
804+
host: createTestHostEnvironment(),
787805
command: 'replay',
788806
positionals: ['one.ad', 'two.ad'],
789807
flags: {
@@ -814,6 +832,7 @@ test('wait keeps CLI bare text behavior through the typed client command API', a
814832
};
815833

816834
const handled = await tryRunClientBackedCommand({
835+
host: createTestHostEnvironment(),
817836
command: 'wait',
818837
positionals: ['Continue', '1500'],
819838
flags: {
@@ -848,6 +867,7 @@ test('keyboard status prints Android input ownership in human output', async ()
848867

849868
const stdout = await withCapturedStdoutAsync(async () => {
850869
const handled = await tryRunClientBackedCommand({
870+
host: createTestHostEnvironment(),
851871
command: 'keyboard',
852872
positionals: ['status'],
853873
flags: {
@@ -880,6 +900,7 @@ test('metro prepare wraps output in the standard success envelope for --json', a
880900

881901
const stdout = await withCapturedStdoutAsync(async () => {
882902
const handled = await tryRunClientBackedCommand({
903+
host: createTestHostEnvironment(),
883904
command: 'metro',
884905
positionals: ['prepare'],
885906
flags: {
@@ -953,6 +974,7 @@ test('metro prepare with --remote-config loads profile defaults', async () => {
953974

954975
const stdout = await withCapturedStdoutAsync(async () => {
955976
const handled = await tryRunClientBackedCommand({
977+
host: createTestHostEnvironment(),
956978
command: 'metro',
957979
positionals: ['prepare'],
958980
flags: parsed.flags,
@@ -993,6 +1015,7 @@ test('install prints command-owned success output in human mode', async () => {
9931015

9941016
const stdout = await withCapturedStdoutAsync(async () => {
9951017
const handled = await tryRunClientBackedCommand({
1018+
host: createTestHostEnvironment(),
9961019
command: 'install',
9971020
positionals: ['Demo', '/tmp/Demo.app'],
9981021
flags: {
@@ -1023,6 +1046,7 @@ test('settings location set forwards coordinates to client settings update', asy
10231046
});
10241047

10251048
const handled = await tryRunClientBackedCommand({
1049+
host: createTestHostEnvironment(),
10261050
command: 'settings',
10271051
positionals: ['location', 'set', '37.3349', '-122.009'],
10281052
flags: {
@@ -1064,6 +1088,7 @@ test('settings location set rejects invalid coordinates before client call', asy
10641088
await assert.rejects(
10651089
() =>
10661090
tryRunClientBackedCommand({
1091+
host: createTestHostEnvironment(),
10671092
command: 'settings',
10681093
positionals,
10691094
flags: {

‎src/__tests__/cli-install-from-source.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createTestHostEnvironment } from '@agent-device/test-kit';
12
import assert from 'node:assert/strict';
23
import { test } from 'vitest';
34
import { AppError } from '@agent-device/kernel/errors';
@@ -15,6 +16,7 @@ async function runInstallFromSource(source: string) {
1516
},
1617
} as unknown as AgentDeviceClient;
1718
return await tryRunClientBackedCommand({
19+
host: createTestHostEnvironment(),
1820
command: 'install-from-source',
1921
positionals: [source],
2022
flags: { json: false, help: false, version: false },

‎src/__tests__/cloud-connect-auth.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { beginStdoutCapture } from './test-utils/stream-capture.ts';
77
import { resolveCloudAccessForConnect } from '../cli/auth-session.ts';
88
import { readActiveConnectionState } from '@agent-device/remote/remote-connection-state';
99
import type { AgentDeviceClient } from '../agent-device-client.ts';
10-
import { mkdtempForTestSync, bindTestCommandHost } from '@agent-device/test-kit';
10+
import {
11+
mkdtempForTestSync,
12+
bindTestCommandHost,
13+
createTestHostEnvironment,
14+
} from '@agent-device/test-kit';
1115

1216
bindTestCommandHost();
1317

@@ -86,6 +90,7 @@ async function connectWithNoLogin(stateDir: string): Promise<void> {
8690
const stdoutCapture = beginStdoutCapture();
8791
try {
8892
await connectCommand({
93+
host: createTestHostEnvironment(),
8994
positionals: [],
9095
flags: {
9196
json: true,

‎src/__tests__/cloud-connect-profile.test.ts‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { withCommandHost } from '@agent-device/host-kit/command';
21
import { afterEach, beforeEach, test, vi } from 'vitest';
32
import assert from 'node:assert/strict';
43
import fs from 'node:fs';
@@ -21,7 +20,11 @@ import {
2120
verifyAwsDeviceFarmConnection,
2221
verifyBrowserStackConnection,
2322
} from '@agent-device/provider-webdriver/connection-verification';
24-
import { mkdtempForTestSync, bindTestCommandHost } from '@agent-device/test-kit';
23+
import {
24+
mkdtempForTestSync,
25+
bindTestCommandHost,
26+
createTestHostEnvironment,
27+
} from '@agent-device/test-kit';
2528
import { connectWithGeneratedProviderProfile } from './test-utils/connect-command.ts';
2629

2730
bindTestCommandHost();
@@ -164,6 +167,7 @@ test('connect limrun generates a local daemon remote profile', async () => {
164167
try {
165168
await captureConnectStdout(async () => {
166169
await connectCommand({
170+
host: createTestHostEnvironment(),
167171
positionals: ['limrun'],
168172
flags: {
169173
json: true,
@@ -217,6 +221,7 @@ test('connect limrun attaches to an existing instance without storing its token'
217221

218222
await captureConnectStdout(async () => {
219223
await connectCommand({
224+
host: createTestHostEnvironment(),
220225
positionals: ['limrun'],
221226
flags: { json: true, help: false, version: false, stateDir, platform: 'ios' },
222227
client: {} as AgentDeviceClient,
@@ -237,6 +242,7 @@ test('connect limrun persists deferred Metro bridge settings', async () => {
237242
try {
238243
await captureConnectStdout(async () => {
239244
await connectCommand({
245+
host: createTestHostEnvironment(),
240246
positionals: ['limrun'],
241247
flags: {
242248
json: true,
@@ -295,6 +301,7 @@ test('connect limrun requires LIMRUN_API_KEY or access to an existing instance',
295301
try {
296302
await assert.rejects(
297303
connectCommand({
304+
host: createTestHostEnvironment(),
298305
positionals: ['limrun'],
299306
flags: {
300307
json: true,
@@ -554,20 +561,17 @@ test('connect browserstack persists a local app artifact as an absolute path', a
554561

555562
try {
556563
// The host the command runs in decides what `./sample.apk` is relative to.
557-
await withCommandHost(
558-
{ host: { env: process.env, cwd: fs.realpathSync(tempRoot) }, requestEnv: undefined },
559-
async () =>
560-
await connectWithGeneratedProviderProfile({
561-
stateDir,
562-
positionals: ['browserstack'],
563-
flags: {
564-
platform: 'android',
565-
device: 'Google Pixel 8',
566-
providerOsVersion: '14.0',
567-
providerApp: './sample.apk',
568-
},
569-
}),
570-
);
564+
await connectWithGeneratedProviderProfile({
565+
stateDir,
566+
host: { env: process.env, cwd: fs.realpathSync(tempRoot) },
567+
positionals: ['browserstack'],
568+
flags: {
569+
platform: 'android',
570+
device: 'Google Pixel 8',
571+
providerOsVersion: '14.0',
572+
providerApp: './sample.apk',
573+
},
574+
});
571575

572576
const state = readRequiredActiveState(stateDir);
573577
assert.equal(readGeneratedConfig(state.remoteConfigPath).providerApp, fs.realpathSync(appPath));
@@ -795,6 +799,7 @@ test('connect does not activate provider state when verification fails', async (
795799
try {
796800
await assert.rejects(
797801
connectCommand({
802+
host: createTestHostEnvironment(),
798803
positionals: ['limrun'],
799804
flags: {
800805
json: true,
@@ -903,6 +908,7 @@ function fetchProfileUrl(fetchMock: ReturnType<typeof vi.fn>): string | undefine
903908
async function connectWithGeneratedCloudProfile(stateDir: string): Promise<void> {
904909
await captureConnectStdout(async () => {
905910
await connectCommand({
911+
host: createTestHostEnvironment(),
906912
positionals: [],
907913
flags: {
908914
json: true,

‎src/__tests__/remote-connection-harmonyos.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bindTestCommandHost } from '@agent-device/test-kit';
1+
import { bindTestCommandHost, createTestHostEnvironment } from '@agent-device/test-kit';
22
import { expect, test } from 'vitest';
33
import fs from 'node:fs';
44
import {
@@ -45,6 +45,7 @@ test('stored Harmony runtime compatibility keeps the HarmonyOS runtime for a Har
4545
},
4646
});
4747
const materialized = await materializeRemoteConnectionForCommand({
48+
host: createTestHostEnvironment(),
4849
command: 'snapshot',
4950
client: createTestClient(),
5051
flags: {
@@ -73,6 +74,7 @@ async function expectRejectedSelection(
7374
): Promise<void> {
7475
await expect(
7576
materializeRemoteConnectionForCommand({
77+
host: createTestHostEnvironment(),
7678
command: 'snapshot',
7779
client: createTestClient(),
7880
flags: {
@@ -176,6 +178,7 @@ test('proxy HarmonyOS inventory materializes a scoped lease and closes that same
176178
session: 'harmony-proxy',
177179
};
178180
const materialized = await materializeRemoteConnectionForCommand({
181+
host: createTestHostEnvironment(),
179182
command: 'open',
180183
flags,
181184
client,
@@ -189,7 +192,7 @@ test('proxy HarmonyOS inventory materializes a scoped lease and closes that same
189192
deviceKey: 'harmonyos:mobile:127.0.0.1:16001',
190193
});
191194
expect(readRemoteConnectionState({ stateDir, session: flags.session })?.leaseId).toBe(leaseId);
192-
await disconnectCommand({ positionals: [], flags, client });
195+
await disconnectCommand({ host: createTestHostEnvironment(), positionals: [], flags, client });
193196
expect(released).toEqual([leaseId]);
194197
expect(readRemoteConnectionState({ stateDir, session: flags.session })).toBeNull();
195198
});

0 commit comments

Comments
 (0)