|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/steveyegge/wasteland/internal/commons" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGenerateStampID_Format(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + id := generateStampID("w-abc123", "my-rig") |
| 14 | + if !strings.HasPrefix(id, "s-") { |
| 15 | + t.Errorf("generateStampID() = %q, want prefix 's-'", id) |
| 16 | + } |
| 17 | + // "s-" + 16 hex chars = 18 chars total |
| 18 | + if len(id) != 18 { |
| 19 | + t.Errorf("generateStampID() length = %d, want 18", len(id)) |
| 20 | + } |
| 21 | + hexPart := id[2:] |
| 22 | + for _, c := range hexPart { |
| 23 | + if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 24 | + t.Errorf("generateStampID() contains non-hex char %q in %q", string(c), id) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestValidateAcceptInputs(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + quality int |
| 34 | + reliability int |
| 35 | + severity string |
| 36 | + wantErr string |
| 37 | + }{ |
| 38 | + {"valid", 3, 4, "leaf", ""}, |
| 39 | + {"quality too low", 0, 3, "leaf", "invalid quality"}, |
| 40 | + {"quality too high", 6, 3, "leaf", "invalid quality"}, |
| 41 | + {"reliability too low", 3, 0, "leaf", "invalid reliability"}, |
| 42 | + {"reliability too high", 3, 6, "leaf", "invalid reliability"}, |
| 43 | + {"bad severity", 3, 3, "bad", "invalid severity"}, |
| 44 | + {"valid branch", 5, 5, "branch", ""}, |
| 45 | + {"valid root", 1, 1, "root", ""}, |
| 46 | + } |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + err := validateAcceptInputs(tt.quality, tt.reliability, tt.severity) |
| 51 | + if tt.wantErr == "" { |
| 52 | + if err != nil { |
| 53 | + t.Errorf("validateAcceptInputs() unexpected error: %v", err) |
| 54 | + } |
| 55 | + } else { |
| 56 | + if err == nil { |
| 57 | + t.Fatalf("validateAcceptInputs() expected error containing %q", tt.wantErr) |
| 58 | + } |
| 59 | + if !strings.Contains(err.Error(), tt.wantErr) { |
| 60 | + t.Errorf("error = %q, want to contain %q", err.Error(), tt.wantErr) |
| 61 | + } |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestAcceptCompletion_Success(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + store := newFakeWLCommonsStore() |
| 70 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug"}) |
| 71 | + _ = store.ClaimWanted("w-abc", "worker-rig") |
| 72 | + _ = store.SubmitCompletion("c-test123", "w-abc", "worker-rig", "https://github.com/pr/1") |
| 73 | + |
| 74 | + stamp, err := acceptCompletion(store, "w-abc", "reviewer-rig", 4, 3, "leaf", []string{"go", "auth"}, "solid work") |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("acceptCompletion() error: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + if stamp.Quality != 4 { |
| 80 | + t.Errorf("stamp.Quality = %d, want 4", stamp.Quality) |
| 81 | + } |
| 82 | + if stamp.Reliability != 3 { |
| 83 | + t.Errorf("stamp.Reliability = %d, want 3", stamp.Reliability) |
| 84 | + } |
| 85 | + if stamp.Severity != "leaf" { |
| 86 | + t.Errorf("stamp.Severity = %q, want %q", stamp.Severity, "leaf") |
| 87 | + } |
| 88 | + if stamp.Subject != "worker-rig" { |
| 89 | + t.Errorf("stamp.Subject = %q, want %q", stamp.Subject, "worker-rig") |
| 90 | + } |
| 91 | + if stamp.Author != "reviewer-rig" { |
| 92 | + t.Errorf("stamp.Author = %q, want %q", stamp.Author, "reviewer-rig") |
| 93 | + } |
| 94 | + if stamp.Message != "solid work" { |
| 95 | + t.Errorf("stamp.Message = %q, want %q", stamp.Message, "solid work") |
| 96 | + } |
| 97 | + if len(stamp.SkillTags) != 2 || stamp.SkillTags[0] != "go" || stamp.SkillTags[1] != "auth" { |
| 98 | + t.Errorf("stamp.SkillTags = %v, want [go auth]", stamp.SkillTags) |
| 99 | + } |
| 100 | + |
| 101 | + item, _ := store.QueryWanted("w-abc") |
| 102 | + if item.Status != "completed" { |
| 103 | + t.Errorf("Status = %q, want %q", item.Status, "completed") |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestAcceptCompletion_NotInReview(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + store := newFakeWLCommonsStore() |
| 110 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug"}) |
| 111 | + |
| 112 | + _, err := acceptCompletion(store, "w-abc", "reviewer-rig", 4, 3, "leaf", nil, "") |
| 113 | + if err == nil { |
| 114 | + t.Fatal("acceptCompletion() expected error for non-in_review item") |
| 115 | + } |
| 116 | + if !strings.Contains(err.Error(), "not in_review") { |
| 117 | + t.Errorf("error = %q, want to contain 'not in_review'", err.Error()) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestAcceptCompletion_NotFound(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + store := newFakeWLCommonsStore() |
| 124 | + |
| 125 | + _, err := acceptCompletion(store, "w-nonexistent", "reviewer-rig", 4, 3, "leaf", nil, "") |
| 126 | + if err == nil { |
| 127 | + t.Fatal("acceptCompletion() expected error for missing item") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestAcceptCompletion_SelfAccept(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + store := newFakeWLCommonsStore() |
| 134 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug"}) |
| 135 | + _ = store.ClaimWanted("w-abc", "my-rig") |
| 136 | + _ = store.SubmitCompletion("c-test123", "w-abc", "my-rig", "evidence") |
| 137 | + |
| 138 | + _, err := acceptCompletion(store, "w-abc", "my-rig", 4, 3, "leaf", nil, "") |
| 139 | + if err == nil { |
| 140 | + t.Fatal("acceptCompletion() expected error for self-accept") |
| 141 | + } |
| 142 | + if !strings.Contains(err.Error(), "cannot accept your own completion") { |
| 143 | + t.Errorf("error = %q, want to contain 'cannot accept your own completion'", err.Error()) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestAcceptCompletion_QueryCompletionError(t *testing.T) { |
| 148 | + t.Parallel() |
| 149 | + store := newFakeWLCommonsStore() |
| 150 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug", Status: "in_review"}) |
| 151 | + store.QueryCompletionErr = fmt.Errorf("completion query error") |
| 152 | + |
| 153 | + _, err := acceptCompletion(store, "w-abc", "reviewer-rig", 4, 3, "leaf", nil, "") |
| 154 | + if err == nil { |
| 155 | + t.Fatal("acceptCompletion() expected error when QueryCompletion fails") |
| 156 | + } |
| 157 | + if !strings.Contains(err.Error(), "completion query error") { |
| 158 | + t.Errorf("error = %q, want to contain 'completion query error'", err.Error()) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestAcceptCompletion_AcceptCompletionError(t *testing.T) { |
| 163 | + t.Parallel() |
| 164 | + store := newFakeWLCommonsStore() |
| 165 | + _ = store.InsertWanted(&commons.WantedItem{ID: "w-abc", Title: "Fix bug"}) |
| 166 | + _ = store.ClaimWanted("w-abc", "worker-rig") |
| 167 | + _ = store.SubmitCompletion("c-test123", "w-abc", "worker-rig", "evidence") |
| 168 | + store.AcceptCompletionErr = fmt.Errorf("accept store error") |
| 169 | + |
| 170 | + _, err := acceptCompletion(store, "w-abc", "reviewer-rig", 4, 3, "leaf", nil, "") |
| 171 | + if err == nil { |
| 172 | + t.Fatal("acceptCompletion() expected error when AcceptCompletion fails") |
| 173 | + } |
| 174 | + if !strings.Contains(err.Error(), "accept store error") { |
| 175 | + t.Errorf("error = %q, want to contain 'accept store error'", err.Error()) |
| 176 | + } |
| 177 | +} |
0 commit comments