|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import type http from 'node:http'; |
3 | | -import { test } from 'vitest'; |
| 2 | +import http from 'node:http'; |
| 3 | +import { test, vi } from 'vitest'; |
4 | 4 | import { |
5 | 5 | closeLoopbackServer, |
6 | 6 | listenOnLoopback, |
7 | 7 | skipWhenLoopbackUnavailable, |
8 | 8 | } from '../../__tests__/test-utils/loopback.ts'; |
9 | 9 | import { HUMAN_CONTROL_HTTP_PREFIX } from '../human-control-contract.ts'; |
10 | 10 | import { LeaseRegistry } from '../lease-registry.ts'; |
11 | | -import { HUMAN_CONTROL_SCOPE, humanControlRequest } from './human-control-fixtures.ts'; |
| 11 | +import { |
| 12 | + HUMAN_CONTROL_SCOPE, |
| 13 | + humanControlRequest, |
| 14 | + createControlLatch, |
| 15 | +} from './human-control-fixtures.ts'; |
12 | 16 | import { createHumanControlHarness } from './human-control-router-fixture.ts'; |
13 | 17 | import { tryHandleHumanControlHttpRoute } from '../human-control-http.ts'; |
14 | 18 | import { createDaemonHttpServer } from '../server/http-server.ts'; |
@@ -47,6 +51,76 @@ test('malformed request URLs return a normalized error', async () => { |
47 | 51 | assert.equal((JSON.parse(responseBody) as { code?: string }).code, 'INVALID_ARGS'); |
48 | 52 | }); |
49 | 53 |
|
| 54 | +for (const transport of ['tenant RPC', 'host PUT'] as const) { |
| 55 | + test(`${transport} disconnect during drain removes its pending hold before the mutation ends`, async (t) => { |
| 56 | + if (await skipWhenLoopbackUnavailable(t)) return; |
| 57 | + const { registry, lease, handleRequest } = createHumanControlHarness(); |
| 58 | + const finish = createControlLatch(); |
| 59 | + const disconnected = createControlLatch(); |
| 60 | + let mutationFinished = false; |
| 61 | + const mutation = registry.runDeviceMutation(lease, async () => { |
| 62 | + await finish.promise; |
| 63 | + mutationFinished = true; |
| 64 | + }); |
| 65 | + const server = await createDaemonHttpServer({ |
| 66 | + token: 'test-token', |
| 67 | + leaseRegistry: registry, |
| 68 | + handleRequest, |
| 69 | + }); |
| 70 | + server.on('request', (_req, res) => { |
| 71 | + res.once('close', () => { |
| 72 | + if (!res.writableFinished) disconnected.resolve(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + let request: http.ClientRequest | undefined; |
| 76 | + try { |
| 77 | + const port = await listenOnLoopback(server); |
| 78 | + const isRpc = transport === 'tenant RPC'; |
| 79 | + const body = JSON.stringify( |
| 80 | + isRpc |
| 81 | + ? { |
| 82 | + jsonrpc: '2.0', |
| 83 | + id: 'disconnected-takeover', |
| 84 | + method: 'agent_device.command', |
| 85 | + params: humanControlRequest(lease, 'human_control', ['put', 'disconnected', '{}']), |
| 86 | + } |
| 87 | + : { scope: HUMAN_CONTROL_SCOPE }, |
| 88 | + ); |
| 89 | + request = http.request({ |
| 90 | + host: '127.0.0.1', |
| 91 | + port, |
| 92 | + path: isRpc ? '/rpc' : `${HUMAN_CONTROL_HTTP_PREFIX}/disconnected`, |
| 93 | + method: isRpc ? 'POST' : 'PUT', |
| 94 | + headers: { |
| 95 | + authorization: 'Bearer test-token', |
| 96 | + 'content-type': 'application/json', |
| 97 | + 'content-length': Buffer.byteLength(body), |
| 98 | + }, |
| 99 | + }); |
| 100 | + request.on('error', () => undefined); |
| 101 | + request.end(body); |
| 102 | + await vi.waitFor(() => { |
| 103 | + assert.equal(registry.listHumanControlHolds({ kind: 'host' })[0]?.state, 'activating'); |
| 104 | + }); |
| 105 | + request.destroy(); |
| 106 | + await disconnected.promise; |
| 107 | + await vi.waitFor(() => { |
| 108 | + assert.deepEqual(registry.listHumanControlHolds({ kind: 'host' }), []); |
| 109 | + }); |
| 110 | + assert.equal(mutationFinished, false); |
| 111 | + finish.resolve(); |
| 112 | + await mutation; |
| 113 | + assert.deepEqual(registry.listHumanControlHolds({ kind: 'host' }), []); |
| 114 | + assert.equal(await registry.runDeviceMutation(lease, async () => 'resumed'), 'resumed'); |
| 115 | + } finally { |
| 116 | + request?.destroy(); |
| 117 | + finish.resolve(); |
| 118 | + await mutation; |
| 119 | + await closeLoopbackServer(server); |
| 120 | + } |
| 121 | + }); |
| 122 | +} |
| 123 | + |
50 | 124 | test('host administration and tenant RPC use the same lease registry with distinct authority', async (t) => { |
51 | 125 | if (await skipWhenLoopbackUnavailable(t)) return; |
52 | 126 | const { registry, lease, handleRequest } = createHumanControlHarness(); |
|
0 commit comments