|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestColorize(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + text string |
| 12 | + code string |
| 13 | + want string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "Bold text", |
| 17 | + text: "hello", |
| 18 | + code: ansiBold, |
| 19 | + want: "\033[1mhello\033[0m", |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "Red text", |
| 23 | + text: "dirty", |
| 24 | + code: ansiRed, |
| 25 | + want: "\033[31mdirty\033[0m", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Green text", |
| 29 | + text: "clean", |
| 30 | + code: ansiGreen, |
| 31 | + want: "\033[32mclean\033[0m", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "Yellow text", |
| 35 | + text: "3", |
| 36 | + code: ansiYellow, |
| 37 | + want: "\033[33m3\033[0m", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Cyan text", |
| 41 | + text: "*", |
| 42 | + code: ansiCyan, |
| 43 | + want: "\033[36m*\033[0m", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Dim text", |
| 47 | + text: "no upstream", |
| 48 | + code: ansiDim, |
| 49 | + want: "\033[2mno upstream\033[0m", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "Empty string", |
| 53 | + text: "", |
| 54 | + code: ansiBold, |
| 55 | + want: "\033[1m\033[0m", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "Combined bold+cyan", |
| 59 | + text: "*", |
| 60 | + code: ansiBold + ";" + ansiCyanRaw, |
| 61 | + want: "\033[1;36m*\033[0m", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + got := colorize(tt.text, tt.code) |
| 68 | + if got != tt.want { |
| 69 | + t.Errorf("colorize(%q, %q) = %q, want %q", tt.text, tt.code, got, tt.want) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestIsColorEnabledRespectsNO_COLOR(t *testing.T) { |
| 76 | + // When NO_COLOR is set, color should be disabled regardless |
| 77 | + t.Setenv("NO_COLOR", "1") |
| 78 | + if isColorEnabled() { |
| 79 | + t.Error("isColorEnabled() = true, want false when NO_COLOR is set") |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestIsColorEnabledRespectsEmptyNO_COLOR(t *testing.T) { |
| 84 | + // When NO_COLOR is set to empty string, it still counts per no-color.org spec |
| 85 | + t.Setenv("NO_COLOR", "") |
| 86 | + if isColorEnabled() { |
| 87 | + t.Error("isColorEnabled() = true, want false when NO_COLOR is set (even empty)") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestFormatStatusLineNoColor(t *testing.T) { |
| 92 | + // When color is disabled, formatStatusLine should produce the same output as before |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + entry worktreeStatus |
| 96 | + }{ |
| 97 | + { |
| 98 | + name: "Current worktree dirty with ahead/behind", |
| 99 | + entry: worktreeStatus{ |
| 100 | + Path: "/path/to/worktree", |
| 101 | + Branch: "feat/foo", |
| 102 | + HEAD: "abc1234", |
| 103 | + Dirty: true, |
| 104 | + Ahead: 2, |
| 105 | + Behind: 1, |
| 106 | + Current: true, |
| 107 | + HasUpstream: true, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "Non-current clean worktree", |
| 112 | + entry: worktreeStatus{ |
| 113 | + Path: "/path/to/main", |
| 114 | + Branch: "main", |
| 115 | + HEAD: "def5678", |
| 116 | + Dirty: false, |
| 117 | + Ahead: 0, |
| 118 | + Behind: 0, |
| 119 | + Current: false, |
| 120 | + HasUpstream: true, |
| 121 | + }, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "No upstream", |
| 125 | + entry: worktreeStatus{ |
| 126 | + Path: "/path/to/wt", |
| 127 | + Branch: "fix/bar", |
| 128 | + HEAD: "ghi9012", |
| 129 | + Dirty: false, |
| 130 | + Ahead: 0, |
| 131 | + Behind: 0, |
| 132 | + Current: false, |
| 133 | + HasUpstream: false, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + for _, tt := range tests { |
| 139 | + t.Run(tt.name, func(t *testing.T) { |
| 140 | + noColor := formatStatusLineColor(tt.entry, false) |
| 141 | + // Should not contain any ANSI escape sequences |
| 142 | + if strings.Contains(noColor, "\033[") { |
| 143 | + t.Errorf("formatStatusLineColor(color=false) contains ANSI codes: %q", noColor) |
| 144 | + } |
| 145 | + // Verify same content as original formatStatusLine |
| 146 | + original := formatStatusLine(tt.entry) |
| 147 | + if noColor != original { |
| 148 | + t.Errorf("formatStatusLineColor(color=false) = %q, want %q (same as formatStatusLine)", noColor, original) |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestFormatStatusLineWithColor(t *testing.T) { |
| 155 | + tests := []struct { |
| 156 | + name string |
| 157 | + entry worktreeStatus |
| 158 | + wantContains []string |
| 159 | + wantNotContain []string |
| 160 | + }{ |
| 161 | + { |
| 162 | + name: "Current dirty worktree with ahead/behind", |
| 163 | + entry: worktreeStatus{ |
| 164 | + Path: "/path/to/worktree", |
| 165 | + Branch: "feat/foo", |
| 166 | + HEAD: "abc1234", |
| 167 | + Dirty: true, |
| 168 | + Ahead: 2, |
| 169 | + Behind: 1, |
| 170 | + Current: true, |
| 171 | + HasUpstream: true, |
| 172 | + }, |
| 173 | + wantContains: []string{ |
| 174 | + "\033[1;36m*\033[0m", // bold+cyan marker |
| 175 | + "\033[1mfeat/foo\033[0m", // bold branch |
| 176 | + "\033[31mdirty\033[0m", // red dirty |
| 177 | + "\033[33m", // yellow for ahead/behind |
| 178 | + }, |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "Non-current clean worktree", |
| 182 | + entry: worktreeStatus{ |
| 183 | + Path: "/path/to/main", |
| 184 | + Branch: "main", |
| 185 | + HEAD: "def5678", |
| 186 | + Dirty: false, |
| 187 | + Ahead: 0, |
| 188 | + Behind: 0, |
| 189 | + Current: false, |
| 190 | + HasUpstream: true, |
| 191 | + }, |
| 192 | + wantContains: []string{ |
| 193 | + "\033[32mclean\033[0m", // green clean |
| 194 | + }, |
| 195 | + wantNotContain: []string{ |
| 196 | + "\033[1;36m*\033[0m", // should NOT have colored marker |
| 197 | + }, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "No upstream", |
| 201 | + entry: worktreeStatus{ |
| 202 | + Path: "/path/to/wt", |
| 203 | + Branch: "fix/bar", |
| 204 | + HEAD: "ghi9012", |
| 205 | + Dirty: false, |
| 206 | + Ahead: 0, |
| 207 | + Behind: 0, |
| 208 | + Current: false, |
| 209 | + HasUpstream: false, |
| 210 | + }, |
| 211 | + wantContains: []string{ |
| 212 | + "\033[2mno upstream\033[0m", // dim no upstream |
| 213 | + }, |
| 214 | + }, |
| 215 | + { |
| 216 | + name: "Ahead only colored yellow", |
| 217 | + entry: worktreeStatus{ |
| 218 | + Path: "/path/to/wt", |
| 219 | + Branch: "feat/x", |
| 220 | + HEAD: "abc123", |
| 221 | + Dirty: false, |
| 222 | + Ahead: 5, |
| 223 | + Behind: 0, |
| 224 | + Current: false, |
| 225 | + HasUpstream: true, |
| 226 | + }, |
| 227 | + wantContains: []string{ |
| 228 | + "\033[33m↑5\033[0m", // yellow ahead |
| 229 | + }, |
| 230 | + }, |
| 231 | + { |
| 232 | + name: "Behind only colored yellow", |
| 233 | + entry: worktreeStatus{ |
| 234 | + Path: "/path/to/wt", |
| 235 | + Branch: "feat/y", |
| 236 | + HEAD: "abc123", |
| 237 | + Dirty: false, |
| 238 | + Ahead: 0, |
| 239 | + Behind: 3, |
| 240 | + Current: false, |
| 241 | + HasUpstream: true, |
| 242 | + }, |
| 243 | + wantContains: []string{ |
| 244 | + "\033[33m↓3\033[0m", // yellow behind |
| 245 | + }, |
| 246 | + }, |
| 247 | + } |
| 248 | + |
| 249 | + for _, tt := range tests { |
| 250 | + t.Run(tt.name, func(t *testing.T) { |
| 251 | + got := formatStatusLineColor(tt.entry, true) |
| 252 | + for _, want := range tt.wantContains { |
| 253 | + if !strings.Contains(got, want) { |
| 254 | + t.Errorf("formatStatusLineColor(color=true) = %q, want it to contain %q", got, want) |
| 255 | + } |
| 256 | + } |
| 257 | + for _, notWant := range tt.wantNotContain { |
| 258 | + if strings.Contains(got, notWant) { |
| 259 | + t.Errorf("formatStatusLineColor(color=true) = %q, should NOT contain %q", got, notWant) |
| 260 | + } |
| 261 | + } |
| 262 | + }) |
| 263 | + } |
| 264 | +} |
0 commit comments