60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / tui / statusbar_test.go
11.9 KB
1package tui 2 3import ( 4 "strings" 5 "testing" 6 "time" 7 8 "github.com/charmbracelet/lipgloss" 9 "github.com/charmbracelet/x/exp/golden" 10) 11 12// TestStatusBar tests status bar rendering with various configurations. 13func TestStatusBar(t *testing.T) { 14 defer setupGoldenTest(t)() 15 16 tests := []struct { 17 name string 18 view View 19 focusID string 20 myses []MysisInfo 21 netActivity NetActivity 22 netPosition int 23 providerErrs []time.Time 24 width int 25 }{ 26 { 27 name: "dashboard_no_myses", 28 view: ViewDashboard, 29 myses: []MysisInfo{}, 30 netActivity: NetActivityIdle, 31 width: 120, 32 }, 33 { 34 name: "dashboard_single_running", 35 view: ViewDashboard, 36 myses: []MysisInfo{ 37 {ID: "test-1", Name: "alpha", State: "running"}, 38 }, 39 netActivity: NetActivityLLM, 40 netPosition: 6, // Middle of progress bar 41 width: 120, 42 }, 43 { 44 name: "dashboard_mixed_states", 45 view: ViewDashboard, 46 myses: []MysisInfo{ 47 {ID: "test-1", Name: "alpha", State: "running"}, 48 {ID: "test-2", Name: "beta", State: "idle"}, 49 {ID: "test-3", Name: "gamma", State: "running"}, 50 }, 51 netActivity: NetActivityLLM, 52 netPosition: 0, // Start of progress bar 53 width: 120, 54 }, 55 { 56 name: "dashboard_full_swarm", 57 view: ViewDashboard, 58 myses: makeFullSwarm16(), 59 netActivity: NetActivityLLM, 60 netPosition: 11, // End of progress bar 61 width: 120, 62 }, 63 { 64 name: "focus_short_id", 65 view: ViewFocus, 66 focusID: "abc123", 67 myses: []MysisInfo{ 68 {ID: "abc123", Name: "alpha", State: "running"}, 69 }, 70 netActivity: NetActivityLLM, 71 netPosition: 6, 72 width: 120, 73 }, 74 { 75 name: "focus_long_id", 76 view: ViewFocus, 77 focusID: "6b152b72-09e4-4695-aaa2-9a529147d3d7", 78 myses: []MysisInfo{ 79 {ID: "6b152b72-09e4-4695-aaa2-9a529147d3d7", Name: "alpha", State: "running"}, 80 }, 81 netActivity: NetActivityLLM, 82 netPosition: 6, 83 width: 120, 84 }, 85 { 86 name: "llm_progress_0_percent", 87 view: ViewDashboard, 88 myses: []MysisInfo{ 89 {ID: "test-1", Name: "alpha", State: "running"}, 90 }, 91 netActivity: NetActivityLLM, 92 netPosition: 0, 93 width: 120, 94 }, 95 { 96 name: "llm_progress_50_percent", 97 view: ViewDashboard, 98 myses: []MysisInfo{ 99 {ID: "test-1", Name: "alpha", State: "running"}, 100 }, 101 netActivity: NetActivityLLM, 102 netPosition: 6, 103 width: 120, 104 }, 105 { 106 name: "llm_progress_100_percent", 107 view: ViewDashboard, 108 myses: []MysisInfo{ 109 {ID: "test-1", Name: "alpha", State: "running"}, 110 }, 111 netActivity: NetActivityLLM, 112 netPosition: 11, 113 width: 120, 114 }, 115 { 116 name: "with_provider_errors", 117 view: ViewDashboard, 118 myses: []MysisInfo{ 119 {ID: "test-1", Name: "alpha", State: "running"}, 120 }, 121 netActivity: NetActivityLLM, 122 netPosition: 6, 123 providerErrs: []time.Time{ 124 time.Now(), 125 time.Now(), 126 time.Now(), 127 }, 128 width: 120, 129 }, 130 { 131 name: "narrow_width_80", 132 view: ViewDashboard, 133 myses: []MysisInfo{ 134 {ID: "test-1", Name: "alpha", State: "running"}, 135 }, 136 netActivity: NetActivityLLM, 137 netPosition: 6, 138 width: 80, 139 }, 140 { 141 name: "wide_width_160", 142 view: ViewDashboard, 143 myses: []MysisInfo{ 144 {ID: "test-1", Name: "alpha", State: "running"}, 145 {ID: "test-2", Name: "beta", State: "idle"}, 146 {ID: "test-3", Name: "gamma", State: "running"}, 147 }, 148 netActivity: NetActivityLLM, 149 netPosition: 6, 150 width: 160, 151 }, 152 { 153 name: "idle_activity", 154 view: ViewDashboard, 155 myses: []MysisInfo{}, 156 netActivity: NetActivityIdle, 157 netPosition: 0, 158 width: 120, 159 }, 160 { 161 name: "mcp_activity", 162 view: ViewDashboard, 163 myses: []MysisInfo{ 164 {ID: "test-1", Name: "alpha", State: "running"}, 165 }, 166 netActivity: NetActivityMCP, 167 netPosition: 6, 168 width: 120, 169 }, 170 } 171 172 for _, tt := range tests { 173 t.Run(tt.name, func(t *testing.T) { 174 m, cleanup := setupTestModel(t) 175 defer cleanup() 176 177 m.view = tt.view 178 m.focusID = tt.focusID 179 m.myses = tt.myses 180 m.width = tt.width 181 m.providerErrorTimes = tt.providerErrs 182 183 // Set network indicator state 184 m.netIndicator.activity = tt.netActivity 185 m.netIndicator.position = tt.netPosition 186 187 // Render status bar 188 output := m.renderStatusBar() 189 190 // Test ANSI output 191 t.Run("ANSI", func(t *testing.T) { 192 golden.RequireEqual(t, []byte(output)) 193 }) 194 195 // Test stripped output 196 t.Run("Stripped", func(t *testing.T) { 197 stripped := stripANSIForGolden(output) 198 golden.RequireEqual(t, []byte(stripped)) 199 }) 200 }) 201 } 202} 203 204// TestStatusBarWidthHandling tests width calculation and truncation. 205func TestStatusBarWidthHandling(t *testing.T) { 206 tests := []struct { 207 name string 208 width int 209 mysisID string 210 wantWidth int 211 }{ 212 { 213 name: "width_80", 214 width: 80, 215 mysisID: "short", 216 wantWidth: 80, 217 }, 218 { 219 name: "width_120", 220 width: 120, 221 mysisID: "medium-length", 222 wantWidth: 120, 223 }, 224 { 225 name: "width_160", 226 width: 160, 227 mysisID: "6b152b72-09e4-4695-aaa2-9a529147d3d7", 228 wantWidth: 160, 229 }, 230 } 231 232 for _, tt := range tests { 233 t.Run(tt.name, func(t *testing.T) { 234 m, cleanup := setupTestModel(t) 235 defer cleanup() 236 237 m.view = ViewFocus 238 m.focusID = tt.mysisID 239 m.width = tt.width 240 m.myses = []MysisInfo{ 241 {ID: tt.mysisID, Name: "test", State: "running"}, 242 } 243 244 output := m.renderStatusBar() 245 246 // Strip ANSI and check display width 247 stripped := stripANSIForGolden(output) 248 249 // Check that output doesn't overflow 250 if len(stripped) > tt.wantWidth*2 { // *2 for Unicode safety 251 t.Errorf("Status bar may overflow: stripped length %d exceeds %d*2", len(stripped), tt.wantWidth) 252 } 253 }) 254 } 255} 256 257// TestStatusBarMysisCount tests mysis state count display logic. 258func TestStatusBarMysisCount(t *testing.T) { 259 tests := []struct { 260 name string 261 myses []MysisInfo 262 wantContains string // Check that output contains this substring 263 }{ 264 { 265 name: "zero_myses", 266 myses: []MysisInfo{}, 267 wantContains: "(no myses)", 268 }, 269 { 270 name: "one_running", 271 myses: []MysisInfo{ 272 {ID: "1", State: "running"}, 273 }, 274 wantContains: "1", // Should show "⬡ 1" or spinner + "1" 275 }, 276 { 277 name: "mixed_states", 278 myses: []MysisInfo{ 279 {ID: "1", State: "running"}, 280 {ID: "2", State: "idle"}, 281 {ID: "3", State: "stopped"}, 282 }, 283 wantContains: "1", // Should show state counts with icons 284 }, 285 { 286 name: "all_running", 287 myses: []MysisInfo{ 288 {ID: "1", State: "running"}, 289 {ID: "2", State: "running"}, 290 {ID: "3", State: "running"}, 291 }, 292 wantContains: "3", // Should show "⬡ 3" or spinner + "3" 293 }, 294 { 295 name: "full_swarm_16", 296 myses: makeFullSwarm16(), 297 wantContains: "16", // Should show "⬡ 16" or spinner + "16" 298 }, 299 } 300 301 for _, tt := range tests { 302 t.Run(tt.name, func(t *testing.T) { 303 m, cleanup := setupTestModel(t) 304 defer cleanup() 305 306 m.myses = tt.myses 307 m.width = 120 308 309 output := m.renderStatusBar() 310 stripped := stripANSIForGolden(output) 311 312 // Check that expected content appears in output 313 if !strings.Contains(stripped, tt.wantContains) { 314 t.Errorf("Expected %q in status bar, got: %s", tt.wantContains, stripped) 315 } 316 317 // Verify tick timestamp format is present (T#### ⬡ [HH:MM]) 318 if !strings.Contains(stripped, "T") || !strings.Contains(stripped, "[") { 319 t.Errorf("Expected tick timestamp format in status bar, got: %s", stripped) 320 } 321 }) 322 } 323} 324 325// TestStatusBarFocusIDTruncation removed - view name/focus ID no longer shown in status bar. 326// Status bar now shows: [activity indicator] | [tick + timestamp] | [state counts] 327 328// TestStatusBarLipglossWidth tests that rendered output matches expected width. 329func TestStatusBarLipglossWidth(t *testing.T) { 330 tests := []struct { 331 name string 332 width int 333 }{ 334 {name: "width_80", width: 80}, 335 {name: "width_120", width: 120}, 336 {name: "width_160", width: 160}, 337 } 338 339 for _, tt := range tests { 340 t.Run(tt.name, func(t *testing.T) { 341 m, cleanup := setupTestModel(t) 342 defer cleanup() 343 344 m.width = tt.width 345 m.myses = []MysisInfo{ 346 {ID: "test", Name: "alpha", State: "running"}, 347 } 348 349 output := m.renderStatusBar() 350 351 // Check lipgloss width (accounts for ANSI) 352 actualWidth := lipgloss.Width(output) 353 if actualWidth != tt.width { 354 t.Errorf("Expected lipgloss width %d, got %d", tt.width, actualWidth) 355 } 356 }) 357 } 358} 359 360// TestStatusBarStateCounts tests status bar with state counts view model. 361func TestStatusBarStateCounts(t *testing.T) { 362 defer setupGoldenTest(t)() 363 364 tests := []struct { 365 name string 366 myses []MysisInfo 367 width int 368 currentTick int64 369 spinnerPos int // spinner frame position for running/loading states 370 }{ 371 { 372 name: "empty_swarm", 373 myses: []MysisInfo{}, 374 width: 120, 375 currentTick: 0, 376 spinnerPos: 0, 377 }, 378 { 379 name: "single_idle", 380 myses: []MysisInfo{ 381 {ID: "test-1", Name: "alpha", State: "idle"}, 382 }, 383 width: 120, 384 currentTick: 100, 385 spinnerPos: 0, 386 }, 387 { 388 name: "single_running", 389 myses: []MysisInfo{ 390 {ID: "test-1", Name: "alpha", State: "running"}, 391 }, 392 width: 120, 393 currentTick: 200, 394 spinnerPos: 0, // First frame 395 }, 396 { 397 name: "single_stopped", 398 myses: []MysisInfo{ 399 {ID: "test-1", Name: "alpha", State: "stopped"}, 400 }, 401 width: 120, 402 currentTick: 150, 403 spinnerPos: 0, 404 }, 405 { 406 name: "single_errored", 407 myses: []MysisInfo{ 408 {ID: "test-1", Name: "alpha", State: "errored"}, 409 }, 410 width: 120, 411 currentTick: 175, 412 spinnerPos: 0, 413 }, 414 { 415 name: "mixed_states", 416 myses: []MysisInfo{ 417 {ID: "test-1", Name: "alpha", State: "running"}, 418 {ID: "test-2", Name: "beta", State: "idle"}, 419 {ID: "test-3", Name: "gamma", State: "stopped"}, 420 {ID: "test-4", Name: "delta", State: "errored"}, 421 }, 422 width: 120, 423 currentTick: 250, 424 spinnerPos: 2, // Mid-animation frame 425 }, 426 { 427 name: "all_running", 428 myses: []MysisInfo{ 429 {ID: "test-1", Name: "a", State: "running"}, 430 {ID: "test-2", Name: "b", State: "running"}, 431 {ID: "test-3", Name: "c", State: "running"}, 432 }, 433 width: 120, 434 currentTick: 300, 435 spinnerPos: 4, // Different frame 436 }, 437 { 438 name: "full_swarm_mixed", 439 myses: []MysisInfo{ 440 {ID: "1", State: "running"}, 441 {ID: "2", State: "running"}, 442 {ID: "3", State: "running"}, 443 {ID: "4", State: "running"}, 444 {ID: "5", State: "running"}, 445 {ID: "6", State: "idle"}, 446 {ID: "7", State: "idle"}, 447 {ID: "8", State: "stopped"}, 448 {ID: "9", State: "stopped"}, 449 {ID: "10", State: "errored"}, 450 }, 451 width: 160, 452 currentTick: 1000, 453 spinnerPos: 6, // Last animation frame 454 }, 455 { 456 name: "narrow_width_state_counts", 457 myses: []MysisInfo{ 458 {ID: "test-1", State: "running"}, 459 {ID: "test-2", State: "idle"}, 460 }, 461 width: 80, 462 currentTick: 500, 463 spinnerPos: 3, 464 }, 465 { 466 name: "tick_5000_timestamp", 467 myses: []MysisInfo{}, 468 width: 120, 469 currentTick: 5000, 470 spinnerPos: 0, 471 }, 472 { 473 name: "tick_99999_large", 474 myses: []MysisInfo{}, 475 width: 120, 476 currentTick: 99999, 477 spinnerPos: 0, 478 }, 479 } 480 481 for _, tt := range tests { 482 t.Run(tt.name, func(t *testing.T) { 483 m, cleanup := setupTestModel(t) 484 defer cleanup() 485 486 m.myses = tt.myses 487 m.width = tt.width 488 m.currentTick = tt.currentTick 489 490 // Note: spinner position varies by frame, tests will use current frame 491 output := m.renderStatusBar() 492 493 // Test ANSI output 494 t.Run("ANSI", func(t *testing.T) { 495 golden.RequireEqual(t, []byte(output)) 496 }) 497 498 // Test stripped output 499 t.Run("Stripped", func(t *testing.T) { 500 stripped := stripANSIForGolden(output) 501 golden.RequireEqual(t, []byte(stripped)) 502 }) 503 }) 504 } 505} 506 507// Helper: makeFullSwarm16 creates 16 running myses for full swarm tests. 508func makeFullSwarm16() []MysisInfo { 509 myses := make([]MysisInfo, 16) 510 for i := 0; i < 16; i++ { 511 myses[i] = MysisInfo{ 512 ID: "test-" + string(rune('a'+i)), 513 Name: "mysis-" + string(rune('a'+i)), 514 State: "running", 515 } 516 } 517 return myses 518}