| 1 | |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { spawnSync } from 'node:child_process'; |
| 4 | import { mkdtemp, readFile, rm, stat } from 'node:fs/promises'; |
| 5 | import { tmpdir } from 'node:os'; |
| 6 | import { isAbsolute, join } from 'node:path'; |
| 7 | import { setTimeout } from 'node:timers/promises'; |
| 8 | |
| 9 | const binary = process.argv[2]; |
| 10 | assert.ok(binary && isAbsolute(binary), 'Pass an absolute Cage CLI binary path.'); |
| 11 | const profile = await mkdtemp(join(tmpdir(), 'cage-agent-cli-')); |
| 12 | const config = join(profile, 'profile', 'config.json'); |
| 13 | let controllerPID; |
| 14 | function call(args, input, expectedCode = 0) { |
| 15 | const result = spawnSync(binary, ['agent', ...args, '--config', config], { input: input === undefined ? undefined : JSON.stringify(input), encoding: 'utf8', timeout: 30000 }); |
| 16 | assert.ifError(result.error); |
| 17 | assert.equal(result.status, expectedCode, `Unexpected exit from ${args[0]}: ${result.stdout} ${result.stderr}`); |
| 18 | const reply = JSON.parse(result.stdout); |
| 19 | assert.equal(reply.protocol, 1); |
| 20 | assert.equal(reply.config_path, config); |
| 21 | return reply; |
| 22 | } |
| 23 | try { |
| 24 | const capabilities = call(['capabilities']); |
| 25 | assert.ok(capabilities.data.operations.some(operation => operation.name === 'settings.patch')); |
| 26 | assert.equal(call(['doctor']).data.checks[0].code, 'config_missing'); |
| 27 | assert.equal(call(['call', 'settings.get'], undefined, 1).error.code, 'service_unavailable'); |
| 28 | await assert.rejects(stat(join(profile, 'profile')), { code: 'ENOENT' }); |
| 29 | call(['init']); |
| 30 | const originalConfig = await readFile(config, 'utf8'); |
| 31 | call(['init']); assert.equal(await readFile(config, 'utf8'), originalConfig); |
| 32 | call(['service.start']); |
| 33 | controllerPID = JSON.parse(await readFile(join(profile, 'profile', 'control.json'), 'utf8')).pid; |
| 34 | call(['service.start']); |
| 35 | assert.equal(JSON.parse(await readFile(join(profile, 'profile', 'control.json'), 'utf8')).pid, controllerPID); |
| 36 | assert.equal(call(['call', 'settings.get'], undefined, 3).error.code, 'account_locked'); |
| 37 | call(['call', 'account.create', '--input', '-'], { username: 'agent_acceptance', password: 'Ephemeral-Test-Password-123!', confirm_password: 'Ephemeral-Test-Password-123!' }); |
| 38 | const before = call(['call', 'settings.get']).data; |
| 39 | const patch = { balanced_display: !before.balanced_display }; |
| 40 | call(['call', 'settings.patch', '--input', '-'], { patch, dry_run: true }); |
| 41 | assert.deepEqual(call(['call', 'settings.get']).data, before); |
| 42 | const applied = call(['call', 'settings.patch', '--input', '-', '--wait', '10s'], { patch }); |
| 43 | assert.equal(applied.status, 'completed'); assert.equal(applied.data.running, false); |
| 44 | assert.equal(call(['job', applied.data.id]).data.id, applied.data.id); |
| 45 | assert.equal(call(['call', 'settings.get']).data.balanced_display, patch.balanced_display); |
| 46 | call(['call','browser.preferences.set','--input','-'],{browser_home_url:'https://example.org/'}); |
| 47 | assert.equal(call(['call','settings.get']).data.browser_home_url,'https://example.org/'); |
| 48 | const rejected = call(['call', 'settings.patch', '--input', '-'], { patch: { proxy_url: 'socks5://127.0.0.1:1' } }, 1); |
| 49 | assert.equal(rejected.error.code, 'request_rejected'); |
| 50 | const proxy = call(['call', 'proxy.status']); |
| 51 | assert.equal(proxy.data.sources.length, 0); |
| 52 | assert.ok(!JSON.stringify(proxy).includes('proxy_url')); |
| 53 | const bundle = join(profile, 'diagnostics.zip'); |
| 54 | call(['diagnostics', '--output', bundle]); |
| 55 | assert.ok((await stat(bundle)).size > 0); |
| 56 | assert.equal(call(['diagnostics', '--output', bundle], undefined, 2).error.code, 'invalid_input'); |
| 57 | console.log('PASS: real CLI discovery, offline doctor, no implicit writes, idempotent init/start, account locking, private input, dry run, settings patch, tracked completion, proxy status and diagnostic export.'); |
| 58 | } finally { |
| 59 | if (controllerPID) { |
| 60 | try { process.kill(controllerPID, 'SIGTERM'); } catch (error) { if (error.code !== 'ESRCH') throw error; } |
| 61 | |
| 62 | let retired = false; |
| 63 | for (let attempt = 0; attempt < 100; attempt++) { |
| 64 | try { await stat(join(profile, 'profile', 'control.json')); } catch (error) { if (error.code === 'ENOENT') { retired = true; break; } throw error; } |
| 65 | await setTimeout(100); |
| 66 | } |
| 67 | assert.ok(retired, 'Controller did not retire; preserve the isolated profile for inspection.'); |
| 68 | } |
| 69 | await rm(profile, { recursive: true, force: true }); |
| 70 | } |