|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { runCli } from '../cli.ts'; |
| 4 | +import type { DaemonRequest, DaemonResponse } from '../daemon-client.ts'; |
| 5 | + |
| 6 | +class ExitSignal extends Error { |
| 7 | + public readonly code: number; |
| 8 | + |
| 9 | + constructor(code: number) { |
| 10 | + super(`EXIT_${code}`); |
| 11 | + this.code = code; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +type RunResult = { |
| 16 | + code: number | null; |
| 17 | + stdout: string; |
| 18 | + stderr: string; |
| 19 | + calls: Omit<DaemonRequest, 'token'>[]; |
| 20 | +}; |
| 21 | + |
| 22 | +async function runCliCapture( |
| 23 | + argv: string[], |
| 24 | + responder: (req: Omit<DaemonRequest, 'token'>) => Promise<DaemonResponse>, |
| 25 | +): Promise<RunResult> { |
| 26 | + let stdout = ''; |
| 27 | + let stderr = ''; |
| 28 | + let code: number | null = null; |
| 29 | + const calls: Array<Omit<DaemonRequest, 'token'>> = []; |
| 30 | + |
| 31 | + const originalExit = process.exit; |
| 32 | + const originalStdoutWrite = process.stdout.write.bind(process.stdout); |
| 33 | + const originalStderrWrite = process.stderr.write.bind(process.stderr); |
| 34 | + |
| 35 | + (process as any).exit = ((nextCode?: number) => { |
| 36 | + throw new ExitSignal(nextCode ?? 0); |
| 37 | + }) as typeof process.exit; |
| 38 | + (process.stdout as any).write = ((chunk: unknown) => { |
| 39 | + stdout += String(chunk); |
| 40 | + return true; |
| 41 | + }) as typeof process.stdout.write; |
| 42 | + (process.stderr as any).write = ((chunk: unknown) => { |
| 43 | + stderr += String(chunk); |
| 44 | + return true; |
| 45 | + }) as typeof process.stderr.write; |
| 46 | + |
| 47 | + const sendToDaemon = async (req: Omit<DaemonRequest, 'token'>): Promise<DaemonResponse> => { |
| 48 | + calls.push(req); |
| 49 | + return await responder(req); |
| 50 | + }; |
| 51 | + |
| 52 | + try { |
| 53 | + await runCli(argv, { sendToDaemon }); |
| 54 | + } catch (error) { |
| 55 | + if (error instanceof ExitSignal) code = error.code; |
| 56 | + else throw error; |
| 57 | + } finally { |
| 58 | + process.exit = originalExit; |
| 59 | + process.stdout.write = originalStdoutWrite; |
| 60 | + process.stderr.write = originalStderrWrite; |
| 61 | + } |
| 62 | + |
| 63 | + return { code, stdout, stderr, calls }; |
| 64 | +} |
| 65 | + |
| 66 | +test('network dump prints parsed entries and metadata', async () => { |
| 67 | + const result = await runCliCapture(['network', 'dump', '10', 'all'], async () => ({ |
| 68 | + ok: true, |
| 69 | + data: { |
| 70 | + path: '/tmp/app.log', |
| 71 | + include: 'all', |
| 72 | + active: true, |
| 73 | + state: 'active', |
| 74 | + backend: 'android', |
| 75 | + scannedLines: 120, |
| 76 | + matchedLines: 2, |
| 77 | + entries: [ |
| 78 | + { |
| 79 | + timestamp: '2026-02-24T10:00:01Z', |
| 80 | + method: 'POST', |
| 81 | + url: 'https://api.example.com/v1/login', |
| 82 | + status: 401, |
| 83 | + headers: '{"x-id":"abc"}', |
| 84 | + requestBody: '{"email":"u@example.com"}', |
| 85 | + responseBody: '{"error":"denied"}', |
| 86 | + }, |
| 87 | + ], |
| 88 | + notes: ['best-effort parser'], |
| 89 | + }, |
| 90 | + })); |
| 91 | + |
| 92 | + assert.equal(result.code, null); |
| 93 | + assert.equal(result.calls.length, 1); |
| 94 | + assert.deepEqual(result.calls[0]?.positionals, ['dump', '10', 'all']); |
| 95 | + assert.match(result.stdout, /\/tmp\/app\.log/); |
| 96 | + assert.match(result.stdout, /POST https:\/\/api\.example\.com\/v1\/login status=401/); |
| 97 | + assert.match(result.stdout, /headers:/); |
| 98 | + assert.match(result.stdout, /request:/); |
| 99 | + assert.match(result.stdout, /response:/); |
| 100 | + assert.match(result.stderr, /active=true/); |
| 101 | + assert.match(result.stderr, /include=all/); |
| 102 | + assert.match(result.stderr, /matchedLines=2/); |
| 103 | + assert.match(result.stderr, /best-effort parser/); |
| 104 | +}); |
0 commit comments