|
| 1 | +// Every transport failure discloses whether the request can have reached the device. Each row |
| 2 | +// drives the real transport against a loopback stand-in daemon that fails at one point of the |
| 3 | +// exchange; the verdict comes from the command's declared effect once the request was sent. |
| 4 | + |
| 5 | +import type { DaemonWireRequest } from '@agent-device/contracts/command'; |
| 6 | +import fs from 'node:fs'; |
| 7 | +import http from 'node:http'; |
| 8 | +import net from 'node:net'; |
| 9 | +import path from 'node:path'; |
| 10 | +import assert from 'node:assert/strict'; |
| 11 | +import { test } from 'vitest'; |
| 12 | +import { AppError } from '@agent-device/kernel/errors'; |
| 13 | +import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog'; |
| 14 | +import type { DaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution'; |
| 15 | +import { |
| 16 | + assertDispatchDisclosureDriversMatchRows, |
| 17 | + closeLoopbackServer, |
| 18 | + createTestHostEnvironment, |
| 19 | + DISPATCH_DISCLOSURE_TABLE_PATH, |
| 20 | + dispatchDisclosureRowsOwnedBy, |
| 21 | + listenOnLoopback, |
| 22 | + type LoopbackServer, |
| 23 | + mkdtempForTestSync, |
| 24 | + skipWhenLoopbackUnavailable, |
| 25 | +} from '@agent-device/test-kit'; |
| 26 | +import { sendRequest } from '../daemon-client-transport.ts'; |
| 27 | +import type { DaemonInfo } from '../daemon-client-metadata.ts'; |
| 28 | + |
| 29 | +const IDENTITY = { token: 'test-token', pid: process.pid, processStartTime: 'stand-in' }; |
| 30 | + |
| 31 | +function statePaths(): DaemonPaths { |
| 32 | + const baseDir = mkdtempForTestSync('agent-device-dispatch-disclosure-test'); |
| 33 | + return { |
| 34 | + baseDir, |
| 35 | + infoPath: path.join(baseDir, 'daemon.json'), |
| 36 | + lockPath: path.join(baseDir, 'daemon.lock'), |
| 37 | + logPath: path.join(baseDir, 'daemon.log'), |
| 38 | + allocationsDir: path.join(baseDir, 'allocations'), |
| 39 | + } as DaemonPaths; |
| 40 | +} |
| 41 | + |
| 42 | +function request(command: string): DaemonWireRequest { |
| 43 | + return { |
| 44 | + token: IDENTITY.token, |
| 45 | + session: 'default', |
| 46 | + command, |
| 47 | + positionals: [], |
| 48 | + flags: {}, |
| 49 | + meta: { requestId: `dispatch-${command}` }, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** A socket daemon that reads one request line and then does `onRequest` with its socket. */ |
| 54 | +async function socketDaemon(onRequest: (socket: net.Socket) => void): Promise<LoopbackServer> { |
| 55 | + const server = net.createServer((socket) => { |
| 56 | + socket.on('error', () => {}); |
| 57 | + socket.once('data', () => onRequest(socket)); |
| 58 | + }); |
| 59 | + await listenOnLoopback(server); |
| 60 | + return server; |
| 61 | +} |
| 62 | + |
| 63 | +function portOf(server: LoopbackServer): number { |
| 64 | + const address = server.address(); |
| 65 | + assert.ok(address && typeof address === 'object'); |
| 66 | + return address.port; |
| 67 | +} |
| 68 | + |
| 69 | +async function overSocket( |
| 70 | + command: string, |
| 71 | + onRequest: (socket: net.Socket) => void, |
| 72 | +): Promise<unknown> { |
| 73 | + const server = await socketDaemon(onRequest); |
| 74 | + try { |
| 75 | + return await sendRequest( |
| 76 | + { ...IDENTITY, port: portOf(server) }, |
| 77 | + request(command), |
| 78 | + 'socket', |
| 79 | + statePaths(), |
| 80 | + undefined, |
| 81 | + { host: createTestHostEnvironment() }, |
| 82 | + ); |
| 83 | + } finally { |
| 84 | + await closeLoopbackServer(server); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +async function overHttp(info: (port: number) => DaemonInfo, timeoutMs?: number): Promise<unknown> { |
| 89 | + const server: LoopbackServer = http.createServer((req) => { |
| 90 | + req.on('error', () => {}); |
| 91 | + // Reads the whole request, then (without a timeout) drops the connection before replying. |
| 92 | + req.on('end', () => { |
| 93 | + if (timeoutMs === undefined) req.socket.destroy(); |
| 94 | + }); |
| 95 | + req.resume(); |
| 96 | + }); |
| 97 | + const port = await listenOnLoopback(server); |
| 98 | + try { |
| 99 | + return await sendRequest( |
| 100 | + info(port), |
| 101 | + request(PUBLIC_COMMANDS.press), |
| 102 | + 'http', |
| 103 | + statePaths(), |
| 104 | + timeoutMs, |
| 105 | + { host: createTestHostEnvironment() }, |
| 106 | + ); |
| 107 | + } finally { |
| 108 | + await closeLoopbackServer(server); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** A remote daemon that answers the request with one artifact and then refuses its download. */ |
| 113 | +async function artifactFetchAfterAnswer(): Promise<unknown> { |
| 114 | + const server: LoopbackServer = http.createServer((req, res) => { |
| 115 | + req.resume(); |
| 116 | + req.on('end', () => { |
| 117 | + if (req.method !== 'POST') { |
| 118 | + res.statusCode = 404; |
| 119 | + res.end(); |
| 120 | + return; |
| 121 | + } |
| 122 | + res.setHeader('content-type', 'application/json'); |
| 123 | + res.end( |
| 124 | + JSON.stringify({ |
| 125 | + jsonrpc: '2.0', |
| 126 | + id: 'dispatch-press', |
| 127 | + result: { |
| 128 | + ok: true, |
| 129 | + data: { |
| 130 | + artifacts: [{ artifactId: 'gone', field: 'path', fileName: 'screen.png' }], |
| 131 | + }, |
| 132 | + }, |
| 133 | + }), |
| 134 | + ); |
| 135 | + }); |
| 136 | + }); |
| 137 | + const port = await listenOnLoopback(server); |
| 138 | + try { |
| 139 | + return await sendRequest( |
| 140 | + { ...IDENTITY, baseUrl: `http://127.0.0.1:${port}` }, |
| 141 | + request(PUBLIC_COMMANDS.press), |
| 142 | + 'http', |
| 143 | + statePaths(), |
| 144 | + undefined, |
| 145 | + { host: createTestHostEnvironment() }, |
| 146 | + ); |
| 147 | + } finally { |
| 148 | + await closeLoopbackServer(server); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +async function refusedBeforeSend(): Promise<unknown> { |
| 153 | + const server = net.createServer(); |
| 154 | + const port = await listenOnLoopback(server); |
| 155 | + await closeLoopbackServer(server); |
| 156 | + return await sendRequest( |
| 157 | + { ...IDENTITY, port }, |
| 158 | + request(PUBLIC_COMMANDS.press), |
| 159 | + 'socket', |
| 160 | + statePaths(), |
| 161 | + undefined, |
| 162 | + { host: createTestHostEnvironment() }, |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +const DRIVERS: Record<string, () => Promise<unknown>> = { |
| 167 | + 'daemon-client.socket-closed-after-send': () => |
| 168 | + overSocket(PUBLIC_COMMANDS.press, (socket) => socket.destroy()), |
| 169 | + 'daemon-client.http-reset-after-send': () => |
| 170 | + overHttp((port) => ({ ...IDENTITY, httpPort: port })), |
| 171 | + 'daemon-client.invalid-response-after-send': () => |
| 172 | + overSocket(PUBLIC_COMMANDS.press, (socket) => socket.end('not a json-rpc response\n')), |
| 173 | + 'daemon-client.timeout-after-send': () => |
| 174 | + overHttp((port) => ({ ...IDENTITY, baseUrl: `http://127.0.0.1:${port}` }), 60), |
| 175 | + 'daemon-client.read-failure-after-send': () => |
| 176 | + overSocket(PUBLIC_COMMANDS.snapshot, (socket) => socket.destroy()), |
| 177 | + 'daemon-client.refused-before-send': refusedBeforeSend, |
| 178 | + 'daemon-client.delegated-composite-after-send': () => |
| 179 | + overSocket(PUBLIC_COMMANDS.batch, (socket) => socket.destroy()), |
| 180 | + 'daemon-client.artifact-fetch-after-answer': artifactFetchAfterAnswer, |
| 181 | +}; |
| 182 | + |
| 183 | +const ROWS = dispatchDisclosureRowsOwnedBy( |
| 184 | + import.meta.url, |
| 185 | + fs.readFileSync(DISPATCH_DISCLOSURE_TABLE_PATH, 'utf8'), |
| 186 | +); |
| 187 | + |
| 188 | +test('every daemon-client dispatch-disclosure row has exactly one driver', () => { |
| 189 | + assertDispatchDisclosureDriversMatchRows(ROWS, Object.keys(DRIVERS)); |
| 190 | +}); |
| 191 | + |
| 192 | +for (const row of ROWS) { |
| 193 | + test(`${row.id}: ${row.trigger} → dispatched ${row.dispatched}`, async (t) => { |
| 194 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 195 | + const drive = DRIVERS[row.id]; |
| 196 | + assert.ok(drive, `no driver for ${row.id}`); |
| 197 | + await assert.rejects(drive(), (error: unknown) => { |
| 198 | + assert.ok(error instanceof AppError, `unexpected failure: ${String(error)}`); |
| 199 | + assert.equal(error.details?.dispatched, row.dispatched); |
| 200 | + return true; |
| 201 | + }); |
| 202 | + }); |
| 203 | +} |
| 204 | + |
| 205 | +// The registry gives a command without a declared effect no verdict, at the daemon and here alike, |
| 206 | +// so the CLI prints no dispatch fact instead of a snapshot step for a request that has no screen. |
| 207 | +test('a sent request for a command without a declared effect gets no dispatch verdict', async (t) => { |
| 208 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 209 | + await assert.rejects( |
| 210 | + overSocket(PUBLIC_COMMANDS.devices, (socket) => socket.destroy()), |
| 211 | + (error: unknown) => { |
| 212 | + assert.ok(error instanceof AppError); |
| 213 | + // A socket closed without a reply surfaces as either post-send reason, depending on timing. |
| 214 | + assert.ok( |
| 215 | + ['daemon_transport_failure', 'daemon_response_invalid'].includes( |
| 216 | + String(error.details?.reason), |
| 217 | + ), |
| 218 | + ); |
| 219 | + assert.equal('dispatched' in (error.details ?? {}), false); |
| 220 | + return true; |
| 221 | + }, |
| 222 | + ); |
| 223 | +}); |
0 commit comments