|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestManager_Remove_DeletesRuntimeAuth(t *testing.T) { |
| 10 | + manager := NewManager(nil, nil, nil) |
| 11 | + ctx := context.Background() |
| 12 | + |
| 13 | + auth := &Auth{ |
| 14 | + ID: "remove-runtime-auth", |
| 15 | + Provider: "claude", |
| 16 | + Status: StatusActive, |
| 17 | + Metadata: map[string]any{"email": "x@example.com"}, |
| 18 | + } |
| 19 | + if _, errRegister := manager.Register(ctx, auth); errRegister != nil { |
| 20 | + t.Fatalf("register auth: %v", errRegister) |
| 21 | + } |
| 22 | + |
| 23 | + manager.Remove(ctx, auth.ID) |
| 24 | + |
| 25 | + if _, ok := manager.GetByID(auth.ID); ok { |
| 26 | + t.Fatalf("expected auth %q to be removed", auth.ID) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestManager_Update_MissingAuthIsNoOp(t *testing.T) { |
| 31 | + manager := NewManager(nil, nil, nil) |
| 32 | + ctx := context.Background() |
| 33 | + |
| 34 | + auth := &Auth{ |
| 35 | + ID: "missing-update-auth", |
| 36 | + Provider: "claude", |
| 37 | + Status: StatusActive, |
| 38 | + } |
| 39 | + if _, errRegister := manager.Register(ctx, auth); errRegister != nil { |
| 40 | + t.Fatalf("register auth: %v", errRegister) |
| 41 | + } |
| 42 | + manager.Remove(ctx, auth.ID) |
| 43 | + |
| 44 | + updated, errUpdate := manager.Update(ctx, &Auth{ |
| 45 | + ID: auth.ID, |
| 46 | + Provider: "claude", |
| 47 | + Status: StatusDisabled, |
| 48 | + Disabled: true, |
| 49 | + }) |
| 50 | + if errUpdate != nil { |
| 51 | + t.Fatalf("update removed auth: %v", errUpdate) |
| 52 | + } |
| 53 | + if updated != nil { |
| 54 | + t.Fatalf("expected update on removed auth to be no-op, got %#v", updated) |
| 55 | + } |
| 56 | + if _, ok := manager.GetByID(auth.ID); ok { |
| 57 | + t.Fatalf("expected removed auth to stay absent after late update") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestManager_Remove_UnschedulesAutoRefresh(t *testing.T) { |
| 62 | + ctx := context.Background() |
| 63 | + |
| 64 | + manager := NewManager(nil, nil, nil) |
| 65 | + loop := newAuthAutoRefreshLoop(manager, time.Second, 1) |
| 66 | + manager.mu.Lock() |
| 67 | + manager.refreshLoop = loop |
| 68 | + manager.mu.Unlock() |
| 69 | + |
| 70 | + lead := 10 * time.Minute |
| 71 | + setRefreshLeadFactory(t, "provider-lead-expiry", func() *time.Duration { |
| 72 | + d := lead |
| 73 | + return &d |
| 74 | + }) |
| 75 | + |
| 76 | + auth := &Auth{ |
| 77 | + ID: "remove-refresh-auth", |
| 78 | + Provider: "provider-lead-expiry", |
| 79 | + Metadata: map[string]any{ |
| 80 | + "email": "x@example.com", |
| 81 | + "expires_at": time.Now().Add(time.Hour).Format(time.RFC3339), |
| 82 | + }, |
| 83 | + } |
| 84 | + if _, errRegister := manager.Register(ctx, auth); errRegister != nil { |
| 85 | + t.Fatalf("register auth: %v", errRegister) |
| 86 | + } |
| 87 | + |
| 88 | + now := time.Now() |
| 89 | + if _, ok := nextRefreshCheckAt(now, auth, time.Second); !ok { |
| 90 | + t.Fatalf("expected auth to be scheduled before removal") |
| 91 | + } |
| 92 | + loop.applyDirty(now) |
| 93 | + loop.mu.Lock() |
| 94 | + if _, ok := loop.index[auth.ID]; !ok { |
| 95 | + loop.mu.Unlock() |
| 96 | + t.Fatalf("expected auth %q to be present in auto-refresh index before removal", auth.ID) |
| 97 | + } |
| 98 | + loop.mu.Unlock() |
| 99 | + |
| 100 | + manager.Remove(ctx, auth.ID) |
| 101 | + |
| 102 | + if _, ok := manager.GetByID(auth.ID); ok { |
| 103 | + t.Fatalf("expected auth to be removed") |
| 104 | + } |
| 105 | + loop.mu.Lock() |
| 106 | + if _, ok := loop.index[auth.ID]; ok { |
| 107 | + loop.mu.Unlock() |
| 108 | + t.Fatalf("expected auth %q to be removed from auto-refresh index", auth.ID) |
| 109 | + } |
| 110 | + loop.mu.Unlock() |
| 111 | +} |
0 commit comments