-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathcli-agent-cdp-session.test.ts
More file actions
98 lines (90 loc) · 3.17 KB
/
Copy pathcli-agent-cdp-session.test.ts
File metadata and controls
98 lines (90 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, test, vi } from 'vitest';
import assert from 'node:assert/strict';
vi.mock('../cli/commands/agent-cdp.ts', async (importOriginal) => {
const actual = await importOriginal<typeof import('../cli/commands/agent-cdp.ts')>();
return {
...actual,
runAgentCdpCommand: vi.fn(async () => 0),
};
});
import { runCli } from '../cli.ts';
import { runAgentCdpCommand } from '../cli/commands/agent-cdp.ts';
import { installIsolatedCliTestEnv } from './cli-test-env.ts';
import { hashRemoteConfigFile, writeRemoteConnectionState } from '../remote-connection-state.ts';
import type { DaemonResponse } from '../daemon-client.ts';
afterEach(() => {
vi.clearAllMocks();
});
test('cdp receives active remote connection session and runtime after defaults are merged', async () => {
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-agent-cdp-session-'));
const stateDir = path.join(tempRoot, 'state');
const remoteConfigPath = path.join(tempRoot, 'remote.json');
fs.writeFileSync(
remoteConfigPath,
JSON.stringify({
daemonBaseUrl: 'https://daemon.example.test',
platform: 'android',
metroProxyBaseUrl: 'https://bridge.example.test',
metroBearerToken: 'token',
}),
);
const runtime = {
platform: 'android' as const,
bundleUrl: 'https://bridge.example.test/api/metro/runtimes/runtime-1/index.bundle',
};
writeRemoteConnectionState({
stateDir,
state: {
version: 1,
session: 'adc-android',
remoteConfigPath,
remoteConfigHash: hashRemoteConfigFile(remoteConfigPath),
daemon: { baseUrl: 'https://daemon.example.test', transport: 'http' },
tenant: 'tenant-1',
runId: 'run-1',
leaseId: 'lease-1',
leaseBackend: 'android-instance',
platform: 'android',
runtime,
connectedAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
});
const originalExit = process.exit;
let exitCode: number | undefined;
const restoreEnv = installIsolatedCliTestEnv();
(process as any).exit = ((code?: number) => {
exitCode = code ?? 0;
}) as typeof process.exit;
const sendToDaemon = async (req: { command: string }): Promise<DaemonResponse> => {
if (req.command === 'lease_heartbeat') {
return {
ok: true,
data: {
lease: {
leaseId: 'lease-1',
tenantId: 'tenant-1',
runId: 'run-1',
backend: 'android-instance',
},
},
};
}
return { ok: true, data: {} };
};
try {
await runCli(['--state-dir', stateDir, 'cdp', 'target', 'list'], { sendToDaemon });
} finally {
restoreEnv();
process.exit = originalExit;
fs.rmSync(tempRoot, { recursive: true, force: true });
}
assert.equal(exitCode, 0);
assert.equal(vi.mocked(runAgentCdpCommand).mock.calls.length, 1);
assert.deepEqual(vi.mocked(runAgentCdpCommand).mock.calls[0]?.[0], ['target', 'list']);
assert.equal(vi.mocked(runAgentCdpCommand).mock.calls[0]?.[1]?.flags?.session, 'adc-android');
assert.deepEqual(vi.mocked(runAgentCdpCommand).mock.calls[0]?.[1]?.runtime, runtime);
});