Files
pipelines/internal/cli/claude_account_test.go
T
alan ed9072b79a Claude accounts: switch a chat refused for usage to another saved account and continue it
When Claude Code refuses an operator chat's turn for a usage limit, the
server marks the refused account until its reset and, if the chat's Claude
home allows it (claude_account_auto_switch / `pipelines claude-account auto
on|off`, on by default, kept in pipelines-accounts/settings.json), switches
the home to the saved account with the most usage left through the
claude_account_use path. Switches of one home are serialized, so chats
refused together switch once. Each refused chat gets one queued prompt,
marked as sent automatically, that asks the agent to check it is on track
before continuing; the app's auto-continue-at-reset prompt for the refusal is
removed (and refused if it arrives later), and the operator gets a
low-priority notification.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 00:23:43 +00:00

113 lines
4.4 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)
}
}
func TestClaudeAccountAutoCommandTogglesAutomaticSwitching(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("list", "--dir", dir); err != nil || !strings.Contains(out, "Switch automatically when usage runs out: on") {
t.Fatalf("default: %v\n%s", err, out)
}
if out, err := run("auto", "off", "--dir", dir); err != nil || !strings.Contains(out, "Switch automatically when usage runs out: off") {
t.Fatalf("auto off: %v\n%s", err, out)
}
out, err := run("list", "--dir", dir, "--json")
var list protocol.ClaudeAccountList
if err != nil || json.Unmarshal([]byte(out), &list) != nil || list.AutoSwitch {
t.Fatalf("list json: %v\n%s", err, out)
}
info, err := os.Stat(filepath.Join(profile, "pipelines-accounts", "settings.json"))
if err != nil || info.Mode().Perm() != 0o600 {
t.Fatalf("settings file: %v %v", info, err)
}
if out, err := run("auto", "on", "--dir", dir); err != nil || !strings.Contains(out, "runs out: on") {
t.Fatalf("auto on: %v\n%s", err, out)
}
if _, err := run("auto", "maybe", "--dir", dir); err == nil {
t.Fatal("auto maybe was accepted")
}
}