|
1 | 1 | import { spawn } from 'node:child_process'; |
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import crypto from 'node:crypto'; |
| 4 | +import fs from 'node:fs'; |
4 | 5 | import http from 'node:http'; |
| 6 | +import os from 'node:os'; |
| 7 | +import path from 'node:path'; |
5 | 8 | import type { Duplex } from 'node:stream'; |
6 | 9 | import { setTimeout as delay } from 'node:timers/promises'; |
7 | 10 | import { afterEach, test } from 'vitest'; |
@@ -488,3 +491,150 @@ test('metro companion worker reconnects after the bridge closes immediately afte |
488 | 491 |
|
489 | 492 | assert.equal(bridgeConnections, 2); |
490 | 493 | }); |
| 494 | + |
| 495 | +test('metro companion worker exits after its state file is removed', async () => { |
| 496 | + const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-metro-companion-worker-')); |
| 497 | + const statePath = path.join(tempRoot, 'metro-companion.json'); |
| 498 | + fs.writeFileSync(statePath, '{}', 'utf8'); |
| 499 | + cleanupTasks.push(async () => { |
| 500 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 501 | + }); |
| 502 | + |
| 503 | + const bridgeSocketReady = createDeferred<void>(); |
| 504 | + let bridgePort = 0; |
| 505 | + let bridgeSocketRef: Duplex | null = null; |
| 506 | + |
| 507 | + const localServer = http.createServer((_, res) => { |
| 508 | + res.writeHead(404); |
| 509 | + res.end('not found'); |
| 510 | + }); |
| 511 | + cleanupTasks.push(() => closeServer(localServer)); |
| 512 | + const localPort = await listen(localServer); |
| 513 | + |
| 514 | + const bridgeServer = http.createServer((req, res) => { |
| 515 | + const url = new URL(req.url || '/', 'http://127.0.0.1'); |
| 516 | + if (req.method === 'POST' && url.pathname === '/api/metro/companion/register') { |
| 517 | + req.resume(); |
| 518 | + req.on('end', () => { |
| 519 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 520 | + res.end( |
| 521 | + JSON.stringify({ |
| 522 | + ok: true, |
| 523 | + data: { ws_url: `ws://127.0.0.1:${bridgePort}/bridge` }, |
| 524 | + }), |
| 525 | + ); |
| 526 | + }); |
| 527 | + return; |
| 528 | + } |
| 529 | + res.writeHead(404); |
| 530 | + res.end('not found'); |
| 531 | + }); |
| 532 | + bridgeServer.on('upgrade', (req, socket) => { |
| 533 | + if (req.url !== '/bridge') { |
| 534 | + socket.destroy(); |
| 535 | + return; |
| 536 | + } |
| 537 | + bridgeSocketRef = socket; |
| 538 | + const key = req.headers['sec-websocket-key']; |
| 539 | + if (typeof key !== 'string') { |
| 540 | + socket.destroy(); |
| 541 | + return; |
| 542 | + } |
| 543 | + const accept = crypto |
| 544 | + .createHash('sha1') |
| 545 | + .update(`${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`) |
| 546 | + .digest('base64'); |
| 547 | + socket.write( |
| 548 | + [ |
| 549 | + 'HTTP/1.1 101 Switching Protocols', |
| 550 | + 'Upgrade: websocket', |
| 551 | + 'Connection: Upgrade', |
| 552 | + `Sec-WebSocket-Accept: ${accept}`, |
| 553 | + '\r\n', |
| 554 | + ].join('\r\n'), |
| 555 | + ); |
| 556 | + bridgeSocketReady.resolve(); |
| 557 | + }); |
| 558 | + cleanupTasks.push(() => closeServer(bridgeServer)); |
| 559 | + cleanupTasks.push(async () => { |
| 560 | + bridgeSocketRef?.destroy(); |
| 561 | + }); |
| 562 | + bridgePort = await listen(bridgeServer); |
| 563 | + |
| 564 | + const companion = spawn( |
| 565 | + process.execPath, |
| 566 | + ['--experimental-strip-types', 'src/metro-companion.ts', '--agent-device-run-metro-companion'], |
| 567 | + { |
| 568 | + cwd: process.cwd(), |
| 569 | + env: { |
| 570 | + ...process.env, |
| 571 | + AGENT_DEVICE_METRO_COMPANION_SERVER_BASE_URL: `http://127.0.0.1:${bridgePort}`, |
| 572 | + AGENT_DEVICE_METRO_COMPANION_BEARER_TOKEN: 'test-token', |
| 573 | + AGENT_DEVICE_METRO_COMPANION_LOCAL_BASE_URL: `http://127.0.0.1:${localPort}`, |
| 574 | + AGENT_DEVICE_METRO_COMPANION_STATE_PATH: statePath, |
| 575 | + }, |
| 576 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 577 | + }, |
| 578 | + ); |
| 579 | + cleanupTasks.push(() => stopChild(companion)); |
| 580 | + |
| 581 | + let stderr = ''; |
| 582 | + companion.stderr.on('data', (chunk) => { |
| 583 | + stderr += chunk.toString(); |
| 584 | + }); |
| 585 | + |
| 586 | + await waitFor(bridgeSocketReady.promise, 5_000, 'bridge websocket connection'); |
| 587 | + fs.unlinkSync(statePath); |
| 588 | + |
| 589 | + const exit = await waitFor( |
| 590 | + new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => { |
| 591 | + companion.once('exit', (code, signal) => resolve({ code, signal })); |
| 592 | + }), |
| 593 | + 5_000, |
| 594 | + 'worker exit after state cleanup', |
| 595 | + ); |
| 596 | + |
| 597 | + assert.equal(exit.signal, null, `unexpected worker stderr: ${stderr}`); |
| 598 | + assert.equal(exit.code, 0, `unexpected worker stderr: ${stderr}`); |
| 599 | +}); |
| 600 | + |
| 601 | +test('metro companion worker exits immediately when its state file is already missing', async () => { |
| 602 | + const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-metro-companion-worker-')); |
| 603 | + const statePath = path.join(tempRoot, 'missing-metro-companion.json'); |
| 604 | + cleanupTasks.push(async () => { |
| 605 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 606 | + }); |
| 607 | + |
| 608 | + const companion = spawn( |
| 609 | + process.execPath, |
| 610 | + ['--experimental-strip-types', 'src/metro-companion.ts', '--agent-device-run-metro-companion'], |
| 611 | + { |
| 612 | + cwd: process.cwd(), |
| 613 | + env: { |
| 614 | + ...process.env, |
| 615 | + AGENT_DEVICE_METRO_COMPANION_SERVER_BASE_URL: 'http://127.0.0.1:1', |
| 616 | + AGENT_DEVICE_METRO_COMPANION_BEARER_TOKEN: 'test-token', |
| 617 | + AGENT_DEVICE_METRO_COMPANION_LOCAL_BASE_URL: 'http://127.0.0.1:1', |
| 618 | + AGENT_DEVICE_METRO_COMPANION_STATE_PATH: statePath, |
| 619 | + }, |
| 620 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 621 | + }, |
| 622 | + ); |
| 623 | + cleanupTasks.push(() => stopChild(companion)); |
| 624 | + |
| 625 | + let stderr = ''; |
| 626 | + companion.stderr.on('data', (chunk) => { |
| 627 | + stderr += chunk.toString(); |
| 628 | + }); |
| 629 | + |
| 630 | + const exit = await waitFor( |
| 631 | + new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => { |
| 632 | + companion.once('exit', (code, signal) => resolve({ code, signal })); |
| 633 | + }), |
| 634 | + 5_000, |
| 635 | + 'worker exit with missing state file', |
| 636 | + ); |
| 637 | + |
| 638 | + assert.equal(exit.signal, null, `unexpected worker stderr: ${stderr}`); |
| 639 | + assert.equal(exit.code, 0, `unexpected worker stderr: ${stderr}`); |
| 640 | +}); |
0 commit comments