|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + plugin "github.com/Paca-AI/plugin-sdk-go" |
| 9 | + gp "github.com/Paca-AI/plugin-sdk-go/gitprovider" |
| 10 | +) |
| 11 | + |
| 12 | +func TestListIssuesFiltersPullRequestsWithoutLosingPages(t *testing.T) { |
| 13 | + p, g, _ := seededProvider(t) |
| 14 | + fetch = func(method, rawURL string, headers map[string]string, body string) (*plugin.FetchResponse, error) { |
| 15 | + u, err := url.Parse(rawURL) |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + if u.Path != "/repos/acme/web/issues" { |
| 20 | + return g.fetch(method, rawURL, headers, body) |
| 21 | + } |
| 22 | + if method != "GET" || u.Query().Get("state") != "closed" || u.Query().Get("per_page") != "1" { |
| 23 | + t.Fatalf("unexpected request: %s %s", method, rawURL) |
| 24 | + } |
| 25 | + switch u.Query().Get("page") { |
| 26 | + case "1": |
| 27 | + return resp(200, []map[string]any{{"number": 1, "pull_request": map[string]any{"url": "x"}}}) |
| 28 | + case "2": |
| 29 | + return resp(200, []map[string]any{{"number": 101, "title": "Bug", "body": "Details", "state": "closed", "html_url": "https://github.com/acme/web/issues/101", "user": map[string]string{"login": "alice"}, "labels": []map[string]string{{"name": "bug"}}, "assignees": []map[string]string{{"login": "bob"}}}}) |
| 30 | + case "3": |
| 31 | + return resp(200, []map[string]any{}) |
| 32 | + default: |
| 33 | + t.Fatalf("unexpected page: %s", rawURL) |
| 34 | + return nil, nil |
| 35 | + } |
| 36 | + } |
| 37 | + first, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget(), State: "closed"}) |
| 38 | + if err != nil || len(first.Items) != 0 || first.NextCursor != "2" { |
| 39 | + t.Fatalf("first page: %+v, %v", first, err) |
| 40 | + } |
| 41 | + second, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget(), State: "closed", Cursor: first.NextCursor}) |
| 42 | + if err != nil || len(second.Items) != 1 || second.NextCursor != "3" { |
| 43 | + t.Fatalf("second page: %+v, %v", second, err) |
| 44 | + } |
| 45 | + third, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget(), State: "closed", Cursor: second.NextCursor}) |
| 46 | + if err != nil || len(third.Items) != 0 || third.NextCursor != "" { |
| 47 | + t.Fatalf("third page: %+v, %v", third, err) |
| 48 | + } |
| 49 | + i := second.Items[0] |
| 50 | + if i.Number != "101" || i.Title != "Bug" || i.Body != "Details" || i.State != "closed" || i.Author != "alice" || i.URL != "https://github.com/acme/web/issues/101" || len(i.Labels) != 1 || i.Labels[0] != "bug" || len(i.Assignees) != 1 || i.Assignees[0] != "bob" { |
| 51 | + t.Fatalf("issue fields: %+v", i) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestListIssuesValidatesAndPropagatesErrors(t *testing.T) { |
| 56 | + p, g, _ := seededProvider(t) |
| 57 | + for _, req := range []gp.ListIssuesRequest{ |
| 58 | + {Target: webTarget(), State: "invalid"}, |
| 59 | + {Target: webTarget(), Cursor: "-1"}, |
| 60 | + } { |
| 61 | + _, err := p.ListIssues(bg, req) |
| 62 | + if codeOf(t, err) != gp.CodeValidation { |
| 63 | + t.Fatalf("request %+v: %v", req, err) |
| 64 | + } |
| 65 | + } |
| 66 | + g.forceError["GET /repos/acme/web/issues"] = fakeResponse{403, `{"message":"Resource not accessible by integration"}`, nil} |
| 67 | + _, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget()}) |
| 68 | + if codeOf(t, err) != gp.CodeForbidden { |
| 69 | + t.Fatalf("GitHub error: %v", err) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestListIssuesWireAndDefaultState(t *testing.T) { |
| 74 | + p, g, _ := seededProvider(t) |
| 75 | + fetch = func(method, rawURL string, headers map[string]string, body string) (*plugin.FetchResponse, error) { |
| 76 | + u, _ := url.Parse(rawURL) |
| 77 | + if u.Path == "/repos/acme/web/issues" { |
| 78 | + if u.Query().Get("state") != "open" { |
| 79 | + t.Fatalf("default state: %s", rawURL) |
| 80 | + } |
| 81 | + return resp(200, []any{}) |
| 82 | + } |
| 83 | + return g.fetch(method, rawURL, headers, body) |
| 84 | + } |
| 85 | + if !p.Describe().Capabilities.IssueList { |
| 86 | + t.Fatal("issue listing must be declared") |
| 87 | + } |
| 88 | + body, err := json.Marshal(gp.ListIssuesRequest{Target: webTarget()}) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + status, out := gp.Dispatch(bg, p, "list_issues", body) |
| 93 | + if status != 200 { |
| 94 | + t.Fatalf("wire status %d: %s", status, out) |
| 95 | + } |
| 96 | + var wire struct { |
| 97 | + OK bool `json:"ok"` |
| 98 | + Result gp.IssuePage `json:"result"` |
| 99 | + } |
| 100 | + if err := json.Unmarshal(out, &wire); err != nil || !wire.OK || len(wire.Result.Items) != 0 { |
| 101 | + t.Fatalf("wire result: %s (%v)", out, err) |
| 102 | + } |
| 103 | +} |
0 commit comments