|
| 1 | +/* |
| 2 | +* Copyright 2025 Google LLC. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | + */ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "magician/github" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestExecReassignReviewer(t *testing.T) { |
| 26 | + availableReviewers := github.AvailableReviewers(nil) |
| 27 | + if len(availableReviewers) < 3 { |
| 28 | + t.Fatalf("not enough available reviewers (%v) to run TestExecRequestReviewer (need at least 3)", availableReviewers) |
| 29 | + } |
| 30 | + cases := map[string]struct { |
| 31 | + newPrimaryReviewer string |
| 32 | + comments []github.PullRequestComment |
| 33 | + expectSpecificReviewers []string |
| 34 | + expectRemovedReviewers []string |
| 35 | + }{ |
| 36 | + "reassign from no current reviewer to random reviewer": { |
| 37 | + comments: []github.PullRequestComment{}, |
| 38 | + }, |
| 39 | + "reassign from no current reviewer to alice": { |
| 40 | + newPrimaryReviewer: "alice", |
| 41 | + comments: []github.PullRequestComment{}, |
| 42 | + expectSpecificReviewers: []string{"alice"}, |
| 43 | + }, |
| 44 | + "reassign from bob to random reviewer": { |
| 45 | + comments: []github.PullRequestComment{ |
| 46 | + { |
| 47 | + Body: github.FormatReviewerComment("bob"), |
| 48 | + ID: 1234, |
| 49 | + }, |
| 50 | + }, |
| 51 | + expectRemovedReviewers: []string{"bob"}, |
| 52 | + }, |
| 53 | + "reassign from bob to alice": { |
| 54 | + newPrimaryReviewer: "alice", |
| 55 | + comments: []github.PullRequestComment{ |
| 56 | + { |
| 57 | + Body: github.FormatReviewerComment("bob"), |
| 58 | + ID: 1234, |
| 59 | + }, |
| 60 | + }, |
| 61 | + expectSpecificReviewers: []string{"alice"}, |
| 62 | + expectRemovedReviewers: []string{"bob"}, |
| 63 | + }, |
| 64 | + } |
| 65 | + for tn, tc := range cases { |
| 66 | + t.Run(tn, func(t *testing.T) { |
| 67 | + gh := &mockGithub{ |
| 68 | + pullRequest: github.PullRequest{ |
| 69 | + User: github.User{Login: "author"}, |
| 70 | + }, |
| 71 | + calledMethods: make(map[string][][]any), |
| 72 | + pullRequestComments: tc.comments, |
| 73 | + } |
| 74 | + |
| 75 | + err := execReassignReviewer("1", tc.newPrimaryReviewer, gh) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("execReassignReviewer failed: %v", err) |
| 78 | + } |
| 79 | + if len(gh.calledMethods["RequestPullRequestReviewers"]) != 1 { |
| 80 | + t.Errorf("Expected RequestPullRequestReviewers called 1 time, got %v", len(gh.calledMethods["RequestPullRequestReviewers"])) |
| 81 | + } |
| 82 | + |
| 83 | + var assignedReviewers []string |
| 84 | + for _, args := range gh.calledMethods["RequestPullRequestReviewers"] { |
| 85 | + assignedReviewers = append(assignedReviewers, args[1].([]string)...) |
| 86 | + } |
| 87 | + var removedReviewers []string |
| 88 | + for _, args := range gh.calledMethods["RemovePullRequestReviewers"] { |
| 89 | + removedReviewers = append(removedReviewers, args[1].([]string)...) |
| 90 | + } |
| 91 | + |
| 92 | + if tc.expectSpecificReviewers != nil { |
| 93 | + for _, reviewer := range assignedReviewers { |
| 94 | + assert.Contains(t, tc.expectSpecificReviewers, reviewer) |
| 95 | + } |
| 96 | + } |
| 97 | + if tc.expectRemovedReviewers != nil { |
| 98 | + for _, reviewer := range removedReviewers { |
| 99 | + assert.Contains(t, tc.expectRemovedReviewers, reviewer) |
| 100 | + } |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments