Skip to content

Commit f78c2a1

Browse files
committed
test(cli): tighten proxy host/port and connection notice assertions
Review follow-up: record the arguments the command passes to the proxy server's listen so the --proxy-host/--proxy-port test cannot pass on defaults, and pin the absence of the deferred Metro notice in the bound-lease connection status case.
1 parent 85e7a7e commit f78c2a1

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎src/cli/commands/__tests__/connection.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ test('connection status renders a bound lease and a prepared runtime without not
120120
assert.match(result.stdout, /tenant=acme runId=run-123 leaseId=lease-1 backend=android-instance/);
121121
assert.match(result.stdout, /metro=prepared/);
122122
assert.doesNotMatch(result.stdout, /No live device session has been created/);
123+
assert.doesNotMatch(result.stdout, /Metro runtime is not prepared yet/);
123124
});
124125

125126
test('connection status re-emits the deferred Metro notice for a bound lease', async () => {

‎src/cli/commands/__tests__/proxy.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { afterEach, test, vi } from 'vitest';
88
import {
99
skipWhenLoopbackUnavailable,
1010
closeLoopbackServer,
11+
listenOnLoopback,
1112
} from '../../../__tests__/test-utils/loopback.ts';
1213
import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts';
1314
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
@@ -16,6 +17,7 @@ const mocks = vi.hoisted(() => ({
1617
ensureDaemon: vi.fn(),
1718
writeCommandOutput: vi.fn(async (..._args: unknown[]) => undefined),
1819
createdServers: [] as http.Server[],
20+
listenArgs: [] as { port: number; host: string }[],
1921
}));
2022

2123
vi.mock('../../../daemon-client/daemon-client-lifecycle.ts', async (importOriginal) => ({
@@ -28,6 +30,12 @@ vi.mock('@agent-device/proxy', async (importOriginal) => {
2830
...actual,
2931
createDaemonProxyServer: (options: Parameters<typeof actual.createDaemonProxyServer>[0]) => {
3032
const server = actual.createDaemonProxyServer(options);
33+
// Record what the command asks the real server to bind; the bind itself stays real.
34+
const listen = server.listen.bind(server);
35+
server.listen = ((port?: number, host?: string, callback?: () => void) => {
36+
mocks.listenArgs.push({ port: port ?? 0, host: String(host) });
37+
return listen(port, host, callback);
38+
}) as typeof server.listen;
3139
mocks.createdServers.push(server);
3240
return server;
3341
},
@@ -39,6 +47,7 @@ import { proxyCommand } from '../proxy.ts';
3947

4048
afterEach(async () => {
4149
vi.clearAllMocks();
50+
mocks.listenArgs.splice(0, mocks.listenArgs.length);
4251
const servers = mocks.createdServers.splice(0, mocks.createdServers.length);
4352
for (const server of servers) {
4453
await closeLoopbackServer(server);
@@ -114,6 +123,7 @@ test('proxy binds a loopback listener and reports the supplied client token', as
114123
});
115124
const { data, render } = await waitForStartupOutput();
116125

126+
assert.deepEqual(mocks.listenArgs, [{ port: 0, host: '127.0.0.1' }]);
117127
assert.equal(data.upstreamBaseUrl, 'http://127.0.0.1:54321');
118128
assert.equal(data.token, 'proxy-token');
119129
assert.equal(data.stateDir, resolveDaemonPaths(stateDir).baseDir);
@@ -135,10 +145,18 @@ test('proxy honors proxy host and port flags and mints a token when none is supp
135145
info: { httpPort: 54322, token: 'daemon-secret', pid: 4242 },
136146
startedByClient: false,
137147
});
148+
// Reserve a real port through an ephemeral bind so the flag value is distinguishable
149+
// from the `0` default the command would otherwise pass to listen.
150+
const sentinel = http.createServer();
151+
const reservedPort = await listenOnLoopback(sentinel);
152+
await closeLoopbackServer(sentinel);
138153

139-
runProxy({ proxyHost: ' localhost ', proxyPort: 0 });
154+
runProxy({ proxyHost: ' localhost ', proxyPort: reservedPort });
140155
const { data } = await waitForStartupOutput();
141156

142-
assert.match(String(data.proxyBaseUrl), /^http:\/\/(localhost|\[::1\]|127\.0\.0\.1):\d+$/);
157+
assert.deepEqual(mocks.listenArgs, [{ port: reservedPort, host: 'localhost' }]);
158+
// The reported URL carries the bound address, which names the reserved port either way.
159+
assert.match(String(data.proxyBaseUrl), /^http:\/\/(localhost|127\.0\.0\.1|\[::1\]):\d+$/);
160+
assert.ok(String(data.proxyBaseUrl).endsWith(`:${reservedPort}`));
143161
assert.match(String(data.token), /^[0-9a-f]{64}$/);
144162
});

0 commit comments

Comments
 (0)