Files
magpie/internal/gateway/claude_made_first_test.go
T
yetone d113d7613c claude: an account Claude Code was moved off is never answered, nor counted, as the one it was moved to (nil_1024, v0.1.641, routing Smart: Claude Code signed in to A, B added in magpie and made first, B's 5 hours spent; the Usage page showed A's 5 hours spent too though A was never used, and every Claude request ended 499 until magpie's Claude was turned off and Claude Code signed in to A again; Claude Code's /status said B) — "Make first" on a saved Claude account signs Claude Code itself in to it (SwitchLogin): B's sign-in goes into Claude Code's keychain and ~/.claude.json, A's is saved and from then on run in a config directory of its own. But the Claude Code processes the bridge keeps for a conversation's next turn were found by owner, p.ID + the account's name alone, and A's conversations were kept in Claude Code's own home, from when A was its sign-in. Claude Code reads its keychain again every 30 s (2.1.287, the keychain reader's ASt=30000), so those processes went on as B: A's next turns, routed to the saved A, resumed them and were B's, refused as spent, and the rate_limit_event they printed (B's five_hour, rejected, 100%) was noted under A, so A showed spent and rested until B's reset; with B spent too, every request was turned away with 429, which the agent kept retrying until it gave up (the gateway's 499, "the agent canceled the request"). Signing Claude Code in to A again made those processes A's again, which is why that fixed it. Now a run in Claude Code's own home has "own" in its owner (Account.AgentsOwn: no token of magpie's), so a saved account's turn never resumes one kept from when it was Claude Code's own sign-in, and starts a Claude Code on its own sign-in in its own directory; what such a run says of the account it is on is noted only while Claude Code is still signed in to its owner (ClaudeCodeMovedOff); and Claude Code's /usage reading for the account it is signed in to is kept only when Claude Code was on that account before and after it ran (claudeWindows), so a reading taken across a switch, or asked for an account Claude Code was just moved off, isn't kept as that account's. TestClaudeMadeFirstKeepsTheOtherAccountItsOwn (A's turn after the switch was answered "tok-b turn 2" by the Claude Code kept from before, and A's 5 hours read 100%, without the change; B is still asked as itself and reads 100%) and TestClaudeMadeFirstSpentGoesToTheOther (through the gateway, routing Smart, B refusing as spent: A's conversation was a 429 "You've hit your limit", now answered on A) run a fake Claude Code that answers as the sign-in it reads at each turn, in a temp HOME, with the real SwitchLogin; TestClaudeUsageOfAnotherAccountNotKept (Claude Code moved to B while /usage ran: A read 100% without the change) covers the reading. No real Claude Code, account or keychain was used, and no request was made to Anthropic. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-02 17:02:42 +08:00

224 lines
8.9 KiB
Go

