Merge pull request #8843 from r266-tech/codex/oauth-deadline-clock-fixture

test(oauth): make late refresh deadline deterministic
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-11-27 17:06:15 +08:00
commit aaec3887d0
1 file changed
+18 -14
@@ -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 {