c490f71042
Saved accounts live under <CLAUDE_CONFIG_DIR>/pipelines-accounts/<slug>/ (0700 dir, 0600 files). Switching changes what /login changes: the claudeAiOauth entry of .credentials.json and oauthAccount of .claude.json (atomic temp+rename, mode and other keys kept) and the org caches /login drops, under Claude Code's own refresh, storage and config locks, so running CLIs continue on the new account without a restart. The active login's rotated tokens are written back to its slot first. Server: claude_accounts / claude_account_save / _use / _remove, chat-scoped and Claude-only; a switch moves the home's open chats to the new account's quota (usage.account.switched_at), clearing a reached limit. CLI: pipelines claude-account list|save|use|remove [--dir]. App: Claude account in the chat menu, and Switch to <account> on the usage-limit and usage-warning banners, with a confirmation snack. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
99 lines
4.2 KiB
Go
99 lines
4.2 KiB
Go
package claude
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func writeLogin(t *testing.T, profile, email, uuid, token string) {
|
|
t.Helper()
|
|
writeFile(t, filepath.Join(profile, ".credentials.json"), `{"claudeAiOauth":{"accessToken":"access-`+token+`","refreshToken":"refresh-`+token+`","expiresAt":1,"subscriptionType":"pro","accountUuid":"`+uuid+`"}}`)
|
|
writeFile(t, filepath.Join(profile, ".claude.json"), "{\n \"numStartups\": 3,\n \"oauthAccount\": {\"accountUuid\":\""+uuid+"\",\"emailAddress\":\""+email+"\",\"organizationName\":\"Org\"}\n}")
|
|
}
|
|
|
|
func TestUseAccountMovesTheHomesChatsToTheNewAccountQuota(t *testing.T) {
|
|
chat := directoryChat(t)
|
|
var logs bytes.Buffer
|
|
log.SetOutput(&logs)
|
|
defer log.SetOutput(os.Stderr)
|
|
profile := t.TempDir()
|
|
directory := t.TempDir()
|
|
writeFile(t, filepath.Join(directory, ".envrc"), "export CLAUDE_CONFIG_DIR=...\n")
|
|
writeFile(t, filepath.Join(directory, ".envrc.allowed"), "")
|
|
writeFile(t, filepath.Join(directory, ".envrc.json"), `{"CLAUDE_CONFIG_DIR":"`+profile+`"}`)
|
|
ctx := context.Background()
|
|
|
|
writeLogin(t, profile, "a@example.invalid", "uuid-a", "a1")
|
|
if _, err := chat.ClaudeAccounts(ctx, "s", directory, protocol.ClaudeAccountSave, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writeLogin(t, profile, "b@example.invalid", "uuid-b", "b1")
|
|
if _, err := chat.ClaudeAccounts(ctx, "s", directory, protocol.ClaudeAccountSave, "B"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Two chats on the home are on b, which is out of quota; a third chat
|
|
// elsewhere is on another account.
|
|
oldKey := accountKeyFor("b@example.invalid", "Org")
|
|
limited := &protocol.ChatAccountUsage{Harness: quotaHarness, Windows: []protocol.ChatQuotaWindow{{ID: "five_hour", Label: "5-hour", UsedPercent: 100, ResetsAt: 1}}}
|
|
chat.quotas = map[string]*protocol.ChatAccountUsage{oldKey: limited}
|
|
here := chat.sessionFor("11111111-1111-4111-8111-111111111111", directory)
|
|
alsoHere := chat.sessionFor("22222222-2222-4222-8222-222222222222", directory)
|
|
elsewhere := chat.sessionFor("33333333-3333-4333-8333-333333333333", t.TempDir())
|
|
for _, s := range []*session{here, alsoHere} {
|
|
s.account = accountIdentity{key: oldKey, plan: "pro"}
|
|
}
|
|
elsewhere.account = accountIdentity{key: "other"}
|
|
frames := subscribe(t, chat, here.id)
|
|
|
|
list, err := chat.ClaudeAccounts(ctx, here.id, "", protocol.ClaudeAccountUse, "a@example.invalid")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if list.Active == nil || list.Active.Email != "a@example.invalid" || list.Usage == nil || list.Usage.SwitchedAt == 0 || len(list.Usage.Windows) != 0 {
|
|
t.Fatalf("use reply = %+v usage=%+v", list, list.Usage)
|
|
}
|
|
newKey := accountKeyFor("a@example.invalid", "Org")
|
|
if here.account.key != newKey || alsoHere.account.key != newKey || elsewhere.account.key != "other" {
|
|
t.Fatalf("keys = %q %q %q", here.account.key, alsoHere.account.key, elsewhere.account.key)
|
|
}
|
|
pushed := waitFrame(t, frames, "switched usage", func(frame protocol.ChatEventFrame) bool {
|
|
return frame.Kind == protocol.ChatEventUsage && frame.Usage != nil && frame.Usage.Account != nil && frame.Usage.Account.SwitchedAt != 0
|
|
}).Usage.Account
|
|
if len(pushed.Windows) != 0 {
|
|
t.Fatalf("the new account inherited the old quota: %+v", pushed)
|
|
}
|
|
data, _ := os.ReadFile(filepath.Join(profile, ".credentials.json"))
|
|
if !strings.Contains(string(data), "refresh-a1") {
|
|
t.Fatal("credentials not switched")
|
|
}
|
|
for _, secret := range []string{"access-", "refresh-"} {
|
|
if strings.Contains(logs.String(), secret) {
|
|
t.Fatalf("log carries a token: %s", logs.String())
|
|
}
|
|
}
|
|
|
|
// A later quota report on the new account keeps the switch.
|
|
known := mergeRateLimit(chat.quotas[newKey], []byte(`{"rateLimitType":"five_hour","utilization":0.2,"resetsAt":1900000000}`), "pro", time.Now())
|
|
if known == nil || known.SwitchedAt != pushed.SwitchedAt {
|
|
t.Fatalf("merge dropped the switch: %+v", known)
|
|
}
|
|
}
|
|
|
|
func TestAccountsRefusedWithoutAClaudeHome(t *testing.T) {
|
|
chat := directoryChat(t)
|
|
t.Setenv("CLAUDE_CONFIG_DIR", "")
|
|
os.Unsetenv("CLAUDE_CONFIG_DIR")
|
|
if _, err := chat.ClaudeAccounts(context.Background(), "s", t.TempDir(), protocol.ClaudeAccounts, ""); err != ErrNoConfigDir {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
}
|