Files
CLIProxyAPI/internal/runtime/executor/codex_refresh_outcome_test.go
Luis Pater 68cd75724e fix(codex): prevent replay after uncertain refresh outcomes
- Preserve refresh failure stages and prevent immediate or later grant replay.
- Coordinate durable claims and recovery receipts across file, PostgreSQL, Git, and S3 stores.
- Add regression coverage for retries, concurrency, persistence, and recovery.

Closes: #7289
2026-11-07 01:44:42 +08:00

53 lines
2.2 KiB
Go

package executor
import (
"context"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
codexauth "github.com/router-for-me/CLIProxyAPI/v8/internal/auth/codex"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
)
func TestCodexUnknownRefreshSurvivesForcedRefreshAndFileAlias(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "credential.json")
if errWrite := os.WriteFile(path, []byte(`{"type":"codex","access_token":"valid","refresh_token":"alias-grant","expired":"2099-01-01T00:00:00Z"}`), 0o600); errWrite != nil {
t.Fatal(errWrite)
}
alias := filepath.Join(t.TempDir(), "alias.json")
if errSymlink := os.Symlink(path, alias); errSymlink != nil {
t.Skipf("symlink unavailable: %v", errSymlink)
}
calls := 0
original := http.DefaultTransport
http.DefaultTransport = roundTripFunc(func(req *http.Request) (*http.Response, error) {
calls++
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"access_token":`)), Request: req}, nil
})
t.Cleanup(func() { http.DefaultTransport = original })
for _, filename := range []string{path, alias} {
manager := cliproxyauth.NewManager(nil, nil, nil)
manager.RegisterExecutor(NewCodexExecutor(&config.Config{}))
auth := &cliproxyauth.Auth{ID: filename, FileName: filename, Provider: "codex", Status: cliproxyauth.StatusActive, Attributes: map[string]string{cliproxyauth.AttributePath: filename}, Metadata: map[string]any{"access_token": "valid", "refresh_token": "alias-grant", "expired": "2099-01-01T00:00:00Z"}}
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
t.Fatal(errRegister)
}
if _, errRefresh := manager.ForceRefreshAuth(context.Background(), auth.ID); !codexauth.IsRefreshOutcomeUnknown(errRefresh) {
t.Fatalf("expected unknown outcome, got %v", errRefresh)
}
updated, _ := manager.GetByID(auth.ID)
if !updated.RequiresSignIn || updated.Unavailable {
t.Fatal("must require refresh recovery but preserve valid access")
}
}
if calls != 1 {
t.Fatalf("force refresh or file alias replayed unresolved grant: calls=%d, want 1", calls)
}
}