|
1 | 1 | package github |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "regexp" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/shurcooL/githubv4" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "go.abhg.dev/gs/internal/forge" |
| 16 | + "go.abhg.dev/gs/internal/logtest" |
| 17 | + "go.abhg.dev/testing/stub" |
| 18 | +) |
4 | 19 |
|
5 | 20 | // SetListChangeCommentsPageSize changes the page size |
6 | 21 | // used for listing change comments. |
7 | 22 | // |
8 | 23 | // It restores the old value after the test finishes. |
9 | 24 | func SetListChangeCommentsPageSize(t testing.TB, pageSize int) { |
10 | | - old := _listChangeCommentsPageSize |
11 | | - _listChangeCommentsPageSize = pageSize |
12 | | - t.Cleanup(func() { |
13 | | - _listChangeCommentsPageSize = old |
14 | | - }) |
| 25 | + t.Cleanup(stub.Value(&_listChangeCommentsPageSize, pageSize)) |
| 26 | +} |
| 27 | + |
| 28 | +func TestListChangeComments(t *testing.T) { |
| 29 | + type commentRes struct { |
| 30 | + ID string `json:"id"` |
| 31 | + Body string `json:"body"` |
| 32 | + URL string `json:"url"` |
| 33 | + |
| 34 | + ViewerCanUpdate bool `json:"viewerCanUpdate"` |
| 35 | + ViewerDidAuthor bool `json:"viewerDidAuthor"` |
| 36 | + |
| 37 | + CreatedAt time.Time `json:"createdAt"` |
| 38 | + UpdatedAt time.Time `json:"updatedAt"` |
| 39 | + } |
| 40 | + |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + give []commentRes |
| 44 | + opts *forge.ListChangeCommentsOptions |
| 45 | + |
| 46 | + wantBodies []string |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "NoFilter", |
| 50 | + give: []commentRes{ |
| 51 | + { |
| 52 | + ID: "abc", |
| 53 | + Body: "hello", |
| 54 | + URL: "https://example.com/comment/abc", |
| 55 | + }, |
| 56 | + { |
| 57 | + ID: "def", |
| 58 | + Body: "world", |
| 59 | + URL: "https://example.com/comment/def", |
| 60 | + }, |
| 61 | + }, |
| 62 | + wantBodies: []string{"hello", "world"}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "BodyMatchesAll", |
| 66 | + give: []commentRes{ |
| 67 | + { |
| 68 | + ID: "abc", |
| 69 | + Body: "hello", |
| 70 | + URL: "https://example.com/comment/abc", |
| 71 | + }, |
| 72 | + { |
| 73 | + ID: "def", |
| 74 | + Body: "world", |
| 75 | + URL: "https://example.com/comment/def", |
| 76 | + }, |
| 77 | + }, |
| 78 | + opts: &forge.ListChangeCommentsOptions{ |
| 79 | + BodyMatchesAll: []*regexp.Regexp{ |
| 80 | + regexp.MustCompile(`d$`), |
| 81 | + }, |
| 82 | + }, |
| 83 | + wantBodies: []string{"world"}, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "CanUpdate", |
| 87 | + give: []commentRes{ |
| 88 | + { |
| 89 | + ID: "abc", |
| 90 | + Body: "hello", |
| 91 | + URL: "https://example.com/comment/abc", |
| 92 | + ViewerCanUpdate: true, |
| 93 | + }, |
| 94 | + { |
| 95 | + ID: "def", |
| 96 | + Body: "world", |
| 97 | + URL: "https://example.com/comment/def", |
| 98 | + ViewerCanUpdate: false, |
| 99 | + }, |
| 100 | + }, |
| 101 | + opts: &forge.ListChangeCommentsOptions{ |
| 102 | + CanUpdate: true, |
| 103 | + }, |
| 104 | + wantBodies: []string{"hello"}, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + response := map[string]any{ |
| 111 | + "data": map[string]any{ |
| 112 | + "node": map[string]any{ |
| 113 | + "comments": map[string]any{ |
| 114 | + "pageInfo": map[string]any{ |
| 115 | + "hasNextPage": false, |
| 116 | + }, |
| 117 | + "nodes": tt.give, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 124 | + enc := json.NewEncoder(w) |
| 125 | + enc.SetIndent("", " ") |
| 126 | + assert.NoError(t, enc.Encode(response)) |
| 127 | + })) |
| 128 | + defer srv.Close() |
| 129 | + |
| 130 | + repo, err := newRepository( |
| 131 | + context.Background(), new(Forge), |
| 132 | + "owner", "repo", |
| 133 | + logtest.New(t), |
| 134 | + githubv4.NewEnterpriseClient(srv.URL, nil), |
| 135 | + "repoID", |
| 136 | + ) |
| 137 | + require.NoError(t, err) |
| 138 | + |
| 139 | + prID := PR{Number: 1, GQLID: "prID"} |
| 140 | + |
| 141 | + ctx := context.Background() |
| 142 | + var bodies []string |
| 143 | + for comment, err := range repo.ListChangeComments(ctx, &prID, tt.opts) { |
| 144 | + require.NoError(t, err) |
| 145 | + bodies = append(bodies, comment.Body) |
| 146 | + } |
| 147 | + |
| 148 | + assert.Equal(t, tt.wantBodies, bodies) |
| 149 | + }) |
| 150 | + } |
15 | 151 | } |
0 commit comments