fix(github): validate conflicting commit IDs and extract test helper
Address review findings from sonnet-review-bot (review 3086): - PostReview now returns ErrConflictingCommitIDs when comments specify different non-empty CommitIDs, since the GitHub API accepts only a single commit_id per review. Previously the discrepancy was silently ignored, using only the first commit's ID. - Extract newTestClient into helpers_test.go to make cross-file sharing between review_test.go and identity_test.go explicit. Refs: #81
This commit is contained in:
+21
-14
@@ -6,26 +6,12 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.weiker.me/rodin/review-bot/vcs"
|
||||
)
|
||||
|
||||
func newTestClient(t *testing.T, handler http.HandlerFunc) *Client {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(handler)
|
||||
t.Cleanup(srv.Close)
|
||||
c := NewClient("test-token", srv.URL, AllowInsecureHTTP())
|
||||
c.SetHTTPClient(srv.Client())
|
||||
if err := c.SetRetryBackoff([]time.Duration{1 * time.Millisecond, 1 * time.Millisecond}); err != nil {
|
||||
t.Fatalf("SetRetryBackoff: %v", err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// --- PostReview tests ---
|
||||
|
||||
func TestPostReview_HappyPath(t *testing.T) {
|
||||
@@ -379,3 +365,24 @@ func TestTranslateGitHubReviewState(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostReview_ConflictingCommitIDs(t *testing.T) {
|
||||
c := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Fatal("request should not be sent when commit IDs conflict")
|
||||
})
|
||||
|
||||
_, err := c.PostReview(context.Background(), "owner", "repo", 5, vcs.ReviewRequest{
|
||||
Body: "Review",
|
||||
Event: vcs.ReviewEventComment,
|
||||
Comments: []vcs.ReviewComment{
|
||||
{Path: "a.go", Position: 1, CommitID: "sha-1", Body: "first"},
|
||||
{Path: "b.go", Position: 2, CommitID: "sha-2", Body: "second"},
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for conflicting commit IDs")
|
||||
}
|
||||
if !errors.Is(err, ErrConflictingCommitIDs) {
|
||||
t.Errorf("expected ErrConflictingCommitIDs, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user