@@ -1,537 +0,0 @@
1-package tui
2-
3-import (
4- "encoding/json"
5- "strings"
6- "testing"
7- "time"
8-
9- "github.com/charmbracelet/x/exp/golden"
10-)
11-
12-// TestJSONContentInToolResults tests tool results that contain JSON.
13-func TestJSONContentInToolResults(t *testing.T) {
14- defer setupGoldenTest(t)()
15-
16- tests := []struct {
17- name string
18- content string
19- verbose bool
20- }{
21- {
22- name: "simple_json_object",
23- content: `{"status": "success", "ore_collected": 42}`,
24- verbose: false,
25- },
26- {
27- name: "json_array",
28- content: `[{"id": 1, "type": "asteroid"}, {"id": 2, "type": "station"}]`,
29- verbose: false,
30- },
31- {
32- name: "nested_json",
33- content: `{"ship": {"id": "abc123", "cargo": {"ore": 10, "fuel": 50}}}`,
34- verbose: false,
35- },
36- {
37- name: "json_with_tool_call_prefix",
38- content: `call_abc123:{"status": "success", "message": "Mining complete"}`,
39- verbose: false,
40- },
41- }
42-
43- for _, tt := range tests {
44- t.Run(tt.name, func(t *testing.T) {
45- entry := LogEntry{
46- Role: "tool",
47- Source: "tool",
48- Content: tt.content,
49- Reasoning: "",
50- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
51- }
52-
53- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
54-
55- t.Run("ANSI", func(t *testing.T) {
56- golden.RequireEqual(t, []byte(output))
57- })
58-
59- t.Run("Stripped", func(t *testing.T) {
60- stripped := stripANSIForGolden(output)
61- golden.RequireEqual(t, []byte(stripped))
62- })
63- })
64- }
65-}
66-
67-// TestJSONContentWithNestedJSON tests content that contains JSON strings (double-encoding scenario).
68-func TestJSONContentWithNestedJSON(t *testing.T) {
69- defer setupGoldenTest(t)()
70-
71- tests := []struct {
72- name string
73- content string
74- verbose bool
75- }{
76- {
77- name: "json_with_escaped_json_string",
78- content: func() string {
79- // Create a JSON object with a field containing JSON as a string
80- inner := map[string]interface{}{"status": "ok"}
81- innerJSON, _ := json.Marshal(inner)
82- outer := map[string]interface{}{
83- "response": string(innerJSON),
84- "code": 200,
85- }
86- outerJSON, _ := json.Marshal(outer)
87- return string(outerJSON)
88- }(),
89- verbose: false,
90- },
91- {
92- name: "json_array_with_json_strings",
93- content: func() string {
94- // Array where each element is a JSON string
95- arr := []string{
96- `{"type": "error", "msg": "failed"}`,
97- `{"type": "success", "msg": "ok"}`,
98- }
99- arrJSON, _ := json.Marshal(arr)
100- return string(arrJSON)
101- }(),
102- verbose: false,
103- },
104- }
105-
106- for _, tt := range tests {
107- t.Run(tt.name, func(t *testing.T) {
108- entry := LogEntry{
109- Role: "tool",
110- Source: "tool",
111- Content: tt.content,
112- Reasoning: "",
113- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
114- }
115-
116- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
117-
118- t.Run("ANSI", func(t *testing.T) {
119- golden.RequireEqual(t, []byte(output))
120- })
121-
122- t.Run("Stripped", func(t *testing.T) {
123- stripped := stripANSIForGolden(output)
124- golden.RequireEqual(t, []byte(stripped))
125- })
126- })
127- }
128-}
129-
130-// TestJSONContentInAssistantMessages tests assistant messages that include JSON examples.
131-func TestJSONContentInAssistantMessages(t *testing.T) {
132- defer setupGoldenTest(t)()
133-
134- tests := []struct {
135- name string
136- role string
137- content string
138- verbose bool
139- }{
140- {
141- name: "assistant_with_json_in_text",
142- role: "assistant",
143- content: `I found the following data: {"ore": 42, "fuel": 50}. Should I proceed?`,
144- verbose: false,
145- },
146- {
147- name: "assistant_with_json_code_block",
148- role: "assistant",
149- content: "Here's the status:\n```json\n{\"status\": \"running\", \"health\": 100}\n```\nLooks good!",
150- verbose: false,
151- },
152- {
153- name: "user_with_json_example",
154- role: "user",
155- content: `Please parse this: {"command": "mine", "target": "asteroid_1"}`,
156- verbose: false,
157- },
158- }
159-
160- for _, tt := range tests {
161- t.Run(tt.name, func(t *testing.T) {
162- entry := LogEntry{
163- Role: tt.role,
164- Source: "direct",
165- Content: tt.content,
166- Reasoning: "",
167- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
168- }
169-
170- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
171-
172- t.Run("ANSI", func(t *testing.T) {
173- golden.RequireEqual(t, []byte(output))
174- })
175-
176- t.Run("Stripped", func(t *testing.T) {
177- stripped := stripANSIForGolden(output)
178- golden.RequireEqual(t, []byte(stripped))
179- })
180- })
181- }
182-}
183-
184-// TestJSONContentMixedWithText tests mixed content with JSON and text.
185-func TestJSONContentMixedWithText(t *testing.T) {
186- defer setupGoldenTest(t)()
187-
188- tests := []struct {
189- name string
190- content string
191- verbose bool
192- }{
193- {
194- name: "text_before_json",
195- content: `Mining successful! Result: {"ore": 10, "location": "asteroid_42"}`,
196- verbose: false,
197- },
198- {
199- name: "json_with_text_after",
200- content: `{"status": "complete"} - Operation finished successfully`,
201- verbose: false,
202- },
203- {
204- name: "multiple_json_snippets",
205- content: `First: {"a": 1} and second: {"b": 2} both valid`,
206- verbose: false,
207- },
208- }
209-
210- for _, tt := range tests {
211- t.Run(tt.name, func(t *testing.T) {
212- entry := LogEntry{
213- Role: "tool",
214- Source: "tool",
215- Content: tt.content,
216- Reasoning: "",
217- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
218- }
219-
220- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
221-
222- t.Run("ANSI", func(t *testing.T) {
223- golden.RequireEqual(t, []byte(output))
224- })
225-
226- t.Run("Stripped", func(t *testing.T) {
227- stripped := stripANSIForGolden(output)
228- golden.RequireEqual(t, []byte(stripped))
229- })
230- })
231- }
232-}
233-
234-// TestJSONContentMalformed tests malformed JSON in content.
235-func TestJSONContentMalformed(t *testing.T) {
236- defer setupGoldenTest(t)()
237-
238- tests := []struct {
239- name string
240- content string
241- verbose bool
242- }{
243- {
244- name: "incomplete_json_object",
245- content: `{"status": "running", "ore":`,
246- verbose: false,
247- },
248- {
249- name: "invalid_json_syntax",
250- content: `{invalid: json, missing: "quotes"}`,
251- verbose: false,
252- },
253- {
254- name: "json_with_trailing_comma",
255- content: `{"a": 1, "b": 2,}`,
256- verbose: false,
257- },
258- {
259- name: "looks_like_json_but_not",
260- content: `{this looks like json but isn't valid}`,
261- verbose: false,
262- },
263- }
264-
265- for _, tt := range tests {
266- t.Run(tt.name, func(t *testing.T) {
267- entry := LogEntry{
268- Role: "tool",
269- Source: "tool",
270- Content: tt.content,
271- Reasoning: "",
272- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
273- }
274-
275- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
276-
277- t.Run("ANSI", func(t *testing.T) {
278- golden.RequireEqual(t, []byte(output))
279- })
280-
281- t.Run("Stripped", func(t *testing.T) {
282- stripped := stripANSIForGolden(output)
283- golden.RequireEqual(t, []byte(stripped))
284- })
285- })
286- }
287-}
288-
289-// TestJSONContentVeryLarge tests very large JSON objects.
290-func TestJSONContentVeryLarge(t *testing.T) {
291- defer setupGoldenTest(t)()
292-
293- tests := []struct {
294- name string
295- content string
296- verbose bool
297- }{
298- {
299- name: "large_array_verbose_off",
300- content: func() string {
301- items := make([]map[string]interface{}, 100)
302- for i := 0; i < 100; i++ {
303- items[i] = map[string]interface{}{
304- "id": i,
305- "value": i * 100,
306- "name": "item_" + string(rune('A'+i%26)),
307- }
308- }
309- jsonBytes, _ := json.Marshal(items)
310- return string(jsonBytes)
311- }(),
312- verbose: false,
313- },
314- {
315- name: "large_array_verbose_on",
316- content: func() string {
317- items := make([]map[string]interface{}, 20)
318- for i := 0; i < 20; i++ {
319- items[i] = map[string]interface{}{
320- "id": i,
321- "value": i * 100,
322- }
323- }
324- jsonBytes, _ := json.Marshal(items)
325- return string(jsonBytes)
326- }(),
327- verbose: true,
328- },
329- {
330- name: "deeply_nested_json",
331- content: func() string {
332- // Create deeply nested structure: 10 levels deep
333- var data interface{} = "deepest"
334- for i := 0; i < 10; i++ {
335- data = map[string]interface{}{
336- "level": i,
337- "next": data,
338- }
339- }
340- jsonBytes, _ := json.Marshal(data)
341- return string(jsonBytes)
342- }(),
343- verbose: false,
344- },
345- {
346- name: "wide_object_many_fields",
347- content: func() string {
348- obj := make(map[string]interface{}, 50)
349- for i := 0; i < 50; i++ {
350- key := "field_" + string(rune('a'+i%26)) + string(rune('0'+i/26))
351- obj[key] = i * 10
352- }
353- jsonBytes, _ := json.Marshal(obj)
354- return string(jsonBytes)
355- }(),
356- verbose: false,
357- },
358- }
359-
360- for _, tt := range tests {
361- t.Run(tt.name, func(t *testing.T) {
362- entry := LogEntry{
363- Role: "tool",
364- Source: "tool",
365- Content: tt.content,
366- Reasoning: "",
367- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
368- }
369-
370- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
371-
372- t.Run("ANSI", func(t *testing.T) {
373- golden.RequireEqual(t, []byte(output))
374- })
375-
376- t.Run("Stripped", func(t *testing.T) {
377- stripped := stripANSIForGolden(output)
378- golden.RequireEqual(t, []byte(stripped))
379- })
380- })
381- }
382-}
383-
384-// TestJSONContentEdgeCases tests edge cases in JSON rendering.
385-func TestJSONContentEdgeCases(t *testing.T) {
386- defer setupGoldenTest(t)()
387-
388- tests := []struct {
389- name string
390- content string
391- verbose bool
392- }{
393- {
394- name: "empty_json_object",
395- content: `{}`,
396- verbose: false,
397- },
398- {
399- name: "empty_json_array",
400- content: `[]`,
401- verbose: false,
402- },
403- {
404- name: "json_with_null_values",
405- content: `{"a": null, "b": null, "c": 123}`,
406- verbose: false,
407- },
408- {
409- name: "json_with_boolean_values",
410- content: `{"enabled": true, "disabled": false, "count": 0}`,
411- verbose: false,
412- },
413- {
414- name: "json_with_very_long_string",
415- content: `{"description": "` + strings.Repeat("a", 500) + `"}`,
416- verbose: false,
417- },
418- {
419- name: "json_with_unicode_characters",
420- content: `{"emoji": "🚀⬡⬥", "text": "Hello 世界"}`,
421- verbose: false,
422- },
423- {
424- name: "json_single_value",
425- content: `"just a string"`,
426- verbose: false,
427- },
428- {
429- name: "json_single_number",
430- content: `42`,
431- verbose: false,
432- },
433- }
434-
435- for _, tt := range tests {
436- t.Run(tt.name, func(t *testing.T) {
437- entry := LogEntry{
438- Role: "tool",
439- Source: "tool",
440- Content: tt.content,
441- Reasoning: "",
442- Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
443- }
444-
445- output := strings.Join(renderLogEntryImpl(entry, 80, tt.verbose, 42000), "\n")
446-
447- t.Run("ANSI", func(t *testing.T) {
448- golden.RequireEqual(t, []byte(output))
449- })
450-
451- t.Run("Stripped", func(t *testing.T) {
452- stripped := stripANSIForGolden(output)
453- golden.RequireEqual(t, []byte(stripped))
454- })
455- })
456- }
457-}
458-
459-// TestDoubleParsingIssue specifically tests the double-parsing scenario.
460-func TestDoubleParsingIssue(t *testing.T) {
461- // This test demonstrates the potential double-parsing issue:
462- // When tool result content is already JSON, and we try to parse it again,
463- // what happens?
464-
465- t.Run("tool_result_already_parsed", func(t *testing.T) {
466- // Simulate what happens when content is JSON
467- jsonContent := `{"status": "success", "data": {"ore": 42}}`
468-
469- // First parse (what renderJSONTree does)
470- tree1, err1 := renderJSONTree(jsonContent, false, 80)
471- if err1 != nil {
472- t.Fatalf("First parse failed: %v", err1)
473- }
474-
475- // Second parse (if we accidentally parse the tree output)
476- _, err2 := renderJSONTree(tree1, false, 80)
477- if err2 == nil {
478- t.Error("Second parse should fail - tree output is not valid JSON")
479- }
480-
481- // Verify tree1 is NOT valid JSON
482- var dummy interface{}
483- if json.Unmarshal([]byte(tree1), &dummy) == nil {
484- t.Error("Tree output should not be valid JSON (contains Unicode box chars)")
485- }
486- })
487-
488- t.Run("tool_result_with_call_prefix", func(t *testing.T) {
489- // Test the tool call ID prefix stripping
490- content := `call_abc123:{"status": "success"}`
491-
492- // The isJSON function should detect this as JSON
493- if !isJSON(content) {
494- t.Error("isJSON should detect tool result with call prefix")
495- }
496-
497- // renderJSONTree does NOT strip the prefix - that's done in renderLogEntryImpl
498- // So calling it directly with the prefix should fail
499- _, err := renderJSONTree(content, false, 80)
500- if err == nil {
501- t.Error("renderJSONTree should fail on content with call prefix (prefix stripping is done in renderLogEntryImpl)")
502- }
503-
504- // The prefix is stripped in renderLogEntryImpl before calling renderJSONTree
505- // Let's verify the logic in focus.go lines 330-334 works
506- if idx := strings.Index(content, ":"); idx > 0 && strings.HasPrefix(content, "call_") {
507- jsonContent := content[idx+1:]
508- _, err2 := renderJSONTree(jsonContent, false, 80)
509- if err2 != nil {
510- t.Errorf("Stripped content should parse: %v", err2)
511- }
512- }
513- })
514-
515- t.Run("nested_json_string_not_double_parsed", func(t *testing.T) {
516- // Create JSON with a field containing JSON as a STRING (escaped)
517- inner := `{"nested": "value"}`
518- innerEscaped, _ := json.Marshal(inner) // This escapes it: "\"nested\": \"value\"}"
519- outer := `{"data": ` + string(innerEscaped) + `}`
520-
521- tree, err := renderJSONTree(outer, false, 80)
522- if err != nil {
523- t.Fatalf("Parse failed: %v", err)
524- }
525-
526- // The tree should show "data" field with an escaped JSON STRING value
527- // NOT a parsed nested object
528- if strings.Contains(tree, `├─"nested"`) {
529- t.Error("Nested JSON string should NOT be parsed as object - it's a string value")
530- }
531-
532- // It should show the escaped string
533- if !strings.Contains(tree, `"data"`) {
534- t.Error("Should show outer 'data' field")
535- }
536- })
537-}