Fixed: #797
**test(auth): add test for preserving ModelStates during auth updates**
This commit is contained in:
@@ -463,9 +463,14 @@ func (m *Manager) Update(ctx context.Context, auth *Auth) (*Auth, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
if existing, ok := m.auths[auth.ID]; ok && existing != nil && !auth.indexAssigned && auth.Index == "" {
|
if existing, ok := m.auths[auth.ID]; ok && existing != nil {
|
||||||
auth.Index = existing.Index
|
if !auth.indexAssigned && auth.Index == "" {
|
||||||
auth.indexAssigned = existing.indexAssigned
|
auth.Index = existing.Index
|
||||||
|
auth.indexAssigned = existing.indexAssigned
|
||||||
|
}
|
||||||
|
if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
|
||||||
|
auth.ModelStates = existing.ModelStates
|
||||||
|
}
|
||||||
}
|
}
|
||||||
auth.EnsureIndex()
|
auth.EnsureIndex()
|
||||||
m.auths[auth.ID] = auth.Clone()
|
m.auths[auth.ID] = auth.Clone()
|
||||||
|
|||||||
49
sdk/cliproxy/auth/conductor_update_test.go
Normal file
49
sdk/cliproxy/auth/conductor_update_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestManager_Update_PreservesModelStates(t *testing.T) {
|
||||||
|
m := NewManager(nil, nil, nil)
|
||||||
|
|
||||||
|
model := "test-model"
|
||||||
|
backoffLevel := 7
|
||||||
|
|
||||||
|
if _, errRegister := m.Register(context.Background(), &Auth{
|
||||||
|
ID: "auth-1",
|
||||||
|
Provider: "claude",
|
||||||
|
Metadata: map[string]any{"k": "v"},
|
||||||
|
ModelStates: map[string]*ModelState{
|
||||||
|
model: {
|
||||||
|
Quota: QuotaState{BackoffLevel: backoffLevel},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}); errRegister != nil {
|
||||||
|
t.Fatalf("register auth: %v", errRegister)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, errUpdate := m.Update(context.Background(), &Auth{
|
||||||
|
ID: "auth-1",
|
||||||
|
Provider: "claude",
|
||||||
|
Metadata: map[string]any{"k": "v2"},
|
||||||
|
}); errUpdate != nil {
|
||||||
|
t.Fatalf("update auth: %v", errUpdate)
|
||||||
|
}
|
||||||
|
|
||||||
|
updated, ok := m.GetByID("auth-1")
|
||||||
|
if !ok || updated == nil {
|
||||||
|
t.Fatalf("expected auth to be present")
|
||||||
|
}
|
||||||
|
if len(updated.ModelStates) == 0 {
|
||||||
|
t.Fatalf("expected ModelStates to be preserved")
|
||||||
|
}
|
||||||
|
state := updated.ModelStates[model]
|
||||||
|
if state == nil {
|
||||||
|
t.Fatalf("expected model state to be present")
|
||||||
|
}
|
||||||
|
if state.Quota.BackoffLevel != backoffLevel {
|
||||||
|
t.Fatalf("expected BackoffLevel to be %d, got %d", backoffLevel, state.Quota.BackoffLevel)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -290,6 +290,9 @@ func (s *Service) applyCoreAuthAddOrUpdate(ctx context.Context, auth *coreauth.A
|
|||||||
auth.CreatedAt = existing.CreatedAt
|
auth.CreatedAt = existing.CreatedAt
|
||||||
auth.LastRefreshedAt = existing.LastRefreshedAt
|
auth.LastRefreshedAt = existing.LastRefreshedAt
|
||||||
auth.NextRefreshAfter = existing.NextRefreshAfter
|
auth.NextRefreshAfter = existing.NextRefreshAfter
|
||||||
|
if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
|
||||||
|
auth.ModelStates = existing.ModelStates
|
||||||
|
}
|
||||||
op = "update"
|
op = "update"
|
||||||
_, err = s.coreManager.Update(ctx, auth)
|
_, err = s.coreManager.Update(ctx, auth)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user