60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / tui / layout_test.go
13.4 KB
1package tui 2 3import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/charmbracelet/bubbles/viewport" 9) 10 11// TestDashboardLayoutCalculations tests dashboard height calculations with various scenarios. 12func TestDashboardLayoutCalculations(t *testing.T) { 13 tests := []struct { 14 name string 15 termWidth int 16 termHeight int 17 numMyses int 18 numSwarmMsgs int 19 minListHeight int 20 shouldNotPanic bool 21 }{ 22 // Small terminal sizes 23 {name: "min_terminal_0_myses_0_msgs", termWidth: 80, termHeight: 20, numMyses: 0, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 24 {name: "min_terminal_1_mysis_0_msgs", termWidth: 80, termHeight: 20, numMyses: 1, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 25 {name: "min_terminal_5_myses_0_msgs", termWidth: 80, termHeight: 20, numMyses: 5, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 26 {name: "min_terminal_0_myses_5_msgs", termWidth: 80, termHeight: 20, numMyses: 0, numSwarmMsgs: 5, minListHeight: 3, shouldNotPanic: true}, 27 {name: "min_terminal_5_myses_5_msgs", termWidth: 80, termHeight: 20, numMyses: 5, numSwarmMsgs: 5, minListHeight: 3, shouldNotPanic: true}, 28 29 // Medium terminal sizes 30 {name: "medium_terminal_0_myses_0_msgs", termWidth: 120, termHeight: 30, numMyses: 0, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 31 {name: "medium_terminal_10_myses_0_msgs", termWidth: 120, termHeight: 30, numMyses: 10, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 32 {name: "medium_terminal_0_myses_10_msgs", termWidth: 120, termHeight: 30, numMyses: 0, numSwarmMsgs: 10, minListHeight: 3, shouldNotPanic: true}, 33 {name: "medium_terminal_10_myses_10_msgs", termWidth: 120, termHeight: 30, numMyses: 10, numSwarmMsgs: 10, minListHeight: 3, shouldNotPanic: true}, 34 35 // Large terminal sizes 36 {name: "large_terminal_0_myses_0_msgs", termWidth: 160, termHeight: 40, numMyses: 0, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 37 {name: "large_terminal_20_myses_0_msgs", termWidth: 160, termHeight: 40, numMyses: 20, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 38 {name: "large_terminal_0_myses_20_msgs", termWidth: 160, termHeight: 40, numMyses: 0, numSwarmMsgs: 20, minListHeight: 3, shouldNotPanic: true}, 39 {name: "large_terminal_20_myses_20_msgs", termWidth: 160, termHeight: 40, numMyses: 20, numSwarmMsgs: 20, minListHeight: 3, shouldNotPanic: true}, 40 41 // Very large terminal sizes 42 {name: "xlarge_terminal_0_myses_0_msgs", termWidth: 200, termHeight: 60, numMyses: 0, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 43 {name: "xlarge_terminal_20_myses_10_msgs", termWidth: 200, termHeight: 60, numMyses: 20, numSwarmMsgs: 10, minListHeight: 3, shouldNotPanic: true}, 44 45 // Very tall terminal 46 {name: "tall_terminal_20_myses_10_msgs", termWidth: 120, termHeight: 100, numMyses: 20, numSwarmMsgs: 10, minListHeight: 3, shouldNotPanic: true}, 47 48 // Edge cases - extremely small terminals 49 {name: "tiny_terminal_0_myses_0_msgs", termWidth: 80, termHeight: 10, numMyses: 0, numSwarmMsgs: 0, minListHeight: 3, shouldNotPanic: true}, 50 {name: "tiny_terminal_5_myses_5_msgs", termWidth: 80, termHeight: 10, numMyses: 5, numSwarmMsgs: 5, minListHeight: 3, shouldNotPanic: true}, 51 52 // Maximum swarm messages (10) 53 {name: "max_swarm_msgs_0_myses", termWidth: 120, termHeight: 30, numMyses: 0, numSwarmMsgs: 10, minListHeight: 3, shouldNotPanic: true}, 54 {name: "max_swarm_msgs_10_myses", termWidth: 120, termHeight: 30, numMyses: 10, numSwarmMsgs: 10, minListHeight: 3, shouldNotPanic: true}, 55 } 56 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 // Setup test data 60 myses := make([]MysisInfo, tt.numMyses) 61 for i := 0; i < tt.numMyses; i++ { 62 myses[i] = makeMysisInfo(i+1, "running") 63 } 64 65 swarmMsgs := make([]SwarmMessageInfo, tt.numSwarmMsgs) 66 for i := 0; i < tt.numSwarmMsgs; i++ { 67 swarmMsgs[i] = SwarmMessageInfo{ 68 SenderID: "sender-1", 69 SenderName: "sender", 70 Content: "Test message", 71 CreatedAt: time.Now(), 72 } 73 } 74 75 loadingSet := make(map[string]bool) 76 spinnerView := "⬡" 77 78 // Test that rendering doesn't panic 79 defer func() { 80 if r := recover(); r != nil { 81 if tt.shouldNotPanic { 82 t.Errorf("Dashboard rendering panicked: %v", r) 83 } 84 } 85 }() 86 87 // Render dashboard (account for input bar height: -3) 88 contentHeight := tt.termHeight - 3 89 output := RenderDashboard(myses, swarmMsgs, 0, tt.termWidth, contentHeight, loadingSet, spinnerView, 0, nil) 90 91 // Basic validations 92 if output == "" { 93 t.Error("Dashboard output is empty") 94 } 95 }) 96 } 97} 98 99// TestDashboardHeightCalculation tests the specific height calculation logic. 100func TestDashboardHeightCalculation(t *testing.T) { 101 tests := []struct { 102 name string 103 termHeight int 104 numSwarmMsgs int 105 expectedMinHeight int 106 }{ 107 {name: "height_20_no_msgs", termHeight: 20, numSwarmMsgs: 0, expectedMinHeight: 3}, 108 {name: "height_20_5_msgs", termHeight: 20, numSwarmMsgs: 5, expectedMinHeight: 3}, 109 {name: "height_20_10_msgs", termHeight: 20, numSwarmMsgs: 10, expectedMinHeight: 3}, 110 {name: "height_30_no_msgs", termHeight: 30, numSwarmMsgs: 0, expectedMinHeight: 3}, 111 {name: "height_30_10_msgs", termHeight: 30, numSwarmMsgs: 10, expectedMinHeight: 3}, 112 {name: "height_40_no_msgs", termHeight: 40, numSwarmMsgs: 0, expectedMinHeight: 3}, 113 {name: "height_60_no_msgs", termHeight: 60, numSwarmMsgs: 0, expectedMinHeight: 3}, 114 {name: "height_100_no_msgs", termHeight: 100, numSwarmMsgs: 0, expectedMinHeight: 3}, 115 } 116 117 for _, tt := range tests { 118 t.Run(tt.name, func(t *testing.T) { 119 // Simulate the height calculation from dashboard.go lines 116-125 120 msgLines := tt.numSwarmMsgs 121 if msgLines == 0 { 122 msgLines = 1 // Placeholder line 123 } 124 125 // From dashboard.go: 126 // usedHeight := 5 // header (3 + margin) + mysis header (1) + footer (1) 127 // usedHeight += 1 + len(msgLines) // Swarm section: header (1) + content lines 128 // usedHeight += 2 // Account for panel borders (top + bottom = 2 lines) 129 usedHeight := 5 + 1 + msgLines + 2 130 131 mysisListHeight := tt.termHeight - usedHeight 132 if mysisListHeight < 3 { 133 mysisListHeight = 3 134 } 135 136 // Verify minimum height is enforced 137 if mysisListHeight < tt.expectedMinHeight { 138 t.Errorf("mysisListHeight = %d, want >= %d", mysisListHeight, tt.expectedMinHeight) 139 } 140 }) 141 } 142} 143 144// TestFocusViewLayoutCalculations tests focus view viewport height calculations. 145func TestFocusViewLayoutCalculations(t *testing.T) { 146 tests := []struct { 147 name string 148 termWidth int 149 termHeight int 150 numLogs int 151 minVpHeight int 152 shouldNotPanic bool 153 }{ 154 // Small terminal sizes 155 {name: "min_terminal_0_logs", termWidth: 80, termHeight: 20, numLogs: 0, minVpHeight: 5, shouldNotPanic: true}, 156 {name: "min_terminal_5_logs", termWidth: 80, termHeight: 20, numLogs: 5, minVpHeight: 5, shouldNotPanic: true}, 157 {name: "min_terminal_20_logs", termWidth: 80, termHeight: 20, numLogs: 20, minVpHeight: 5, shouldNotPanic: true}, 158 159 // Medium terminal sizes 160 {name: "medium_terminal_0_logs", termWidth: 120, termHeight: 30, numLogs: 0, minVpHeight: 5, shouldNotPanic: true}, 161 {name: "medium_terminal_50_logs", termWidth: 120, termHeight: 30, numLogs: 50, minVpHeight: 5, shouldNotPanic: true}, 162 {name: "medium_terminal_100_logs", termWidth: 120, termHeight: 30, numLogs: 100, minVpHeight: 5, shouldNotPanic: true}, 163 164 // Large terminal sizes 165 {name: "large_terminal_0_logs", termWidth: 160, termHeight: 40, numLogs: 0, minVpHeight: 5, shouldNotPanic: true}, 166 {name: "large_terminal_100_logs", termWidth: 160, termHeight: 40, numLogs: 100, minVpHeight: 5, shouldNotPanic: true}, 167 {name: "large_terminal_500_logs", termWidth: 160, termHeight: 40, numLogs: 500, minVpHeight: 5, shouldNotPanic: true}, 168 169 // Very large terminal sizes 170 {name: "xlarge_terminal_0_logs", termWidth: 200, termHeight: 60, numLogs: 0, minVpHeight: 5, shouldNotPanic: true}, 171 {name: "xlarge_terminal_1000_logs", termWidth: 200, termHeight: 60, numLogs: 1000, minVpHeight: 5, shouldNotPanic: true}, 172 173 // Very tall terminal 174 {name: "tall_terminal_500_logs", termWidth: 120, termHeight: 100, numLogs: 500, minVpHeight: 5, shouldNotPanic: true}, 175 176 // Edge cases - extremely small terminals 177 {name: "tiny_terminal_0_logs", termWidth: 80, termHeight: 10, numLogs: 0, minVpHeight: 5, shouldNotPanic: true}, 178 {name: "tiny_terminal_10_logs", termWidth: 80, termHeight: 10, numLogs: 10, minVpHeight: 5, shouldNotPanic: true}, 179 } 180 181 for _, tt := range tests { 182 t.Run(tt.name, func(t *testing.T) { 183 // Create test mysis 184 mysis := makeMysisInfo(1, "running") 185 186 // Create viewport with calculated dimensions 187 // From app.go lines 133-141: 188 headerHeight := 6 // approximate height used by header/info/title 189 footerHeight := 2 190 vpHeight := tt.termHeight - headerHeight - footerHeight - 3 191 if vpHeight < 5 { 192 vpHeight = 5 193 } 194 vpWidth := tt.termWidth - 6 - 2 // -6 for panel padding, -2 for scrollbar 195 196 // Create test viewport 197 vp := viewport.New(vpWidth, vpHeight) 198 199 // Test that rendering doesn't panic 200 defer func() { 201 if r := recover(); r != nil { 202 if tt.shouldNotPanic { 203 t.Errorf("Focus view rendering panicked: %v", r) 204 } 205 } 206 }() 207 208 // Render focus view 209 output := RenderFocusViewWithViewport(mysis, vp, tt.termWidth, false, "⬡", false, tt.numLogs, 1, 1, 0, nil, 0, nil) 210 211 // Basic validations 212 if output == "" { 213 t.Error("Focus view output is empty") 214 } 215 216 // Verify viewport height meets minimum 217 if vpHeight < tt.minVpHeight { 218 t.Errorf("Viewport height = %d, want >= %d", vpHeight, tt.minVpHeight) 219 } 220 }) 221 } 222} 223 224// TestViewportHeightCalculation tests the specific viewport height calculation logic. 225func TestViewportHeightCalculation(t *testing.T) { 226 tests := []struct { 227 name string 228 termHeight int 229 expectedMinHeight int 230 }{ 231 {name: "height_10", termHeight: 10, expectedMinHeight: 5}, 232 {name: "height_20", termHeight: 20, expectedMinHeight: 5}, 233 {name: "height_30", termHeight: 30, expectedMinHeight: 5}, 234 {name: "height_40", termHeight: 40, expectedMinHeight: 5}, 235 {name: "height_60", termHeight: 60, expectedMinHeight: 5}, 236 {name: "height_100", termHeight: 100, expectedMinHeight: 5}, 237 } 238 239 for _, tt := range tests { 240 t.Run(tt.name, func(t *testing.T) { 241 // Simulate the viewport height calculation from app.go lines 133-141 242 headerHeight := 6 243 footerHeight := 2 244 vpHeight := tt.termHeight - headerHeight - footerHeight - 3 245 if vpHeight < 5 { 246 vpHeight = 5 247 } 248 249 // Verify minimum height is enforced 250 if vpHeight < tt.expectedMinHeight { 251 t.Errorf("vpHeight = %d, want >= %d", vpHeight, tt.expectedMinHeight) 252 } 253 254 // Verify height never goes negative 255 if vpHeight < 0 { 256 t.Errorf("vpHeight = %d, must not be negative", vpHeight) 257 } 258 }) 259 } 260} 261 262// TestLayoutNoNegativeHeights verifies that layout calculations never produce negative heights. 263func TestLayoutNoNegativeHeights(t *testing.T) { 264 // Test extreme cases where terminal height is very small 265 extremeCases := []struct { 266 name string 267 termHeight int 268 termWidth int 269 }{ 270 {name: "height_5", termHeight: 5, termWidth: 80}, 271 {name: "height_8", termHeight: 8, termWidth: 80}, 272 {name: "height_10", termHeight: 10, termWidth: 80}, 273 {name: "height_15", termHeight: 15, termWidth: 80}, 274 } 275 276 for _, tc := range extremeCases { 277 t.Run(tc.name, func(t *testing.T) { 278 // Dashboard mysis list height calculation 279 msgLines := 5 280 usedHeight := 5 + 1 + msgLines + 2 281 mysisListHeight := tc.termHeight - usedHeight 282 if mysisListHeight < 3 { 283 mysisListHeight = 3 284 } 285 286 if mysisListHeight < 0 { 287 t.Errorf("Dashboard mysisListHeight = %d, must not be negative", mysisListHeight) 288 } 289 290 // Focus view viewport height calculation 291 headerHeight := 6 292 footerHeight := 2 293 vpHeight := tc.termHeight - headerHeight - footerHeight - 3 294 if vpHeight < 5 { 295 vpHeight = 5 296 } 297 298 if vpHeight < 0 { 299 t.Errorf("Focus view vpHeight = %d, must not be negative", vpHeight) 300 } 301 }) 302 } 303} 304 305// TestContentWidthCalculation tests width calculations for dashboard content. 306func TestContentWidthCalculation(t *testing.T) { 307 tests := []struct { 308 name string 309 termWidth int 310 expectedMinWidth int 311 }{ 312 {name: "width_80", termWidth: 80, expectedMinWidth: 20}, 313 {name: "width_40", termWidth: 40, expectedMinWidth: 20}, 314 {name: "width_20", termWidth: 20, expectedMinWidth: 20}, 315 {name: "width_10", termWidth: 10, expectedMinWidth: 20}, 316 {name: "width_120", termWidth: 120, expectedMinWidth: 20}, 317 {name: "width_160", termWidth: 160, expectedMinWidth: 20}, 318 {name: "width_200", termWidth: 200, expectedMinWidth: 20}, 319 } 320 321 for _, tt := range tests { 322 t.Run(tt.name, func(t *testing.T) { 323 // Simulate content width calculation from dashboard.go lines 128-131 324 // DoubleBorder adds 2 chars each side, so content width is width-4 325 contentWidth := tt.termWidth - 4 326 if contentWidth < 20 { 327 contentWidth = 20 328 } 329 330 // Verify minimum width is enforced 331 if contentWidth < tt.expectedMinWidth { 332 t.Errorf("contentWidth = %d, want >= %d", contentWidth, tt.expectedMinWidth) 333 } 334 335 // Verify width never goes negative 336 if contentWidth < 0 { 337 t.Errorf("contentWidth = %d, must not be negative", contentWidth) 338 } 339 }) 340 } 341} 342 343// makeMysisInfo creates a test MysisInfo instance. 344func makeMysisInfo(id int, state string) MysisInfo { 345 return MysisInfo{ 346 ID: fmt.Sprintf("mysis-%d", id), 347 Name: fmt.Sprintf("test-mysis-%d", id), 348 State: state, 349 Provider: "ollama-qwen", 350 AccountUsername: "test_user", 351 LastMessage: "Test message", 352 LastError: "", 353 CreatedAt: time.Now(), 354 } 355}