status_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package core_test
  2. import (
  3. "fmt"
  4. "image/color"
  5. "testing"
  6. "github.com/charmbracelet/crush/internal/tui/components/core"
  7. "github.com/charmbracelet/x/exp/golden"
  8. )
  9. func TestStatus(t *testing.T) {
  10. t.Parallel()
  11. tests := []struct {
  12. name string
  13. opts core.StatusOpts
  14. width int
  15. }{
  16. {
  17. name: "Default",
  18. opts: core.StatusOpts{
  19. Title: "Status",
  20. Description: "Everything is working fine",
  21. },
  22. width: 80,
  23. },
  24. {
  25. name: "WithCustomIcon",
  26. opts: core.StatusOpts{
  27. Icon: "✓",
  28. Title: "Success",
  29. Description: "Operation completed successfully",
  30. },
  31. width: 80,
  32. },
  33. {
  34. name: "NoIcon",
  35. opts: core.StatusOpts{
  36. Title: "Info",
  37. Description: "This status has no icon",
  38. },
  39. width: 80,
  40. },
  41. {
  42. name: "WithColors",
  43. opts: core.StatusOpts{
  44. Icon: "⚠",
  45. Title: "Warning",
  46. TitleColor: color.RGBA{255, 255, 0, 255}, // Yellow
  47. Description: "This is a warning message",
  48. DescriptionColor: color.RGBA{255, 0, 0, 255}, // Red
  49. },
  50. width: 80,
  51. },
  52. {
  53. name: "WithExtraContent",
  54. opts: core.StatusOpts{
  55. Title: "Build",
  56. Description: "Building project",
  57. ExtraContent: "[2/5]",
  58. },
  59. width: 80,
  60. },
  61. {
  62. name: "LongDescription",
  63. opts: core.StatusOpts{
  64. Title: "Processing",
  65. Description: "This is a very long description that should be truncated when the width is too small to display it completely without wrapping",
  66. },
  67. width: 60,
  68. },
  69. {
  70. name: "NarrowWidth",
  71. opts: core.StatusOpts{
  72. Icon: "●",
  73. Title: "Status",
  74. Description: "Short message",
  75. },
  76. width: 30,
  77. },
  78. {
  79. name: "VeryNarrowWidth",
  80. opts: core.StatusOpts{
  81. Icon: "●",
  82. Title: "Test",
  83. Description: "This will be truncated",
  84. },
  85. width: 20,
  86. },
  87. {
  88. name: "EmptyDescription",
  89. opts: core.StatusOpts{
  90. Icon: "●",
  91. Title: "Title Only",
  92. },
  93. width: 80,
  94. },
  95. {
  96. name: "AllFieldsWithExtraContent",
  97. opts: core.StatusOpts{
  98. Icon: "🚀",
  99. Title: "Deployment",
  100. TitleColor: color.RGBA{0, 0, 255, 255}, // Blue
  101. Description: "Deploying to production environment",
  102. DescriptionColor: color.RGBA{128, 128, 128, 255}, // Gray
  103. ExtraContent: "v1.2.3",
  104. },
  105. width: 80,
  106. },
  107. }
  108. for _, tt := range tests {
  109. t.Run(tt.name, func(t *testing.T) {
  110. t.Parallel()
  111. output := core.Status(tt.opts, tt.width)
  112. golden.RequireEqual(t, []byte(output))
  113. })
  114. }
  115. }
  116. func TestStatusTruncation(t *testing.T) {
  117. t.Parallel()
  118. opts := core.StatusOpts{
  119. Icon: "●",
  120. Title: "Very Long Title",
  121. Description: "This is an extremely long description that definitely needs to be truncated",
  122. ExtraContent: "[extra]",
  123. }
  124. // Test different widths to ensure truncation works correctly
  125. widths := []int{20, 30, 40, 50, 60}
  126. for _, width := range widths {
  127. t.Run(fmt.Sprintf("Width%d", width), func(t *testing.T) {
  128. t.Parallel()
  129. output := core.Status(opts, width)
  130. golden.RequireEqual(t, []byte(output))
  131. })
  132. }
  133. }