Files
pipelines/internal/server/chat_claude_accounts_test.go
T
alan c490f71042 Claude account switcher: save and switch a chat's Claude login in place
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>
2026-11-30 15:10:08 +00:00

91 lines
3.9 KiB
Go

package server
import (
"context"
"encoding/json"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// accountChat is a Claude harness with saved accounts.
type accountChat struct {
*fakeChat
calls []string
}
func (c *accountChat) ClaudeAccounts(_ context.Context, sessionID, directory, operation, name string) (protocol.ClaudeAccountList, error) {
c.calls = append(c.calls, strings.Join([]string{sessionID, directory, operation, name}, "|"))
return protocol.ClaudeAccountList{
ConfigDir: "/home/op/caplens/.claude-profile",
Active: &protocol.ClaudeAccount{Name: "b@example.invalid", Email: "b@example.invalid", Active: true, Saved: true},
Accounts: []protocol.ClaudeAccount{
{Name: "a@example.invalid", Email: "a@example.invalid", Plan: "max", Saved: true},
{Name: "b@example.invalid", Email: "b@example.invalid", Active: true, Saved: true},
},
}, nil
}
func TestClaudeAccountRequestsRouteToTheChatsClaudeHome(t *testing.T) {
ctx := context.Background()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
claudeID := "01900000-0000-7000-8000-000000000021"
codexID := "01900000-0000-7000-8000-000000000022"
for id, backend := range map[string]string{claudeID: "claude", codexID: "codex"} {
if err := store.RecordOperatorSession(ctx, id, "/home/op/caplens", "chat", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if err := store.SetOperatorSessionBackend(ctx, id, backend); err != nil {
t.Fatal(err)
}
}
claudeChat := &accountChat{fakeChat: scriptedChat()}
manager := &chatManager{claude: claudeChat, codex: scriptedChat(), registry: store, cache: map[string]string{}}
var reply protocol.Message
capture := func(msg protocol.Message) error { reply = msg; return nil }
for _, kind := range []string{protocol.ClaudeAccounts, protocol.ClaudeAccountSave, protocol.ClaudeAccountUse, protocol.ClaudeAccountRemove} {
handled := handleChat(ctx, protocol.Message{Type: kind, ID: "r-" + kind, ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: claudeID, Name: "a@example.invalid"}}, manager, nil, store, "client", capture)
if !handled || reply.Type != kind || reply.ID != "r-"+kind || reply.ClaudeAccounts == nil || len(reply.ClaudeAccounts.Accounts) != 2 {
t.Fatalf("%s reply = %+v", kind, reply)
}
}
want := []string{
claudeID + "|/home/op/caplens|claude_accounts|a@example.invalid",
claudeID + "|/home/op/caplens|claude_account_save|a@example.invalid",
claudeID + "|/home/op/caplens|claude_account_use|a@example.invalid",
claudeID + "|/home/op/caplens|claude_account_remove|a@example.invalid",
}
if strings.Join(claudeChat.calls, "\n") != strings.Join(want, "\n") {
t.Fatalf("calls = %v", claudeChat.calls)
}
wire, _ := json.Marshal(reply)
if !strings.Contains(string(wire), `"claude_accounts":{"config_dir":"/home/op/caplens/.claude-profile","active":{"name":"b@example.invalid"`) {
t.Fatalf("wire = %s", wire)
}
// Other harnesses, a missing name for use/remove, and a bad id are refused.
for _, request := range []protocol.Message{
{Type: protocol.ClaudeAccounts, ID: "x1", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: codexID}},
{Type: protocol.ClaudeAccountUse, ID: "x2", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: claudeID}},
{Type: protocol.ClaudeAccountRemove, ID: "x3", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: claudeID}},
{Type: protocol.ClaudeAccounts, ID: "x4", ClaudeAccount: &protocol.ClaudeAccountRequest{SessionID: "../etc"}},
{Type: protocol.ClaudeAccounts, ID: "x5"},
} {
handleChat(ctx, request, manager, nil, store, "client", capture)
if reply.Type != protocol.Error || reply.ID != request.ID {
t.Fatalf("%s accepted: %+v", request.ID, reply)
}
}
if len(claudeChat.calls) != 4 {
t.Fatalf("a refused request reached the harness: %v", claudeChat.calls)
}
}