initial working read only state
This commit is contained in:
91
github_test.go
Normal file
91
github_test.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListPullRequestsFiltersToViewer(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer secret" {
|
||||
t.Fatalf("authorization = %q", got)
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{
|
||||
"viewer": map[string]any{"login": "zam"},
|
||||
"repository": map[string]any{"pullRequests": map[string]any{"nodes": []any{
|
||||
map[string]any{"id": "1", "number": 1, "title": "mine", "url": "u", "isDraft": false, "updatedAt": "2026-01-01T00:00:00Z", "author": map[string]any{"login": "zam"}, "reviewThreads": map[string]any{"totalCount": 2}},
|
||||
map[string]any{"id": "2", "number": 2, "title": "theirs", "url": "u", "isDraft": false, "updatedAt": "2026-01-01T00:00:00Z", "author": map[string]any{"login": "other"}, "reviewThreads": map[string]any{"totalCount": 1}},
|
||||
}}},
|
||||
}})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewGitHubClient(server.URL, "secret")
|
||||
prs, err := client.ListPullRequests(context.Background(), "o", "r", 50, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(prs) != 1 || prs[0].Number != 1 || prs[0].ReviewCount != 2 {
|
||||
t.Fatalf("unexpected PRs: %#v", prs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGraphQLErrorsAreReturned(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"errors":[{"message":"no access"}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
client := NewGitHubClient(server.URL, "secret")
|
||||
_, err := client.ListPullRequests(context.Background(), "o", "r", 50, false)
|
||||
if err == nil || !strings.Contains(err.Error(), "no access") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPullRequestUsesOriginalLineAndMetadata(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"data":{"repository":{"pullRequest":{
|
||||
"id":"pr","number":9,"title":"Fix","url":"u","body":"body","isDraft":false,
|
||||
"updatedAt":"2026-01-01T00:00:00Z","mergeable":"MERGEABLE","reviewDecision":"APPROVED",
|
||||
"baseRefName":"main","headRefName":"fix","author":{"login":"zam"},
|
||||
"assignees":{"nodes":[{"login":"sam"}]},
|
||||
"reviewRequests":{"nodes":[{"requestedReviewer":{"login":"lee"}}]},
|
||||
"latestReviews":{"nodes":[{"state":"CHANGES_REQUESTED","author":{"login":"pat"}}]},
|
||||
"commits":{"nodes":[{"commit":{"statusCheckRollup":{"state":"FAILURE"}}}]},
|
||||
"reviewThreads":{"pageInfo":{"hasNextPage":false},"nodes":[{
|
||||
"id":"t","isResolved":false,"isOutdated":true,"path":"main.go",
|
||||
"line":null,"originalLine":42,"diffSide":"RIGHT",
|
||||
"startLine":null,"originalStartLine":40,"startDiffSide":"RIGHT",
|
||||
"comments":{"pageInfo":{"hasNextPage":true},"nodes":[{
|
||||
"id":"c","body":"change this","diffHunk":"@@ -1 +1 @@","createdAt":"2026-01-01T00:00:00Z",
|
||||
"url":"cu","author":{"login":"reviewer"},"outdated":true,
|
||||
"line":100,"startLine":99,"originalLine":42,"originalStartLine":40,
|
||||
"originalCommit":{"oid":"0123456789abcdef"}
|
||||
}]}
|
||||
}]}
|
||||
}}}}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := NewGitHubClient(server.URL, "secret")
|
||||
got, err := client.GetPullRequest(context.Background(), "o", "r", 9)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.CheckState != "FAILURE" || got.BaseRef != "main" || got.HeadRef != "fix" || got.ReviewDecision != "APPROVED" {
|
||||
t.Fatalf("unexpected metadata: %#v", got)
|
||||
}
|
||||
if len(got.Threads) != 1 || got.Threads[0].Line != 42 || got.Threads[0].StartLine != 40 ||
|
||||
got.Threads[0].DiffSide != "RIGHT" || !got.Threads[0].IsTruncated {
|
||||
t.Fatalf("unexpected thread: %#v", got.Threads)
|
||||
}
|
||||
comment := got.Threads[0].Comments[0]
|
||||
if comment.OriginalLine != 42 || comment.OriginalStartLine != 40 ||
|
||||
comment.OriginalCommitOID != "0123456789abcdef" || !comment.Outdated {
|
||||
t.Fatalf("unexpected comment snapshot: %#v", comment)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user