xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / reasoning_test.go

8.3 KB
1package tui
2
3import (
4	"strings"
5	"testing"
6	"time"
7
8	"github.com/charmbracelet/x/exp/golden"
9)
10
11// TestReasoningWithTimestamp verifies reasoning displays timestamp like other messages.
12func TestReasoningWithTimestamp(t *testing.T) {
13	defer setupGoldenTest(t)()
14
15	testCases := []struct {
16		name     string
17		entry    LogEntry
18		verbose  bool
19		maxWidth int
20		tick     int64
21	}{
22		{
23			name: "verbose_mode_with_timestamp",
24			entry: LogEntry{
25				Role:      "assistant",
26				Source:    "llm",
27				Content:   "I will mine the asteroid.",
28				Reasoning: "The asteroid contains valuable ore. Mining will increase our resources.",
29				Timestamp: time.Date(2026, 2, 6, 15, 30, 0, 0, time.UTC),
30			},
31			verbose:  true,
32			maxWidth: 100,
33			tick:     42337,
34		},
35		{
36			name: "non_verbose_mode_with_timestamp",
37			entry: LogEntry{
38				Role:    "assistant",
39				Source:  "llm",
40				Content: "I will travel to the mining station.",
41				Reasoning: strings.Join([]string{
42					"First line of reasoning",
43					"This is a very long middle section that should be truncated",
44					"More middle content",
45					"Even more middle content",
46					"Second to last line",
47					"Last line of reasoning",
48				}, "\n"),
49				Timestamp: time.Date(2026, 2, 6, 16, 45, 0, 0, time.UTC),
50			},
51			verbose:  false,
52			maxWidth: 100,
53			tick:     42500,
54		},
55		{
56			name: "short_reasoning_no_truncation",
57			entry: LogEntry{
58				Role:      "assistant",
59				Source:    "llm",
60				Content:   "Mining complete.",
61				Reasoning: "Short reasoning that won't be truncated.",
62				Timestamp: time.Date(2026, 2, 6, 17, 10, 0, 0, time.UTC),
63			},
64			verbose:  false,
65			maxWidth: 100,
66			tick:     42600,
67		},
68		{
69			name: "narrow_terminal",
70			entry: LogEntry{
71				Role:      "assistant",
72				Source:    "llm",
73				Content:   "Action.",
74				Reasoning: "This reasoning text is displayed in a narrow terminal and should wrap properly.",
75				Timestamp: time.Date(2026, 2, 6, 18, 0, 0, 0, time.UTC),
76			},
77			verbose:  true,
78			maxWidth: 60,
79			tick:     42700,
80		},
81		{
82			name: "zero_timestamp",
83			entry: LogEntry{
84				Role:      "assistant",
85				Source:    "llm",
86				Content:   "No timestamp.",
87				Reasoning: "This entry has no timestamp.",
88				Timestamp: time.Time{},
89			},
90			verbose:  true,
91			maxWidth: 100,
92			tick:     0,
93		},
94	}
95
96	for _, tc := range testCases {
97		t.Run(tc.name, func(t *testing.T) {
98			output := renderLogEntryImpl(tc.entry, tc.maxWidth, tc.verbose, tc.tick)
99			rendered := strings.Join(output, "\n")
100
101			t.Run("ANSI", func(t *testing.T) {
102				golden.RequireEqual(t, []byte(rendered))
103			})
104
105			t.Run("Stripped", func(t *testing.T) {
106				stripped := stripANSIForGolden(rendered)
107				golden.RequireEqual(t, []byte(stripped))
108			})
109		})
110	}
111}
112
113// TestReasoningTimestampFormat verifies timestamp format in reasoning matches main content.
114func TestReasoningTimestampFormat(t *testing.T) {
115	entry := LogEntry{
116		Role:      "assistant",
117		Source:    "llm",
118		Content:   "Test content",
119		Reasoning: "Test reasoning",
120		Timestamp: time.Date(2026, 2, 6, 15, 30, 0, 0, time.UTC),
121	}
122
123	output := renderLogEntryImpl(entry, 100, true, 42337)
124	rendered := strings.Join(output, "\n")
125	stripped := stripANSIForGolden(rendered)
126
127	// Check that reasoning line contains timestamp components
128	lines := strings.Split(stripped, "\n")
129	var reasoningLine string
130	for _, line := range lines {
131		if strings.Contains(line, "REASONING:") {
132			reasoningLine = line
133			break
134		}
135	}
136
137	if reasoningLine == "" {
138		t.Fatal("No REASONING line found in output")
139	}
140
141	// Verify timestamp components are present
142	if !strings.Contains(reasoningLine, "T42337") {
143		t.Errorf("REASONING line missing tick: %q", reasoningLine)
144	}
145	if !strings.Contains(reasoningLine, "[15:30]") {
146		t.Errorf("REASONING line missing time: %q", reasoningLine)
147	}
148	if !strings.Contains(reasoningLine, "⬡") {
149		t.Errorf("REASONING line missing hexagon separator: %q", reasoningLine)
150	}
151}
152
153// TestReasoningVerboseToggle verifies verbose mode affects reasoning display.
154func TestReasoningVerboseToggle(t *testing.T) {
155	entry := LogEntry{
156		Role:    "assistant",
157		Source:  "llm",
158		Content: "Test",
159		Reasoning: strings.Join([]string{
160			"Line 1",
161			"Line 2",
162			"Line 3",
163			"Line 4",
164			"Line 5",
165			"Line 6",
166		}, "\n"),
167		Timestamp: time.Date(2026, 2, 6, 12, 0, 0, 0, time.UTC),
168	}
169
170	// Verbose mode should show all lines
171	verboseOutput := renderLogEntryImpl(entry, 100, true, 42000)
172	verboseRendered := strings.Join(verboseOutput, "\n")
173	verboseStripped := stripANSIForGolden(verboseRendered)
174
175	// Non-verbose mode should truncate
176	nonVerboseOutput := renderLogEntryImpl(entry, 100, false, 42000)
177	nonVerboseRendered := strings.Join(nonVerboseOutput, "\n")
178	nonVerboseStripped := stripANSIForGolden(nonVerboseRendered)
179
180	// Verbose should be longer
181	if len(verboseStripped) <= len(nonVerboseStripped) {
182		t.Error("Verbose mode should produce longer output than non-verbose")
183	}
184
185	// Non-verbose should contain truncation indicator
186	if !strings.Contains(nonVerboseStripped, "[") || !strings.Contains(nonVerboseStripped, "more]") {
187		t.Error("Non-verbose mode should contain truncation indicator like '[3 more]'")
188	}
189
190	// Verbose should contain all lines
191	for i := 1; i <= 6; i++ {
192		lineText := "Line " + string(rune('0'+i))
193		if !strings.Contains(verboseStripped, lineText) {
194			t.Errorf("Verbose mode missing line: %s", lineText)
195		}
196	}
197}
198
199// TestReasoningContentLengths verifies reasoning handles various content lengths.
200func TestReasoningContentLengths(t *testing.T) {
201	defer setupGoldenTest(t)()
202
203	testCases := []struct {
204		name      string
205		reasoning string
206		verbose   bool
207	}{
208		{
209			name:      "empty_reasoning",
210			reasoning: "",
211			verbose:   true,
212		},
213		{
214			name:      "single_word",
215			reasoning: "Think.",
216			verbose:   true,
217		},
218		{
219			name:      "single_line",
220			reasoning: "This is a single line of reasoning.",
221			verbose:   true,
222		},
223		{
224			name: "three_lines_exact_threshold",
225			reasoning: strings.Join([]string{
226				"Line 1",
227				"Line 2",
228				"Line 3",
229			}, "\n"),
230			verbose: false,
231		},
232		{
233			name: "four_lines_triggers_truncation",
234			reasoning: strings.Join([]string{
235				"Line 1",
236				"Line 2",
237				"Line 3",
238				"Line 4",
239			}, "\n"),
240			verbose: false,
241		},
242		{
243			name: "very_long_reasoning",
244			reasoning: strings.Join([]string{
245				"First line of very long reasoning",
246				"Second line",
247				"Third line",
248				"Fourth line",
249				"Fifth line",
250				"Sixth line",
251				"Seventh line",
252				"Eighth line",
253				"Ninth line",
254				"Tenth and final line",
255			}, "\n"),
256			verbose: false,
257		},
258		{
259			name: "very_long_reasoning_verbose",
260			reasoning: strings.Join([]string{
261				"First line of very long reasoning in verbose mode",
262				"Second line",
263				"Third line",
264				"Fourth line",
265				"Fifth line",
266				"Sixth line",
267				"Seventh line",
268				"Eighth line",
269				"Ninth line",
270				"Tenth and final line",
271			}, "\n"),
272			verbose: true,
273		},
274	}
275
276	for _, tc := range testCases {
277		t.Run(tc.name, func(t *testing.T) {
278			entry := LogEntry{
279				Role:      "assistant",
280				Source:    "llm",
281				Content:   "Action taken",
282				Reasoning: tc.reasoning,
283				Timestamp: time.Date(2026, 2, 6, 14, 0, 0, 0, time.UTC),
284			}
285
286			output := renderLogEntryImpl(entry, 100, tc.verbose, 42200)
287			rendered := strings.Join(output, "\n")
288
289			t.Run("ANSI", func(t *testing.T) {
290				golden.RequireEqual(t, []byte(rendered))
291			})
292
293			t.Run("Stripped", func(t *testing.T) {
294				stripped := stripANSIForGolden(rendered)
295				golden.RequireEqual(t, []byte(stripped))
296			})
297		})
298	}
299}
300
301// TestReasoningNoTimestamp verifies reasoning with zero timestamp shows placeholder.
302func TestReasoningNoTimestamp(t *testing.T) {
303	entry := LogEntry{
304		Role:      "assistant",
305		Source:    "llm",
306		Content:   "Content",
307		Reasoning: "Reasoning text",
308		Timestamp: time.Time{}, // Zero timestamp
309	}
310
311	output := renderLogEntryImpl(entry, 100, true, 0)
312	rendered := strings.Join(output, "\n")
313	stripped := stripANSIForGolden(rendered)
314
315	// Find reasoning line
316	lines := strings.Split(stripped, "\n")
317	var reasoningLine string
318	for _, line := range lines {
319		if strings.Contains(line, "REASONING:") {
320			reasoningLine = line
321			break
322		}
323	}
324
325	if reasoningLine == "" {
326		t.Fatal("No REASONING line found in output")
327	}
328
329	// Should contain placeholder timestamp
330	if !strings.Contains(reasoningLine, "T0") {
331		t.Errorf("Expected T0 placeholder, got: %q", reasoningLine)
332	}
333	if !strings.Contains(reasoningLine, "[--:--]") {
334		t.Errorf("Expected [--:--] placeholder, got: %q", reasoningLine)
335	}
336}