status_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. NoIcon: true,
  37. Title: "Info",
  38. Description: "This status has no icon",
  39. },
  40. width: 80,
  41. },
  42. {
  43. name: "WithColors",
  44. opts: core.StatusOpts{
  45. Icon: "⚠",
  46. IconColor: color.RGBA{255, 165, 0, 255}, // Orange
  47. Title: "Warning",
  48. TitleColor: color.RGBA{255, 255, 0, 255}, // Yellow
  49. Description: "This is a warning message",
  50. DescriptionColor: color.RGBA{255, 0, 0, 255}, // Red
  51. },
  52. width: 80,
  53. },
  54. {
  55. name: "WithExtraContent",
  56. opts: core.StatusOpts{
  57. Title: "Build",
  58. Description: "Building project",
  59. ExtraContent: "[2/5]",
  60. },
  61. width: 80,
  62. },
  63. {
  64. name: "LongDescription",
  65. opts: core.StatusOpts{
  66. Title: "Processing",
  67. Description: "This is a very long description that should be truncated when the width is too small to display it completely without wrapping",
  68. },
  69. width: 60,
  70. },
  71. {
  72. name: "NarrowWidth",
  73. opts: core.StatusOpts{
  74. Icon: "●",
  75. Title: "Status",
  76. Description: "Short message",
  77. },
  78. width: 30,
  79. },
  80. {
  81. name: "VeryNarrowWidth",
  82. opts: core.StatusOpts{
  83. Icon: "●",
  84. Title: "Test",
  85. Description: "This will be truncated",
  86. },
  87. width: 20,
  88. },
  89. {
  90. name: "EmptyDescription",
  91. opts: core.StatusOpts{
  92. Icon: "●",
  93. Title: "Title Only",
  94. },
  95. width: 80,
  96. },
  97. {
  98. name: "AllFieldsWithExtraContent",
  99. opts: core.StatusOpts{
  100. Icon: "🚀",
  101. IconColor: color.RGBA{0, 255, 0, 255}, // Green
  102. Title: "Deployment",
  103. TitleColor: color.RGBA{0, 0, 255, 255}, // Blue
  104. Description: "Deploying to production environment",
  105. DescriptionColor: color.RGBA{128, 128, 128, 255}, // Gray
  106. ExtraContent: "v1.2.3",
  107. },
  108. width: 80,
  109. },
  110. }
  111. for _, tt := range tests {
  112. t.Run(tt.name, func(t *testing.T) {
  113. t.Parallel()
  114. output := core.Status(tt.opts, tt.width)
  115. golden.RequireEqual(t, []byte(output))
  116. })
  117. }
  118. }
  119. func TestStatusTruncation(t *testing.T) {
  120. t.Parallel()
  121. opts := core.StatusOpts{
  122. Icon: "●",
  123. Title: "Very Long Title",
  124. Description: "This is an extremely long description that definitely needs to be truncated",
  125. ExtraContent: "[extra]",
  126. }
  127. // Test different widths to ensure truncation works correctly
  128. widths := []int{20, 30, 40, 50, 60}
  129. for _, width := range widths {
  130. t.Run(fmt.Sprintf("Width%d", width), func(t *testing.T) {
  131. t.Parallel()
  132. output := core.Status(opts, width)
  133. golden.RequireEqual(t, []byte(output))
  134. })
  135. }
  136. }