package gitea import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestPostReview_WithComments(t *testing.T) { var gotPayload struct { Body string `json:"body"` Event string `json:"event"` Comments []struct { Path string `json:"path"` NewPosition int64 `json:"new_position"` Body string `json:"body"` } `json:"comments"` } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.NewDecoder(r.Body).Decode(&gotPayload) w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) json.NewEncoder(w).Encode(map[string]any{ "id": 99, "body": gotPayload.Body, "user": map[string]any{"login": "bot"}, }) })) defer server.Close() client := NewClient(server.URL, "test-token") comments := []ReviewComment{ {Path: "main.go", NewPosition: 42, Body: "[MAJOR] Something bad"}, {Path: "util.go", NewPosition: 10, Body: "[MINOR] Style issue"}, } _, err := client.PostReview(context.Background(), "owner", "repo", 1, "REQUEST_CHANGES", "summary", "", comments) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(gotPayload.Comments) != 2 { t.Fatalf("expected 2 comments, got %d", len(gotPayload.Comments)) } if gotPayload.Comments[0].Path != "main.go" { t.Errorf("expected path main.go, got %s", gotPayload.Comments[0].Path) } if gotPayload.Comments[0].NewPosition != 42 { t.Errorf("expected new_position 42, got %d", gotPayload.Comments[0].NewPosition) } if gotPayload.Comments[1].Body != "[MINOR] Style issue" { t.Errorf("unexpected body: %s", gotPayload.Comments[1].Body) } } func TestPostReview_NilComments(t *testing.T) { var gotPayload map[string]any server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.NewDecoder(r.Body).Decode(&gotPayload) w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) json.NewEncoder(w).Encode(map[string]any{ "id": 100, "body": "test", "user": map[string]any{"login": "bot"}, }) })) defer server.Close() client := NewClient(server.URL, "test-token") _, err := client.PostReview(context.Background(), "owner", "repo", 1, "APPROVED", "all good", "", nil) if err != nil { t.Fatalf("unexpected error: %v", err) } // With nil comments, the field should be omitted (omitempty) comments, ok := gotPayload["comments"] if ok && comments != nil { arr, isArr := comments.([]any) if isArr && len(arr) > 0 { t.Error("expected no comments in payload when nil passed") } } }