| 110 | } |
| 111 | |
| 112 | func TestRefresher_WithPRURL(t *testing.T) { |
| 113 | t.Parallel() |
| 114 | |
| 115 | mp := &mockProvider{ |
| 116 | parsePullRequestURL: func(raw string) (gitprovider.PRRef, bool) { |
| 117 | return gitprovider.PRRef{Owner: "org", Repo: "repo", Number: 42}, true |
| 118 | }, |
| 119 | fetchPullRequestStatus: func(_ context.Context, _ string, _ gitprovider.PRRef) (*gitprovider.PRStatus, error) { |
| 120 | return &gitprovider.PRStatus{ |
| 121 | State: gitprovider.PRStateOpen, |
| 122 | DiffStats: gitprovider.DiffStats{ |
| 123 | Additions: 10, |
| 124 | Deletions: 5, |
| 125 | ChangedFiles: 3, |
| 126 | }, |
| 127 | }, nil |
| 128 | }, |
| 129 | } |
| 130 | |
| 131 | providers := func(_ context.Context, _ string) gitprovider.Provider { return mp } |
| 132 | tokens := func(_ context.Context, _ uuid.UUID, _ string) (*string, error) { |
| 133 | return ptr.Ref("test-token"), nil |
| 134 | } |
| 135 | |
| 136 | r := gitsync.NewRefresher(providers, tokens, slogtest.Make(t, nil), quartz.NewReal()) |
| 137 | |
| 138 | chatID := uuid.New() |
| 139 | row := database.ChatDiffStatus{ |
| 140 | ChatID: chatID, |
| 141 | Url: sql.NullString{String: "https://github.com/org/repo/pull/42", Valid: true}, |
| 142 | GitRemoteOrigin: "https://github.com/org/repo", |
| 143 | GitBranch: "feature", |
| 144 | } |
| 145 | |
| 146 | ownerID := uuid.New() |
| 147 | results, err := r.Refresh(context.Background(), []gitsync.RefreshRequest{ |
| 148 | {Row: row, OwnerID: ownerID}, |
| 149 | }) |
| 150 | require.NoError(t, err) |
| 151 | require.Len(t, results, 1) |
| 152 | res := results[0] |
| 153 | |
| 154 | require.NoError(t, res.Error) |
| 155 | require.NotNil(t, res.Params) |
| 156 | |
| 157 | assert.Equal(t, chatID, res.Params.ChatID) |
| 158 | assert.Equal(t, "open", res.Params.PullRequestState.String) |
| 159 | assert.True(t, res.Params.PullRequestState.Valid) |
| 160 | assert.Equal(t, int32(10), res.Params.Additions) |
| 161 | assert.Equal(t, int32(5), res.Params.Deletions) |
| 162 | assert.Equal(t, int32(3), res.Params.ChangedFiles) |
| 163 | |
| 164 | // StaleAt should be ~120s after RefreshedAt. |
| 165 | diff := res.Params.StaleAt.Sub(res.Params.RefreshedAt) |
| 166 | assert.InDelta(t, 120, diff.Seconds(), 5) |
| 167 | } |
| 168 | |
| 169 | func TestRefresher_BranchResolvesToPR(t *testing.T) { |