Repository navigation
Give remote daemon unavailability a typed refusal reason #4532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
packages/daemon-client/src/__tests__/daemon-client-lifecycle-remote-health.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // A remote connection probes GET /health before it sends anything. When that probe answers nothing | ||
| // usable, the refusal is the typed one: the request never left the client, so a caller branches on | ||
| // `details.reason` and `details.dispatched` instead of comparing the message. | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import http from 'node:http'; | ||
| import { test } from 'vitest'; | ||
| import { | ||
| closeLoopbackServer, | ||
| listenOnLoopback, | ||
| skipWhenLoopbackUnavailable, | ||
| } from '@agent-device/test-kit'; | ||
| import { AppError } from '@agent-device/kernel/errors'; | ||
| import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog'; | ||
| import { REMOTE_DAEMON_UNAVAILABLE_REASON } from '../daemon-lifecycle-errors.ts'; | ||
| import { sendToDaemon } from '../index.ts'; | ||
|
|
||
| // A press, so the refusal states the fact a caller needs before resending a mutating command. | ||
| function remotePress(baseUrl: string) { | ||
| return { | ||
| session: 'default', | ||
| command: PUBLIC_COMMANDS.press, | ||
| positionals: ['@e1'], | ||
| flags: { daemonBaseUrl: baseUrl }, | ||
| meta: { requestId: 'req-remote-health-refusal' }, | ||
| }; | ||
| } | ||
|
|
||
| /** A remote daemon that answers `/health` with `healthStatus` and counts the RPCs it receives. */ | ||
| function remoteWithHealthStatus(healthStatus: number): { | ||
| handler: http.Server; | ||
| rpcSends: () => number; | ||
| } { | ||
| let rpcSends = 0; | ||
| const handler = http.createServer((req, res) => { | ||
| req.resume(); | ||
| req.on('end', () => { | ||
| if (req.url?.endsWith('/health')) { | ||
| res.statusCode = healthStatus; | ||
| res.end(); | ||
| return; | ||
| } | ||
| rpcSends += 1; | ||
| res.statusCode = 500; | ||
| res.end(); | ||
| }); | ||
| }); | ||
| return { handler, rpcSends: () => rpcSends }; | ||
| } | ||
|
|
||
| test('a remote press whose health probe answers 5xx refuses with the typed reason and dispatched no', async (t) => { | ||
| if (await skipWhenLoopbackUnavailable(t)) return; | ||
| const { handler, rpcSends } = remoteWithHealthStatus(503); | ||
| const port = await listenOnLoopback(handler); | ||
| const baseUrl = `http://127.0.0.1:${port}`; | ||
| try { | ||
| await assert.rejects(sendToDaemon(remotePress(baseUrl)), (error: unknown) => { | ||
| assert.ok(error instanceof AppError, `unexpected failure: ${String(error)}`); | ||
| assert.equal(error.code, 'COMMAND_FAILED'); | ||
| assert.equal(error.details?.reason, REMOTE_DAEMON_UNAVAILABLE_REASON); | ||
| assert.equal(error.details?.dispatched, 'no'); | ||
| assert.equal(error.details?.daemonBaseUrl, baseUrl); | ||
| return true; | ||
| }); | ||
| assert.equal(rpcSends(), 0, 'an unreachable remote daemon receives no RPC'); | ||
| } finally { | ||
| await closeLoopbackServer(handler); | ||
| } | ||
| }); | ||
|
|
||
| // The closest negative: a probe that answers 4xx counts as reachable, so the press is sent and the | ||
| // unavailable refusal is never stamped on a request that left the client. | ||
| test('a remote press whose health probe answers 4xx is sent and is not the unavailable refusal', async (t) => { | ||
| if (await skipWhenLoopbackUnavailable(t)) return; | ||
| const { handler, rpcSends } = remoteWithHealthStatus(404); | ||
| const port = await listenOnLoopback(handler); | ||
| try { | ||
| await assert.rejects( | ||
| sendToDaemon(remotePress(`http://127.0.0.1:${port}`)), | ||
| (error: unknown) => { | ||
| assert.ok(error instanceof AppError, `unexpected failure: ${String(error)}`); | ||
| assert.notEqual(error.details?.reason, REMOTE_DAEMON_UNAVAILABLE_REASON); | ||
| return true; | ||
| }, | ||
| ); | ||
| assert.equal(rpcSends(), 1); | ||
| } finally { | ||
| await closeLoopbackServer(handler); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // A remote daemon that answers nothing stops the CLI before its request is sent. This runs the real | ||
| // CLI against a base URL with no listener, so what it reads is the `--json` failure envelope a | ||
| // caller branches on: the typed reason, and the fact that nothing was dispatched. | ||
|
|
||
| import http from 'node:http'; | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'vitest'; | ||
| import { runCliCapture } from './cli-capture.ts'; | ||
|
|
||
| /** An ephemeral loopback port with no listener: bind 127.0.0.1:0, read the port, close it. */ | ||
| async function reserveUnusedLoopbackPort(): Promise<number> { | ||
| const server = http.createServer(); | ||
| try { | ||
| await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve)); | ||
| const address = server.address(); | ||
| if (address === null || typeof address === 'string') { | ||
| throw new Error('Expected a TCP port on the loopback listener'); | ||
| } | ||
| return address.port; | ||
| } finally { | ||
| await new Promise<void>((resolve, reject) => | ||
| server.close((error) => (error ? reject(error) : resolve())), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| test('--json carries the remote daemon refusal reason and its non-dispatch', async () => { | ||
| const port = await reserveUnusedLoopbackPort(); | ||
| const result = await runCliCapture(['devices', '--json'], { | ||
| useRealDaemonClient: true, | ||
| env: { AGENT_DEVICE_DAEMON_BASE_URL: `http://127.0.0.1:${port}` }, | ||
| }); | ||
|
|
||
| assert.equal(result.code, 1); | ||
| const payload = JSON.parse(result.stdout) as { | ||
| success: boolean; | ||
| error: { code: string; message: string; details?: Record<string, unknown> }; | ||
| }; | ||
| assert.equal(payload.success, false); | ||
| assert.equal(payload.error.code, 'COMMAND_FAILED'); | ||
| assert.equal(payload.error.message, 'Remote daemon is unavailable'); | ||
| // The published wire values a --json consumer branches on, asserted as the strings themselves. | ||
| assert.equal(payload.error.details?.reason, 'remote_daemon_unavailable'); | ||
| assert.equal(payload.error.details?.dispatched, 'no'); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.