|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseClaudeStdoutLine } from "./parse-stdout.js"; |
| 3 | + |
| 4 | +const TS = "2026-07-15T20:21:29.280Z"; |
| 5 | + |
| 6 | +describe("parseClaudeStdoutLine", () => { |
| 7 | + it("renders assistant text blocks", () => { |
| 8 | + const line = JSON.stringify({ |
| 9 | + type: "assistant", |
| 10 | + message: { role: "assistant", content: [{ type: "text", text: "hello" }] }, |
| 11 | + }); |
| 12 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([{ kind: "assistant", ts: TS, text: "hello" }]); |
| 13 | + }); |
| 14 | + |
| 15 | + it("renders user tool_result blocks", () => { |
| 16 | + const line = JSON.stringify({ |
| 17 | + type: "user", |
| 18 | + message: { |
| 19 | + role: "user", |
| 20 | + content: [{ type: "tool_result", tool_use_id: "toolu_1", content: "ok", is_error: false }], |
| 21 | + }, |
| 22 | + }); |
| 23 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([ |
| 24 | + { kind: "tool_result", ts: TS, toolUseId: "toolu_1", content: "ok", isError: false }, |
| 25 | + ]); |
| 26 | + }); |
| 27 | + |
| 28 | + it("renders the system init frame", () => { |
| 29 | + const line = JSON.stringify({ |
| 30 | + type: "system", |
| 31 | + subtype: "init", |
| 32 | + model: "claude-sonnet-5", |
| 33 | + session_id: "c4a56dd0", |
| 34 | + }); |
| 35 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([ |
| 36 | + { kind: "init", ts: TS, model: "claude-sonnet-5", sessionId: "c4a56dd0" }, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + |
| 40 | + // Regression: these control frames used to fall through to `{ kind: "stdout" }` |
| 41 | + // and dump raw JSON between the nicely-rendered messages in "nice" mode. |
| 42 | + it("suppresses rate_limit_event frames", () => { |
| 43 | + const line = JSON.stringify({ |
| 44 | + type: "rate_limit_event", |
| 45 | + rate_limit_info: { |
| 46 | + status: "allowed", |
| 47 | + resetsAt: 1784153400, |
| 48 | + rateLimitType: "five_hour", |
| 49 | + overageStatus: "rejected", |
| 50 | + overageDisabledReason: "org_level_disabled", |
| 51 | + }, |
| 52 | + }); |
| 53 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("suppresses system thinking_tokens streaming deltas", () => { |
| 57 | + const line = JSON.stringify({ |
| 58 | + type: "system", |
| 59 | + subtype: "thinking_tokens", |
| 60 | + estimated_tokens: 50, |
| 61 | + estimated_tokens_delta: 50, |
| 62 | + uuid: "34603adf", |
| 63 | + session_id: "c4a56dd0", |
| 64 | + }); |
| 65 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("renders sub-task lifecycle frames as compact system notes, not raw JSON", () => { |
| 69 | + const started = parseClaudeStdoutLine( |
| 70 | + JSON.stringify({ |
| 71 | + type: "system", |
| 72 | + subtype: "task_started", |
| 73 | + task_id: "b8luq6h49", |
| 74 | + tool_use_id: "toolu_2", |
| 75 | + description: "Provision founding engineer", |
| 76 | + }), |
| 77 | + TS, |
| 78 | + ); |
| 79 | + expect(started).toEqual([ |
| 80 | + { kind: "system", ts: TS, text: "task started: Provision founding engineer" }, |
| 81 | + ]); |
| 82 | + |
| 83 | + const notified = parseClaudeStdoutLine( |
| 84 | + JSON.stringify({ type: "system", subtype: "task_notification", task_id: "b8luq6h49", status: "completed" }), |
| 85 | + TS, |
| 86 | + ); |
| 87 | + expect(notified).toEqual([{ kind: "system", ts: TS, text: "task completed" }]); |
| 88 | + }); |
| 89 | + |
| 90 | + it("renders other system subtypes as a compact note", () => { |
| 91 | + const line = JSON.stringify({ type: "system", subtype: "compact_boundary" }); |
| 92 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([{ kind: "system", ts: TS, text: "system: compact_boundary" }]); |
| 93 | + }); |
| 94 | + |
| 95 | + it("keeps non-JSON bridge lines as stdout", () => { |
| 96 | + const line = "[paperclip] Claude ACP default unavailable; falling back to Claude CLI."; |
| 97 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([{ kind: "stdout", ts: TS, text: line }]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("does not dump unrecognized JSON events as raw stdout", () => { |
| 101 | + const line = JSON.stringify({ type: "some_future_event", foo: 1 }); |
| 102 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([{ kind: "system", ts: TS, text: "some_future_event" }]); |
| 103 | + }); |
| 104 | + |
| 105 | + it("drops typeless JSON control objects", () => { |
| 106 | + const line = JSON.stringify({ status: "allowed", resetsAt: 1784153400 }); |
| 107 | + expect(parseClaudeStdoutLine(line, TS)).toEqual([]); |
| 108 | + }); |
| 109 | +}); |
0 commit comments