xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / focus_test.go

7.5 KB
1package tui
2
3import (
4	"strings"
5	"testing"
6	"time"
7
8	"github.com/charmbracelet/bubbles/viewport"
9	"github.com/charmbracelet/lipgloss"
10	"github.com/muesli/termenv"
11	"github.com/xonecas/zoea-nova/internal/store"
12)
13
14func TestLogEntryFromMemoryWithSender(t *testing.T) {
15	currentMysisID := "current-mysis"
16	otherMysisID := "other-mysis"
17
18	tests := []struct {
19		name       string
20		memory     *store.Memory
21		currentID  string
22		wantPrefix string
23		senderName string
24	}{
25		{
26			name: "direct message",
27			memory: &store.Memory{
28				Role:    store.MemoryRoleUser,
29				Source:  store.MemorySourceDirect,
30				Content: "direct msg",
31			},
32			currentID:  currentMysisID,
33			wantPrefix: "YOU:",
34		},
35		{
36			name: "broadcast from self",
37			memory: &store.Memory{
38				Role:     store.MemoryRoleUser,
39				Source:   store.MemorySourceBroadcast,
40				SenderID: currentMysisID,
41				Content:  "my broadcast",
42			},
43			currentID:  currentMysisID,
44			wantPrefix: "YOU (BROADCAST):",
45			senderName: "alpha",
46		},
47		{
48			name: "broadcast from other",
49			memory: &store.Memory{
50				Role:     store.MemoryRoleUser,
51				Source:   store.MemorySourceBroadcast,
52				SenderID: otherMysisID,
53				Content:  "other's broadcast",
54			},
55			currentID:  currentMysisID,
56			wantPrefix: "SWARM:",
57			senderName: "beta",
58		},
59		{
60			name: "broadcast legacy (no sender)",
61			memory: &store.Memory{
62				Role:     store.MemoryRoleUser,
63				Source:   store.MemorySourceBroadcast,
64				SenderID: "",
65				Content:  "legacy broadcast",
66			},
67			currentID:  currentMysisID,
68			wantPrefix: "SWARM:",
69		},
70	}
71
72	for _, tt := range tests {
73		t.Run(tt.name, func(t *testing.T) {
74			entry := LogEntryFromMemory(tt.memory, tt.currentID, tt.senderName)
75			if entry.Role != string(tt.memory.Role) {
76				t.Errorf("role: got %q, want %q", entry.Role, tt.memory.Role)
77			}
78			if entry.SenderID != tt.memory.SenderID {
79				t.Errorf("sender_id: got %q, want %q", entry.SenderID, tt.memory.SenderID)
80			}
81			if entry.SenderName != tt.senderName {
82				t.Errorf("sender_name: got %q, want %q", entry.SenderName, tt.senderName)
83			}
84		})
85	}
86}
87
88func TestRenderLogEntryWithReasoning(t *testing.T) {
89	// Force color output for testing
90	lipgloss.SetColorProfile(termenv.TrueColor)
91	defer lipgloss.SetColorProfile(termenv.Ascii)
92
93	entry := LogEntry{
94		Role:      "assistant",
95		Source:    "llm",
96		Content:   "I will mine the asteroid.",
97		Reasoning: "The asteroid contains valuable ore. I have enough fuel and cargo space.",
98		Timestamp: time.Now(),
99	}
100
101	maxWidth := 80
102	lines := renderLogEntryImpl(entry, maxWidth, false, 0)
103
104	// Should have content lines + reasoning section
105	if len(lines) < 4 {
106		t.Errorf("Expected at least 4 lines (padding + content + reasoning header + reasoning), got %d", len(lines))
107	}
108
109	// Join lines and check for reasoning content
110	output := strings.Join(lines, "\n")
111	if !strings.Contains(output, "REASONING:") {
112		t.Error("Expected reasoning header 'REASONING:' in output")
113	}
114	if !strings.Contains(output, "The asteroid contains valuable ore") {
115		t.Error("Expected reasoning content in output")
116	}
117
118	// Verify ANSI codes are present (styling is applied)
119	if !strings.Contains(output, "\x1b[") {
120		t.Error("Expected ANSI escape codes (styling) in output")
121	}
122}
123
124func TestRenderLogEntryWithReasoningTruncation(t *testing.T) {
125	// Force color output for testing
126	lipgloss.SetColorProfile(termenv.TrueColor)
127	defer lipgloss.SetColorProfile(termenv.Ascii)
128
129	// Create reasoning with multiple lines that will wrap
130	longReasoning := "First line of reasoning that explains the initial thought process. " +
131		"Second line continues with more detailed analysis of the situation. " +
132		"Third line adds even more context about the decision. " +
133		"Fourth line provides additional justification. " +
134		"Fifth line concludes the reasoning with final thoughts."
135
136	entry := LogEntry{
137		Role:      "assistant",
138		Source:    "llm",
139		Content:   "I will proceed with the plan.",
140		Reasoning: longReasoning,
141		Timestamp: time.Now(),
142	}
143
144	maxWidth := 80
145
146	// Test with verbose=false (should truncate)
147	linesTruncated := renderLogEntryImpl(entry, maxWidth, false, 0)
148	outputTruncated := strings.Join(linesTruncated, "\n")
149
150	// Should contain truncation indicator
151	if !strings.Contains(outputTruncated, "more]") {
152		t.Error("Expected truncation indicator '[x more]' in non-verbose output")
153	}
154
155	// Should contain first line
156	if !strings.Contains(outputTruncated, "First line") {
157		t.Error("Expected first line of reasoning in truncated output")
158	}
159
160	// Should contain last lines
161	if !strings.Contains(outputTruncated, "final thoughts") {
162		t.Error("Expected last line of reasoning in truncated output")
163	}
164
165	// Test with verbose=true (should show all)
166	linesVerbose := renderLogEntryImpl(entry, maxWidth, true, 0)
167	outputVerbose := strings.Join(linesVerbose, "\n")
168
169	// Should NOT contain truncation indicator
170	if strings.Contains(outputVerbose, "more]") {
171		t.Error("Should not show truncation indicator in verbose mode")
172	}
173
174	// Should contain all parts of reasoning
175	if !strings.Contains(outputVerbose, "First line") {
176		t.Error("Expected first line in verbose output")
177	}
178	if !strings.Contains(outputVerbose, "Third line") {
179		t.Error("Expected middle lines in verbose output")
180	}
181	if !strings.Contains(outputVerbose, "final thoughts") {
182		t.Error("Expected last line in verbose output")
183	}
184}
185
186func TestRenderLogEntryToolWithJSON(t *testing.T) {
187	lipgloss.SetColorProfile(termenv.TrueColor)
188	defer lipgloss.SetColorProfile(termenv.Ascii)
189
190	jsonPayload := `{"ship_id": "abc123", "cargo": [{"item": "iron", "quantity": 50}, {"item": "gold", "quantity": 10}], "fuel": 100}`
191
192	entry := LogEntry{
193		Role:      "tool",
194		Source:    "tool",
195		Content:   jsonPayload,
196		Timestamp: time.Now(),
197	}
198
199	maxWidth := 80
200	lines := renderLogEntryImpl(entry, maxWidth, false, 0)
201
202	output := strings.Join(lines, "\n")
203
204	// Tool results now show "✓ Success" for non-error JSON
205	if !strings.Contains(output, "Success") {
206		t.Error("Expected 'Success' in tool result output")
207	}
208}
209
210func TestRenderLogEntryToolWithPrefixedJSON(t *testing.T) {
211	lipgloss.SetColorProfile(termenv.TrueColor)
212	defer lipgloss.SetColorProfile(termenv.Ascii)
213
214	// Tool result with call ID prefix (format from database)
215	toolResult := `call_59orhh05:{"max_myses": 16, "states": {"errored": 0, "idle": 0, "running": 1, "stopped": 0}, "total_myses": 1}`
216
217	entry := LogEntry{
218		Role:      "tool",
219		Source:    "tool",
220		Content:   toolResult,
221		Timestamp: time.Now(),
222	}
223
224	maxWidth := 80
225	lines := renderLogEntryImpl(entry, maxWidth, false, 0)
226
227	output := strings.Join(lines, "\n")
228
229	// Tool results now show "✓ Success" for non-error JSON
230	if !strings.Contains(output, "Success") {
231		t.Error("Expected 'Success' in tool result output")
232	}
233
234	// Should NOT contain the call ID prefix
235	if strings.Contains(output, "call_59orhh05:") {
236		t.Error("Tool call ID prefix should be stripped")
237	}
238}
239func TestRenderFocusViewWithScrollbar(t *testing.T) {
240	lipgloss.SetColorProfile(termenv.TrueColor)
241	defer lipgloss.SetColorProfile(termenv.Ascii)
242
243	// Create viewport with content
244	vp := viewport.New(80, 10)
245	longContent := strings.Repeat("line\n", 50) // 50 lines
246	vp.SetContent(longContent)
247	vp.GotoTop()
248
249	mysis := MysisInfo{
250		ID:       "test-id",
251		Name:     "test-mysis",
252		State:    "running",
253		Provider: "ollama-qwen",
254	}
255
256	width := 100
257	totalLines := 50
258	output := RenderFocusViewWithViewport(mysis, vp, width, false, "⬡", false, totalLines, 1, 1, 0, nil, 0, nil)
259
260	// Should contain scrollbar characters
261	if !strings.Contains(output, "█") && !strings.Contains(output, "│") {
262		t.Error("Expected scrollbar characters in focus view output")
263	}
264}