| 1 | package stream |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestFormatLineSystem(t *testing.T) { |
| 10 | line := `{"type":"system","subtype":"init","model":"claude-sonnet-4-5-20250929"}` |
| 11 | var buf bytes.Buffer |
| 12 | FormatLine(line, &buf) |
| 13 | |
| 14 | want := "── session started (model: claude-sonnet-4-5-20250929) ──\n" |
| 15 | if buf.String() != want { |
| 16 | t.Errorf("got %q, want %q", buf.String(), want) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestFormatLineSystemNoModel(t *testing.T) { |
| 21 | line := `{"type":"system","subtype":"init"}` |
| 22 | var buf bytes.Buffer |
| 23 | FormatLine(line, &buf) |
| 24 | |
| 25 | want := "── session started (model: unknown) ──\n" |
| 26 | if buf.String() != want { |
| 27 | t.Errorf("got %q, want %q", buf.String(), want) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestFormatLineAssistantText(t *testing.T) { |
| 32 | line := `{"type":"assistant","message":{"content":[{"type":"text","text":"Hello world"}]}}` |
| 33 | var buf bytes.Buffer |
| 34 | FormatLine(line, &buf) |
| 35 | |
| 36 | want := "Hello world\n" |
| 37 | if buf.String() != want { |
| 38 | t.Errorf("got %q, want %q", buf.String(), want) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestFormatLineAssistantToolUse(t *testing.T) { |
| 43 | tests := []struct { |
| 44 | name string |
| 45 | line string |
| 46 | want string |
| 47 | }{ |
| 48 | { |
| 49 | name: "Read", |
| 50 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Read","input":{"file_path":"/foo/bar.go"}}]}}`, |
| 51 | want: "▶ Read /foo/bar.go\n", |
| 52 | }, |
| 53 | { |
| 54 | name: "Edit", |
| 55 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Edit","input":{"file_path":"/foo/bar.go"}}]}}`, |
| 56 | want: "▶ Edit /foo/bar.go\n", |
| 57 | }, |
| 58 | { |
| 59 | name: "Write", |
| 60 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Write","input":{"file_path":"/foo/bar.go"}}]}}`, |
| 61 | want: "▶ Write /foo/bar.go\n", |
| 62 | }, |
| 63 | { |
| 64 | name: "Bash", |
| 65 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Bash","input":{"command":"git status"}}]}}`, |
| 66 | want: "▶ Bash: git status\n", |
| 67 | }, |
| 68 | { |
| 69 | name: "Glob", |
| 70 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Glob","input":{"pattern":"**/*.go"}}]}}`, |
| 71 | want: "▶ Glob **/*.go\n", |
| 72 | }, |
| 73 | { |
| 74 | name: "Grep", |
| 75 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"Grep","input":{"pattern":"TODO"}}]}}`, |
| 76 | want: "▶ Grep TODO\n", |
| 77 | }, |
| 78 | { |
| 79 | name: "unknown tool", |
| 80 | line: `{"type":"assistant","message":{"content":[{"type":"tool_use","name":"CustomTool","input":{}}]}}`, |
| 81 | want: "▶ CustomTool\n", |
| 82 | }, |
| 83 | } |
| 84 | |
| 85 | for _, tt := range tests { |
| 86 | t.Run(tt.name, func(t *testing.T) { |
| 87 | var buf bytes.Buffer |
| 88 | FormatLine(tt.line, &buf) |
| 89 | if buf.String() != tt.want { |
| 90 | t.Errorf("got %q, want %q", buf.String(), tt.want) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestFormatLineResult(t *testing.T) { |
| 97 | line := `{"type":"result","total_cost_usd":3.42,"duration_ms":45000}` |
| 98 | var buf bytes.Buffer |
| 99 | FormatLine(line, &buf) |
| 100 | |
| 101 | want := "\n── done (45.0s, $3.4200) ──\n" |
| 102 | if buf.String() != want { |
| 103 | t.Errorf("got %q, want %q", buf.String(), want) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestFormatLineInvalidJSON(t *testing.T) { |
| 108 | var buf bytes.Buffer |
| 109 | FormatLine("not json", &buf) |
| 110 | if buf.String() != "" { |
| 111 | t.Errorf("invalid JSON should produce no output, got %q", buf.String()) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestFormatStream(t *testing.T) { |
| 116 | input := strings.Join([]string{ |
| 117 | `{"type":"system","subtype":"init","model":"test-model"}`, |
| 118 | `{"type":"assistant","message":{"content":[{"type":"text","text":"Working on it..."}]}}`, |
| 119 | `{"type":"result","total_cost_usd":1.5,"duration_ms":10000}`, |
| 120 | }, "\n") |
| 121 | |
| 122 | var buf bytes.Buffer |
| 123 | err := FormatStream(strings.NewReader(input), &buf) |
| 124 | if err != nil { |
| 125 | t.Fatalf("FormatStream() error: %v", err) |
| 126 | } |
| 127 | |
| 128 | output := buf.String() |
| 129 | if !strings.Contains(output, "session started") { |
| 130 | t.Error("output should contain session started") |
| 131 | } |
| 132 | if !strings.Contains(output, "Working on it...") { |
| 133 | t.Error("output should contain assistant text") |
| 134 | } |
| 135 | if !strings.Contains(output, "done") { |
| 136 | t.Error("output should contain done") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestFormatStreamEmptyLines(t *testing.T) { |
| 141 | input := "\n\n" + `{"type":"system","subtype":"init","model":"m"}` + "\n\n" |
| 142 | var buf bytes.Buffer |
| 143 | err := FormatStream(strings.NewReader(input), &buf) |
| 144 | if err != nil { |
| 145 | t.Fatalf("FormatStream() error: %v", err) |
| 146 | } |
| 147 | if !strings.Contains(buf.String(), "session started") { |
| 148 | t.Error("should handle empty lines gracefully") |
| 149 | } |
| 150 | } |