|
1 | 1 | package main |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/open-code-review/open-code-review/internal/model" |
| 8 | +) |
| 9 | + |
| 10 | +func TestBuildBadge(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + comment model.LlmComment |
| 14 | + want string |
| 15 | + }{ |
| 16 | + {"both fields", model.LlmComment{Category: "security", Severity: "high"}, "[security · high]"}, |
| 17 | + {"category only", model.LlmComment{Category: "bug"}, "[bug]"}, |
| 18 | + {"severity only", model.LlmComment{Severity: "low"}, "[low]"}, |
| 19 | + {"neither", model.LlmComment{}, ""}, |
| 20 | + {"strips control chars", model.LlmComment{Category: "bug\x1b[0m", Severity: "high"}, "[bug[0m · high]"}, |
| 21 | + } |
| 22 | + for _, tt := range tests { |
| 23 | + t.Run(tt.name, func(t *testing.T) { |
| 24 | + if got := buildBadge(tt.comment); got != tt.want { |
| 25 | + t.Errorf("buildBadge() = %q, want %q", got, tt.want) |
| 26 | + } |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestSeverityColor(t *testing.T) { |
| 32 | + // Each known severity must map to a distinct color; unknown falls back to dim. |
| 33 | + seen := map[string]string{} |
| 34 | + for _, sev := range []string{"critical", "high", "medium", "low"} { |
| 35 | + c := severityColor(sev) |
| 36 | + if c == "" { |
| 37 | + t.Errorf("severityColor(%q) is empty", sev) |
| 38 | + } |
| 39 | + if prev, ok := seen[c]; ok { |
| 40 | + t.Errorf("severityColor(%q) shares color with %q", sev, prev) |
| 41 | + } |
| 42 | + seen[c] = sev |
| 43 | + } |
| 44 | + if got := severityColor("bogus"); got != "\033[2m" { |
| 45 | + t.Errorf("severityColor(unknown) = %q, want dim", got) |
| 46 | + } |
| 47 | + if got := severityColor(""); got != "\033[2m" { |
| 48 | + t.Errorf("severityColor(empty) = %q, want dim", got) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// TestRenderComment_BadgeInline verifies the badge is colorized and rendered inline |
| 53 | +// with the first line of the comment content. |
| 54 | +func TestRenderComment_BadgeInline(t *testing.T) { |
| 55 | + out := captureStdout(t, func() { |
| 56 | + renderComment(model.LlmComment{ |
| 57 | + Path: "internal/mcp/client.go", |
| 58 | + StartLine: 27, |
| 59 | + EndLine: 27, |
| 60 | + Content: "Potential environment variable leak.", |
| 61 | + Category: "security", |
| 62 | + Severity: "high", |
| 63 | + }) |
| 64 | + }) |
| 65 | + if !strings.Contains(out, "[security · high]") { |
| 66 | + t.Errorf("expected badge in output, got:\n%s", out) |
| 67 | + } |
| 68 | + // severity high → bright red; the badge must be wrapped in the color + reset. |
| 69 | + if !strings.Contains(out, "\033[91m[security · high]\033[0m") { |
| 70 | + t.Errorf("expected colorized badge, got:\n%q", out) |
| 71 | + } |
| 72 | + if !strings.Contains(out, "Potential environment variable leak.") { |
| 73 | + t.Errorf("expected content in output, got:\n%s", out) |
| 74 | + } |
| 75 | +} |
4 | 76 |
|
5 | 77 | func TestSanitizeTerminal(t *testing.T) { |
6 | 78 | tests := []struct { |
|
0 commit comments