diff --git a/backend/internal/service/oauth_refresh_api_test.go b/backend/internal/service/oauth_refresh_api_test.go index 9319ed6b8f..42d99d5fd6 100644 --- a/backend/internal/service/oauth_refresh_api_test.go +++ b/backend/internal/service/oauth_refresh_api_test.go @@ -9,6 +9,7 @@ import ( "reflect" "sync" "testing" + "testing/synctest" "time" "github.com/Wei-Shaw/sub2api/internal/pkg/openai" @@ -681,22 +682,25 @@ func TestRefreshIfNeeded_RequestPathDBRereadMissingGrokRefreshCredentialReturnsP } func TestRefreshIfNeeded_LateSuccessAfterDeadlineDoesNotPersist(t *testing.T) { - account := &Account{ID: 85, Platform: PlatformGrok, Type: AccountTypeOAuth, Status: StatusActive} - repo := &refreshAPIAccountRepo{account: account} - executor := &refreshAPIExecutorStub{ - needsRefresh: true, - credentials: map[string]any{"access_token": "late-token"}, - delay: 30 * time.Millisecond, - } - api := NewOAuthRefreshAPI(repo, nil) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) - defer cancel() + synctest.Test(t, func(t *testing.T) { + account := &Account{ID: 85, Platform: PlatformGrok, Type: AccountTypeOAuth, Status: StatusActive} + repo := &refreshAPIAccountRepo{account: account} + executor := &refreshAPIExecutorStub{ + needsRefresh: true, + credentials: map[string]any{"access_token": "late-token"}, + delay: 30 * time.Millisecond, + } + api := NewOAuthRefreshAPI(repo, nil) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) + defer cancel() - result, err := api.RefreshIfNeeded(ctx, account, executor, time.Hour) + result, err := api.RefreshIfNeeded(ctx, account, executor, time.Hour) - require.ErrorIs(t, err, context.DeadlineExceeded) - require.Nil(t, result) - require.Zero(t, repo.updateCredentialsCalls, "late credentials must not cross the unified API persistence boundary") + require.Equal(t, 1, executor.refreshCalls, "the deadline must expire during the provider call") + require.ErrorIs(t, err, context.DeadlineExceeded) + require.Nil(t, result) + require.Zero(t, repo.updateCredentialsCalls, "late credentials must not cross the unified API persistence boundary") + }) } type openAIRotationClientStub struct {