xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / tool_calls_test.go

8.0 KB
1package tui
2
3import (
4	"testing"
5	"time"
6
7	"github.com/charmbracelet/x/exp/golden"
8	"github.com/xonecas/zoea-nova/internal/constants"
9)
10
11// TestToolCallsRendering tests the rendering of assistant messages containing [TOOL_CALLS].
12func TestToolCallsRendering(t *testing.T) {
13	defer setupGoldenTest(t)()
14
15	// Current tick for timestamp formatting
16	currentTick := int64(42000)
17
18	testCases := []struct {
19		name    string
20		content string
21		verbose bool
22		width   int
23	}{
24		{
25			name: "single_tool_call",
26			content: constants.ToolCallStoragePrefix +
27				"call_abc123:get_ship:{}",
28			verbose: false,
29			width:   120,
30		},
31		{
32			name: "multiple_tool_calls",
33			content: constants.ToolCallStoragePrefix +
34				"call_1:get_ship:{}|call_2:get_system:{\"system_id\":\"sol\"}|call_3:mine:{}",
35			verbose: false,
36			width:   120,
37		},
38		{
39			name: "tool_call_with_string_params",
40			content: constants.ToolCallStoragePrefix +
41				"call_xyz:travel:{\"destination\":\"sol_base\"}",
42			verbose: false,
43			width:   120,
44		},
45		{
46			name: "tool_call_with_number_params",
47			content: constants.ToolCallStoragePrefix +
48				"call_123:buy:{\"item_id\":42,\"quantity\":10,\"price\":150.5}",
49			verbose: false,
50			width:   120,
51		},
52		{
53			name: "tool_call_with_array_params",
54			content: constants.ToolCallStoragePrefix +
55				"call_arr:scan:{\"targets\":[\"ship_001\",\"ship_002\",\"ship_003\"]}",
56			verbose: false,
57			width:   120,
58		},
59		{
60			name: "tool_call_with_object_params",
61			content: constants.ToolCallStoragePrefix +
62				"call_obj:configure:{\"settings\":{\"speed\":\"fast\",\"mode\":\"aggressive\",\"target\":{\"x\":100,\"y\":200}}}",
63			verbose: false,
64			width:   120,
65		},
66		{
67			name: "long_tool_name",
68			content: constants.ToolCallStoragePrefix +
69				"call_long:zoea_search_messages:{\"mysis_id\":\"abc123\",\"query\":\"ore\",\"limit\":20}",
70			verbose: false,
71			width:   120,
72		},
73		{
74			name: "tool_call_verbose_mode",
75			content: constants.ToolCallStoragePrefix +
76				"call_v:get_poi:{\"poi_id\":\"asteroid_field_alpha\"}",
77			verbose: true,
78			width:   120,
79		},
80		{
81			name: "tool_call_narrow_width",
82			content: constants.ToolCallStoragePrefix +
83				"call_narrow:travel:{\"destination\":\"sol_base\"}",
84			verbose: false,
85			width:   80,
86		},
87		{
88			name: "tool_call_very_narrow_width",
89			content: constants.ToolCallStoragePrefix +
90				"call_vn:get_ship:{}",
91			verbose: false,
92			width:   60,
93		},
94		{
95			name: "empty_tool_arguments",
96			content: constants.ToolCallStoragePrefix +
97				"call_empty1:get_notifications:{}|call_empty2:get_status:{}",
98			verbose: false,
99			width:   120,
100		},
101		{
102			name: "complex_nested_json",
103			content: constants.ToolCallStoragePrefix +
104				"call_complex:execute:{\"plan\":{\"steps\":[{\"action\":\"travel\",\"target\":\"sol\"},{\"action\":\"mine\",\"resource\":\"iron\"}],\"priority\":\"high\"},\"timeout\":3600}",
105			verbose: false,
106			width:   120,
107		},
108	}
109
110	for _, tc := range testCases {
111		t.Run(tc.name, func(t *testing.T) {
112			entry := LogEntry{
113				Role:      "assistant",
114				Source:    "llm",
115				Content:   tc.content,
116				Reasoning: "",
117				Timestamp: time.Date(2026, 2, 6, 14, 30, 45, 0, time.UTC),
118			}
119
120			output := renderLogEntryImpl(entry, tc.width-6, tc.verbose, currentTick)
121			rendered := joinLines(output)
122
123			// ANSI variant
124			t.Run("ANSI", func(t *testing.T) {
125				golden.RequireEqual(t, []byte(rendered))
126			})
127
128			// Stripped variant
129			t.Run("Stripped", func(t *testing.T) {
130				stripped := stripANSIForGolden(rendered)
131				golden.RequireEqual(t, []byte(stripped))
132			})
133		})
134	}
135}
136
137// TestToolCallsWithReasoning tests tool calls combined with reasoning output.
138func TestToolCallsWithReasoning(t *testing.T) {
139	defer setupGoldenTest(t)()
140
141	currentTick := int64(42000)
142
143	testCases := []struct {
144		name      string
145		content   string
146		reasoning string
147		verbose   bool
148		width     int
149	}{
150		{
151			name: "single_tool_with_reasoning",
152			content: constants.ToolCallStoragePrefix +
153				"call_r1:get_ship:{}",
154			reasoning: "I need to check the ship's current status before deciding on the next action.",
155			verbose:   false,
156			width:     120,
157		},
158		{
159			name: "single_tool_with_reasoning_verbose",
160			content: constants.ToolCallStoragePrefix +
161				"call_r2:travel:{\"destination\":\"sol_base\"}",
162			reasoning: "The ship is currently at coordinates (150, 200) and needs to travel to sol_base to sell the ore. This will take approximately 5 ticks and consume fuel.",
163			verbose:   true,
164			width:     120,
165		},
166		{
167			name: "multiple_tools_with_long_reasoning",
168			content: constants.ToolCallStoragePrefix +
169				"call_m1:get_system:{}|call_m2:get_poi:{}|call_m3:mine:{}",
170			reasoning: "First, I'll check the current system to understand what resources are available. Then I'll get the point of interest data to find the best mining location. Finally, I'll execute the mining operation to gather resources. This is a strategic three-step approach that maximizes efficiency.",
171			verbose:   false,
172			width:     120,
173		},
174		{
175			name: "tool_with_multiline_reasoning",
176			content: constants.ToolCallStoragePrefix +
177				"call_ml:scan:{\"range\":1000}",
178			reasoning: "Step 1: Scan the area for potential threats\nStep 2: Identify resource-rich locations\nStep 3: Plan optimal mining route\nStep 4: Execute mining sequence",
179			verbose:   false,
180			width:     120,
181		},
182	}
183
184	for _, tc := range testCases {
185		t.Run(tc.name, func(t *testing.T) {
186			entry := LogEntry{
187				Role:      "assistant",
188				Source:    "llm",
189				Content:   tc.content,
190				Reasoning: tc.reasoning,
191				Timestamp: time.Date(2026, 2, 6, 14, 30, 45, 0, time.UTC),
192			}
193
194			output := renderLogEntryImpl(entry, tc.width-6, tc.verbose, currentTick)
195			rendered := joinLines(output)
196
197			// ANSI variant
198			t.Run("ANSI", func(t *testing.T) {
199				golden.RequireEqual(t, []byte(rendered))
200			})
201
202			// Stripped variant
203			t.Run("Stripped", func(t *testing.T) {
204				stripped := stripANSIForGolden(rendered)
205				golden.RequireEqual(t, []byte(stripped))
206			})
207		})
208	}
209}
210
211// TestToolCallsEdgeCases tests edge cases in tool call rendering.
212func TestToolCallsEdgeCases(t *testing.T) {
213	defer setupGoldenTest(t)()
214
215	currentTick := int64(42000)
216
217	testCases := []struct {
218		name    string
219		content string
220		verbose bool
221		width   int
222	}{
223		{
224			name:    "malformed_no_prefix",
225			content: "call_bad:get_ship:{}",
226			verbose: false,
227			width:   120,
228		},
229		{
230			name:    "empty_after_prefix",
231			content: constants.ToolCallStoragePrefix,
232			verbose: false,
233			width:   120,
234		},
235		{
236			name:    "single_field_incomplete",
237			content: constants.ToolCallStoragePrefix + "call_only",
238			verbose: false,
239			width:   120,
240		},
241		{
242			name:    "two_fields_incomplete",
243			content: constants.ToolCallStoragePrefix + "call_id:get_ship",
244			verbose: false,
245			width:   120,
246		},
247		{
248			name: "invalid_json_args",
249			content: constants.ToolCallStoragePrefix +
250				"call_invalid:get_ship:{not valid json}",
251			verbose: false,
252			width:   120,
253		},
254		{
255			name: "unicode_in_tool_args",
256			content: constants.ToolCallStoragePrefix +
257				"call_unicode:send_message:{\"message\":\"Hello 世界 🚀\"}",
258			verbose: false,
259			width:   120,
260		},
261		{
262			name: "very_long_tool_arguments",
263			content: constants.ToolCallStoragePrefix +
264				"call_long:execute:{\"data\":\"" + string(make([]byte, 500)) + "\"}",
265			verbose: false,
266			width:   120,
267		},
268	}
269
270	for _, tc := range testCases {
271		t.Run(tc.name, func(t *testing.T) {
272			entry := LogEntry{
273				Role:      "assistant",
274				Source:    "llm",
275				Content:   tc.content,
276				Reasoning: "",
277				Timestamp: time.Date(2026, 2, 6, 14, 30, 45, 0, time.UTC),
278			}
279
280			output := renderLogEntryImpl(entry, tc.width-6, tc.verbose, currentTick)
281			rendered := joinLines(output)
282
283			// ANSI variant
284			t.Run("ANSI", func(t *testing.T) {
285				golden.RequireEqual(t, []byte(rendered))
286			})
287
288			// Stripped variant
289			t.Run("Stripped", func(t *testing.T) {
290				stripped := stripANSIForGolden(rendered)
291				golden.RequireEqual(t, []byte(stripped))
292			})
293		})
294	}
295}
296
297// joinLines joins a slice of strings with newlines.
298func joinLines(lines []string) string {
299	result := ""
300	for i, line := range lines {
301		if i > 0 {
302			result += "\n"
303		}
304		result += line
305	}
306	return result
307}