xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / tui / golden_test.go

18.3 KB
1package tui
2
3import (
4	"strings"
5	"testing"
6	"time"
7
8	"github.com/charmbracelet/bubbles/viewport"
9	"github.com/charmbracelet/x/exp/golden"
10	"github.com/xonecas/zoea-nova/internal/store"
11)
12
13// TestDashboard tests dashboard rendering with various states
14func TestDashboard(t *testing.T) {
15	defer setupGoldenTest(t)()
16
17	// Use fixed timestamps for deterministic golden files.
18	tests := []struct {
19		name        string
20		myses       []MysisInfo
21		swarmMsgs   []SwarmMessageInfo
22		selectedIdx int
23		width       int
24		height      int
25		loadingSet  map[string]bool
26	}{
27		{
28			name:        "empty_swarm",
29			myses:       []MysisInfo{},
30			swarmMsgs:   []SwarmMessageInfo{},
31			selectedIdx: 0,
32			width:       TestTerminalWidth,
33			height:      TestTerminalHeight,
34			loadingSet:  map[string]bool{},
35		},
36		{
37			name: "with_swarm_messages",
38			myses: []MysisInfo{
39				{
40					ID:              "mysis-1",
41					Name:            "alpha",
42					State:           "running",
43					Provider:        "ollama-qwen",
44					AccountUsername: "crab_warrior",
45					LastMessage:     "Mining asteroid belt",
46					LastMessageAt:   time.Date(2026, 1, 15, 10, 45, 0, 0, time.UTC),
47					CreatedAt:       time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC),
48				},
49				{
50					ID:              "mysis-2",
51					Name:            "beta",
52					State:           "idle",
53					Provider:        "opencode_zen",
54					AccountUsername: "crab_trader",
55					LastMessage:     "Waiting for orders",
56					LastMessageAt:   time.Date(2026, 1, 15, 10, 46, 0, 0, time.UTC),
57					CreatedAt:       time.Date(2026, 1, 15, 10, 35, 0, 0, time.UTC),
58				},
59			},
60			swarmMsgs: []SwarmMessageInfo{
61				{SenderID: "mysis-1", SenderName: "alpha", Content: "All units: proceed to sector 7", CreatedAt: time.Date(2026, 1, 15, 11, 0, 0, 0, time.UTC)},
62				{SenderID: "mysis-2", SenderName: "beta", Content: "Target rich environment detected", CreatedAt: time.Date(2026, 1, 15, 11, 5, 0, 0, time.UTC)},
63			},
64			selectedIdx: 0,
65			width:       TestTerminalWidth,
66			height:      TestTerminalHeight,
67			loadingSet:  map[string]bool{},
68		},
69		{
70			name: "with_multiline_broadcast",
71			myses: []MysisInfo{
72				{
73					ID:              "mysis-3",
74					Name:            "gamma",
75					State:           "idle",
76					Provider:        "ollama-qwen",
77					AccountUsername: "crab_cartographer",
78					LastMessage:     "Standing by",
79					LastMessageAt:   time.Date(2026, 1, 15, 12, 0, 0, 0, time.UTC),
80					CreatedAt:       time.Date(2026, 1, 15, 11, 55, 0, 0, time.UTC),
81				},
82			},
83			swarmMsgs: []SwarmMessageInfo{
84				{SenderID: "mysis-3", SenderName: "gamma", Content: "Line one\nLine two", CreatedAt: time.Date(2026, 1, 15, 12, 1, 0, 0, time.UTC)},
85			},
86			selectedIdx: 0,
87			width:       TestTerminalWidth,
88			height:      TestTerminalHeight,
89			loadingSet:  map[string]bool{},
90		},
91		{
92			name: "with_loading_mysis",
93			myses: []MysisInfo{
94				{
95					ID:              "mysis-4",
96					Name:            "delta",
97					State:           "running",
98					Provider:        "ollama-qwen",
99					AccountUsername: "crab_runner",
100					LastMessage:     "Processing",
101					LastMessageAt:   time.Date(2026, 1, 15, 12, 5, 0, 0, time.UTC),
102					CreatedAt:       time.Date(2026, 1, 15, 11, 50, 0, 0, time.UTC),
103				},
104			},
105			swarmMsgs:   []SwarmMessageInfo{},
106			selectedIdx: 0,
107			width:       TestTerminalWidth,
108			height:      TestTerminalHeight,
109			loadingSet:  map[string]bool{"mysis-4": true},
110		},
111		{
112			name: "narrow_terminal",
113			myses: []MysisInfo{
114				{
115					ID:              "mysis-5",
116					Name:            "epsilon",
117					State:           "idle",
118					Provider:        "ollama-qwen",
119					AccountUsername: "crab_navigator",
120					LastMessage:     "Holding position",
121					LastMessageAt:   time.Date(2026, 1, 15, 12, 10, 0, 0, time.UTC),
122					CreatedAt:       time.Date(2026, 1, 15, 12, 5, 0, 0, time.UTC),
123				},
124			},
125			swarmMsgs:   []SwarmMessageInfo{},
126			selectedIdx: 0,
127			width:       80,
128			height:      24,
129			loadingSet:  map[string]bool{},
130		},
131		{
132			name: "wide_terminal",
133			myses: []MysisInfo{
134				{
135					ID:              "mysis-6",
136					Name:            "zeta",
137					State:           "running",
138					Provider:        "opencode_zen",
139					AccountUsername: "crab_scout",
140					LastMessage:     "Surveying sector",
141					LastMessageAt:   time.Date(2026, 1, 15, 12, 20, 0, 0, time.UTC),
142					CreatedAt:       time.Date(2026, 1, 15, 12, 15, 0, 0, time.UTC),
143				},
144			},
145			swarmMsgs:   []SwarmMessageInfo{},
146			selectedIdx: 0,
147			width:       200,
148			height:      60,
149			loadingSet:  map[string]bool{},
150		},
151	}
152
153	for _, tt := range tests {
154		t.Run(tt.name, func(t *testing.T) {
155			output := RenderDashboard(tt.myses, tt.swarmMsgs, tt.selectedIdx, tt.width, tt.height, tt.loadingSet, "⠋", 0, nil)
156
157			t.Run("ANSI", func(t *testing.T) {
158				golden.RequireEqual(t, []byte(output))
159			})
160
161			t.Run("Stripped", func(t *testing.T) {
162				stripped := stripANSIForGolden(output)
163				golden.RequireEqual(t, []byte(stripped))
164			})
165		})
166	}
167}
168
169// TestFocusView tests focus view rendering with various log entries
170func TestFocusView(t *testing.T) {
171	defer setupGoldenTest(t)()
172
173	tests := []struct {
174		name   string
175		mysis  MysisInfo
176		logs   []LogEntry
177		width  int
178		height int
179	}{
180		{
181			name: "with_all_roles",
182			mysis: MysisInfo{
183				ID:              "test-mysis",
184				Name:            "alpha",
185				State:           "running",
186				Provider:        "ollama-qwen",
187				AccountUsername: "crab_explorer",
188				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
189			},
190			logs: []LogEntry{
191				{
192					Role:      "system",
193					Source:    "system",
194					Content:   "You are a space exploration AI controlling a ship in the Crustacean Cosmos.",
195					Timestamp: time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
196				},
197				{
198					Role:      "user",
199					Source:    "direct",
200					Content:   "What is your current status?",
201					Timestamp: time.Date(2026, 1, 15, 10, 1, 0, 0, time.UTC),
202				},
203				{
204					Role:      "assistant",
205					Source:    "llm",
206					Content:   "I am currently orbiting planet Crabulous Prime. All systems nominal.",
207					Reasoning: "The ship sensors show stable orbit. Fuel at 75%. Cargo hold empty.",
208					Timestamp: time.Date(2026, 1, 15, 10, 1, 30, 0, time.UTC),
209				},
210				{
211					Role:      "tool",
212					Source:    "tool",
213					Content:   `{"name": "alpha", "fuel": 75}`,
214					Timestamp: time.Date(2026, 1, 15, 10, 1, 35, 0, time.UTC),
215				},
216			},
217			width:  TestTerminalWidth,
218			height: TestTerminalHeight,
219		},
220		{
221			name: "with_scrollbar",
222			mysis: MysisInfo{
223				ID:        "test-mysis",
224				Name:      "beta",
225				State:     "running",
226				Provider:  "ollama-qwen",
227				CreatedAt: time.Date(2026, 1, 15, 9, 30, 0, 0, time.UTC),
228			},
229			logs: func() []LogEntry {
230				// Create enough logs to trigger scrollbar
231				logs := make([]LogEntry, 30)
232				for i := 0; i < 30; i++ {
233					logs[i] = LogEntry{
234						Role:      "user",
235						Source:    "direct",
236						Content:   "Test message line " + string(rune('A'+i)),
237						Timestamp: time.Date(2026, 1, 15, 10, i, 0, 0, time.UTC),
238					}
239				}
240				return logs
241			}(),
242			width:  TestTerminalWidth,
243			height: TestTerminalHeight,
244		},
245		{
246			name: "narrow_terminal",
247			mysis: MysisInfo{
248				ID:              "test-mysis",
249				Name:            "gamma",
250				State:           "idle",
251				Provider:        "opencode_zen",
252				AccountUsername: "crab_surveyor",
253				CreatedAt:       time.Date(2026, 1, 15, 9, 45, 0, 0, time.UTC),
254			},
255			logs: []LogEntry{
256				{
257					Role:      "assistant",
258					Source:    "llm",
259					Content:   "Short response for narrow view.",
260					Timestamp: time.Date(2026, 1, 15, 9, 46, 0, 0, time.UTC),
261				},
262			},
263			width:  80,
264			height: 24,
265		},
266	}
267
268	for _, tt := range tests {
269		t.Run(tt.name, func(t *testing.T) {
270			output := RenderFocusView(tt.mysis, tt.logs, tt.width, tt.height, false, "⠋", false, 1, 1, 0, nil)
271
272			t.Run("ANSI", func(t *testing.T) {
273				golden.RequireEqual(t, []byte(output))
274			})
275
276			t.Run("Stripped", func(t *testing.T) {
277				stripped := stripANSIForGolden(output)
278				golden.RequireEqual(t, []byte(stripped))
279			})
280		})
281	}
282}
283
284// TestHelp tests help screen rendering
285func TestHelp(t *testing.T) {
286	defer setupGoldenTest(t)()
287
288	tests := []struct {
289		name   string
290		width  int
291		height int
292	}{
293		{
294			name:   "content",
295			width:  TestTerminalWidth,
296			height: TestTerminalHeight,
297		},
298	}
299
300	for _, tt := range tests {
301		t.Run(tt.name, func(t *testing.T) {
302			output := RenderHelp(tt.width, tt.height)
303
304			t.Run("ANSI", func(t *testing.T) {
305				golden.RequireEqual(t, []byte(output))
306			})
307
308			t.Run("Stripped", func(t *testing.T) {
309				stripped := stripANSIForGolden(output)
310				golden.RequireEqual(t, []byte(stripped))
311			})
312		})
313	}
314}
315
316// TestLogEntry tests individual log entry rendering
317func TestLogEntry(t *testing.T) {
318	defer setupGoldenTest(t)()
319
320	tests := []struct {
321		name     string
322		entry    LogEntry
323		maxWidth int
324		verbose  bool
325	}{
326		{
327			name: "with_reasoning",
328			entry: LogEntry{
329				Role:      "assistant",
330				Source:    "llm",
331				Content:   "I will mine the asteroid.",
332				Reasoning: "The asteroid contains valuable ore. I have enough fuel and cargo space.",
333				Timestamp: time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC),
334			},
335			maxWidth: 80,
336			verbose:  false,
337		},
338		{
339			name: "with_reasoning_truncation",
340			entry: LogEntry{
341				Role:    "assistant",
342				Source:  "llm",
343				Content: "I will proceed with the plan.",
344				Reasoning: "First line of reasoning that explains the initial thought process. " +
345					"Second line continues with more detailed analysis of the situation. " +
346					"Third line adds even more context about the decision. " +
347					"Fourth line provides additional justification. " +
348					"Fifth line continues the explanation with further details. " +
349					"Sixth line adds more reasoning depth. " +
350					"Seventh line provides even more context. " +
351					"Eighth line concludes the reasoning with final thoughts.",
352				Timestamp: time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC),
353			},
354			maxWidth: 80,
355			verbose:  false,
356		},
357		{
358			name: "tool_with_json",
359			entry: LogEntry{
360				Role:      "tool",
361				Source:    "tool",
362				Content:   `{"name": "mysis-1", "state": "running", "fuel": 100}`,
363				Timestamp: time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC),
364			},
365			maxWidth: 80,
366			verbose:  false,
367		},
368		{
369			name: "tool_with_prefixed_json",
370			entry: LogEntry{
371				Role:      "tool",
372				Source:    "tool",
373				Content:   `call_59orhh05:{"total": 1, "max": 16, "status": "ok"}`,
374				Timestamp: time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC),
375			},
376			maxWidth: 80,
377			verbose:  false,
378		},
379	}
380
381	for _, tt := range tests {
382		t.Run(tt.name, func(t *testing.T) {
383			lines := renderLogEntryImpl(tt.entry, tt.maxWidth, tt.verbose, 0)
384			output := strings.Join(lines, "\n")
385
386			t.Run("ANSI", func(t *testing.T) {
387				golden.RequireEqual(t, []byte(output))
388			})
389
390			t.Run("Stripped", func(t *testing.T) {
391				stripped := stripANSIForGolden(output)
392				golden.RequireEqual(t, []byte(stripped))
393			})
394		})
395	}
396}
397
398// TestFocusViewWithViewport tests focus view with viewport for scrollbar rendering
399func TestFocusViewWithViewport(t *testing.T) {
400	defer setupGoldenTest(t)()
401
402	tests := []struct {
403		name       string
404		mysis      MysisInfo
405		logs       []LogEntry
406		width      int
407		height     int
408		totalLines int
409	}{
410		{
411			name: "with_scrollbar",
412			mysis: MysisInfo{
413				ID:              "test-id",
414				Name:            "test-mysis",
415				State:           "running",
416				Provider:        "ollama-qwen",
417				AccountUsername: "crab_pilot",
418				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
419			},
420			logs: func() []LogEntry {
421				logs := make([]LogEntry, 50)
422				for i := 0; i < 50; i++ {
423					logs[i] = LogEntry{
424						Role:      "user",
425						Source:    "direct",
426						Content:   "Test line " + string(rune('0'+i%10)),
427						Timestamp: time.Date(2026, 1, 15, 10, i%60, 0, 0, time.UTC),
428					}
429				}
430				return logs
431			}(),
432			width:      TestTerminalWidth,
433			height:     20,
434			totalLines: 50,
435		},
436		{
437			name: "with_scroll_indicator",
438			mysis: MysisInfo{
439				ID:              "test-id",
440				Name:            "test-mysis",
441				State:           "running",
442				Provider:        "ollama-qwen",
443				AccountUsername: "crab_pilot",
444				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
445			},
446			logs: func() []LogEntry {
447				logs := make([]LogEntry, 30)
448				for i := 0; i < 30; i++ {
449					logs[i] = LogEntry{
450						Role:      "assistant",
451						Source:    "llm",
452						Content:   "Focus log line " + string(rune('A'+i)),
453						Timestamp: time.Date(2026, 1, 15, 10, i%60, 0, 0, time.UTC),
454					}
455				}
456				return logs
457			}(),
458			width:      TestTerminalWidth,
459			height:     20,
460			totalLines: 30,
461		},
462	}
463
464	for _, tt := range tests {
465		t.Run(tt.name, func(t *testing.T) {
466			// Create viewport
467			vp := viewport.New(tt.width-4, 10)
468			var contentLines []string
469			for _, log := range tt.logs {
470				lines := renderLogEntryImpl(log, tt.width-4, false, 0)
471				contentLines = append(contentLines, lines...)
472			}
473			vp.SetContent(strings.Join(contentLines, "\n"))
474			vp.GotoTop()
475
476			output := RenderFocusViewWithViewport(tt.mysis, vp, tt.width, false, "⠋", false, tt.totalLines, 1, 1, 0, nil, 0, nil)
477
478			t.Run("ANSI", func(t *testing.T) {
479				golden.RequireEqual(t, []byte(output))
480			})
481
482			t.Run("Stripped", func(t *testing.T) {
483				stripped := stripANSIForGolden(output)
484				golden.RequireEqual(t, []byte(stripped))
485			})
486		})
487	}
488}
489
490// TestJSONTree tests JSON tree rendering
491// TestScrollbar tests scrollbar rendering
492func TestScrollbar(t *testing.T) {
493	defer setupGoldenTest(t)()
494
495	tests := []struct {
496		name         string
497		height       int
498		totalLines   int
499		scrollOffset int
500	}{
501		{
502			name:         "at_top",
503			height:       10,
504			totalLines:   100,
505			scrollOffset: 0,
506		},
507		{
508			name:         "at_bottom",
509			height:       10,
510			totalLines:   100,
511			scrollOffset: 90,
512		},
513		{
514			name:         "middle",
515			height:       10,
516			totalLines:   100,
517			scrollOffset: 45,
518		},
519		{
520			name:         "no_scroll",
521			height:       10,
522			totalLines:   5,
523			scrollOffset: 0,
524		},
525	}
526
527	for _, tt := range tests {
528		t.Run(tt.name, func(t *testing.T) {
529			output := renderScrollbar(tt.height, tt.totalLines, tt.scrollOffset)
530
531			t.Run("ANSI", func(t *testing.T) {
532				golden.RequireEqual(t, []byte(output))
533			})
534
535			t.Run("Stripped", func(t *testing.T) {
536				stripped := stripANSIForGolden(output)
537				golden.RequireEqual(t, []byte(stripped))
538			})
539		})
540	}
541}
542
543// TestMysisLine tests mysis line rendering with account information
544func TestMysisLine(t *testing.T) {
545	defer setupGoldenTest(t)()
546
547	tests := []struct {
548		name    string
549		mysis   MysisInfo
550		width   int
551		loading bool
552	}{
553		{
554			name: "with_account",
555			mysis: MysisInfo{
556				ID:              "abc123",
557				Name:            "test-mysis",
558				State:           "running",
559				Provider:        "ollama-qwen",
560				AccountUsername: "crab_miner",
561				LastMessage:     "Mining asteroid",
562				LastMessageAt:   time.Date(2026, 1, 15, 10, 5, 0, 0, time.UTC),
563				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
564			},
565			width: 100,
566		},
567		{
568			name: "without_account",
569			mysis: MysisInfo{
570				ID:              "abc123",
571				Name:            "test-mysis",
572				State:           "idle",
573				Provider:        "ollama-qwen",
574				AccountUsername: "",
575				LastMessage:     "",
576				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
577			},
578			width: 100,
579		},
580		{
581			name: "long_name",
582			mysis: MysisInfo{
583				ID:              "abc123",
584				Name:            "mysis-with-a-very-long-name",
585				State:           "running",
586				Provider:        "ollama-qwen",
587				AccountUsername: "crab_miner",
588				LastMessage:     "Holding",
589				LastMessageAt:   time.Date(2026, 1, 15, 10, 10, 0, 0, time.UTC),
590				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
591			},
592			width: 100,
593		},
594		{
595			name: "errored_state",
596			mysis: MysisInfo{
597				ID:              "abc123",
598				Name:            "test-mysis",
599				State:           "errored",
600				Provider:        "ollama-qwen",
601				AccountUsername: "crab_miner",
602				LastMessage:     "",
603				LastError:       "connection lost",
604				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
605			},
606			width: 100,
607		},
608		{
609			name: "loading_state",
610			mysis: MysisInfo{
611				ID:              "abc123",
612				Name:            "test-mysis",
613				State:           "running",
614				Provider:        "ollama-qwen",
615				AccountUsername: "crab_miner",
616				LastMessage:     "Processing",
617				LastMessageAt:   time.Date(2026, 1, 15, 10, 15, 0, 0, time.UTC),
618				CreatedAt:       time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC),
619			},
620			width:   100,
621			loading: true,
622		},
623	}
624
625	for _, tt := range tests {
626		t.Run(tt.name, func(t *testing.T) {
627			lines := renderMysisLine(tt.mysis, false, tt.loading, "⬡", tt.width, 0)
628			output := strings.Join(lines, "\n")
629
630			t.Run("ANSI", func(t *testing.T) {
631				golden.RequireEqual(t, []byte(output))
632			})
633
634			t.Run("Stripped", func(t *testing.T) {
635				stripped := stripANSIForGolden(output)
636				golden.RequireEqual(t, []byte(stripped))
637			})
638		})
639	}
640}
641
642// TestBroadcastLabels tests broadcast message sender labels
643func TestBroadcastLabels(t *testing.T) {
644	defer setupGoldenTest(t)()
645
646	currentMysisID := "current-mysis"
647
648	tests := []struct {
649		name       string
650		memory     *store.Memory
651		senderName string
652	}{
653		{
654			name: "direct_message",
655			memory: &store.Memory{
656				Role:    store.MemoryRoleUser,
657				Source:  store.MemorySourceDirect,
658				Content: "Direct command to this mysis",
659			},
660		},
661		{
662			name: "broadcast_from_self",
663			memory: &store.Memory{
664				Role:     store.MemoryRoleUser,
665				Source:   store.MemorySourceBroadcast,
666				SenderID: currentMysisID,
667				Content:  "My broadcast to the swarm",
668			},
669			senderName: "alpha",
670		},
671		{
672			name: "broadcast_from_other",
673			memory: &store.Memory{
674				Role:     store.MemoryRoleUser,
675				Source:   store.MemorySourceBroadcast,
676				SenderID: "other-mysis",
677				Content:  "Another mysis's broadcast",
678			},
679			senderName: "beta",
680		},
681		{
682			name: "broadcast_legacy_no_sender",
683			memory: &store.Memory{
684				Role:     store.MemoryRoleUser,
685				Source:   store.MemorySourceBroadcast,
686				SenderID: "",
687				Content:  "Legacy broadcast without sender ID",
688			},
689		},
690	}
691
692	for _, tt := range tests {
693		t.Run(tt.name, func(t *testing.T) {
694			entry := LogEntryFromMemory(tt.memory, currentMysisID, tt.senderName)
695			lines := renderLogEntryImpl(entry, 80, false, 0)
696			output := strings.Join(lines, "\n")
697
698			t.Run("ANSI", func(t *testing.T) {
699				golden.RequireEqual(t, []byte(output))
700			})
701
702			t.Run("Stripped", func(t *testing.T) {
703				stripped := stripANSIForGolden(output)
704				golden.RequireEqual(t, []byte(stripped))
705			})
706		})
707	}
708}