xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / message_frame_test.go

10.4 KB
1package tui
2
3import (
4	"strings"
5	"testing"
6	"time"
7
8	"github.com/charmbracelet/x/exp/golden"
9)
10
11// TestMessageFrameLayout tests the conversation log message frame structure.
12// This test verifies that:
13// 1. Timestamp and message type label are on their own line
14// 2. Content starts on the next line (not on same line as timestamp)
15// 3. All message types render correctly with proper frame structure
16func TestMessageFrameLayout(t *testing.T) {
17	defer setupGoldenTest(t)()
18
19	// Fixed timestamp for deterministic output
20	fixedTime := time.Date(2026, 2, 6, 14, 30, 0, 0, time.UTC)
21	const currentTick = int64(42337)
22	const maxWidth = 100
23
24	tests := []struct {
25		name    string
26		entry   LogEntry
27		verbose bool
28	}{
29		// System message
30		{
31			name: "system_message_short",
32			entry: LogEntry{
33				Role:      "system",
34				Source:    "system",
35				Content:   "You are a helpful assistant.",
36				Timestamp: fixedTime,
37			},
38			verbose: false,
39		},
40		{
41			name: "system_message_long",
42			entry: LogEntry{
43				Role:      "system",
44				Source:    "system",
45				Content:   "You are a helpful assistant. Your goal is to help users navigate the SpaceMolt universe. Always check your status before taking actions. Use the captain's log for persistent memory.",
46				Timestamp: fixedTime,
47			},
48			verbose: false,
49		},
50		// User direct message
51		{
52			name: "user_direct_short",
53			entry: LogEntry{
54				Role:      "user",
55				Source:    "direct",
56				Content:   "Check your cargo",
57				Timestamp: fixedTime,
58			},
59			verbose: false,
60		},
61		{
62			name: "user_direct_long",
63			entry: LogEntry{
64				Role:      "user",
65				Source:    "direct",
66				Content:   "Travel to the nearest mining station and sell all your ore. After that, buy fuel and repair kits for the next mission. Make sure to check prices before purchasing.",
67				Timestamp: fixedTime,
68			},
69			verbose: false,
70		},
71		// User broadcast (self)
72		{
73			name: "user_broadcast_self_short",
74			entry: LogEntry{
75				Role:      "user",
76				Source:    "broadcast_self",
77				Content:   "All units: mine asteroids",
78				Timestamp: fixedTime,
79			},
80			verbose: false,
81		},
82		{
83			name: "user_broadcast_self_long",
84			entry: LogEntry{
85				Role:      "user",
86				Source:    "broadcast_self",
87				Content:   "All units: proceed to sector 7 and begin mining operations. Prioritize iron and copper ore. Report back when cargo is full or if you encounter any hostiles. Stay in formation and watch for pirates.",
88				Timestamp: fixedTime,
89			},
90			verbose: false,
91		},
92		// Swarm broadcast (from another mysis)
93		{
94			name: "swarm_broadcast_short",
95			entry: LogEntry{
96				Role:       "user",
97				Source:     "broadcast",
98				SenderID:   "mysis-123",
99				SenderName: "alpha",
100				Content:    "Enemy spotted in sector 5",
101				Timestamp:  fixedTime,
102			},
103			verbose: false,
104		},
105		{
106			name: "swarm_broadcast_long",
107			entry: LogEntry{
108				Role:       "user",
109				Source:     "broadcast",
110				SenderID:   "mysis-456",
111				SenderName: "beta-prime",
112				Content:    "I've discovered a rich asteroid field at coordinates [100, 200, 300]. High concentrations of rare ore detected. Recommend all mining units converge on this location. I'm claiming the northern quadrant for efficiency.",
113				Timestamp:  fixedTime,
114			},
115			verbose: false,
116		},
117		// Assistant message
118		{
119			name: "assistant_message_short",
120			entry: LogEntry{
121				Role:      "assistant",
122				Source:    "llm",
123				Content:   "Checking cargo now.",
124				Timestamp: fixedTime,
125			},
126			verbose: false,
127		},
128		{
129			name: "assistant_message_long",
130			entry: LogEntry{
131				Role:      "assistant",
132				Source:    "llm",
133				Content:   "I'll travel to the nearest mining station to sell the ore. First, let me check my current location and find the closest station with good ore prices. Then I'll plan the route and execute the travel command.",
134				Timestamp: fixedTime,
135			},
136			verbose: false,
137		},
138		// Assistant with reasoning (verbose off)
139		{
140			name: "assistant_with_reasoning_verbose_off",
141			entry: LogEntry{
142				Role:      "assistant",
143				Source:    "llm",
144				Content:   "I'll check my cargo status first.",
145				Reasoning: "The user asked me to check cargo. I need to verify what items I'm carrying and how much space is available. This will help me plan my next actions. I should use the get_cargo tool to retrieve this information. The tool requires no parameters and will return a list of items in my cargo hold along with capacity information.",
146				Timestamp: fixedTime,
147			},
148			verbose: false,
149		},
150		// Assistant with reasoning (verbose on)
151		{
152			name: "assistant_with_reasoning_verbose_on",
153			entry: LogEntry{
154				Role:      "assistant",
155				Source:    "llm",
156				Content:   "I'll check my cargo status first.",
157				Reasoning: "The user asked me to check cargo. I need to verify what items I'm carrying and how much space is available. This will help me plan my next actions. I should use the get_cargo tool to retrieve this information. The tool requires no parameters and will return a list of items in my cargo hold along with capacity information.",
158				Timestamp: fixedTime,
159			},
160			verbose: true,
161		},
162		// Tool call
163		{
164			name: "tool_call_short",
165			entry: LogEntry{
166				Role:      "tool",
167				Source:    "tool",
168				Content:   `call_abc123: {"status": "success"}`,
169				Timestamp: fixedTime,
170			},
171			verbose: false,
172		},
173		{
174			name: "tool_call_json_tree_verbose_off",
175			entry: LogEntry{
176				Role:      "tool",
177				Source:    "tool",
178				Content:   `call_xyz789: {"ship": {"name": "Crab Cruiser", "hull": 100, "fuel": 500, "cargo": {"capacity": 1000, "used": 250, "items": [{"name": "Iron Ore", "quantity": 100}, {"name": "Copper Ore", "quantity": 150}]}}}`,
179				Timestamp: fixedTime,
180			},
181			verbose: false,
182		},
183		{
184			name: "tool_call_json_tree_verbose_on",
185			entry: LogEntry{
186				Role:      "tool",
187				Source:    "tool",
188				Content:   `call_xyz789: {"ship": {"name": "Crab Cruiser", "hull": 100, "fuel": 500, "cargo": {"capacity": 1000, "used": 250, "items": [{"name": "Iron Ore", "quantity": 100}, {"name": "Copper Ore", "quantity": 150}]}}}`,
189				Timestamp: fixedTime,
190			},
191			verbose: true,
192		},
193	}
194
195	for _, tt := range tests {
196		t.Run(tt.name, func(t *testing.T) {
197			// Render the log entry
198			lines := renderLogEntryImpl(tt.entry, maxWidth, tt.verbose, currentTick)
199
200			// Join lines to create output
201			output := ""
202			for _, line := range lines {
203				output += line + "\n"
204			}
205
206			// Test ANSI variant (with color codes)
207			t.Run("ANSI", func(t *testing.T) {
208				golden.RequireEqual(t, []byte(output))
209			})
210
211			// Test Stripped variant (content only, no ANSI)
212			t.Run("Stripped", func(t *testing.T) {
213				stripped := stripANSIForGolden(output)
214				golden.RequireEqual(t, []byte(stripped))
215			})
216		})
217	}
218}
219
220// TestMessageFrameStructure tests the structural requirements of message frames.
221// This is a unit test (not golden) to explicitly verify layout rules.
222func TestMessageFrameStructure(t *testing.T) {
223	fixedTime := time.Date(2026, 2, 6, 14, 30, 0, 0, time.UTC)
224	const currentTick = int64(42337)
225	const maxWidth = 100
226
227	tests := []struct {
228		name  string
229		entry LogEntry
230	}{
231		{
232			name: "system_message",
233			entry: LogEntry{
234				Role:      "system",
235				Source:    "system",
236				Content:   "Test content",
237				Timestamp: fixedTime,
238			},
239		},
240		{
241			name: "user_message",
242			entry: LogEntry{
243				Role:      "user",
244				Source:    "direct",
245				Content:   "Test content",
246				Timestamp: fixedTime,
247			},
248		},
249		{
250			name: "assistant_message",
251			entry: LogEntry{
252				Role:      "assistant",
253				Source:    "llm",
254				Content:   "Test content",
255				Timestamp: fixedTime,
256			},
257		},
258		{
259			name: "tool_message",
260			entry: LogEntry{
261				Role:      "tool",
262				Source:    "tool",
263				Content:   "Test content",
264				Timestamp: fixedTime,
265			},
266		},
267	}
268
269	for _, tt := range tests {
270		t.Run(tt.name, func(t *testing.T) {
271			lines := renderLogEntryImpl(tt.entry, maxWidth, false, currentTick)
272
273			// Verify minimum structure
274			if len(lines) < 2 {
275				t.Fatalf("Expected at least 2 lines (timestamp line + content line), got %d", len(lines))
276			}
277
278			// Strip ANSI codes for content checking
279			stripped := make([]string, len(lines))
280			for i, line := range lines {
281				stripped[i] = stripANSIForGolden(line)
282			}
283
284			// Line 0 should be empty padding (may contain spaces for width)
285			trimmedLine0 := strings.TrimSpace(stripped[0])
286			if trimmedLine0 != "" {
287				t.Errorf("Line 0 should be empty padding (possibly with spaces), got: %q", stripped[0])
288			}
289
290			// Line 1 should contain timestamp, role prefix, and horizontal separator (no content)
291			// Expected format: " T42337 ⬡ [14:30] ROLE: ────────────────────"
292			// Content should start on line 2
293			if len(stripped) > 1 {
294				headerLine := stripped[1]
295
296				// Check that timestamp format is present
297				if !containsAny(headerLine, []string{"T42337", "[14:30]"}) {
298					t.Errorf("Line 1 should contain timestamp (T42337 or [14:30]), got: %q", headerLine)
299				}
300
301				// Check that role prefix is present
302				var expectedRole string
303				switch tt.entry.Role {
304				case "system":
305					expectedRole = "SYS:"
306				case "user":
307					expectedRole = "YOU:"
308				case "assistant":
309					expectedRole = "AI:"
310				case "tool":
311					expectedRole = "TOOL:"
312				}
313				if !containsAny(headerLine, []string{expectedRole}) {
314					t.Errorf("Line 1 should contain role prefix %q, got: %q", expectedRole, headerLine)
315				}
316
317				// Check that horizontal separator is present (indicates no content on this line)
318				if !containsAny(headerLine, []string{"─", "──", "───"}) {
319					t.Errorf("Line 1 should contain horizontal separator, got: %q", headerLine)
320				}
321
322				// Content should NOT be on this line (header line)
323				if containsAny(headerLine, []string{"Test content"}) {
324					t.Errorf("Line 1 should NOT contain message content (should be on line 2), got: %q", headerLine)
325				}
326			}
327
328			// Line 2 should contain the content
329			if len(stripped) > 2 {
330				contentLine := stripped[2]
331				// Tool messages show "✓ Success" for non-error results
332				expectedContent := []string{"Test content"}
333				if tt.entry.Role == "tool" {
334					expectedContent = []string{"Success", "✓"}
335				}
336				if !containsAny(contentLine, expectedContent) {
337					t.Errorf("Line 2 should contain message content, got: %q", contentLine)
338				}
339			}
340		})
341	}
342}
343
344// containsAny checks if s contains any of the substrings.
345func containsAny(s string, substrings []string) bool {
346	for _, sub := range substrings {
347		if len(sub) > 0 && len(s) >= len(sub) {
348			for i := 0; i <= len(s)-len(sub); i++ {
349				if s[i:i+len(sub)] == sub {
350					return true
351				}
352			}
353		}
354	}
355	return false
356}