package gateway
import (
"context"
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// claudeMadeFirst signs Claude Code in to a@example.com, with
// b@example.com added in magpie beside it, as nil_1024 had them, and puts
// a Claude Code in PATH that answers as the account whose sign-in it reads
// at each turn, from the config directory it runs in, as Claude Code reads
// its keychain again every half minute: b's 5 hours are spent, a's not;
// with bRefuses b's turns are turned away as Claude Code does, out of
// quota, else answered.
func claudeMadeFirst(t *testing.T, bRefuses bool) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
oauth := func(tok string) map[string]any {
return map[string]any{"claudeAiOauth": map[string]any{"accessToken": tok, "refreshToken": tok + "-refresh",
"expiresAt": time.Now().Add(8 * time.Hour).UnixMilli(), "subscriptionType": "max"}}
}
os.MkdirAll(filepath.Join(home, ".claude"), 0o700)
os.WriteFile(filepath.Join(home, ".claude", ".credentials.json"), mustJSON(oauth("tok-a")), 0o600)
os.WriteFile(filepath.Join(home, ".claude.json"), mustJSON(map[string]any{
"oauthAccount": map[string]any{"emailAddress": "a@example.com", "accountUuid": "u-a"}}), 0o600)
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(filepath.Join(filepath.Dir(provider.Path()), "logins.json"), mustJSON([]map[string]any{
{"agent": "claude", "user": "b@example.com", "plan": "max", "on": true, "seen": time.Now(), "auth": oauth("tok-b"),
"profile": map[string]any{"emailAddress": "b@example.com", "accountUuid": "u-b"}},
}), 0o600)
bin := t.TempDir()
refuses := ""
if bRefuses {
refuses = "yes"
}
day := time.Now().UTC().AddDate(0, 0, 1).Format("Jan 2")
reset := strconv.FormatInt(time.Now().Add(3*time.Hour).Unix(), 10)
script := `#!/bin/sh
case "$1" in auth) exit 1;; esac
creds="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
if [ "$2" = "/usage" ]; then
used=10
grep -q tok-b "$creds" && used=100
echo '{"type":"result","is_error":false,"result":"Current session: '$used'% used · resets ` + day + ` at 3:30pm (UTC)"}'
exit 0
fi
n=0
while read -r line; do
n=$((n+1))
tok=$(grep -o 'tok-[a-z]*' "$creds" | head -1)
if [ "$tok" = "tok-b" ]; then
echo '{"type":"rate_limit_event","rate_limit_info":{"status":"rejected","rateLimitType":"five_hour","utilization":1,"resetsAt":` + reset + `}}'
if [ -n "` + refuses + `" ]; then
echo '{"type":"assistant","message":{"id":"x","model":"<synthetic>","role":"assistant","content":[{"type":"text","text":"You'"'"'ve hit your limit"}]},"error":"rate_limit"}'
echo '{"type":"result","subtype":"success","is_error":true,"result":"You'"'"'ve hit your limit"}'
continue
fi
else
echo '{"type":"rate_limit_event","rate_limit_info":{"status":"allowed","rateLimitType":"five_hour","utilization":0.1,"resetsAt":` + reset + `}}'
fi
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'$tok' turn '$n'"}}}'
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":3}}}'
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
done
`
os.WriteFile(filepath.Join(bin, "claude"), []byte(script), 0o755)
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
provider.ForgetAccounts()
t.Cleanup(provider.ForgetAccounts)
}
// nil_1024: Claude Code signed in to A, B added in magpie and made first
// (Claude Code's own sign-in moved to B), B's 5 hours spent. A went on
// being asked in the Claude Code kept for its conversation, in Claude
// Code's own home, which by then read B's sign-in: A's turns were B's,
// refused as spent, and what Claude Code said of B's 5 hours was kept as
// A's, A shown as spent though nobody had used it. A's next turn goes to
// a Claude Code of A's own, on A's sign-in, and A's 5 hours are A's.
func TestClaudeMadeFirstKeepsTheOtherAccountItsOwn(t *testing.T) {
claudeMadeFirst(t, false)
s := New()
t.Cleanup(s.subscription.abortAll)
ask := func(p provider.Provider, msgs string) string {
t.Helper()
body := `{"model":"claude-sonnet-5","max_tokens":100,"tools":[{"name":"read","input_schema":{"type":"object"}}],"messages":` + msgs + `}`
rec := httptest.NewRecorder()
var u Usage
if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
t.Fatalf("%d %s", code, msg)
}
var res struct {
Content []struct{ Text string } `json:"content"`
}
json.Unmarshal(rec.Body.Bytes(), &res)
if len(res.Content) == 0 {
t.Fatalf("no answer: %s", rec.Body)
}
return res.Content[0].Text
}
msg := func(role, text string) string { return `{"role":"` + role + `","content":` + strconv.Quote(text) + `}` }
// Claude Code's own sign-in, A
a := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "a@example.com"}}
first := ask(a, `[`+msg("user", "hi")+`]`+"")
if first != "tok-a turn 1" {
t.Fatalf("first turn: %q", first)
}
// B made first: Claude Code is signed in to B, A is saved beside it
if err := provider.SwitchLogin("claude", "b@example.com"); err != nil {
t.Fatal(err)
}
b := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "b@example.com"}}
var saved *provider.Provider
for _, q := range b.AlsoOn() {
if q.Account.User == "a@example.com" {
saved = &q
}
}
if saved == nil {
t.Fatalf("A isn't on beside B: %+v", provider.Logins("claude"))
}
// A's conversation goes on
second := ask(*saved, `[`+msg("user", "hi")+`,`+msg("assistant", first)+`,`+msg("user", "and?")+`]`)
if second != "tok-a turn 1" {
t.Errorf("A's next turn was answered %q: not by a Claude Code on A's own sign-in", second)
}
// B, Claude Code's own now, is asked as itself
if got := ask(b, `[`+msg("user", "hello")+`]`); got != "tok-b turn 1" {
t.Errorf("B: %q", got)
}
usage := provider.LoginUsage(context.Background(), "claude")
for user, want := range map[string]float64{"a@example.com": 10, "b@example.com": 100} {
q := usage[user]
if len(q.Windows) == 0 {
t.Fatalf("%s's usage: %+v", user, q)
}
for _, w := range q.Windows {
if w.Name == "5 hours" && w.Used != want {
t.Errorf("%s's 5 hours: %v%% used, want %v %+v", user, w.Used, want, q)
}
}
}
}
// nil_1024, through the gateway, routing Smart: B made first and spent,
// A's conversation goes on on A, answered, where it was turned away as B
// (and the agent, retrying, gave up: 499).
func TestClaudeMadeFirstSpentGoesToTheOther(t *testing.T) {
claudeMadeFirst(t, true)
restingUntil.Lock()
restingUntil.m = map[string]time.Time{}
restingUntil.Unlock()
if p, err := provider.Find("claude"); err != nil || p.Routing != "" {
t.Fatalf("routing %q (want Smart, the default): %v", p.Routing, err)
}
s := New()
t.Cleanup(s.subscription.abortAll)
h := s.Handler()
send := func(msgs string) (int, string) {
t.Helper()
body := `{"model":"claude/claude-sonnet-5","max_tokens":100,"tools":[{"name":"read","input_schema":{"type":"object"}}],"messages":` + msgs + `}`
done := make(chan struct{})
rec := httptest.NewRecorder()
go func() {
defer close(done)
h.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)))
}()
select {
case <-done:
case <-time.After(20 * time.Second):
t.Fatal("no answer")
}
var res struct {
Content []struct{ Text string } `json:"content"`
}
json.Unmarshal(rec.Body.Bytes(), &res)
if len(res.Content) == 0 {
return rec.Code, rec.Body.String()
}
return rec.Code, res.Content[0].Text
}
msg := func(role, text string) string { return `{"role":"` + role + `","content":` + strconv.Quote(text) + `}` }
code, first := send(`[` + msg("user", "hi") + `]`)
if code != 200 || first != "tok-a turn 1" {
t.Fatalf("first turn: %d %q", code, first)
}
if err := provider.SwitchLogin("claude", "b@example.com"); err != nil {
t.Fatal(err)
}
code, second := send(`[` + msg("user", "hi") + `,` + msg("assistant", first) + `,` + msg("user", "and?") + `]`)
if code != 200 || !strings.HasPrefix(second, "tok-a turn ") {
t.Fatalf("A's conversation with B made first and spent: %d %q", code, second)
}
q := provider.LoginUsage(context.Background(), "claude")["a@example.com"]
for _, w := range q.Windows {
if w.Name == "5 hours" && w.Used != 10 {
t.Fatalf("A's 5 hours: %v%% used, want 10 %+v", w.Used, q)
}
}
}