diff --git a/forge/gitea/client_test.go b/forge/gitea/client_test.go index 1700058..06e3cfe 100644 --- a/forge/gitea/client_test.go +++ b/forge/gitea/client_test.go @@ -1,6 +1,12 @@ package main import ( + "encoding/json" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "slices" "testing" "github.com/samcharles93/archipelago/sdk/forge" @@ -17,3 +23,103 @@ func TestReviewEvent(t *testing.T) { } } } + +// A fake Gitea API captures the review object CreateReviewComments submits: +// exactly one review carrying the event, the body and the inline comments. +func TestCreateReviewCommentsSubmitsOneReview(t *testing.T) { + type reviewComment struct { + Path string `json:"path"` + Body string `json:"body"` + NewLineNum int64 `json:"new_position"` + } + type reviewRequest struct { + State string `json:"event"` + Body string `json:"body"` + CommitID string `json:"commit_id"` + Comments []reviewComment `json:"comments"` + } + const headSHA = "deadbeefcafe" + var got reviewRequest + posts := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && r.URL.Path == "/api/v1/version": + _, _ = w.Write([]byte(`{"version":"1.23.0"}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/v1/repos/o/r/pulls/6": + _, _ = w.Write([]byte(`{"user":{"login":"alice"},"head":{"sha":"` + headSHA + `","ref":"feat"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/v1/user": + _, _ = w.Write([]byte(`{"login":"archie"}`)) + case r.Method == http.MethodPost && r.URL.Path == "/api/v1/repos/o/r/pulls/6/reviews": + posts++ + if err := json.NewDecoder(r.Body).Decode(&got); err != nil { + t.Errorf("decode submitted review: %v", err) + w.WriteHeader(http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{"id":9}`)) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + f, err := newGitea("token", srv.URL, slog.New(slog.NewTextHandler(io.Discard, nil))) + if err != nil { + t.Fatalf("newGitea: %v", err) + } + writer, ok := f.(forge.ReviewCommentWriter) + if !ok { + t.Fatal("the Gitea extension is not a ReviewCommentWriter") + } + for _, tc := range []struct { + name string + review forge.ReviewSubmission + wantState string + wantBody string + wantComments []reviewComment + }{ + { + name: "comment event carries the body and the inline comments", + review: forge.ReviewSubmission{ + Event: forge.ReviewEventComment, + Body: "looks good overall", + Comments: []forge.InlineReviewComment{{Path: "main.go", Line: 12, Body: "rename this"}}, + }, + wantState: forge.ReviewEventComment, + wantBody: "looks good overall", + wantComments: []reviewComment{{Path: "main.go", Body: "rename this", NewLineNum: 12}}, + }, + { + name: "request changes lists the unanchored finding in the review body", + review: forge.ReviewSubmission{ + Event: forge.ReviewEventRequestChanges, + Body: "Findings outside the diff:\n\n- `main.go`: blocking finding", + }, + wantState: forge.ReviewEventRequestChanges, + wantBody: "Findings outside the diff:\n\n- `main.go`: blocking finding", + }, + } { + t.Run(tc.name, func(t *testing.T) { + got, posts = reviewRequest{}, 0 + if err := writer.CreateReviewComments(t.Context(), "o", "r", 6, headSHA, tc.review); err != nil { + t.Fatalf("CreateReviewComments: %v", err) + } + if posts != 1 { + t.Fatalf("submitted %d review objects, want exactly 1", posts) + } + if got.State != tc.wantState { + t.Errorf("state = %q, want %q", got.State, tc.wantState) + } + if got.Body != tc.wantBody { + t.Errorf("body = %q, want %q", got.Body, tc.wantBody) + } + if got.CommitID != headSHA { + t.Errorf("commit_id = %q, want the reviewed head %q", got.CommitID, headSHA) + } + if !slices.Equal(got.Comments, tc.wantComments) { + t.Errorf("comments = %+v, want %+v", got.Comments, tc.wantComments) + } + }) + } +} diff --git a/forge/github/client_test.go b/forge/github/client_test.go index 97aa0ff..d37034f 100644 --- a/forge/github/client_test.go +++ b/forge/github/client_test.go @@ -1,6 +1,11 @@ package main import ( + "encoding/json" + "io" + "log/slog" + "net/http" + "net/http/httptest" "slices" "testing" @@ -82,3 +87,111 @@ func TestSplitInvitations(t *testing.T) { }) } } + +// A fake GitHub API captures the review object CreateReviewComments submits: +// exactly one review carrying the event, the body and the inline comments. +func TestCreateReviewCommentsSubmitsOneReview(t *testing.T) { + type draftComment struct { + Path string `json:"path"` + Line int `json:"line"` + Side string `json:"side"` + Body string `json:"body"` + } + type reviewRequest struct { + CommitID string `json:"commit_id"` + Body string `json:"body"` + Event string `json:"event"` + Comments []draftComment `json:"comments"` + } + const headSHA = "deadbeefcafe" + var got reviewRequest + posts := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/o/r/pulls/6": + _, _ = w.Write([]byte(`{"head":{"sha":"` + headSHA + `"},"user":{"login":"alice"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/v3/user": + _, _ = w.Write([]byte(`{"login":"archie-bot"}`)) + case r.Method == http.MethodPost && r.URL.Path == "/api/v3/repos/o/r/pulls/6/reviews": + posts++ + if err := json.NewDecoder(r.Body).Decode(&got); err != nil { + t.Errorf("decode submitted review: %v", err) + w.WriteHeader(http.StatusBadRequest) + return + } + _, _ = w.Write([]byte(`{"id":1}`)) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + f, err := newGitHub("token", srv.URL, slog.New(slog.NewTextHandler(io.Discard, nil))) + if err != nil { + t.Fatalf("newGitHub: %v", err) + } + writer, ok := f.(forge.ReviewCommentWriter) + if !ok { + t.Fatal("the GitHub extension is not a ReviewCommentWriter") + } + for _, tc := range []struct { + name string + review forge.ReviewSubmission + wantEvent string + wantBody string + wantComments []draftComment + }{ + { + name: "comment event carries the body and the inline comments", + review: forge.ReviewSubmission{ + Event: forge.ReviewEventComment, + Body: "looks good overall", + Comments: []forge.InlineReviewComment{{Path: "main.go", Line: 12, Body: "rename this"}}, + }, + wantEvent: forge.ReviewEventComment, + wantBody: "looks good overall", + wantComments: []draftComment{{Path: "main.go", Line: 12, Side: "RIGHT", Body: "rename this"}}, + }, + { + name: "request changes lists the unanchored finding in the review body", + review: forge.ReviewSubmission{ + Event: forge.ReviewEventRequestChanges, + Body: "Findings outside the diff:\n\n- `main.go`: blocking finding", + }, + wantEvent: forge.ReviewEventRequestChanges, + wantBody: "Findings outside the diff:\n\n- `main.go`: blocking finding", + }, + { + name: "request changes without a body still posts one", + review: forge.ReviewSubmission{ + Event: forge.ReviewEventRequestChanges, + Comments: []forge.InlineReviewComment{{Path: "main.go", Line: 3, Body: "blocking"}}, + }, + wantEvent: forge.ReviewEventRequestChanges, + wantBody: "Changes requested.", + wantComments: []draftComment{{Path: "main.go", Line: 3, Side: "RIGHT", Body: "blocking"}}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + got, posts = reviewRequest{}, 0 + if err := writer.CreateReviewComments(t.Context(), "o", "r", 6, headSHA, tc.review); err != nil { + t.Fatalf("CreateReviewComments: %v", err) + } + if posts != 1 { + t.Fatalf("submitted %d review objects, want exactly 1", posts) + } + if got.Event != tc.wantEvent { + t.Errorf("event = %q, want %q", got.Event, tc.wantEvent) + } + if got.Body != tc.wantBody { + t.Errorf("body = %q, want %q", got.Body, tc.wantBody) + } + if got.CommitID != headSHA { + t.Errorf("commit_id = %q, want the reviewed head %q", got.CommitID, headSHA) + } + if !slices.Equal(got.Comments, tc.wantComments) { + t.Errorf("comments = %+v, want %+v", got.Comments, tc.wantComments) + } + }) + } +}