| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "sync" |
| 12 | "sync/atomic" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | func TestClaudeDefaultDiscoveryJoinAndLiveIdentity(t *testing.T) { |
| 18 | s := testServer(t) |
| 19 | s.ready = true |
| 20 | at := time.Now().Unix() |
| 21 | a := claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]", Organization: "personal", Quota: &claudeQuota{ObservedAt: at, FiveHour: &claudeWindow{UsedPercentage: 37, ResetsAt: at + 300}}} |
| 22 | b := claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]", Organization: "work"} |
| 23 | current := a |
| 24 | var unavailable bool |
| 25 | var probes int |
| 26 | s.claudeAccounts.probe = func(_ context.Context, id string) (claudeGuestSnapshot, error) { |
| 27 | if id != "default" { |
| 28 | t.Errorf("unexpected probe %q", id) |
| 29 | } |
| 30 | probes++ |
| 31 | if unavailable { |
| 32 | return claudeGuestSnapshot{}, errors.New("SSH unavailable") |
| 33 | } |
| 34 | return current, nil |
| 35 | } |
| 36 | panel := func() claudePanel { |
| 37 | t.Helper() |
| 38 | w := request(s, "GET", "/api/claude-accounts", "", "", true) |
| 39 | var v claudePanel |
| 40 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &v) != nil { |
| 41 | t.Fatal(w.Code, w.Body) |
| 42 | } |
| 43 | return v |
| 44 | } |
| 45 | join := func(person claudeGuestSnapshot, status int) { |
| 46 | t.Helper() |
| 47 | w := request(s, "POST", "/api/claude-accounts", s.origin, `{"action":"join","identity":"`+person.subject()+`"}`, true) |
| 48 | if w.Code != status { |
| 49 | t.Fatalf("join got %d, want %d (%s)", w.Code, status, w.Body) |
| 50 | } |
| 51 | } |
| 52 | if v := panel(); v.DetectionStatus != "ready" || v.Detected == nil || v.Detected.Email != a.Email || len(v.Accounts) != 0 { |
| 53 | t.Fatal(v) |
| 54 | } |
| 55 | _ = panel() |
| 56 | if probes != 1 { |
| 57 | t.Fatal("panel did not cache initial probe", probes) |
| 58 | } |
| 59 | if _, err := os.Stat(filepath.Join(s.profile, "claude-accounts.json")); !os.IsNotExist(err) { |
| 60 | t.Fatal("discovery enrolled or wrote an identity") |
| 61 | } |
| 62 | current = b |
| 63 | join(a, 409) |
| 64 | current = a |
| 65 | join(a, 200) |
| 66 | choice, err := s.selectClaudeAccount("", false, "/p") |
| 67 | if err != nil || choice.Subject != a.subject() || choice.GuestID != "default" || !validClaudeID(choice.ID) { |
| 68 | t.Fatal(choice, err) |
| 69 | } |
| 70 | current = b |
| 71 | if _, err = s.selectClaudeAccount("", false, "/p"); err == nil { |
| 72 | t.Fatal("selected a default identity that was no longer present") |
| 73 | } |
| 74 | if v := panel(); v.Detected == nil || v.Detected.Identity != b.subject() || len(v.Accounts) != 1 || v.Accounts[0].Status != "changed" { |
| 75 | t.Fatal(v) |
| 76 | } |
| 77 | join(b, 200) |
| 78 | joined, err := s.selectClaudeAccount("", false, "/p") |
| 79 | if err != nil || joined.ID == choice.ID || joined.Subject != b.subject() || choice.Subject != a.subject() { |
| 80 | t.Fatalf("selection lost the original snapshot: %+v %+v %v", choice, joined, err) |
| 81 | } |
| 82 | if v := panel(); v.Detected != nil || len(v.Accounts) != 2 || v.Accounts[1].Quota != nil { |
| 83 | t.Fatalf("inherited A's quota or registered B twice: %+v", v) |
| 84 | } |
| 85 | unavailable = true |
| 86 | if _, err = s.selectClaudeAccount("", false, "/p"); err == nil { |
| 87 | t.Fatal("stale default identity passed a failed live check") |
| 88 | } |
| 89 | if v := panel(); v.DetectionStatus != "unavailable" || v.DetectionError == "" || v.Detected != nil { |
| 90 | t.Fatal(v) |
| 91 | } |
| 92 | if _, err = s.selectClaudeAccount(choice.ID, true, ""); err == nil { |
| 93 | t.Fatal("login tool could mutate default credentials") |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestClaudeDefaultManagedDuplicateAndFallback(t *testing.T) { |
| 98 | s := testServer(t) |
| 99 | read := claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]", Organization: "team"} |
| 100 | s.claudeAccounts.probe = func(context.Context, string) (claudeGuestSnapshot, error) { return read, nil } |
| 101 | list := claudeAccountList{Accounts: []claudeAccount{ |
| 102 | {ID: strings.Repeat("a", 24), Source: "managed", Email: read.Email, Organization: read.Organization, Enabled: true, Status: "ready"}, |
| 103 | {ID: strings.Repeat("b", 24), Source: "default", Email: "[email protected]", Organization: "team", Enabled: true, Status: "ready"}, |
| 104 | }} |
| 105 | if err := s.saveClaudeAccounts(list); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | s.ready = true |
| 109 | v, _, err := s.claudePanelView(context.Background()) |
| 110 | if err != nil || v.Detected != nil || v.Accounts[1].Status != "changed" { |
| 111 | t.Fatal(v, err) |
| 112 | } |
| 113 | if err = s.joinClaudeDefault(context.Background(), read.subject()); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | noReadings := func(context.Context, []string) (map[string]claudeGuestQuota, error) { return nil, nil } |
| 117 | selection, err := s.allocateClaudeAccount(list, "", noReadings) |
| 118 | if err != nil || selection.ID != list.Accounts[0].ID || selection.GuestID != selection.ID || selection.Subject != read.subject() { |
| 119 | t.Fatal(selection, err) |
| 120 | } |
| 121 | got, _ := s.loadClaudeAccounts() |
| 122 | if len(got.Accounts) != 2 { |
| 123 | t.Fatal("second copy of the same subscribed identity") |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestClaudeOneStepLoginSurvivesLockAndRestart(t *testing.T) { |
| 128 | s := testServer(t) |
| 129 | var current claudeGuestSnapshot |
| 130 | s.claudeAccounts.probe = func(context.Context, string) (claudeGuestSnapshot, error) { return current, nil } |
| 131 | |
| 132 | login, err := s.selectClaudeAccount("", true, "") |
| 133 | if err != nil || !validClaudeID(login.ID) || login.GuestID != login.ID { |
| 134 | t.Fatal(login, err) |
| 135 | } |
| 136 | retry, err := s.selectClaudeAccount("", true, "") |
| 137 | if err != nil || login != retry { |
| 138 | t.Fatal("retry created another profile", login, retry, err) |
| 139 | } |
| 140 | list, _ := s.loadClaudeAccounts() |
| 141 | if len(list.Accounts) != 0 || list.PendingLogin != login.ID { |
| 142 | t.Fatal("pending attempt exposed an account") |
| 143 | } |
| 144 | if legacy, err := s.selectClaudeAccount("", false, ""); err != nil || legacy.ID != "" { |
| 145 | t.Fatal("pending login blocked existing workflow", legacy, err) |
| 146 | } |
| 147 | s.account.lock() |
| 148 | current = claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]", Organization: "work", Plan: "pro"} |
| 149 | if err = s.confirmClaudeLogin(context.Background(), login.ID); err == nil { |
| 150 | t.Fatal("registered while locked") |
| 151 | } |
| 152 | s.account.setKey(make([]byte, 32)) |
| 153 | restarted := &Server{profile: s.profile, account: s.account, ready: true} |
| 154 | restarted.claudeAccounts.probe = func(_ context.Context, id string) (claudeGuestSnapshot, error) { |
| 155 | if id == "default" { |
| 156 | return claudeGuestSnapshot{}, nil |
| 157 | } |
| 158 | if id != login.ID { |
| 159 | t.Fatal("opened wrong profile", id) |
| 160 | } |
| 161 | return current, nil |
| 162 | } |
| 163 | v, _, err := restarted.claudePanelView(context.Background()) |
| 164 | if err != nil || len(v.Accounts) != 1 || v.Accounts[0].ID != login.ID || v.Accounts[0].Label != current.Email || !v.Accounts[0].Enabled || v.Accounts[0].Status != "ready" || v.Accounts[0].Source != "managed" { |
| 165 | t.Fatalf("successful login was not recovered: %+v %v", v, err) |
| 166 | } |
| 167 | list, _ = restarted.loadClaudeAccounts() |
| 168 | if list.PendingLogin != "" { |
| 169 | t.Fatal("finished enrollment retained pending marker") |
| 170 | } |
| 171 | if err = restarted.confirmClaudeLogin(context.Background(), login.ID); err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | list, _ = restarted.loadClaudeAccounts() |
| 175 | if len(list.Accounts) != 1 { |
| 176 | t.Fatal("repeated completion registered twice") |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func TestClaudeUnsuccessfulLoginAndDuplicateStayOutsidePool(t *testing.T) { |
| 181 | s := testServer(t) |
| 182 | var current claudeGuestSnapshot |
| 183 | s.claudeAccounts.probe = func(context.Context, string) (claudeGuestSnapshot, error) { return current, nil } |
| 184 | login, err := s.selectClaudeAccount("", true, "") |
| 185 | if err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | if err = s.confirmClaudeLogin(context.Background(), login.ID); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | list, _ := s.loadClaudeAccounts() |
| 192 | if len(list.Accounts) != 0 || list.PendingLogin != "" { |
| 193 | t.Fatalf("cancelled login became a profile: %+v", list) |
| 194 | } |
| 195 | current = claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]"} |
| 196 | original := strings.Repeat("1", 24) |
| 197 | list.Accounts = append(list.Accounts, claudeAccount{ID: original, Source: "managed", Email: current.Email, Enabled: false, Status: "ready"}) |
| 198 | if err = s.saveClaudeAccounts(list); err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | login, err = s.selectClaudeAccount("", true, "") |
| 202 | if err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | if err = s.confirmClaudeLogin(context.Background(), login.ID); err != nil { |
| 206 | t.Fatal(err) |
| 207 | } |
| 208 | list, _ = s.loadClaudeAccounts() |
| 209 | if list.PendingLogin != "" || len(list.Accounts) != 1 || list.Accounts[0].Enabled || list.Accounts[0].ID != original { |
| 210 | t.Fatal("duplicate login altered membership", list) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestClaudeDefaultConcurrentDiscoverySingleProbe(t *testing.T) { |
| 215 | s := testServer(t) |
| 216 | s.ready = true |
| 217 | var calls atomic.Int32 |
| 218 | entered, release := make(chan struct{}), make(chan struct{}) |
| 219 | s.claudeAccounts.probe = func(context.Context, string) (claudeGuestSnapshot, error) { |
| 220 | if calls.Add(1) == 1 { |
| 221 | close(entered) |
| 222 | } |
| 223 | <-release |
| 224 | return claudeGuestSnapshot{}, nil |
| 225 | } |
| 226 | var wg sync.WaitGroup |
| 227 | for range 2 { |
| 228 | wg.Add(1) |
| 229 | go func() { |
| 230 | defer wg.Done() |
| 231 | v, _, err := s.claudePanelView(context.Background()) |
| 232 | if err != nil || v.DetectionStatus != "signed_out" { |
| 233 | t.Error(v, err) |
| 234 | } |
| 235 | }() |
| 236 | } |
| 237 | <-entered |
| 238 | close(release) |
| 239 | wg.Wait() |
| 240 | if calls.Load() != 1 { |
| 241 | t.Fatal("concurrent panel refresh duplicated a guest probe") |
| 242 | } |
| 243 | s.guestUpdateRequired = true |
| 244 | v, _, err := s.claudePanelView(context.Background()) |
| 245 | if err != nil || v.DetectionStatus != "update_required" || v.DetectionError == "" { |
| 246 | t.Fatal(v, err) |
| 247 | } |
| 248 | if w := request(s, "POST", "/api/claude-accounts", s.origin, `{"action":"create","label":"phantom"}`, true); w.Code == http.StatusOK { |
| 249 | t.Fatal("registration without real login accepted") |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | func TestClaudeIndependentLoginKeepsDefaultIdentityAndMakesBothAccountsAvailable(t *testing.T) { |
| 254 | s := testServer(t) |
| 255 | ctx := context.Background() |
| 256 | a := claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]", Organization: "home"} |
| 257 | b := claudeGuestSnapshot{LoggedIn: true, Email: "[email protected]", Organization: "work"} |
| 258 | current := a |
| 259 | var independent string |
| 260 | s.claudeAccounts.probe = func(_ context.Context, id string) (claudeGuestSnapshot, error) { |
| 261 | switch id { |
| 262 | case "default": |
| 263 | return current, nil |
| 264 | case independent: |
| 265 | return a, nil |
| 266 | default: |
| 267 | return claudeGuestSnapshot{}, errors.New("wrong configuration directory") |
| 268 | } |
| 269 | } |
| 270 | if err := s.joinClaudeDefault(ctx, a.subject()); err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | list, _ := s.loadClaudeAccounts() |
| 274 | stable := list.bySubject(a.subject()).ID |
| 275 | list.Accounts[0].Label, list.Accounts[0].Enabled = "个人空间", false |
| 276 | if err := s.saveClaudeAccounts(list); err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | current = b |
| 280 | if err := s.joinClaudeDefault(ctx, b.subject()); err != nil { |
| 281 | t.Fatal(err) |
| 282 | } |
| 283 | next, err := s.selectClaudeAccount("", true, "") |
| 284 | if err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | independent = next.GuestID |
| 288 | if err = s.confirmClaudeLogin(ctx, next.ID); err != nil { |
| 289 | t.Fatal(err) |
| 290 | } |
| 291 | if err = s.confirmClaudeLogin(ctx, next.ID); err != nil { |
| 292 | t.Fatal("duplicate completion probed the wrong directory", err) |
| 293 | } |
| 294 | list, err = s.loadClaudeAccounts() |
| 295 | old := list.find(stable) |
| 296 | if err != nil || len(list.Accounts) != 2 || old == nil || old.Source != "managed" || old.ProfileID != independent || old.Enabled || old.Label != "个人空间" || old.Status != "ready" { |
| 297 | t.Fatal(list, err) |
| 298 | } |
| 299 | |
| 300 | old.Enabled = true |
| 301 | if err = s.saveClaudeAccounts(list); err != nil { |
| 302 | t.Fatal(err) |
| 303 | } |
| 304 | read := func(_ context.Context, ids []string) (map[string]claudeGuestQuota, error) { |
| 305 | if len(ids) != 1 || ids[0] != independent { |
| 306 | t.Errorf("wrong guest quota directories: %v", ids) |
| 307 | } |
| 308 | return nil, nil |
| 309 | } |
| 310 | first, err := s.allocateClaudeAccount(list, "", read) |
| 311 | if err != nil || first.ID != stable || first.GuestID != independent || first.Subject != a.subject() { |
| 312 | t.Fatal(first, err) |
| 313 | } |
| 314 | s.noteClaudeSelection(first.ID, "") |
| 315 | second, err := s.allocateClaudeAccount(list, "", read) |
| 316 | if err != nil || second.ID == stable || second.GuestID != "default" || second.Subject != b.subject() { |
| 317 | t.Fatal(second, err) |
| 318 | } |
| 319 | again, err := s.selectClaudeAccount(stable, true, "") |
| 320 | if err != nil || again.GuestID != independent { |
| 321 | t.Fatal(again, err) |
| 322 | } |
| 323 | } |