|
| 1 | +// A remote connection probes GET /health before it sends anything. When that probe answers nothing |
| 2 | +// usable, the refusal is the typed one: the request never left the client, so a caller branches on |
| 3 | +// `details.reason` and `details.dispatched` instead of comparing the message. |
| 4 | + |
| 5 | +import assert from 'node:assert/strict'; |
| 6 | +import http from 'node:http'; |
| 7 | +import { test } from 'vitest'; |
| 8 | +import { |
| 9 | + closeLoopbackServer, |
| 10 | + listenOnLoopback, |
| 11 | + skipWhenLoopbackUnavailable, |
| 12 | +} from '@agent-device/test-kit'; |
| 13 | +import { AppError } from '@agent-device/kernel/errors'; |
| 14 | +import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog'; |
| 15 | +import { REMOTE_DAEMON_UNAVAILABLE_REASON } from '../daemon-lifecycle-errors.ts'; |
| 16 | +import { sendToDaemon } from '../index.ts'; |
| 17 | + |
| 18 | +// A press, so the refusal states the fact a caller needs before resending a mutating command. |
| 19 | +function remotePress(baseUrl: string) { |
| 20 | + return { |
| 21 | + session: 'default', |
| 22 | + command: PUBLIC_COMMANDS.press, |
| 23 | + positionals: ['@e1'], |
| 24 | + flags: { daemonBaseUrl: baseUrl }, |
| 25 | + meta: { requestId: 'req-remote-health-refusal' }, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/** A remote daemon that answers `/health` with `healthStatus` and counts the RPCs it receives. */ |
| 30 | +function remoteWithHealthStatus(healthStatus: number): { |
| 31 | + handler: http.Server; |
| 32 | + rpcSends: () => number; |
| 33 | +} { |
| 34 | + let rpcSends = 0; |
| 35 | + const handler = http.createServer((req, res) => { |
| 36 | + req.resume(); |
| 37 | + req.on('end', () => { |
| 38 | + if (req.url?.endsWith('/health')) { |
| 39 | + res.statusCode = healthStatus; |
| 40 | + res.end(); |
| 41 | + return; |
| 42 | + } |
| 43 | + rpcSends += 1; |
| 44 | + res.statusCode = 500; |
| 45 | + res.end(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + return { handler, rpcSends: () => rpcSends }; |
| 49 | +} |
| 50 | + |
| 51 | +test('a remote press whose health probe answers 5xx refuses with the typed reason and dispatched no', async (t) => { |
| 52 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 53 | + const { handler, rpcSends } = remoteWithHealthStatus(503); |
| 54 | + const port = await listenOnLoopback(handler); |
| 55 | + const baseUrl = `http://127.0.0.1:${port}`; |
| 56 | + try { |
| 57 | + await assert.rejects(sendToDaemon(remotePress(baseUrl)), (error: unknown) => { |
| 58 | + assert.ok(error instanceof AppError, `unexpected failure: ${String(error)}`); |
| 59 | + assert.equal(error.code, 'COMMAND_FAILED'); |
| 60 | + assert.equal(error.details?.reason, REMOTE_DAEMON_UNAVAILABLE_REASON); |
| 61 | + assert.equal(error.details?.dispatched, 'no'); |
| 62 | + assert.equal(error.details?.daemonBaseUrl, baseUrl); |
| 63 | + return true; |
| 64 | + }); |
| 65 | + assert.equal(rpcSends(), 0, 'an unreachable remote daemon receives no RPC'); |
| 66 | + } finally { |
| 67 | + await closeLoopbackServer(handler); |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +// The closest negative: a probe that answers 4xx counts as reachable, so the press is sent and the |
| 72 | +// unavailable refusal is never stamped on a request that left the client. |
| 73 | +test('a remote press whose health probe answers 4xx is sent and is not the unavailable refusal', async (t) => { |
| 74 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 75 | + const { handler, rpcSends } = remoteWithHealthStatus(404); |
| 76 | + const port = await listenOnLoopback(handler); |
| 77 | + try { |
| 78 | + await assert.rejects( |
| 79 | + sendToDaemon(remotePress(`http://127.0.0.1:${port}`)), |
| 80 | + (error: unknown) => { |
| 81 | + assert.ok(error instanceof AppError, `unexpected failure: ${String(error)}`); |
| 82 | + assert.notEqual(error.details?.reason, REMOTE_DAEMON_UNAVAILABLE_REASON); |
| 83 | + return true; |
| 84 | + }, |
| 85 | + ); |
| 86 | + assert.equal(rpcSends(), 1); |
| 87 | + } finally { |
| 88 | + await closeLoopbackServer(handler); |
| 89 | + } |
| 90 | +}); |
0 commit comments