60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / tui / spinner_test.go
8.8 KB
1package tui 2 3import ( 4 "strings" 5 "testing" 6 "time" 7 8 "github.com/charmbracelet/bubbles/spinner" 9 "github.com/charmbracelet/lipgloss" 10 "github.com/charmbracelet/x/exp/golden" 11) 12 13// TestSpinnerFrames tests that all spinner frames render without errors 14func TestSpinnerFrames(t *testing.T) { 15 defer setupGoldenTest(t)() 16 17 // Create spinner with hexagonal theme 18 sp := spinner.New() 19 sp.Spinner = spinner.Spinner{ 20 Frames: []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"}, 21 FPS: time.Second / 8, // 8 frames per second 22 } 23 sp.Style = lipgloss.NewStyle().Foreground(colorBrand) 24 25 tests := []struct { 26 name string 27 frame int 28 }{ 29 {"frame_0", 0}, 30 {"frame_1", 1}, 31 {"frame_2", 2}, 32 {"frame_3", 3}, 33 {"frame_4", 4}, 34 {"frame_5", 5}, 35 {"frame_6", 6}, 36 {"frame_7", 7}, 37 } 38 39 for _, tt := range tests { 40 t.Run(tt.name, func(t *testing.T) { 41 // Get the frame character 42 frame := sp.Spinner.Frames[tt.frame] 43 // Apply styling 44 output := sp.Style.Render(frame) 45 46 t.Run("ANSI", func(t *testing.T) { 47 golden.RequireEqual(t, []byte(output)) 48 }) 49 50 t.Run("Stripped", func(t *testing.T) { 51 stripped := stripANSIForGolden(output) 52 golden.RequireEqual(t, []byte(stripped)) 53 }) 54 }) 55 } 56} 57 58// TestSpinnerCycling tests frame cycling logic 59func TestSpinnerCycling(t *testing.T) { 60 sp := spinner.New() 61 sp.Spinner = spinner.Spinner{ 62 Frames: []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"}, 63 FPS: time.Second / 8, 64 } 65 sp.Style = lipgloss.NewStyle().Foreground(colorBrand) 66 67 // Test that frames cycle correctly 68 expectedFrames := []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"} 69 70 for i := 0; i < len(expectedFrames)*2; i++ { 71 expectedIdx := i % len(expectedFrames) 72 actualFrame := sp.Spinner.Frames[expectedIdx] 73 expectedFrame := expectedFrames[expectedIdx] 74 75 if actualFrame != expectedFrame { 76 t.Errorf("Frame %d: expected %q, got %q", i, expectedFrame, actualFrame) 77 } 78 } 79} 80 81// TestSpinnerFPS tests FPS timing configuration 82func TestSpinnerFPS(t *testing.T) { 83 sp := spinner.New() 84 sp.Spinner = spinner.Spinner{ 85 Frames: []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"}, 86 FPS: time.Second / 8, // 8 frames per second = 125ms per frame 87 } 88 89 expectedFPS := time.Second / 8 90 expectedDuration := 125 * time.Millisecond 91 92 if sp.Spinner.FPS != expectedFPS { 93 t.Errorf("FPS: expected %v, got %v", expectedFPS, sp.Spinner.FPS) 94 } 95 96 if sp.Spinner.FPS != expectedDuration { 97 t.Errorf("Frame duration: expected %v, got %v", expectedDuration, sp.Spinner.FPS) 98 } 99} 100 101// TestSpinnerFrameCount tests correct number of frames 102func TestSpinnerFrameCount(t *testing.T) { 103 sp := spinner.New() 104 sp.Spinner = spinner.Spinner{ 105 Frames: []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"}, 106 FPS: time.Second / 8, 107 } 108 109 expectedCount := 8 110 actualCount := len(sp.Spinner.Frames) 111 112 if actualCount != expectedCount { 113 t.Errorf("Frame count: expected %d, got %d", expectedCount, actualCount) 114 } 115} 116 117// TestSpinnerUnicodeCharacters tests that spinner uses correct Unicode characters 118func TestSpinnerUnicodeCharacters(t *testing.T) { 119 sp := spinner.New() 120 sp.Spinner = spinner.Spinner{ 121 Frames: []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"}, 122 FPS: time.Second / 8, 123 } 124 125 // Expected characters (hexagonal theme matching logo) 126 expectedChars := map[int]string{ 127 0: "⬡", // U+2B21 WHITE HEXAGON 128 1: "⬢", // U+2B22 BLACK HEXAGON 129 2: "⬡", // U+2B21 WHITE HEXAGON 130 3: "⬢", // U+2B22 BLACK HEXAGON 131 4: "⬦", // U+2B26 WHITE MEDIUM DIAMOND 132 5: "⬥", // U+2B25 BLACK MEDIUM DIAMOND 133 6: "⬦", // U+2B26 WHITE MEDIUM DIAMOND 134 7: "⬥", // U+2B25 BLACK MEDIUM DIAMOND 135 } 136 137 for idx, expectedChar := range expectedChars { 138 actualChar := sp.Spinner.Frames[idx] 139 if actualChar != expectedChar { 140 t.Errorf("Frame %d: expected %q (U+%04X), got %q", 141 idx, expectedChar, []rune(expectedChar)[0], actualChar) 142 } 143 } 144} 145 146// TestStateIndicators tests state indicator rendering for all states 147func TestStateIndicators(t *testing.T) { 148 defer setupGoldenTest(t)() 149 150 tests := []struct { 151 name string 152 state string 153 isLoading bool 154 spinnerView string 155 }{ 156 { 157 name: "running_with_spinner", 158 state: "running", 159 isLoading: false, 160 spinnerView: "⬡", 161 }, 162 { 163 name: "idle", 164 state: "idle", 165 isLoading: false, 166 spinnerView: "⬡", 167 }, 168 { 169 name: "stopped", 170 state: "stopped", 171 isLoading: false, 172 spinnerView: "⬡", 173 }, 174 { 175 name: "errored", 176 state: "errored", 177 isLoading: false, 178 spinnerView: "⬡", 179 }, 180 { 181 name: "loading", 182 state: "running", 183 isLoading: true, 184 spinnerView: "⬡", 185 }, 186 } 187 188 for _, tt := range tests { 189 t.Run(tt.name, func(t *testing.T) { 190 mysis := MysisInfo{ 191 ID: "test-id", 192 Name: "test-mysis", 193 State: tt.state, 194 Provider: "ollama-qwen", 195 AccountUsername: "test_user", 196 LastMessage: "Test message", 197 LastMessageAt: time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC), 198 CreatedAt: time.Date(2026, 1, 15, 9, 0, 0, 0, time.UTC), 199 } 200 201 lines := renderMysisLine(mysis, false, tt.isLoading, tt.spinnerView, 100, 0) 202 output := strings.Join(lines, "\n") 203 204 t.Run("ANSI", func(t *testing.T) { 205 golden.RequireEqual(t, []byte(output)) 206 }) 207 208 t.Run("Stripped", func(t *testing.T) { 209 stripped := stripANSIForGolden(output) 210 golden.RequireEqual(t, []byte(stripped)) 211 }) 212 }) 213 } 214} 215 216// TestStateIndicatorAlignment tests that spinner frames don't shift alignment 217func TestStateIndicatorAlignment(t *testing.T) { 218 defer setupGoldenTest(t)() 219 220 mysis := MysisInfo{ 221 ID: "test-id", 222 Name: "test-mysis", 223 State: "running", 224 Provider: "ollama-qwen", 225 AccountUsername: "test_user", 226 LastMessage: "Test message", 227 LastMessageAt: time.Date(2026, 1, 15, 10, 0, 0, 0, time.UTC), 228 CreatedAt: time.Date(2026, 1, 15, 9, 0, 0, 0, time.UTC), 229 } 230 231 spinnerFrames := []string{"⬡", "⬢", "⬡", "⬢", "⬦", "⬥", "⬦", "⬥"} 232 233 tests := []struct { 234 name string 235 frame string 236 }{ 237 {"spinner_frame_0", spinnerFrames[0]}, 238 {"spinner_frame_1", spinnerFrames[1]}, 239 {"spinner_frame_2", spinnerFrames[2]}, 240 {"spinner_frame_3", spinnerFrames[3]}, 241 {"spinner_frame_4", spinnerFrames[4]}, 242 {"spinner_frame_5", spinnerFrames[5]}, 243 {"spinner_frame_6", spinnerFrames[6]}, 244 {"spinner_frame_7", spinnerFrames[7]}, 245 } 246 247 // Store widths to verify consistency 248 var widths []int 249 var strippedWidths []int 250 251 for _, tt := range tests { 252 t.Run(tt.name, func(t *testing.T) { 253 lines := renderMysisLine(mysis, false, false, tt.frame, 100, 0) 254 output := strings.Join(lines, "\n") 255 stripped := stripANSIForGolden(output) 256 257 // Check width consistency (use first line for width since that's the info row) 258 width := lipgloss.Width(lines[0]) 259 strippedWidth := len(strings.TrimSpace(stripped)) 260 261 widths = append(widths, width) 262 strippedWidths = append(strippedWidths, strippedWidth) 263 264 t.Run("ANSI", func(t *testing.T) { 265 golden.RequireEqual(t, []byte(output)) 266 }) 267 268 t.Run("Stripped", func(t *testing.T) { 269 golden.RequireEqual(t, []byte(stripped)) 270 }) 271 }) 272 } 273 274 // Verify all widths are consistent across frames 275 if len(widths) > 0 { 276 firstWidth := widths[0] 277 for i, w := range widths { 278 if w != firstWidth { 279 t.Errorf("Frame %d width inconsistency: expected %d, got %d", i, firstWidth, w) 280 } 281 } 282 } 283 284 // Verify stripped widths are consistent 285 if len(strippedWidths) > 0 { 286 firstStrippedWidth := strippedWidths[0] 287 for i, w := range strippedWidths { 288 if w != firstStrippedWidth { 289 t.Errorf("Frame %d stripped width inconsistency: expected %d, got %d", i, firstStrippedWidth, w) 290 } 291 } 292 } 293} 294 295// TestSpinnerInDashboard tests spinner rendering in full dashboard context 296func TestSpinnerInDashboard(t *testing.T) { 297 defer setupGoldenTest(t)() 298 299 tests := []struct { 300 name string 301 spinnerView string 302 }{ 303 {"with_spinner_frame_0", "⬡"}, 304 {"with_spinner_frame_4", "⬦"}, 305 {"with_spinner_frame_7", "⬥"}, 306 } 307 308 for _, tt := range tests { 309 t.Run(tt.name, func(t *testing.T) { 310 myses := []MysisInfo{ 311 { 312 ID: "mysis-1", 313 Name: "alpha", 314 State: "running", 315 Provider: "ollama-qwen", 316 AccountUsername: "crab_warrior", 317 LastMessage: "Mining asteroid belt", 318 LastMessageAt: time.Date(2026, 1, 15, 10, 45, 0, 0, time.UTC), 319 CreatedAt: time.Date(2026, 1, 15, 10, 30, 0, 0, time.UTC), 320 }, 321 } 322 323 output := RenderDashboard(myses, []SwarmMessageInfo{}, 0, TestTerminalWidth, TestTerminalHeight, map[string]bool{}, tt.spinnerView, 0, nil) 324 325 t.Run("ANSI", func(t *testing.T) { 326 golden.RequireEqual(t, []byte(output)) 327 }) 328 329 t.Run("Stripped", func(t *testing.T) { 330 stripped := stripANSIForGolden(output) 331 golden.RequireEqual(t, []byte(stripped)) 332 }) 333 }) 334 } 335}