|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import os from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { runCli } from '../cli.ts'; |
| 7 | +import type { DaemonRequest, DaemonResponse } from '../daemon-client.ts'; |
| 8 | + |
| 9 | +class ExitSignal extends Error { |
| 10 | + public readonly code: number; |
| 11 | + |
| 12 | + constructor(code: number) { |
| 13 | + super(`EXIT_${code}`); |
| 14 | + this.code = code; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +type RunResult = { |
| 19 | + code: number | null; |
| 20 | + stdout: string; |
| 21 | + stderr: string; |
| 22 | + calls: Omit<DaemonRequest, 'token'>[]; |
| 23 | +}; |
| 24 | + |
| 25 | +async function runCliCapture( |
| 26 | + argv: string[], |
| 27 | +): Promise<RunResult> { |
| 28 | + let stdout = ''; |
| 29 | + let stderr = ''; |
| 30 | + let code: number | null = null; |
| 31 | + const calls: Array<Omit<DaemonRequest, 'token'>> = []; |
| 32 | + |
| 33 | + const originalExit = process.exit; |
| 34 | + const originalStdoutWrite = process.stdout.write.bind(process.stdout); |
| 35 | + const originalStderrWrite = process.stderr.write.bind(process.stderr); |
| 36 | + |
| 37 | + (process as any).exit = ((nextCode?: number) => { |
| 38 | + throw new ExitSignal(nextCode ?? 0); |
| 39 | + }) as typeof process.exit; |
| 40 | + (process.stdout as any).write = ((chunk: unknown) => { |
| 41 | + stdout += String(chunk); |
| 42 | + return true; |
| 43 | + }) as typeof process.stdout.write; |
| 44 | + (process.stderr as any).write = ((chunk: unknown) => { |
| 45 | + stderr += String(chunk); |
| 46 | + return true; |
| 47 | + }) as typeof process.stderr.write; |
| 48 | + |
| 49 | + const sendToDaemon = async (req: Omit<DaemonRequest, 'token'>): Promise<DaemonResponse> => { |
| 50 | + calls.push(req); |
| 51 | + return { ok: true, data: { total: 1, executed: 1, totalDurationMs: 1 } }; |
| 52 | + }; |
| 53 | + |
| 54 | + try { |
| 55 | + await runCli(argv, { sendToDaemon }); |
| 56 | + } catch (error) { |
| 57 | + if (error instanceof ExitSignal) code = error.code; |
| 58 | + else throw error; |
| 59 | + } finally { |
| 60 | + process.exit = originalExit; |
| 61 | + process.stdout.write = originalStdoutWrite; |
| 62 | + process.stderr.write = originalStderrWrite; |
| 63 | + } |
| 64 | + |
| 65 | + return { code, stdout, stderr, calls }; |
| 66 | +} |
| 67 | + |
| 68 | +test('batch --steps parses JSON and forwards batchSteps only', async () => { |
| 69 | + const result = await runCliCapture([ |
| 70 | + 'batch', |
| 71 | + '--session', |
| 72 | + 'sim', |
| 73 | + '--platform', |
| 74 | + 'ios', |
| 75 | + '--steps', |
| 76 | + '[{"command":"open","positionals":["settings"]}]', |
| 77 | + '--json', |
| 78 | + ]); |
| 79 | + assert.equal(result.code, null); |
| 80 | + assert.equal(result.calls.length, 1); |
| 81 | + const req = result.calls[0]; |
| 82 | + assert.equal(req.command, 'batch'); |
| 83 | + assert.equal(req.session, 'sim'); |
| 84 | + assert.equal(req.flags?.platform, 'ios'); |
| 85 | + assert.ok(Array.isArray(req.flags?.batchSteps)); |
| 86 | + assert.equal((req.flags?.batchSteps ?? [])[0]?.command, 'open'); |
| 87 | + assert.equal(Object.hasOwn(req.flags ?? {}, 'steps'), false); |
| 88 | +}); |
| 89 | + |
| 90 | +test('batch --steps-file parses file payload', async () => { |
| 91 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-batch-')); |
| 92 | + const stepsPath = path.join(tmpDir, 'steps.json'); |
| 93 | + fs.writeFileSync(stepsPath, JSON.stringify([{ command: 'wait', positionals: ['100'] }]), 'utf8'); |
| 94 | + const result = await runCliCapture(['batch', '--steps-file', stepsPath, '--json']); |
| 95 | + assert.equal(result.code, null); |
| 96 | + assert.equal(result.calls.length, 1); |
| 97 | + const req = result.calls[0]; |
| 98 | + assert.equal(req.command, 'batch'); |
| 99 | + assert.equal((req.flags?.batchSteps ?? [])[0]?.command, 'wait'); |
| 100 | +}); |
| 101 | + |
| 102 | +test('batch --steps-file returns clear error for missing file', async () => { |
| 103 | + const result = await runCliCapture(['batch', '--steps-file', '/tmp/definitely-missing-batch-steps.json']); |
| 104 | + assert.equal(result.code, 1); |
| 105 | + assert.equal(result.calls.length, 0); |
| 106 | + assert.match(result.stderr, /Failed to read --steps-file/); |
| 107 | +}); |
| 108 | + |
| 109 | +test('batch --steps-file rejects invalid JSON payload', async () => { |
| 110 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-batch-invalid-')); |
| 111 | + const stepsPath = path.join(tmpDir, 'steps.json'); |
| 112 | + fs.writeFileSync(stepsPath, '{"command":"open"', 'utf8'); |
| 113 | + const result = await runCliCapture(['batch', '--steps-file', stepsPath]); |
| 114 | + assert.equal(result.code, 1); |
| 115 | + assert.equal(result.calls.length, 0); |
| 116 | + assert.match(result.stderr, /Batch steps must be valid JSON/); |
| 117 | +}); |
0 commit comments