| 500 | } |
| 501 | |
| 502 | func TestRefresher_UsesInjectedClock(t *testing.T) { |
| 503 | t.Parallel() |
| 504 | |
| 505 | mClock := quartz.NewMock(t) |
| 506 | fixedTime := time.Date(2025, 6, 15, 12, 0, 0, 0, time.UTC) |
| 507 | mClock.Set(fixedTime) |
| 508 | |
| 509 | mp := &mockProvider{ |
| 510 | parsePullRequestURL: func(raw string) (gitprovider.PRRef, bool) { |
| 511 | return gitprovider.PRRef{Owner: "org", Repo: "repo", Number: 42}, true |
| 512 | }, |
| 513 | fetchPullRequestStatus: func(_ context.Context, _ string, _ gitprovider.PRRef) (*gitprovider.PRStatus, error) { |
| 514 | return &gitprovider.PRStatus{ |
| 515 | State: gitprovider.PRStateOpen, |
| 516 | DiffStats: gitprovider.DiffStats{ |
| 517 | Additions: 10, |
| 518 | Deletions: 5, |
| 519 | ChangedFiles: 3, |
| 520 | }, |
| 521 | }, nil |
| 522 | }, |
| 523 | } |
| 524 | |
| 525 | providers := func(_ context.Context, _ string) gitprovider.Provider { return mp } |
| 526 | tokens := func(_ context.Context, _ uuid.UUID, _ string) (*string, error) { |
| 527 | return ptr.Ref("test-token"), nil |
| 528 | } |
| 529 | |
| 530 | r := gitsync.NewRefresher(providers, tokens, slogtest.Make(t, nil), mClock) |
| 531 | |
| 532 | chatID := uuid.New() |
| 533 | row := database.ChatDiffStatus{ |
| 534 | ChatID: chatID, |
| 535 | Url: sql.NullString{String: "https://github.com/org/repo/pull/42", Valid: true}, |
| 536 | GitRemoteOrigin: "https://github.com/org/repo", |
| 537 | GitBranch: "feature", |
| 538 | } |
| 539 | |
| 540 | ownerID := uuid.New() |
| 541 | results, err := r.Refresh(context.Background(), []gitsync.RefreshRequest{ |
| 542 | {Row: row, OwnerID: ownerID}, |
| 543 | }) |
| 544 | require.NoError(t, err) |
| 545 | require.Len(t, results, 1) |
| 546 | res := results[0] |
| 547 | |
| 548 | require.NoError(t, res.Error) |
| 549 | require.NotNil(t, res.Params) |
| 550 | |
| 551 | // The mock clock is deterministic, so times must be exact. |
| 552 | assert.Equal(t, fixedTime, res.Params.RefreshedAt) |
| 553 | assert.Equal(t, fixedTime.Add(gitsync.DiffStatusTTL), res.Params.StaleAt) |
| 554 | } |
| 555 | |
| 556 | func TestRefresher_RateLimitSkipsRemainingInGroup(t *testing.T) { |
| 557 | t.Parallel() |