Files
callstack__agent-device/src/__tests__/cli-agent-cdp-session.test.ts
Michał Pierzchała bcf910a2bb refactor: split daemon client driver into daemon/client/ — Phase 5 (#962)
Move the daemon CLIENT driver (the in-process side that sends requests to a
running daemon) out of the src/ root into src/daemon/client/, per
plans/perfect-shape.md §5.5 ('daemon/client/ <- daemon-client*.ts'; the
daemon- prefix co-located client driver + server bootstrap at src root).

Files moved (7): daemon-client{,-lifecycle,-metadata,-progress,-rpc,-timeout,
-transport}.

- git renames; 19 importers repointed via the resolve-based codemod
  (intra-set stays ./, kernel -> ../../, daemon/remote deps recomputed)
- Layering Guard verified: none import src/commands/* (safe under src/daemon/)
- not a public export; no rslib impact
- update fallow-baselines/health.json keys

Behaviorless path codemod; typecheck/lint/format/build/tests green.
2026-06-30 15:51:53 +02:00

102 lines
3.2 KiB
TypeScript

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/remote-connection-state.ts';
import type { DaemonResponse } from '../daemon/client/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);
});