mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
1920 lines
64 KiB
Go
1920 lines
64 KiB
Go
package cliproxy
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/MoeForge/moego/json"
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/api"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/pluginhost"
|
|
internalregistry "github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/watcher"
|
|
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/sdk/config"
|
|
)
|
|
|
|
func TestRuntimeAuthSyncHook_SynchronousModelAndSchedulerRestoration(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-sync-test-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-sync.json",
|
|
},
|
|
}
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
// Set up a mock watcher wrapper where dispatchPersistedAuth records updates
|
|
// but DOES NOT consume or execute anything asynchronously, verifying that
|
|
// the synchronous restoration does not depend on the watcher background queue.
|
|
var persistedUpdates []watcher.AuthUpdate
|
|
watcherWrapper := &WatcherWrapper{
|
|
dispatchPersistedAuth: func(update watcher.AuthUpdate) bool {
|
|
persistedUpdates = append(persistedUpdates, update)
|
|
return true
|
|
},
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
watcher: watcherWrapper,
|
|
}
|
|
|
|
hook := service.runtimeAuthSyncHook()
|
|
if hook == nil {
|
|
t.Fatal("runtimeAuthSyncHook() returned nil")
|
|
}
|
|
|
|
// 1. Initial sync (enabled -> registers models)
|
|
if err := hook(context.Background(), auth); err != nil {
|
|
t.Fatalf("hook failed for initial sync: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
models := reg.GetModelsForClient(authID)
|
|
if len(models) == 0 {
|
|
t.Fatal("expected models registered for active auth, got none")
|
|
}
|
|
|
|
// Verify scheduler selection works for enabled auth
|
|
resp, errExec := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExec != nil || string(resp.Payload) != authID {
|
|
t.Fatalf("expected scheduler to select active auth, got resp=%q, err=%v", string(resp.Payload), errExec)
|
|
}
|
|
|
|
// 2. Disable the credential
|
|
disabledAuth := auth.Clone()
|
|
disabledAuth.Disabled = true
|
|
disabledAuth.Status = coreauth.StatusDisabled
|
|
disabledAuth.StatusMessage = "disabled via management API"
|
|
disabledAuth.UpdatedAt = time.Now()
|
|
|
|
if _, errUpdate := manager.Update(context.Background(), disabledAuth); errUpdate != nil {
|
|
t.Fatalf("manager update disable failed: %v", errUpdate)
|
|
}
|
|
if err := hook(context.Background(), disabledAuth); err != nil {
|
|
t.Fatalf("hook failed for disable: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Verify immediately: models must be unregistered from global registry
|
|
modelsAfterDisable := reg.GetModelsForClient(authID)
|
|
if len(modelsAfterDisable) != 0 {
|
|
t.Fatalf("expected 0 models for disabled client, got %d", len(modelsAfterDisable))
|
|
}
|
|
|
|
// Verify scheduler selection fails for disabled auth
|
|
if _, errExecDisabled := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{}); errExecDisabled == nil {
|
|
t.Fatal("expected scheduler execution to fail for disabled auth, but got nil error")
|
|
}
|
|
|
|
// 3. Re-enable the credential without quota
|
|
enabledAuth := disabledAuth.Clone()
|
|
enabledAuth.Disabled = false
|
|
enabledAuth.Status = coreauth.StatusActive
|
|
enabledAuth.StatusMessage = ""
|
|
enabledAuth.UpdatedAt = time.Now()
|
|
|
|
if _, errUpdate := manager.Update(context.Background(), enabledAuth); errUpdate != nil {
|
|
t.Fatalf("manager update enable failed: %v", errUpdate)
|
|
}
|
|
|
|
// 4. Re-enable hook invocation
|
|
if err := hook(context.Background(), enabledAuth); err != nil {
|
|
t.Fatalf("hook failed for re-enable: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Verify immediately: models must be restored in the global registry without restart
|
|
modelsAfterEnable := reg.GetModelsForClient(authID)
|
|
if len(modelsAfterEnable) == 0 {
|
|
t.Fatal("expected models to be restored synchronously upon re-enable, got none")
|
|
}
|
|
|
|
hasAstra := false
|
|
for _, m := range modelsAfterEnable {
|
|
if m != nil && m.ID == "gpt-6-astra" {
|
|
hasAstra = true
|
|
break
|
|
}
|
|
}
|
|
if !hasAstra {
|
|
t.Fatalf("expected gpt-6-astra in restored models: %+v", modelsAfterEnable)
|
|
}
|
|
|
|
// Verify scheduler selection is restored for the re-enabled auth
|
|
respAfterEnable, errExecEnable := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExecEnable != nil || string(respAfterEnable.Payload) != authID {
|
|
t.Fatalf("expected scheduler to select restored auth, got resp=%q, err=%v", string(respAfterEnable.Payload), errExecEnable)
|
|
}
|
|
|
|
// 5. Verify quota cooldown separation: set quota exceeded on the enabled auth
|
|
quotaAuth := enabledAuth.Clone()
|
|
quotaAuth.Quota = coreauth.QuotaState{
|
|
Exceeded: true,
|
|
Reason: "credential_quota",
|
|
NextRecoverAt: time.Now().Add(10 * time.Minute),
|
|
}
|
|
if _, errUpdate := manager.Update(context.Background(), quotaAuth); errUpdate != nil {
|
|
t.Fatalf("manager update quota failed: %v", errUpdate)
|
|
}
|
|
if err := hook(context.Background(), quotaAuth); err != nil {
|
|
t.Fatalf("hook failed for quota update: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Verify quota separation: models remain registered in global registry
|
|
modelsWithQuota := reg.GetModelsForClient(authID)
|
|
if len(modelsWithQuota) == 0 {
|
|
t.Fatal("expected models to remain registered when auth is in quota cooldown")
|
|
}
|
|
// But scheduler blocks execution due to quota exhaustion
|
|
if _, errQuotaExec := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{}); errQuotaExec == nil {
|
|
t.Fatal("expected scheduler to block execution when quota exceeded")
|
|
}
|
|
|
|
// Verify watcher notification occurred
|
|
if len(persistedUpdates) == 0 {
|
|
t.Fatal("expected watcher to be notified of persisted auth updates")
|
|
}
|
|
}
|
|
|
|
func TestAuthIDMatches(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
snapshotID string
|
|
id string
|
|
wantExact bool
|
|
wantFolded bool
|
|
}{
|
|
{name: "identical", snapshotID: "account.json", id: "account.json", wantExact: true, wantFolded: true},
|
|
{name: "windows_store_case", snapshotID: "account.json", id: "Account.json", wantFolded: true},
|
|
{name: "case_sensitive_plugin_ids", snapshotID: "plugin:Account", id: "plugin:account", wantFolded: true},
|
|
{name: "different_ids", snapshotID: "account.json", id: "other.json"},
|
|
} {
|
|
for _, foldCase := range []bool{false, true} {
|
|
t.Run(fmt.Sprintf("%s/fold_case=%t", tc.name, foldCase), func(t *testing.T) {
|
|
want := tc.wantExact
|
|
if foldCase {
|
|
want = tc.wantFolded
|
|
}
|
|
if got := authIDMatches(tc.snapshotID, tc.id, foldCase); got != want {
|
|
t.Fatalf("authIDMatches(%q, %q, %t)=%t, want %t", tc.snapshotID, tc.id, foldCase, got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_FileDeleteOrdering(t *testing.T) {
|
|
for _, mode := range []struct {
|
|
name string
|
|
withRevision bool
|
|
delayHook bool
|
|
}{
|
|
{name: "legacy_callback"},
|
|
{name: "revision_callback", withRevision: true},
|
|
{name: "before_persist_hook", withRevision: true, delayHook: true},
|
|
} {
|
|
for _, deleteBeforeSave := range []bool{true, false} {
|
|
name := fmt.Sprintf("%s/delete_before_save=%t", mode.name, deleteBeforeSave)
|
|
t.Run(name, func(t *testing.T) {
|
|
ctx := coreauth.WithSkipPersist(context.Background())
|
|
authID := "codex-file-delete-ordering.json"
|
|
path := filepath.Join(t.TempDir(), authID)
|
|
reg := GlobalModelRegistry()
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() { reg.UnregisterClient(authID) })
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
coreauth.AttributePath: path,
|
|
},
|
|
}
|
|
w, errNewWatcher := watcher.NewWatcher("", filepath.Dir(path), nil)
|
|
if errNewWatcher != nil {
|
|
t.Fatal(errNewWatcher)
|
|
}
|
|
w.SetConfig(&config.Config{AuthDir: filepath.Dir(path)})
|
|
queue := make(chan watcher.AuthUpdate, 8)
|
|
w.SetAuthUpdateQueue(queue)
|
|
t.Cleanup(func() {
|
|
if errStop := w.Stop(); errStop != nil {
|
|
t.Errorf("stop watcher: %v", errStop)
|
|
}
|
|
})
|
|
wrapper := &WatcherWrapper{dispatchPersistedAuth: w.DispatchPersistedAuthUpdate, snapshotAuthFile: w.SnapshotAuthFile}
|
|
if mode.withRevision {
|
|
wrapper.dispatchPersistedAuthWithRev = w.DispatchPersistedAuthUpdateWithRevision
|
|
}
|
|
service := &Service{cfg: &config.Config{}, coreManager: manager, watcher: wrapper}
|
|
hook := service.runtimeAuthSyncHook()
|
|
save := func(syncHook bool) {
|
|
t.Helper()
|
|
if errWriteFile := os.WriteFile(path, []byte(`{"type":"codex"}`), 0o600); errWriteFile != nil {
|
|
t.Fatalf("write auth file: %v", errWriteFile)
|
|
}
|
|
if _, errRegister := manager.Register(ctx, auth.Clone()); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
if syncHook {
|
|
if errHook := hook(ctx, auth.Clone()); errHook != nil {
|
|
t.Fatalf("sync persisted auth: %v", errHook)
|
|
}
|
|
}
|
|
}
|
|
nextUpdate := func() watcher.AuthUpdate {
|
|
t.Helper()
|
|
select {
|
|
case update := <-queue:
|
|
return update
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("watcher did not dispatch auth update")
|
|
return watcher.AuthUpdate{}
|
|
}
|
|
}
|
|
observeDelete := func() watcher.AuthUpdate {
|
|
t.Helper()
|
|
if errRemove := os.Remove(path); errRemove != nil {
|
|
t.Fatalf("remove auth file: %v", errRemove)
|
|
}
|
|
// Use the same watcher stamping/dispatch machinery as file deletes,
|
|
// without depending on fsnotify scheduling or debounce timers.
|
|
deletion := watcher.AuthUpdate{Action: watcher.AuthUpdateActionDelete, ID: authID}
|
|
deletion.SetFileRemoval(true)
|
|
if !w.DispatchRuntimeAuthUpdate(deletion) {
|
|
t.Fatal("delete was not dispatched")
|
|
}
|
|
update := nextUpdate()
|
|
if update.Action != watcher.AuthUpdateActionDelete || update.Auth != nil || update.Revision() == 0 || !update.IsFileRemoval() {
|
|
t.Fatalf("expected stamped bare watcher delete, got %+v", update)
|
|
}
|
|
return update
|
|
}
|
|
|
|
save(true)
|
|
service.handleAuthUpdate(ctx, nextUpdate())
|
|
var deletion watcher.AuthUpdate
|
|
if deleteBeforeSave {
|
|
deletion = observeDelete()
|
|
}
|
|
// Management uploads publish to coreManager before invoking the persist
|
|
// hook. Exercise that gap as well as the legacy callback's lost revision.
|
|
save(!mode.delayHook)
|
|
if !mode.delayHook {
|
|
// Hold the queued add so restoration depends on the inline persist hook.
|
|
addition := nextUpdate()
|
|
if deleteBeforeSave && addition.Revision() <= deletion.Revision() {
|
|
t.Fatal("re-created auth must have a newer watcher revision")
|
|
}
|
|
}
|
|
if !deleteBeforeSave {
|
|
deletion = observeDelete()
|
|
}
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models registered before delivering delete")
|
|
}
|
|
service.handleAuthUpdate(ctx, deletion)
|
|
_, present := manager.GetByID(authID)
|
|
modelsPresent := len(reg.GetModelsForClient(authID)) > 0
|
|
if present != deleteBeforeSave || modelsPresent != deleteBeforeSave {
|
|
t.Fatalf("after delete: auth present=%t, models present=%t; want both %t", present, modelsPresent, deleteBeforeSave)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_FileDeleteRecreatedSubset(t *testing.T) {
|
|
ctx := coreauth.WithSkipPersist(context.Background())
|
|
path := filepath.Join(t.TempDir(), "accounts.json")
|
|
write := func(data string) {
|
|
t.Helper()
|
|
if errWriteFile := os.WriteFile(path, []byte(data), 0o600); errWriteFile != nil {
|
|
t.Fatal(errWriteFile)
|
|
}
|
|
}
|
|
write(`{"type":"plugin","ids":["a","b"]}`)
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
reg := GlobalModelRegistry()
|
|
auths := make([]*coreauth.Auth, 0, 2)
|
|
deletes := make([]watcher.AuthUpdate, 0, 2)
|
|
for index, id := range []string{"codex-recreated-subset-a", "codex-recreated-subset-b"} {
|
|
reg.UnregisterClient(id)
|
|
t.Cleanup(func() { reg.UnregisterClient(id) })
|
|
auth := &coreauth.Auth{ID: id, Provider: "codex", Status: coreauth.StatusActive, Attributes: map[string]string{coreauth.AttributePath: path}}
|
|
coreauth.MarkPluginVirtualAuth(auth, path, index)
|
|
auths = append(auths, auth)
|
|
deletion := watcher.AuthUpdate{Action: watcher.AuthUpdateActionDelete, ID: id}
|
|
deletion.SetFileRemoval(true)
|
|
deletion.SetRevision(2)
|
|
deletes = append(deletes, deletion)
|
|
}
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
watcher: &WatcherWrapper{snapshotAuthFile: func(snapshotPath string) ([]*coreauth.Auth, error) {
|
|
if snapshotPath != path {
|
|
t.Fatalf("snapshot path=%q, want %q", snapshotPath, path)
|
|
}
|
|
// The re-created source file contains only B, independent of watcher state.
|
|
return []*coreauth.Auth{auths[1].Clone()}, nil
|
|
}},
|
|
}
|
|
for _, auth := range auths {
|
|
addition := watcher.AuthUpdate{Action: watcher.AuthUpdateActionAdd, ID: auth.ID, Auth: auth}
|
|
addition.SetRevision(1)
|
|
service.handleAuthUpdate(ctx, addition)
|
|
if len(reg.GetModelsForClient(auth.ID)) == 0 {
|
|
t.Fatalf("expected initial models for %s", auth.ID)
|
|
}
|
|
}
|
|
if errRemove := os.Remove(path); errRemove != nil {
|
|
t.Fatal(errRemove)
|
|
}
|
|
write(`{"type":"plugin","ids":["b"]}`)
|
|
// Do not deliver the create event: the delayed deletes must reconcile IDs themselves.
|
|
service.handleAuthUpdates(ctx, deletes)
|
|
if _, present := manager.GetByID(auths[0].ID); present || len(reg.GetModelsForClient(auths[0].ID)) != 0 {
|
|
t.Fatal("auth A, absent from the re-created file, must be removed with its models")
|
|
}
|
|
if _, present := manager.GetByID(auths[1].ID); !present || len(reg.GetModelsForClient(auths[1].ID)) == 0 {
|
|
t.Fatal("auth B must remain present with its models")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_FileDeleteSnapshotUncertain(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
watcher *WatcherWrapper
|
|
}{
|
|
{name: "unavailable"},
|
|
{name: "empty", watcher: &WatcherWrapper{snapshotAuthFile: func(string) ([]*coreauth.Auth, error) { return nil, nil }}},
|
|
{name: "invalid_ids", watcher: &WatcherWrapper{snapshotAuthFile: func(string) ([]*coreauth.Auth, error) {
|
|
return []*coreauth.Auth{nil, {}}, nil
|
|
}}},
|
|
{name: "error_with_partial_result", watcher: &WatcherWrapper{snapshotAuthFile: func(string) ([]*coreauth.Auth, error) {
|
|
return []*coreauth.Auth{{ID: "other-auth"}}, fmt.Errorf("simulated parse failure")
|
|
}}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx := coreauth.WithSkipPersist(context.Background())
|
|
path := filepath.Join(t.TempDir(), "auth.json")
|
|
if errWriteFile := os.WriteFile(path, []byte(`{"type":`), 0o600); errWriteFile != nil {
|
|
t.Fatal(errWriteFile)
|
|
}
|
|
authID := "codex-uncertain-file-delete-" + tc.name
|
|
reg := GlobalModelRegistry()
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() { reg.UnregisterClient(authID) })
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
auth := &coreauth.Auth{ID: authID, Provider: "codex", Status: coreauth.StatusActive, Attributes: map[string]string{coreauth.AttributePath: path}}
|
|
service := &Service{cfg: &config.Config{}, coreManager: manager, watcher: tc.watcher}
|
|
service.handleAuthUpdate(ctx, watcher.AuthUpdate{Action: watcher.AuthUpdateActionAdd, ID: authID, Auth: auth})
|
|
deletion := watcher.AuthUpdate{Action: watcher.AuthUpdateActionDelete, ID: authID}
|
|
deletion.SetFileRemoval(true)
|
|
service.handleAuthUpdate(ctx, deletion)
|
|
if _, present := manager.GetByID(authID); !present || len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("uncertain file snapshot must preserve the auth and its models")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_FileDeleteScope(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
attributes map[string]string
|
|
withSnapshot bool
|
|
unmarked bool
|
|
wantPresent bool
|
|
}{
|
|
{name: "file_path", attributes: map[string]string{coreauth.AttributePath: "file"}, wantPresent: true},
|
|
{name: "file_source", attributes: map[string]string{coreauth.AttributeSource: "file"}, wantPresent: true},
|
|
{name: "git", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributeSourceBackend: coreauth.AuthSourceGit}, wantPresent: true},
|
|
{name: "postgres", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributeSourceBackend: coreauth.AuthSourcePostgres}, wantPresent: true},
|
|
{name: "objectstore", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributeSourceBackend: coreauth.AuthSourceObjectStore}, wantPresent: true},
|
|
{name: "plugin_virtual_unmarked", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributePluginVirtual: "true"}, unmarked: true},
|
|
{name: "file_unmarked", attributes: map[string]string{coreauth.AttributePath: "file"}, unmarked: true},
|
|
{name: "remote_without_local_path", attributes: map[string]string{coreauth.AttributeSource: "file", coreauth.AttributeSourceBackend: coreauth.AuthSourceGit}},
|
|
{name: "runtime_only", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributeRuntimeOnly: "true"}},
|
|
{name: "runtime_only_remote", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributeRuntimeOnly: "true", coreauth.AttributeSourceBackend: coreauth.AuthSourceGit}},
|
|
{name: "config", attributes: map[string]string{coreauth.AttributePath: "file", coreauth.AttributeSource: "config:codex[0]"}},
|
|
{name: "api_key", attributes: map[string]string{coreauth.AttributeAPIKey: "test-key"}},
|
|
{name: "snapshot", attributes: map[string]string{coreauth.AttributePath: "file"}, withSnapshot: true},
|
|
} {
|
|
for _, filePresent := range []bool{true, false} {
|
|
t.Run(fmt.Sprintf("%s/file_present=%t", tc.name, filePresent), func(t *testing.T) {
|
|
ctx := coreauth.WithSkipPersist(context.Background())
|
|
authID := "codex-file-delete-scope-" + tc.name
|
|
path := filepath.Join(t.TempDir(), "auth.json")
|
|
if errWriteFile := os.WriteFile(path, []byte(`{"type":"codex"}`), 0o600); errWriteFile != nil {
|
|
t.Fatalf("write auth file: %v", errWriteFile)
|
|
}
|
|
attributes := make(map[string]string, len(tc.attributes))
|
|
for key, value := range tc.attributes {
|
|
if value == "file" {
|
|
value = path
|
|
}
|
|
attributes[key] = value
|
|
}
|
|
reg := GlobalModelRegistry()
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() { reg.UnregisterClient(authID) })
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
auth := &coreauth.Auth{ID: authID, Provider: "codex", Status: coreauth.StatusActive, Attributes: attributes}
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
watcher: &WatcherWrapper{snapshotAuthFile: func(snapshotPath string) ([]*coreauth.Auth, error) {
|
|
if snapshotPath != path {
|
|
t.Fatalf("snapshot path=%q, want %q", snapshotPath, path)
|
|
}
|
|
return []*coreauth.Auth{auth.Clone()}, nil
|
|
}},
|
|
}
|
|
service.handleAuthUpdate(ctx, watcher.AuthUpdate{Action: watcher.AuthUpdateActionAdd, ID: authID, Auth: auth})
|
|
modelsBeforeDelete := len(reg.GetModelsForClient(authID)) > 0
|
|
if !modelsBeforeDelete && tc.name != "api_key" {
|
|
t.Fatal("expected models registered before delete")
|
|
}
|
|
if !filePresent {
|
|
if errRemove := os.Remove(path); errRemove != nil {
|
|
t.Fatalf("remove auth file: %v", errRemove)
|
|
}
|
|
}
|
|
deletion := watcher.AuthUpdate{Action: watcher.AuthUpdateActionDelete, ID: authID}
|
|
deletion.SetFileRemoval(!tc.unmarked)
|
|
if tc.withSnapshot {
|
|
deletion.Auth, _ = manager.GetByID(authID)
|
|
}
|
|
service.handleAuthUpdate(ctx, deletion)
|
|
_, present := manager.GetByID(authID)
|
|
modelsPresent := len(reg.GetModelsForClient(authID)) > 0
|
|
wantPresent := tc.wantPresent && filePresent
|
|
wantModelsPresent := wantPresent && modelsBeforeDelete
|
|
if present != wantPresent || modelsPresent != wantModelsPresent {
|
|
t.Fatalf("after delete: auth present=%t, models present=%t; want auth=%t, models=%t", present, modelsPresent, wantPresent, wantModelsPresent)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
type syncTestExecutor struct{}
|
|
|
|
func (e *syncTestExecutor) Identifier() string { return "codex" }
|
|
|
|
func (e *syncTestExecutor) Execute(ctx context.Context, a *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
|
if a != nil && a.Metadata != nil {
|
|
if tok, ok := a.Metadata["access_token"].(string); ok && tok != "" {
|
|
return cliproxyexecutor.Response{Payload: []byte(tok)}, nil
|
|
}
|
|
}
|
|
return cliproxyexecutor.Response{Payload: []byte(a.ID)}, nil
|
|
}
|
|
|
|
func (e *syncTestExecutor) ExecuteStream(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
|
|
return nil, &coreauth.Error{HTTPStatus: http.StatusNotImplemented, Message: "not implemented"}
|
|
}
|
|
|
|
func (e *syncTestExecutor) Refresh(_ context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) {
|
|
return auth, nil
|
|
}
|
|
|
|
func (e *syncTestExecutor) CountTokens(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
|
return cliproxyexecutor.Response{}, &coreauth.Error{HTTPStatus: http.StatusNotImplemented, Message: "not implemented"}
|
|
}
|
|
|
|
func (e *syncTestExecutor) HttpRequest(context.Context, *coreauth.Auth, *http.Request) (*http.Response, error) {
|
|
return nil, &coreauth.Error{HTTPStatus: http.StatusNotImplemented, Message: "not implemented"}
|
|
}
|
|
|
|
func TestRuntimeAuthSyncHook_NilAndEmptyAuth(t *testing.T) {
|
|
service := &Service{}
|
|
hook := service.runtimeAuthSyncHook()
|
|
if hook == nil {
|
|
t.Fatal("runtimeAuthSyncHook() returned nil")
|
|
}
|
|
|
|
if err := hook(context.Background(), nil); err != nil {
|
|
t.Fatalf("expected nil error for nil auth, got: %v", err)
|
|
}
|
|
|
|
if err := hook(context.Background(), &coreauth.Auth{}); err != nil {
|
|
t.Fatalf("expected nil error for empty auth, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeAuthSyncHook_DoesNotBlockOnUnrelatedAntigravityProbes(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-probe-wait-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-probe-wait.json",
|
|
},
|
|
}
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
pluginHost: pluginhost.New(),
|
|
}
|
|
service.antigravityProbeWg.Add(1)
|
|
done := make(chan error, 1)
|
|
finished := make(chan struct{})
|
|
t.Cleanup(func() {
|
|
service.antigravityProbeWg.Done()
|
|
select {
|
|
case <-finished:
|
|
case <-time.After(2 * time.Second):
|
|
}
|
|
})
|
|
|
|
hook := service.runtimeAuthSyncHook()
|
|
if hook == nil {
|
|
t.Fatal("runtimeAuthSyncHook() returned nil")
|
|
}
|
|
|
|
go func() {
|
|
defer close(finished)
|
|
done <- hook(context.Background(), auth)
|
|
}()
|
|
|
|
select {
|
|
case errHook := <-done:
|
|
if errHook != nil {
|
|
t.Fatalf("hook failed: %v", errHook)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("runtimeAuthSyncHook blocked waiting for unrelated antigravity probes")
|
|
}
|
|
|
|
models := reg.GetModelsForClient(authID)
|
|
if len(models) == 0 {
|
|
t.Fatal("expected models registered for enabled auth, got none")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_DeleteDoesNotBlockOnUnrelatedAntigravityProbes(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-probe-wait-delete-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-probe-wait-delete.json",
|
|
},
|
|
}
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
pluginHost: pluginhost.New(),
|
|
}
|
|
service.registerModelsForAuth(context.Background(), auth)
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models registered before delete")
|
|
}
|
|
|
|
runAuthUpdateWithoutProbeWait(t, service, func() {
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionDelete,
|
|
ID: authID,
|
|
Auth: auth,
|
|
})
|
|
})
|
|
|
|
if _, ok := manager.GetByID(authID); ok {
|
|
t.Fatal("expected auth to be removed")
|
|
}
|
|
if len(reg.GetModelsForClient(authID)) != 0 {
|
|
t.Fatal("expected models unregistered after delete")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_PluginVirtualModifyDoesNotBlockOnUnrelatedAntigravityProbes(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "plugin-virtual-probe-wait-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/plugin-virtual-probe-wait.json",
|
|
},
|
|
}
|
|
coreauth.MarkPluginVirtualAuth(auth, "/path/to/plugin-source.json", 0)
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
pluginHost: pluginhost.New(),
|
|
}
|
|
|
|
runAuthUpdateWithoutProbeWait(t, service, func() {
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: auth,
|
|
})
|
|
})
|
|
}
|
|
|
|
func runAuthUpdateWithoutProbeWait(t *testing.T, service *Service, run func()) {
|
|
t.Helper()
|
|
if service == nil {
|
|
t.Fatal("service is nil")
|
|
}
|
|
service.antigravityProbeWg.Add(1)
|
|
done := make(chan struct{})
|
|
finished := make(chan struct{})
|
|
t.Cleanup(func() {
|
|
service.antigravityProbeWg.Done()
|
|
select {
|
|
case <-finished:
|
|
case <-time.After(2 * time.Second):
|
|
}
|
|
})
|
|
|
|
go func() {
|
|
defer close(finished)
|
|
run()
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("auth update blocked waiting for unrelated antigravity probes")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_ModelRegistrationDoesNotHoldAuthUpdateLock(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authAID := "codex-lock-a-auth"
|
|
authBID := "codex-lock-b-auth"
|
|
reg.UnregisterClient(authAID)
|
|
reg.UnregisterClient(authBID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authAID)
|
|
reg.UnregisterClient(authBID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
authA := &coreauth.Auth{
|
|
ID: authAID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-lock-a.json",
|
|
},
|
|
}
|
|
authB := &coreauth.Auth{
|
|
ID: authBID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-lock-b.json",
|
|
},
|
|
}
|
|
for _, auth := range []*coreauth.Auth{authA, authB} {
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth %s: %v", auth.ID, errRegister)
|
|
}
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
}
|
|
|
|
started := make(chan struct{})
|
|
block := make(chan struct{})
|
|
var first atomic.Bool
|
|
modelRegistrationTaskHook = func() {
|
|
if first.CompareAndSwap(false, true) {
|
|
close(started)
|
|
<-block
|
|
}
|
|
}
|
|
t.Cleanup(func() {
|
|
modelRegistrationTaskHook = nil
|
|
select {
|
|
case <-block:
|
|
default:
|
|
close(block)
|
|
}
|
|
})
|
|
|
|
finishedA := make(chan struct{})
|
|
go func() {
|
|
defer close(finishedA)
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authAID,
|
|
Auth: authA,
|
|
})
|
|
}()
|
|
|
|
select {
|
|
case <-started:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("first auth update did not reach model registration")
|
|
}
|
|
|
|
doneB := make(chan struct{})
|
|
go func() {
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authBID,
|
|
Auth: authB,
|
|
})
|
|
close(doneB)
|
|
}()
|
|
|
|
select {
|
|
case <-doneB:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("second auth update blocked by first auth model registration")
|
|
}
|
|
|
|
close(block)
|
|
select {
|
|
case <-finishedA:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("first auth update did not finish after registration was unblocked")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_OlderGenerationDoesNotOverrideNewerPersist(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-generation-stale-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-generation-stale.json",
|
|
},
|
|
}
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
}
|
|
|
|
disabledAuth := auth.Clone()
|
|
disabledAuth.Disabled = true
|
|
disabledAuth.Status = coreauth.StatusDisabled
|
|
disabledSnapshot, errDisable := manager.Update(context.Background(), disabledAuth)
|
|
if errDisable != nil {
|
|
t.Fatalf("disable update: %v", errDisable)
|
|
}
|
|
|
|
enabledAuth := disabledSnapshot.Clone()
|
|
enabledAuth.Disabled = false
|
|
enabledAuth.Status = coreauth.StatusActive
|
|
enabledSnapshot, errEnable := manager.Update(context.Background(), enabledAuth)
|
|
if errEnable != nil {
|
|
t.Fatalf("enable update: %v", errEnable)
|
|
}
|
|
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: enabledSnapshot,
|
|
})
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models after enable")
|
|
}
|
|
|
|
staleDisable := watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: disabledSnapshot,
|
|
}
|
|
staleDisable.SetRevision(99)
|
|
service.handleAuthUpdate(context.Background(), staleDisable)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
current, ok := manager.GetByID(authID)
|
|
if !ok || current == nil || current.Disabled {
|
|
t.Fatal("expected newer enabled persist to win over older disable snapshot")
|
|
}
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models to remain registered after stale disable update")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_StaleDisableRegistrationDoesNotDropNewerEnable(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-stale-disable-task-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": "/path/to/codex-stale-disable-task.json",
|
|
},
|
|
}
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
}
|
|
|
|
disabledAuth := auth.Clone()
|
|
disabledAuth.Disabled = true
|
|
disabledAuth.Status = coreauth.StatusDisabled
|
|
disabledSnapshot, errDisable := manager.Update(context.Background(), disabledAuth)
|
|
if errDisable != nil {
|
|
t.Fatalf("disable update: %v", errDisable)
|
|
}
|
|
|
|
started := make(chan struct{})
|
|
block := make(chan struct{})
|
|
var first atomic.Bool
|
|
modelRegistrationTaskHook = func() {
|
|
if first.CompareAndSwap(false, true) {
|
|
close(started)
|
|
<-block
|
|
}
|
|
}
|
|
t.Cleanup(func() {
|
|
modelRegistrationTaskHook = nil
|
|
select {
|
|
case <-block:
|
|
default:
|
|
close(block)
|
|
}
|
|
})
|
|
|
|
finishedDisable := make(chan struct{})
|
|
go func() {
|
|
defer close(finishedDisable)
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: disabledSnapshot,
|
|
})
|
|
}()
|
|
|
|
select {
|
|
case <-started:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("disable registration did not start")
|
|
}
|
|
|
|
enabledAuth := disabledSnapshot.Clone()
|
|
enabledAuth.Disabled = false
|
|
enabledAuth.Status = coreauth.StatusActive
|
|
enabledSnapshot, errEnable := manager.Update(context.Background(), enabledAuth)
|
|
if errEnable != nil {
|
|
t.Fatalf("enable update: %v", errEnable)
|
|
}
|
|
service.handleAuthUpdate(context.Background(), watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: enabledSnapshot,
|
|
})
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models after enable")
|
|
}
|
|
|
|
close(block)
|
|
select {
|
|
case <-finishedDisable:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("disable registration did not finish")
|
|
}
|
|
|
|
current, ok := manager.GetByID(authID)
|
|
if !ok || current == nil || current.Disabled {
|
|
t.Fatal("expected auth to remain enabled after stale disable registration")
|
|
}
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models to remain registered after stale disable registration")
|
|
}
|
|
}
|
|
|
|
func TestHandleAuthUpdates_SameRevisionWaitDoesNotWaitForOtherAuthInBatch(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authAID := "codex-batch-wait-a-auth"
|
|
authBID := "codex-batch-wait-b-auth"
|
|
reg.UnregisterClient(authAID)
|
|
reg.UnregisterClient(authBID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authAID)
|
|
reg.UnregisterClient(authBID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
authA := &coreauth.Auth{
|
|
ID: authAID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{"plan_type": "pro", "path": "/path/to/codex-batch-wait-a.json"},
|
|
}
|
|
authB := &coreauth.Auth{
|
|
ID: authBID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{"plan_type": "pro", "path": "/path/to/codex-batch-wait-b.json"},
|
|
}
|
|
for _, auth := range []*coreauth.Auth{authA, authB} {
|
|
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth %s: %v", auth.ID, errRegister)
|
|
}
|
|
}
|
|
|
|
service := &Service{cfg: &config.Config{}, coreManager: manager}
|
|
|
|
bStarted := make(chan struct{})
|
|
bBlock := make(chan struct{})
|
|
var started atomic.Int32
|
|
modelRegistrationTaskHook = func() {
|
|
if started.Add(1) == 2 {
|
|
close(bStarted)
|
|
<-bBlock
|
|
}
|
|
}
|
|
t.Cleanup(func() {
|
|
modelRegistrationTaskHook = nil
|
|
select {
|
|
case <-bBlock:
|
|
default:
|
|
close(bBlock)
|
|
}
|
|
})
|
|
|
|
updateA := watcher.AuthUpdate{Action: watcher.AuthUpdateActionModify, ID: authAID, Auth: authA}
|
|
updateA.SetRevision(1)
|
|
updateB := watcher.AuthUpdate{Action: watcher.AuthUpdateActionModify, ID: authBID, Auth: authB}
|
|
updateB.SetRevision(1)
|
|
|
|
finishedBatch := make(chan struct{})
|
|
go func() {
|
|
defer close(finishedBatch)
|
|
service.handleAuthUpdates(context.Background(), []watcher.AuthUpdate{updateA, updateB})
|
|
}()
|
|
|
|
select {
|
|
case <-bStarted:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("second auth registration in batch did not start")
|
|
}
|
|
|
|
doneA := make(chan struct{})
|
|
go func() {
|
|
service.handleAuthUpdate(context.Background(), updateA)
|
|
close(doneA)
|
|
}()
|
|
select {
|
|
case <-doneA:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("auth A hook wait blocked on unrelated auth B registration")
|
|
}
|
|
|
|
close(bBlock)
|
|
select {
|
|
case <-finishedBatch:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("batch auth update did not finish")
|
|
}
|
|
}
|
|
|
|
func TestEndToEndStatusPatch_RestoresModelsThroughServerPipeline(t *testing.T) {
|
|
authDir := t.TempDir()
|
|
fileName := "codex-e2e.json"
|
|
filePath := filepath.Join(authDir, fileName)
|
|
if errWrite := os.WriteFile(filePath, []byte(`{"type":"codex","disabled":false}`), 0o600); errWrite != nil {
|
|
t.Fatalf("write auth file: %v", errWrite)
|
|
}
|
|
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := fileName
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
secretKey := "test-secret"
|
|
hashedSecret, errHash := bcrypt.GenerateFromPassword([]byte(secretKey), bcrypt.DefaultCost)
|
|
if errHash != nil {
|
|
t.Fatalf("bcrypt hash failed: %v", errHash)
|
|
}
|
|
|
|
cfg := &config.Config{
|
|
AuthDir: authDir,
|
|
RemoteManagement: config.RemoteManagement{
|
|
SecretKey: string(hashedSecret),
|
|
AllowRemote: true,
|
|
},
|
|
}
|
|
|
|
service, errBuild := NewBuilder().
|
|
WithConfig(cfg).
|
|
WithConfigPath(filepath.Join(authDir, "config.yaml")).
|
|
Build()
|
|
if errBuild != nil {
|
|
t.Fatalf("Build() failed: %v", errBuild)
|
|
}
|
|
|
|
server := api.NewServer(service.cfg, service.coreManager, service.accessManager, service.configPath, service.serverOptions...)
|
|
if server == nil {
|
|
t.Fatal("NewServer() returned nil")
|
|
}
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
FileName: fileName,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
"path": filePath,
|
|
},
|
|
}
|
|
if _, errRegister := service.coreManager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
// Initial model sync
|
|
syncHook := service.runtimeAuthSyncHook()
|
|
if err := syncHook(context.Background(), auth); err != nil {
|
|
t.Fatalf("initial sync failed: %v", err)
|
|
}
|
|
|
|
handler := server.Handler()
|
|
|
|
// 1. Confirm models exist before disabling
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/v0/management/auth-files/models?name="+fileName, nil)
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GetAuthFileModels initial status = %d, body = %s", rec.Code, rec.Body.String())
|
|
}
|
|
var resp struct {
|
|
Models []map[string]any `json:"models"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil || len(resp.Models) == 0 {
|
|
t.Fatalf("expected initial models, got %s", rec.Body.String())
|
|
}
|
|
|
|
// 2. Disable credential via PATCH /v0/management/auth-files/status
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/status", strings.NewReader(`{"name":"`+fileName+`","disabled":true}`))
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("PATCH status disable failed: %s", rec.Body.String())
|
|
}
|
|
|
|
// 3. Confirm GET /v0/management/auth-files/models returns empty list immediately
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/v0/management/auth-files/models?name="+fileName, nil)
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GetAuthFileModels after disable status = %d, body = %s", rec.Code, rec.Body.String())
|
|
}
|
|
resp.Models = nil
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil || len(resp.Models) != 0 {
|
|
t.Fatalf("expected 0 models after disable, got: %s", rec.Body.String())
|
|
}
|
|
|
|
// 4. Re-enable credential via PATCH /v0/management/auth-files/status
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/status", strings.NewReader(`{"name":"`+fileName+`","disabled":false}`))
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("PATCH status re-enable failed: %s", rec.Body.String())
|
|
}
|
|
|
|
// 5. Confirm models are synchronously restored immediately without restart
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/v0/management/auth-files/models?name="+fileName, nil)
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GetAuthFileModels after re-enable status = %d, body = %s", rec.Code, rec.Body.String())
|
|
}
|
|
resp.Models = nil
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil || len(resp.Models) == 0 {
|
|
t.Fatalf("expected models to be restored after re-enable, got: %s", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestRuntimeAuthSync_StaleAsyncEventDoesNotOverwriteSynchronousEnable(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-stale-test-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
var currentRevision uint64
|
|
watcherWrapper := &WatcherWrapper{
|
|
dispatchPersistedAuthWithRev: func(update *watcher.AuthUpdate) (bool, uint64) {
|
|
currentRevision++
|
|
return true, currentRevision
|
|
},
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
watcher: watcherWrapper,
|
|
}
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
},
|
|
}
|
|
registeredAuth, errRegister := manager.Register(context.Background(), auth)
|
|
if errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
hook := service.runtimeAuthSyncHook()
|
|
|
|
// Initial sync
|
|
if err := hook(context.Background(), registeredAuth); err != nil {
|
|
t.Fatalf("initial hook sync failed: %v", err)
|
|
}
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models after initial sync")
|
|
}
|
|
|
|
// Step 1: Credential is disabled (Generation advances)
|
|
disabledAuth := registeredAuth.Clone()
|
|
disabledAuth.Disabled = true
|
|
disabledAuth.Status = coreauth.StatusDisabled
|
|
disabledAuth.UpdatedAt = time.Now()
|
|
|
|
updatedDisabled, errDisable := manager.Update(context.Background(), disabledAuth)
|
|
if errDisable != nil {
|
|
t.Fatalf("update disable failed: %v", errDisable)
|
|
}
|
|
|
|
// Capture the stale async event that would normally sit in an async queue
|
|
staleAsyncDisableUpdate := watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: updatedDisabled.Clone(),
|
|
}
|
|
staleAsyncDisableUpdate.SetRevision(2)
|
|
|
|
// Step 2: Synchronous disable hook runs (assigns revision 2)
|
|
if err := hook(context.Background(), updatedDisabled); err != nil {
|
|
t.Fatalf("hook disable failed: %v", err)
|
|
}
|
|
if len(reg.GetModelsForClient(authID)) != 0 {
|
|
t.Fatal("expected 0 models after disable")
|
|
}
|
|
|
|
// Step 3: Credential is immediately re-enabled (advances revision to 3)
|
|
enabledAuth := updatedDisabled.Clone()
|
|
enabledAuth.Disabled = false
|
|
enabledAuth.Status = coreauth.StatusActive
|
|
enabledAuth.UpdatedAt = time.Now()
|
|
|
|
updatedEnabled, errEnable := manager.Update(context.Background(), enabledAuth)
|
|
if errEnable != nil {
|
|
t.Fatalf("update enable failed: %v", errEnable)
|
|
}
|
|
|
|
// Step 4: Synchronous enable hook runs (assigns revision 3)
|
|
if err := hook(context.Background(), updatedEnabled); err != nil {
|
|
t.Fatalf("hook enable failed: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Models must be restored immediately
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models restored immediately upon synchronous enable")
|
|
}
|
|
|
|
// Step 5: The delayed stale disable update from Step 1 is delivered now via consumer
|
|
service.handleAuthUpdate(context.Background(), staleAsyncDisableUpdate)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Step 6: Verify the stale async event was prevented from overwriting the newer enable state!
|
|
currentAuth, ok := manager.GetByID(authID)
|
|
if !ok || currentAuth == nil {
|
|
t.Fatal("expected auth to exist in manager")
|
|
}
|
|
if currentAuth.Disabled {
|
|
t.Fatal("stale async event overwrote the synchronous enable state in manager!")
|
|
}
|
|
modelsAfterStale := reg.GetModelsForClient(authID)
|
|
if len(modelsAfterStale) == 0 {
|
|
t.Fatal("stale async event unregistered models from global registry!")
|
|
}
|
|
|
|
// Scheduler execution must still succeed and select the enabled auth
|
|
resp, errExec := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExec != nil || string(resp.Payload) != authID {
|
|
t.Fatalf("expected scheduler to select active auth, got resp=%q err=%v", string(resp.Payload), errExec)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeAuthSync_StaleWatcherUnversionedEventDoesNotOverwriteSynchronousEnable(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-unversioned-stale-test-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
var currentRevision uint64
|
|
watcherWrapper := &WatcherWrapper{
|
|
dispatchPersistedAuthWithRev: func(update *watcher.AuthUpdate) (bool, uint64) {
|
|
currentRevision++
|
|
return true, currentRevision
|
|
},
|
|
}
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
watcher: watcherWrapper,
|
|
}
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
},
|
|
}
|
|
registeredAuth, errRegister := manager.Register(context.Background(), auth)
|
|
if errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
hook := service.runtimeAuthSyncHook()
|
|
|
|
// Initial sync
|
|
if err := hook(context.Background(), registeredAuth); err != nil {
|
|
t.Fatalf("initial hook sync failed: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models after initial sync")
|
|
}
|
|
|
|
// Step 1: Simulate the watcher generating an unversioned Auth snapshot during a disable
|
|
// event on disk (as synthesizer.SynthesizeAuthFile produces: RegistrationEpoch=0, Generation=0).
|
|
// Timestamp is recorded at observation time T1.
|
|
unversionedDisableTime := time.Now().Add(-50 * time.Millisecond)
|
|
unversionedDisableAuth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Disabled: true,
|
|
Status: coreauth.StatusDisabled,
|
|
CreatedAt: unversionedDisableTime,
|
|
UpdatedAt: unversionedDisableTime,
|
|
RegistrationEpoch: 0, // Unversioned from file watcher synthesizer
|
|
Generation: 0, // Unversioned from file watcher synthesizer
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
},
|
|
}
|
|
delayedWatcherUpdate := watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: unversionedDisableAuth,
|
|
}
|
|
delayedWatcherUpdate.SetRevision(2)
|
|
|
|
// Step 2: In the meantime, the credential was synchronously re-enabled via management API
|
|
// at T2 > T1, updating coreManager with RegistrationEpoch >= 1, Generation >= 2, UpdatedAt = T2.
|
|
enabledAuth := registeredAuth.Clone()
|
|
enabledAuth.Disabled = false
|
|
enabledAuth.Status = coreauth.StatusActive
|
|
enabledAuth.UpdatedAt = time.Now()
|
|
|
|
updatedEnabled, errEnable := manager.Update(context.Background(), enabledAuth)
|
|
if errEnable != nil {
|
|
t.Fatalf("update enable failed: %v", errEnable)
|
|
}
|
|
|
|
// Synchronous enable hook runs and restores models in GlobalModelRegistry
|
|
if err := hook(context.Background(), updatedEnabled); err != nil {
|
|
t.Fatalf("hook enable failed: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models restored immediately upon synchronous enable")
|
|
}
|
|
|
|
// Step 3: Now the delayed unversioned watcher event from Step 1 arrives via the queue consumer
|
|
service.handleAuthUpdate(context.Background(), delayedWatcherUpdate)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Scheduler execution must still succeed and select the enabled auth
|
|
resp, errExec := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExec != nil || string(resp.Payload) != authID {
|
|
t.Fatalf("expected scheduler to select active auth, got resp=%q err=%v", string(resp.Payload), errExec)
|
|
}
|
|
}
|
|
|
|
func TestRuntimeAuthSync_FileSnapshotEnqueuedThenRequestExecutionThenConsume(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-interleaved-request-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
}
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
},
|
|
}
|
|
registeredAuth, errRegister := manager.Register(context.Background(), auth)
|
|
if errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
hook := service.runtimeAuthSyncHook()
|
|
|
|
// Initial sync (Revision 1)
|
|
if err := hook(context.Background(), registeredAuth); err != nil {
|
|
t.Fatalf("initial hook sync failed: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
if len(reg.GetModelsForClient(authID)) == 0 {
|
|
t.Fatal("expected models after initial sync")
|
|
}
|
|
|
|
// Step 1: A file change occurs on disk (e.g. user disables credential in auth file).
|
|
// The watcher generates an AuthUpdate with revision 2 and enqueues it.
|
|
fileSnapshotAuth := registeredAuth.Clone()
|
|
fileSnapshotAuth.Disabled = true
|
|
fileSnapshotAuth.Status = coreauth.StatusDisabled
|
|
fileSnapshotAuth.UpdatedAt = time.Now()
|
|
// File synthesizer snapshots are unversioned; keep this fixture aligned so an
|
|
// intermediate MarkResult generation bump cannot drop a newer watcher revision.
|
|
fileSnapshotAuth.RegistrationEpoch = 0
|
|
fileSnapshotAuth.Generation = 0
|
|
|
|
fileUpdate := watcher.AuthUpdate{
|
|
Action: watcher.AuthUpdateActionModify,
|
|
ID: authID,
|
|
Auth: fileSnapshotAuth,
|
|
}
|
|
fileUpdate.SetRevision(2)
|
|
|
|
// Step 2: Before the queue consumer can process fileUpdate, an API request completes.
|
|
// MarkResult updates the auth in coreManager: auth.Generation++ and auth.UpdatedAt = now.
|
|
manager.MarkResult(context.Background(), coreauth.Result{
|
|
AuthID: authID,
|
|
Provider: "codex",
|
|
Model: "gpt-6-astra",
|
|
Success: true,
|
|
})
|
|
|
|
// Step 3: Now the queue consumer processes the fileUpdate (revision 2).
|
|
// Because revision 2 > revision 1, the file update MUST NOT be discarded due to
|
|
// the intermediate request's UpdatedAt timestamp!
|
|
service.handleAuthUpdate(context.Background(), fileUpdate)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Step 4: Verify the file update successfully took effect!
|
|
currentAuth, ok := manager.GetByID(authID)
|
|
if !ok || currentAuth == nil {
|
|
t.Fatal("expected auth to exist in manager")
|
|
}
|
|
if !currentAuth.Disabled {
|
|
t.Fatal("file update was incorrectly dropped after intermediate request execution!")
|
|
}
|
|
modelsAfterDisable := reg.GetModelsForClient(authID)
|
|
if len(modelsAfterDisable) != 0 {
|
|
t.Fatalf("expected 0 models after file update disable, got %d", len(modelsAfterDisable))
|
|
}
|
|
}
|
|
|
|
func TestRuntimeAuthSyncHook_ContextCancellationDoesNotAbortSync(t *testing.T) {
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := "codex-canceled-ctx-auth"
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
manager := coreauth.NewManager(nil, nil, nil)
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
service := &Service{
|
|
cfg: &config.Config{},
|
|
coreManager: manager,
|
|
}
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Attributes: map[string]string{
|
|
"plan_type": "pro",
|
|
},
|
|
}
|
|
registeredAuth, errRegister := manager.Register(context.Background(), auth)
|
|
if errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
hook := service.runtimeAuthSyncHook()
|
|
|
|
// Create a context that is ALREADY canceled (e.g. client aborted request right after save)
|
|
canceledCtx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
// Calling hook with canceled context must NOT abort registration
|
|
if err := hook(canceledCtx, registeredAuth); err != nil {
|
|
t.Fatalf("hook failed with canceled context: %v", err)
|
|
}
|
|
manager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
// Verify models were registered successfully despite canceled incoming context
|
|
models := reg.GetModelsForClient(authID)
|
|
if len(models) == 0 {
|
|
t.Fatal("expected models to be registered even when incoming context is canceled")
|
|
}
|
|
|
|
// Verify scheduler can execute
|
|
resp, errExec := manager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExec != nil || string(resp.Payload) != authID {
|
|
t.Fatalf("expected scheduler execution to succeed, got resp=%q err=%v", string(resp.Payload), errExec)
|
|
}
|
|
}
|
|
|
|
func TestEndToEndAuthFileReplacement_RestoresModelsInV1ModelsWithoutRestart(t *testing.T) {
|
|
authDir := t.TempDir()
|
|
fileName := "codex-team.json"
|
|
filePath := filepath.Join(authDir, fileName)
|
|
|
|
// Synthetic JWT containing CodexAuthInfo claims with chatgpt_plan_type: team
|
|
teamIDToken := "eyJhbGciOiJub25lIn0.eyJlbWFpbCI6ICJ1c2VyQGV4YW1wbGUuY29tIiwgImh0dHBzOi8vYXBpLm9wZW5haS5jb20vYXV0aCI6IHsiY2hhdGdwdF9wbGFuX3R5cGUiOiAidGVhbSIsICJjaGF0Z3B0X2FjY291bnRfaWQiOiAiYWNjLTEyMyJ9fQ.sig"
|
|
initialContent := fmt.Sprintf(`{"type":"codex","email":"user@example.com","access_token":"token-old","id_token":%q}`, teamIDToken)
|
|
if errWrite := os.WriteFile(filePath, []byte(initialContent), 0o600); errWrite != nil {
|
|
t.Fatalf("write auth file: %v", errWrite)
|
|
}
|
|
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := fileName
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
secretKey := "test-secret"
|
|
hashedSecret, errHash := bcrypt.GenerateFromPassword([]byte(secretKey), bcrypt.DefaultCost)
|
|
if errHash != nil {
|
|
t.Fatalf("bcrypt hash failed: %v", errHash)
|
|
}
|
|
|
|
cfg := &config.Config{
|
|
AuthDir: authDir,
|
|
RemoteManagement: config.RemoteManagement{
|
|
SecretKey: string(hashedSecret),
|
|
AllowRemote: true,
|
|
},
|
|
}
|
|
|
|
service, errBuild := NewBuilder().
|
|
WithConfig(cfg).
|
|
WithConfigPath(filepath.Join(authDir, "config.yaml")).
|
|
Build()
|
|
if errBuild != nil {
|
|
t.Fatalf("Build() failed: %v", errBuild)
|
|
}
|
|
|
|
server := api.NewServer(service.cfg, service.coreManager, service.accessManager, service.configPath, service.serverOptions...)
|
|
if server == nil {
|
|
t.Fatal("NewServer() returned nil")
|
|
}
|
|
handler := server.Handler()
|
|
service.coreManager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
FileName: fileName,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Metadata: map[string]any{
|
|
"type": "codex",
|
|
"email": "user@example.com",
|
|
"access_token": "token-old",
|
|
"id_token": teamIDToken,
|
|
},
|
|
Attributes: map[string]string{
|
|
"plan_type": "team",
|
|
"path": filePath,
|
|
},
|
|
}
|
|
if _, errRegister := service.coreManager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
// Initial model sync
|
|
syncHook := service.runtimeAuthSyncHook()
|
|
if err := syncHook(context.Background(), auth); err != nil {
|
|
t.Fatalf("initial sync failed: %v", err)
|
|
}
|
|
|
|
// 1. Confirm all team models are initially visible in GET /v1/models
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("GET /v1/models initial status = %d, body = %s", rec.Code, rec.Body.String())
|
|
}
|
|
var modelsResp struct {
|
|
Data []map[string]any `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &modelsResp); err != nil {
|
|
t.Fatalf("failed to decode /v1/models response: %v", err)
|
|
}
|
|
hasModel := func(modelID string) bool {
|
|
for _, m := range modelsResp.Data {
|
|
if id, _ := m["id"].(string); id == modelID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
teamModels := []string{"gpt-6-astra", "gpt-5.6-sol", "gpt-5.6-terra", "codex-auto-review"}
|
|
for _, m := range teamModels {
|
|
if !hasModel(m) {
|
|
t.Fatalf("expected %q to be in initial /v1/models list: %s", m, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// 2. Simulate terminal authentication error on all 4 models (invalidated token)
|
|
unauthErr := &coreauth.Error{
|
|
HTTPStatus: http.StatusUnauthorized,
|
|
Code: "unauthorized",
|
|
Message: "Encountered invalidated oauth token: test-token",
|
|
}
|
|
for _, m := range teamModels {
|
|
service.coreManager.MarkResult(context.Background(), coreauth.Result{
|
|
AuthID: authID,
|
|
Provider: "codex",
|
|
Model: m,
|
|
RouteModel: m,
|
|
Success: false,
|
|
Error: unauthErr,
|
|
})
|
|
}
|
|
|
|
// Confirm all 4 models are now omitted from /v1/models due to active error/cooldown
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
handler.ServeHTTP(rec, req)
|
|
modelsResp.Data = nil
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &modelsResp); err != nil {
|
|
t.Fatalf("failed to decode /v1/models response: %v", err)
|
|
}
|
|
for _, m := range teamModels {
|
|
if hasModel(m) {
|
|
t.Fatalf("expected %q to be absent after terminal auth failure", m)
|
|
}
|
|
}
|
|
|
|
// 3. Replace credential via management upload (POST /v0/management/auth-files)
|
|
newContent := fmt.Sprintf(`{"type":"codex","email":"user@example.com","access_token":"token-new","id_token":%q}`, teamIDToken)
|
|
var body bytes.Buffer
|
|
writer := multipart.NewWriter(&body)
|
|
part, err := writer.CreateFormFile("file", fileName)
|
|
if err != nil {
|
|
t.Fatalf("failed to create multipart file: %v", err)
|
|
}
|
|
if _, err = part.Write([]byte(newContent)); err != nil {
|
|
t.Fatalf("failed to write multipart content: %v", err)
|
|
}
|
|
if err = writer.Close(); err != nil {
|
|
t.Fatalf("failed to close multipart writer: %v", err)
|
|
}
|
|
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodPost, "/v0/management/auth-files", &body)
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("upload failed: status=%d, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
// 4. Verify that GET /v1/models immediately includes all team models without restart
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
handler.ServeHTTP(rec, req)
|
|
modelsResp.Data = nil
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &modelsResp); err != nil {
|
|
t.Fatalf("failed to decode /v1/models response: %v", err)
|
|
}
|
|
for _, m := range teamModels {
|
|
if !hasModel(m) {
|
|
t.Fatalf("expected %q to be restored in /v1/models after credential replacement: %s", m, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// 5. Verify direct inference request succeeds using the replacement credential token
|
|
service.coreManager.RegisterExecutor(&syncTestExecutor{})
|
|
resp, errExec := service.coreManager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExec != nil || string(resp.Payload) != "token-new" {
|
|
t.Fatalf("expected inference execution to use replacement token, got resp=%q err=%v", string(resp.Payload), errExec)
|
|
}
|
|
}
|
|
|
|
func TestEndToEndAuthFilePatch_RestoresModelsInV1ModelsWithoutRestart(t *testing.T) {
|
|
authDir := t.TempDir()
|
|
fileName := "codex-patch.json"
|
|
filePath := filepath.Join(authDir, fileName)
|
|
|
|
teamIDToken := "eyJhbGciOiJub25lIn0.eyJlbWFpbCI6ICJ1c2VyQGV4YW1wbGUuY29tIiwgImh0dHBzOi8vYXBpLm9wZW5haS5jb20vYXV0aCI6IHsiY2hhdGdwdF9wbGFuX3R5cGUiOiAidGVhbSIsICJjaGF0Z3B0X2FjY291bnRfaWQiOiAiYWNjLTEyMyJ9fQ.sig"
|
|
initialContent := fmt.Sprintf(`{"type":"codex","email":"user@example.com","access_token":"token-old","id_token":%q}`, teamIDToken)
|
|
if errWrite := os.WriteFile(filePath, []byte(initialContent), 0o600); errWrite != nil {
|
|
t.Fatalf("write auth file: %v", errWrite)
|
|
}
|
|
|
|
reg := internalregistry.GetGlobalRegistry()
|
|
authID := fileName
|
|
reg.UnregisterClient(authID)
|
|
t.Cleanup(func() {
|
|
reg.UnregisterClient(authID)
|
|
})
|
|
|
|
secretKey := "test-secret"
|
|
hashedSecret, errHash := bcrypt.GenerateFromPassword([]byte(secretKey), bcrypt.DefaultCost)
|
|
if errHash != nil {
|
|
t.Fatalf("bcrypt hash failed: %v", errHash)
|
|
}
|
|
|
|
cfg := &config.Config{
|
|
AuthDir: authDir,
|
|
RemoteManagement: config.RemoteManagement{
|
|
SecretKey: string(hashedSecret),
|
|
AllowRemote: true,
|
|
},
|
|
}
|
|
|
|
service, errBuild := NewBuilder().
|
|
WithConfig(cfg).
|
|
WithConfigPath(filepath.Join(authDir, "config.yaml")).
|
|
Build()
|
|
if errBuild != nil {
|
|
t.Fatalf("Build() failed: %v", errBuild)
|
|
}
|
|
|
|
server := api.NewServer(service.cfg, service.coreManager, service.accessManager, service.configPath, service.serverOptions...)
|
|
if server == nil {
|
|
t.Fatal("NewServer() returned nil")
|
|
}
|
|
handler := server.Handler()
|
|
service.coreManager.RegisterExecutor(&syncTestExecutor{})
|
|
|
|
auth := &coreauth.Auth{
|
|
ID: authID,
|
|
FileName: fileName,
|
|
Provider: "codex",
|
|
Status: coreauth.StatusActive,
|
|
Metadata: map[string]any{
|
|
"type": "codex",
|
|
"email": "user@example.com",
|
|
"access_token": "token-old",
|
|
"id_token": teamIDToken,
|
|
},
|
|
Attributes: map[string]string{
|
|
"plan_type": "team",
|
|
"path": filePath,
|
|
},
|
|
}
|
|
if _, errRegister := service.coreManager.Register(context.Background(), auth); errRegister != nil {
|
|
t.Fatalf("register auth: %v", errRegister)
|
|
}
|
|
|
|
syncHook := service.runtimeAuthSyncHook()
|
|
if err := syncHook(context.Background(), auth); err != nil {
|
|
t.Fatalf("initial sync failed: %v", err)
|
|
}
|
|
|
|
// 1. Simulate terminal auth failure on all 4 models
|
|
unauthErr := &coreauth.Error{
|
|
HTTPStatus: http.StatusUnauthorized,
|
|
Code: "unauthorized",
|
|
Message: "Encountered invalidated oauth token: test-token",
|
|
}
|
|
teamModels := []string{"gpt-6-astra", "gpt-5.6-sol", "gpt-5.6-terra", "codex-auto-review"}
|
|
for _, m := range teamModels {
|
|
service.coreManager.MarkResult(context.Background(), coreauth.Result{
|
|
AuthID: authID,
|
|
Provider: "codex",
|
|
Model: m,
|
|
RouteModel: m,
|
|
Success: false,
|
|
Error: unauthErr,
|
|
})
|
|
}
|
|
|
|
// Confirm all 4 models are now omitted from /v1/models due to active error/cooldown
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
handler.ServeHTTP(rec, req)
|
|
var modelsResp struct {
|
|
Data []map[string]any `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &modelsResp); err != nil {
|
|
t.Fatalf("failed to decode /v1/models response: %v", err)
|
|
}
|
|
hasModel := func(modelID string) bool {
|
|
for _, m := range modelsResp.Data {
|
|
if id, _ := m["id"].(string); id == modelID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
for _, m := range teamModels {
|
|
if hasModel(m) {
|
|
t.Fatalf("expected %q to be absent after terminal auth failure", m)
|
|
}
|
|
}
|
|
|
|
// 2. Patch auth file fields with fresh access token
|
|
patchBody := fmt.Sprintf(`{"name":%q,"access_token":"token-fresh","id_token":%q}`, fileName, teamIDToken)
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodPatch, "/v0/management/auth-files/fields", strings.NewReader(patchBody))
|
|
req.Header.Set("Authorization", "Bearer test-secret")
|
|
req.Header.Set("Content-Type", "application/json")
|
|
handler.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("PATCH fields failed: status=%d, body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
// 3. Confirm all team models are restored in /v1/models
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
handler.ServeHTTP(rec, req)
|
|
modelsResp.Data = nil
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &modelsResp); err != nil {
|
|
t.Fatalf("failed to decode /v1/models response: %v", err)
|
|
}
|
|
for _, m := range teamModels {
|
|
if !hasModel(m) {
|
|
t.Fatalf("expected %q to be restored in /v1/models after PATCH: %s", m, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
// 4. Verify direct inference request succeeds using fresh token after PATCH
|
|
service.coreManager.RegisterExecutor(&syncTestExecutor{})
|
|
resp, errExec := service.coreManager.Execute(context.Background(), []string{"codex"}, cliproxyexecutor.Request{Model: "gpt-6-astra"}, cliproxyexecutor.Options{})
|
|
if errExec != nil || string(resp.Payload) != "token-fresh" {
|
|
t.Fatalf("expected inference execution to use fresh token after PATCH, got resp=%q err=%v", string(resp.Payload), errExec)
|
|
}
|
|
}
|