| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestClaudeAutomaticSwitchRequiresQuotaAndConsent(t *testing.T) { |
| 12 | s, id, _ := metadataFixture(t) |
| 13 | s.ready = true |
| 14 | a, b := strings.Repeat("a", 24), strings.Repeat("b", 24) |
| 15 | list := claudeAccountList{Accounts: []claudeAccount{{ID: a, Label: "A", Email: "[email protected]", Enabled: true, Status: "ready", HandoffGroup: "personal"}, {ID: b, Label: "B", Email: "[email protected]", Enabled: true, Status: "ready", HandoffGroup: "personal"}}} |
| 16 | if err := s.saveClaudeAccounts(list); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | record, _ := s.mobileCLI.record(s.profile, id) |
| 20 | record.Launcher = "claude" |
| 21 | record.Account = claudeSelection{a, a, claudeIdentityHash("[email protected]", "")} |
| 22 | epoch, _ := s.account.access() |
| 23 | ctx := context.WithValue(context.Background(), mobileEpochKey{}, epoch) |
| 24 | for _, scenario := range []struct{ policy, failure, reason string }{{"ask", "rate_limit", "手动确认"}, {"allow", "overloaded", "手动确认"}, {"allow", "rate_limit", "尚未确认"}} { |
| 25 | record.SwitchPolicy, record.AccountFailure = scenario.policy, scenario.failure |
| 26 | if err := writePrivate(mobileRecordPath(s.profile, id), record); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | err := s.switchClaudeAccount(ctx, id, claudeSwitchInput{SourceID: record.CLIID, Target: b}, true) |
| 30 | if err == nil || !strings.Contains(err.Error(), scenario.reason) { |
| 31 | t.Fatal(scenario, err) |
| 32 | } |
| 33 | stored, _ := s.mobileCLI.record(s.profile, id) |
| 34 | if stored.Switch != nil || stored.Account.ID != a { |
| 35 | t.Fatal("preflight changed ownership") |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestClaudeSchedulingModesAndPersistence(t *testing.T) { |
| 41 | s := testServer(t) |
| 42 | a, b := strings.Repeat("a", 24), strings.Repeat("b", 24) |
| 43 | list := claudeAccountList{Accounts: []claudeAccount{{ID: a, Label: "A", Enabled: true, Status: "ready", Email: "[email protected]"}, {ID: b, Label: "B", Enabled: true, Status: "ready", Email: "[email protected]"}}} |
| 44 | if err := s.saveClaudeAccounts(list); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | loaded, err := s.loadClaudeAccounts() |
| 48 | if err != nil || loaded.Scheduling.Mode != "balanced" || loaded.Scheduling.SwitchPolicy != "ask" { |
| 49 | t.Fatal("legacy defaults", loaded, err) |
| 50 | } |
| 51 | if id := pickClaudeAccount(loaded, map[string]int{a: 2}, nil, "", "", time.Now()); id != b { |
| 52 | t.Fatal("balance ignored occupancy", id) |
| 53 | } |
| 54 | response := request(s, "POST", "/api/claude-accounts", s.origin, `{"action":"scheduling","scheduling":{"mode":"sequential","switch_policy":"allow"},"order":["`+b+`","`+a+`"]}`, true) |
| 55 | if response.Code != 200 { |
| 56 | t.Fatal(response.Code, response.Body.String()) |
| 57 | } |
| 58 | loaded, err = s.loadClaudeAccounts() |
| 59 | if err != nil || loaded.Scheduling.Mode != "sequential" || loaded.Accounts[0].ID != b { |
| 60 | t.Fatal(loaded, err) |
| 61 | } |
| 62 | if id := pickClaudeAccount(loaded, map[string]int{b: 3}, nil, "", "", time.Now()); id != b { |
| 63 | t.Fatal("sequential did not keep priority", id) |
| 64 | } |
| 65 | loaded.Accounts[0].Quota = &claudeQuota{FiveHour: &claudeWindow{UsedPercentage: 100, ResetsAt: time.Now().Add(time.Hour).Unix(), ObservedAt: time.Now().Unix()}} |
| 66 | if id := pickClaudeAccount(loaded, nil, nil, "", "", time.Now()); id != a { |
| 67 | t.Fatal("exhausted account selected", id) |
| 68 | } |
| 69 | response = request(s, "POST", "/api/claude-accounts", s.origin, `{"action":"scheduling","scheduling":{"mode":"sequential","switch_policy":"allow"},"order":["`+b+`","`+b+`"]}`, true) |
| 70 | if response.Code == 200 { |
| 71 | t.Fatal("duplicate order accepted") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestClaudeSwitchGroupAndDurableBinding(t *testing.T) { |
| 76 | a, b := strings.Repeat("a", 24), strings.Repeat("b", 24) |
| 77 | list := claudeAccountList{Accounts: []claudeAccount{{ID: a, Label: "A", Email: "[email protected]", Enabled: true, Status: "ready"}, {ID: b, Label: "B", Email: "[email protected]", Enabled: true, Status: "ready"}}} |
| 78 | record := &mobileRecord{Account: claudeSelection{ID: a, GuestID: a, Subject: claudeIdentityHash("[email protected]", "")}} |
| 79 | if len(switchCandidates(list, record)) != 0 { |
| 80 | t.Fatal("ungrouped history escaped") |
| 81 | } |
| 82 | for i := range list.Accounts { |
| 83 | list.Accounts[i].HandoffGroup = "personal" |
| 84 | } |
| 85 | if got := switchCandidates(list, record); len(got) != 1 || got[0].ID != b { |
| 86 | t.Fatal(got) |
| 87 | } |
| 88 | list.Accounts[1].HandoffGroup = "company" |
| 89 | if len(switchCandidates(list, record)) != 0 { |
| 90 | t.Fatal("organization boundary escaped") |
| 91 | } |
| 92 | list.Accounts[1].HandoffGroup = "personal" |
| 93 | record.Account.Subject = "changed" |
| 94 | if len(switchCandidates(list, record)) != 0 { |
| 95 | t.Fatal("stale source identity accepted") |
| 96 | } |
| 97 | record.Switch = &claudeAccountSwitch{ID: "receipt", Phase: "starting", Source: claudeExecution{Config: "private-config"}, Target: claudeSelection{Subject: "private-subject"}} |
| 98 | raw, _ := json.Marshal(record.snapshot()) |
| 99 | if strings.Contains(string(raw), "private-") { |
| 100 | t.Fatal("internal authentication binding exposed", string(raw)) |
| 101 | } |
| 102 | } |