|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import { test } from 'vitest'; |
| 4 | +import { runCliCapture } from './cli-capture.ts'; |
| 5 | +import { mkdtempForTestSync } from './test-utils/tmp-dir.ts'; |
| 6 | +import { withCommandExecutorOverride } from '../utils/exec.ts'; |
| 7 | +import { |
| 8 | + installFakeManagedAgentBrowser, |
| 9 | + withNodeRuntime, |
| 10 | + writeFakeManagedAgentBrowserPackage, |
| 11 | +} from '../platforms/web/__tests__/test-utils.ts'; |
| 12 | + |
| 13 | +type SpawnedCommand = { cmd: string; args: string[] }; |
| 14 | + |
| 15 | +// `binaryPath` has been in the published `web setup`/`web doctor` JSON since #833. |
| 16 | +// The Windows spawn fix (#2022) moves execution to `node <entryScript>` and adds |
| 17 | +// `entryScript`/`packageDir`, but the released field stays in the contract. |
| 18 | +test('web doctor --json keeps the published status fields and spawns the JS entry', async () => { |
| 19 | + const stateDir = mkdtempForTestSync('agent device cli web doctor '); |
| 20 | + try { |
| 21 | + const install = installFakeManagedAgentBrowser(stateDir); |
| 22 | + const spawned: SpawnedCommand[] = []; |
| 23 | + |
| 24 | + const result = await withCommandExecutorOverride( |
| 25 | + async (cmd, args) => { |
| 26 | + spawned.push({ cmd, args }); |
| 27 | + return { stdout: 'ok', stderr: '', exitCode: 0 }; |
| 28 | + }, |
| 29 | + async () => |
| 30 | + await runCliCapture(['web', 'doctor', '--json'], { |
| 31 | + env: { AGENT_DEVICE_STATE_DIR: stateDir }, |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | + const status = parseStatus(result.stdout); |
| 36 | + assert.equal(status.binaryPath, install.binaryPath); |
| 37 | + assert.equal(status.entryScript, install.entryScript); |
| 38 | + assert.equal(status.packageDir, install.packageDir); |
| 39 | + assert.equal(status.installDir, install.installDir); |
| 40 | + assert.equal(status.installed, true); |
| 41 | + assert.equal(status.socketDir, undefined); |
| 42 | + assert.deepEqual(spawned, [ |
| 43 | + { cmd: process.execPath, args: [install.entryScript, 'doctor', '--offline', '--quick'] }, |
| 44 | + ]); |
| 45 | + assert.equal(result.calls.length, 0); |
| 46 | + } finally { |
| 47 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +test('web setup --json keeps the published status fields after installing', async () => { |
| 52 | + const stateDir = mkdtempForTestSync('agent device cli web setup '); |
| 53 | + try { |
| 54 | + let install: ReturnType<typeof writeFakeManagedAgentBrowserPackage> | undefined; |
| 55 | + let stdout = ''; |
| 56 | + |
| 57 | + await withNodeRuntime({ version: '24.13.0' }, async () => { |
| 58 | + const result = await withCommandExecutorOverride( |
| 59 | + async (_cmd, args) => { |
| 60 | + // Stand in for the npm run that writes the managed package tree. |
| 61 | + if (args.includes('--prefix')) install = writeFakeManagedAgentBrowserPackage(stateDir); |
| 62 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 63 | + }, |
| 64 | + async () => |
| 65 | + await runCliCapture(['web', 'setup', '--json'], { |
| 66 | + env: { AGENT_DEVICE_STATE_DIR: stateDir }, |
| 67 | + }), |
| 68 | + ); |
| 69 | + stdout = result.stdout; |
| 70 | + }); |
| 71 | + |
| 72 | + const status = parseStatus(stdout); |
| 73 | + assert.equal(status.binaryPath, install?.binaryPath); |
| 74 | + assert.equal(status.entryScript, install?.entryScript); |
| 75 | + assert.equal(status.packageDir, install?.packageDir); |
| 76 | + assert.equal(status.installed, true); |
| 77 | + assert.equal(status.socketDir, undefined); |
| 78 | + } finally { |
| 79 | + fs.rmSync(stateDir, { recursive: true, force: true }); |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +function parseStatus(stdout: string): Record<string, unknown> { |
| 84 | + const payload: unknown = JSON.parse(firstJsonDocument(stdout)); |
| 85 | + assert.ok(isRecord(payload) && payload.success === true, stdout); |
| 86 | + const data = payload.data; |
| 87 | + assert.ok(isRecord(data), stdout); |
| 88 | + const status = data.status; |
| 89 | + assert.ok(isRecord(status), stdout); |
| 90 | + return status; |
| 91 | +} |
| 92 | + |
| 93 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 94 | + return typeof value === 'object' && value !== null; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * The CLI dispatches `web` inside its top-level try, and the capture harness |
| 99 | + * turns `process.exit` into a throw, so the command's payload is followed by the |
| 100 | + * CLI's own report of that synthetic exit. Only the first document is the |
| 101 | + * command's own output; real runs exit for real and print it once. |
| 102 | + */ |
| 103 | +function firstJsonDocument(stdout: string): string { |
| 104 | + const lines = stdout.split('\n'); |
| 105 | + const end = lines.indexOf('}'); |
| 106 | + assert.ok(end >= 0, stdout); |
| 107 | + return lines.slice(0, end + 1).join('\n'); |
| 108 | +} |
0 commit comments