60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / tui / width_test.go
14.0 KB
1package tui 2 3import ( 4 "strings" 5 "testing" 6 7 "github.com/charmbracelet/lipgloss" 8) 9 10// TestWidthCalculations tests that width calculations use lipgloss.Width() 11// instead of len() for proper Unicode and ANSI handling. 12func TestWidthCalculations(t *testing.T) { 13 tests := []struct { 14 name string 15 input string 16 expectedWidth int 17 }{ 18 // ASCII strings 19 { 20 name: "plain_ascii", 21 input: "hello", 22 expectedWidth: 5, 23 }, 24 { 25 name: "ascii_with_spaces", 26 input: "hello world", 27 expectedWidth: 11, 28 }, 29 30 // Unicode strings 31 { 32 name: "emoji", 33 input: "👍", 34 expectedWidth: 2, // Emoji are wide characters 35 }, 36 { 37 name: "multiple_emoji", 38 input: "👍👎", 39 expectedWidth: 4, 40 }, 41 { 42 name: "cjk_characters", 43 input: "你好", 44 expectedWidth: 4, // CJK characters are double-width 45 }, 46 { 47 name: "box_drawing", 48 input: "╔═╗", 49 expectedWidth: 3, 50 }, 51 { 52 name: "diamond_indicators", 53 input: "⬥⬧⬦", 54 expectedWidth: 3, 55 }, 56 { 57 name: "hexagonal_indicators", 58 input: "⬡⬢", 59 expectedWidth: 2, 60 }, 61 62 // ANSI-styled strings 63 { 64 name: "ansi_styled", 65 input: lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Render("hello"), 66 expectedWidth: 5, // ANSI codes don't count toward width 67 }, 68 { 69 name: "ansi_bold", 70 input: lipgloss.NewStyle().Bold(true).Render("test"), 71 expectedWidth: 4, 72 }, 73 { 74 name: "ansi_colored_emoji", 75 input: lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Render("👍"), 76 expectedWidth: 2, 77 }, 78 79 // Mixed content 80 { 81 name: "ascii_and_emoji", 82 input: "hello 👍", 83 expectedWidth: 8, 84 }, 85 { 86 name: "ascii_and_cjk", 87 input: "hello 你好", 88 expectedWidth: 10, 89 }, 90 } 91 92 for _, tt := range tests { 93 t.Run(tt.name, func(t *testing.T) { 94 width := lipgloss.Width(tt.input) 95 if width != tt.expectedWidth { 96 t.Errorf("lipgloss.Width(%q) = %d, want %d", tt.input, width, tt.expectedWidth) 97 } 98 99 // Verify len() would be wrong for non-ASCII 100 byteLen := len(tt.input) 101 if strings.Contains(tt.name, "emoji") || strings.Contains(tt.name, "cjk") || strings.Contains(tt.name, "ansi") { 102 if byteLen == width { 103 t.Logf("WARNING: len() happens to match lipgloss.Width() for %q, but this is coincidental", tt.input) 104 } 105 } 106 }) 107 } 108} 109 110// TestTruncateToWidth tests the truncateToWidth helper function. 111func TestTruncateToWidth(t *testing.T) { 112 tests := []struct { 113 name string 114 input string 115 maxWidth int 116 expectedWidth int 117 description string 118 }{ 119 { 120 name: "no_truncation_needed", 121 input: "hello", 122 maxWidth: 10, 123 expectedWidth: 5, 124 description: "Short string should not be truncated", 125 }, 126 { 127 name: "truncate_ascii", 128 input: "hello world", 129 maxWidth: 5, 130 expectedWidth: 5, 131 description: "ASCII string truncated to exact width", 132 }, 133 { 134 name: "truncate_emoji", 135 input: "👍👎👌", 136 maxWidth: 4, 137 expectedWidth: 4, 138 description: "Emoji string truncated at wide character boundary", 139 }, 140 { 141 name: "truncate_cjk", 142 input: "你好世界", 143 maxWidth: 6, 144 expectedWidth: 6, 145 description: "CJK string truncated at double-width boundary", 146 }, 147 { 148 name: "truncate_mixed", 149 input: "hello 你好", 150 maxWidth: 8, 151 expectedWidth: 8, 152 description: "Mixed ASCII and CJK truncated correctly", 153 }, 154 { 155 name: "zero_width", 156 input: "hello", 157 maxWidth: 0, 158 expectedWidth: 0, 159 description: "Zero width should return empty string", 160 }, 161 { 162 name: "negative_width", 163 input: "hello", 164 maxWidth: -1, 165 expectedWidth: 0, 166 description: "Negative width should return empty string", 167 }, 168 } 169 170 for _, tt := range tests { 171 t.Run(tt.name, func(t *testing.T) { 172 result := truncateToWidth(tt.input, tt.maxWidth) 173 width := lipgloss.Width(result) 174 if tt.maxWidth >= 0 && width > tt.maxWidth { 175 t.Errorf("truncateToWidth(%q, %d) width = %d, exceeds maxWidth %d", 176 tt.input, tt.maxWidth, width, tt.maxWidth) 177 } 178 if tt.expectedWidth > 0 && width != tt.expectedWidth { 179 t.Logf("truncateToWidth(%q, %d) width = %d, expected %d (may vary due to boundary)", 180 tt.input, tt.maxWidth, width, tt.expectedWidth) 181 } 182 }) 183 } 184} 185 186// TestPadRight tests the padRight helper function for proper width handling. 187func TestPadRight(t *testing.T) { 188 tests := []struct { 189 name string 190 input string 191 length int 192 expectedWidth int 193 }{ 194 { 195 name: "pad_ascii", 196 input: "hello", 197 length: 10, 198 expectedWidth: 10, 199 }, 200 { 201 name: "no_pad_needed", 202 input: "hello world", 203 length: 5, 204 expectedWidth: 11, // Original width, no padding 205 }, 206 { 207 name: "pad_emoji", 208 input: "👍", 209 length: 5, 210 expectedWidth: 5, // Emoji (width 2) + 3 spaces 211 }, 212 { 213 name: "pad_cjk", 214 input: "你好", 215 length: 10, 216 expectedWidth: 10, // CJK (width 4) + 6 spaces 217 }, 218 } 219 220 for _, tt := range tests { 221 t.Run(tt.name, func(t *testing.T) { 222 result := padRight(tt.input, tt.length) 223 width := lipgloss.Width(result) 224 if width < tt.expectedWidth { 225 t.Errorf("padRight(%q, %d) width = %d, want >= %d", 226 tt.input, tt.length, width, tt.expectedWidth) 227 } 228 }) 229 } 230} 231 232// TestWidthCalculationEdgeCases tests edge cases for width calculations. 233func TestWidthCalculationEdgeCases(t *testing.T) { 234 tests := []struct { 235 name string 236 input string 237 description string 238 }{ 239 { 240 name: "empty_string", 241 input: "", 242 description: "Empty string should have width 0", 243 }, 244 { 245 name: "only_ansi_codes", 246 input: lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Render(""), 247 description: "ANSI codes without content should have width 0", 248 }, 249 { 250 name: "newlines", 251 input: "hello\nworld", 252 description: "Newlines count as width 0", 253 }, 254 { 255 name: "tabs", 256 input: "hello\tworld", 257 description: "Tabs have variable width", 258 }, 259 { 260 name: "combining_characters", 261 input: "é", // e + combining acute accent 262 description: "Combining characters should not add width", 263 }, 264 } 265 266 for _, tt := range tests { 267 t.Run(tt.name, func(t *testing.T) { 268 width := lipgloss.Width(tt.input) 269 byteLen := len(tt.input) 270 t.Logf("%s: lipgloss.Width=%d, len=%d", tt.description, width, byteLen) 271 272 // Just verify it doesn't panic and returns a non-negative value 273 if width < 0 { 274 t.Errorf("lipgloss.Width returned negative value: %d", width) 275 } 276 }) 277 } 278} 279 280// TestLenVsLipglossWidth demonstrates why len() is incorrect for display width. 281func TestLenVsLipglossWidth(t *testing.T) { 282 cases := []struct { 283 name string 284 input string 285 reason string 286 }{ 287 { 288 name: "emoji", 289 input: "👍", 290 reason: "Emoji are 4 bytes but display as 2 cells", 291 }, 292 { 293 name: "cjk", 294 input: "你", 295 reason: "CJK characters are 3 bytes but display as 2 cells", 296 }, 297 { 298 name: "ansi_styled", 299 input: lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Render("hi"), 300 reason: "ANSI codes add bytes but no display width", 301 }, 302 { 303 name: "box_drawing", 304 input: "═", 305 reason: "Box drawing is 3 bytes but displays as 1 cell", 306 }, 307 } 308 309 for _, tc := range cases { 310 t.Run(tc.name, func(t *testing.T) { 311 byteLen := len(tc.input) 312 displayWidth := lipgloss.Width(tc.input) 313 314 t.Logf("%s: len()=%d, lipgloss.Width()=%d - %s", 315 tc.name, byteLen, displayWidth, tc.reason) 316 317 if byteLen == displayWidth { 318 t.Logf("WARNING: In this case len() happens to equal lipgloss.Width(), but this is not reliable") 319 } 320 }) 321 } 322} 323 324// TestMysisLineWidth tests that mysis lines don't overflow at various terminal widths. 325func TestMysisLineWidth(t *testing.T) { 326 tests := []struct { 327 name string 328 mysis MysisInfo 329 terminalWidth int 330 description string 331 }{ 332 { 333 name: "long_mysis_name", 334 mysis: MysisInfo{ 335 ID: "abc123", 336 Name: "mysis-with-a-very-long-name-that-exceeds-sixteen-characters", 337 State: "running", 338 Provider: "ollama-qwen", 339 AccountUsername: "miner", 340 LastMessage: "Mining", 341 }, 342 terminalWidth: 80, 343 description: "Long mysis name should be truncated to 16 chars", 344 }, 345 { 346 name: "long_provider_name", 347 mysis: MysisInfo{ 348 ID: "abc123", 349 Name: "test", 350 State: "running", 351 Provider: "opencode_zen_with_very_long_name", 352 AccountUsername: "trader", 353 LastMessage: "Trading", 354 }, 355 terminalWidth: 80, 356 description: "Long provider name should not cause overflow", 357 }, 358 { 359 name: "long_account_name", 360 mysis: MysisInfo{ 361 ID: "abc123", 362 Name: "test", 363 State: "running", 364 Provider: "ollama-qwen", 365 AccountUsername: "crab_warrior_with_very_long_username", 366 LastMessage: "Docking", 367 }, 368 terminalWidth: 80, 369 description: "Long account name should not cause overflow", 370 }, 371 { 372 name: "long_last_message", 373 mysis: MysisInfo{ 374 ID: "abc123", 375 Name: "test", 376 State: "running", 377 Provider: "ollama-qwen", 378 AccountUsername: "miner", 379 LastMessage: "This is a very long last message that should be truncated to fit within the available space without causing overflow", 380 }, 381 terminalWidth: 80, 382 description: "Long last message should be truncated", 383 }, 384 { 385 name: "narrow_terminal", 386 mysis: MysisInfo{ 387 ID: "abc123", 388 Name: "test", 389 State: "running", 390 Provider: "ollama-qwen", 391 AccountUsername: "miner", 392 LastMessage: "Mining", 393 }, 394 terminalWidth: 60, 395 description: "Should handle narrow terminal width", 396 }, 397 { 398 name: "very_narrow_terminal", 399 mysis: MysisInfo{ 400 ID: "abc123", 401 Name: "test", 402 State: "running", 403 Provider: "ollama-qwen", 404 AccountUsername: "miner", 405 LastMessage: "Mining", 406 }, 407 terminalWidth: 40, 408 description: "Should handle very narrow terminal width", 409 }, 410 { 411 name: "wide_terminal", 412 mysis: MysisInfo{ 413 ID: "abc123", 414 Name: "test", 415 State: "running", 416 Provider: "ollama-qwen", 417 AccountUsername: "miner", 418 LastMessage: "Mining ore and selling it at the base", 419 }, 420 terminalWidth: 120, 421 description: "Should handle wide terminal width", 422 }, 423 { 424 name: "unicode_in_name", 425 mysis: MysisInfo{ 426 ID: "abc123", 427 Name: "测试-mysis", 428 State: "running", 429 Provider: "ollama-qwen", 430 AccountUsername: "miner", 431 LastMessage: "Mining", 432 }, 433 terminalWidth: 80, 434 description: "Should handle Unicode characters in name", 435 }, 436 { 437 name: "emoji_in_last_message", 438 mysis: MysisInfo{ 439 ID: "abc123", 440 Name: "test", 441 State: "running", 442 Provider: "ollama-qwen", 443 AccountUsername: "miner", 444 LastMessage: "Mining 👍 Going well", 445 }, 446 terminalWidth: 80, 447 description: "Should handle emoji in last message", 448 }, 449 { 450 name: "all_fields_empty", 451 mysis: MysisInfo{ 452 ID: "abc123", 453 Name: "test", 454 State: "idle", 455 Provider: "ollama-qwen", 456 AccountUsername: "", 457 LastMessage: "", 458 }, 459 terminalWidth: 80, 460 description: "Should handle empty optional fields", 461 }, 462 { 463 name: "errored_state_with_error", 464 mysis: MysisInfo{ 465 ID: "abc123", 466 Name: "test", 467 State: "errored", 468 Provider: "ollama-qwen", 469 AccountUsername: "miner", 470 LastMessage: "", 471 LastError: "connection timeout", 472 }, 473 terminalWidth: 80, 474 description: "Should show error message when state is errored", 475 }, 476 } 477 478 for _, tt := range tests { 479 t.Run(tt.name, func(t *testing.T) { 480 // Render mysis line (not selected, not loading, static spinner) 481 lines := renderMysisLine(tt.mysis, false, false, "◦", tt.terminalWidth, 0) 482 line := lines[0] // Check first line (info row) for width 483 484 // Strip ANSI codes to get display width 485 stripped := stripANSI(line) 486 lineWidth := lipgloss.Width(stripped) 487 488 // Verify no overflow 489 if lineWidth > tt.terminalWidth { 490 t.Errorf("%s: line width %d exceeds terminal width %d", 491 tt.description, lineWidth, tt.terminalWidth) 492 t.Logf("Line content: %q", stripped) 493 } 494 495 // Verify we're using display width 496 byteLen := len(stripped) 497 if byteLen != lineWidth && byteLen > lineWidth { 498 t.Logf("Byte length (%d) differs from display width (%d) - good, using lipgloss.Width()", 499 byteLen, lineWidth) 500 } 501 }) 502 } 503} 504 505// TestMysisLineWidthConsistency tests that mysis lines have consistent width handling. 506func TestMysisLineWidthConsistency(t *testing.T) { 507 terminalWidths := []int{40, 60, 80, 100, 120} 508 states := []string{"idle", "running", "stopped", "errored"} 509 510 baseMysis := MysisInfo{ 511 ID: "abc123", 512 Name: "test-mysis", 513 Provider: "ollama-qwen", 514 AccountUsername: "crab_miner", 515 LastMessage: "Performing operation", 516 } 517 518 for _, width := range terminalWidths { 519 for _, state := range states { 520 testName := state + "_width_" + strings.TrimSpace(strings.Join(strings.Fields(strings.Repeat(string(rune('0'+width/10)), 1)), "")) 521 t.Run(testName, func(t *testing.T) { 522 mysis := baseMysis 523 mysis.State = state 524 if state == "errored" { 525 mysis.LastMessage = "" 526 mysis.LastError = "test error" 527 } 528 529 lines := renderMysisLine(mysis, false, false, "◦", width, 0) 530 line := lines[0] // Check first line (info row) for width 531 stripped := stripANSI(line) 532 lineWidth := lipgloss.Width(stripped) 533 534 if lineWidth > width { 535 t.Errorf("State %s at width %d: line width %d exceeds terminal width", 536 state, width, lineWidth) 537 } 538 }) 539 } 540 } 541}