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>
79 lines
2.9 KiB
Go
79 lines
2.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/workenv"
|
|
)
|
|
|
|
func cliLogin(t *testing.T, profile, email, uuid, token string) {
|
|
t.Helper()
|
|
files := map[string]string{
|
|
".credentials.json": `{"claudeAiOauth":{"accessToken":"access-` + token + `","refreshToken":"refresh-` + token + `","subscriptionType":"max","accountUuid":"` + uuid + `"}}`,
|
|
".claude.json": `{"userID":"device","oauthAccount":{"accountUuid":"` + uuid + `","emailAddress":"` + email + `","organizationName":"Org"}}`,
|
|
}
|
|
for name, content := range files {
|
|
if err := os.WriteFile(filepath.Join(profile, name), []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestClaudeAccountCommandSavesAndSwitches(t *testing.T) {
|
|
profile := t.TempDir()
|
|
t.Setenv("CLAUDE_CONFIG_DIR", profile)
|
|
t.Setenv(workenv.DisableEnvKey, "off")
|
|
dir := t.TempDir()
|
|
run := func(args ...string) (string, error) {
|
|
var out, errs bytes.Buffer
|
|
err := App{Out: &out, Err: &errs}.Run(context.Background(), append([]string{"claude-account"}, args...))
|
|
return out.String() + errs.String(), err
|
|
}
|
|
cliLogin(t, profile, "a@example.invalid", "uuid-a", "a1")
|
|
if out, err := run("save", "--dir", dir); err != nil || !strings.Contains(out, "* a@example.invalid (Org, max)") {
|
|
t.Fatalf("save a: %v\n%s", err, out)
|
|
}
|
|
cliLogin(t, profile, "b@example.invalid", "uuid-b", "b1")
|
|
if out, err := run("list", "--dir", dir); err != nil || !strings.Contains(out, " a@example.invalid") || !strings.Contains(out, "* b@example.invalid (signed in, not saved") {
|
|
t.Fatalf("list: %v\n%s", err, out)
|
|
}
|
|
if _, err := run("save", "work", "--dir", dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := run("use", "a@example.invalid", "--dir", dir, "--json")
|
|
if err != nil {
|
|
t.Fatalf("use: %v\n%s", err, out)
|
|
}
|
|
if strings.Contains(out, "access-") || strings.Contains(out, "refresh-") {
|
|
t.Fatalf("output carries a token: %s", out)
|
|
}
|
|
var list protocol.ClaudeAccountList
|
|
if err := json.Unmarshal([]byte(out), &list); err != nil {
|
|
t.Fatalf("json: %v\n%s", err, out)
|
|
}
|
|
if list.ConfigDir != profile || list.Active == nil || list.Active.Email != "a@example.invalid" || len(list.Accounts) != 2 {
|
|
t.Fatalf("use reply = %+v", list)
|
|
}
|
|
data, _ := os.ReadFile(filepath.Join(profile, ".credentials.json"))
|
|
if !strings.Contains(string(data), "refresh-a1") {
|
|
t.Fatal("credentials not switched")
|
|
}
|
|
if out, err := run("remove", "work", "--dir", dir); err != nil || strings.Contains(out, "work") {
|
|
t.Fatalf("remove: %v\n%s", err, out)
|
|
}
|
|
if _, err := run("use", "--dir", dir); err == nil || !strings.Contains(err.Error(), "usage:") {
|
|
t.Fatalf("use without a name: %v", err)
|
|
}
|
|
t.Setenv("CLAUDE_CONFIG_DIR", "")
|
|
if _, err := run("list", "--dir", dir); err == nil || !strings.Contains(err.Error(), "CLAUDE_CONFIG_DIR") {
|
|
t.Fatalf("no Claude home: %v", err)
|
|
}
|
|
}
|