Files
magpie/internal/provider/claude_onboard_test.go
T
yetone 541102eba2 claude: a saved account's .claude.json gets the onboarding mark when it lacks it (Jaxxxxnn on X)
Jaxxxxnn on X (magpie 0.1.2669, Claude Code 2.1.316, macOS arm64): a
saved Claude account's config directory had a .claude.json without
hasCompletedOnboarding. Claude Code's interactive mode then asked for a
login method beside a sign-in that works, the run ended on it, and both
requests and the usage read failed as if signed out. Adding the mark by
hand fixed it with the same token.

magpie wrote the mark only when it made .claude.json itself, so a
directory -p ran in (before the interactive mode was the default) or one
an earlier magpie made kept a file without it. claudeOnboarded now adds
the mark to the file that is there, every other key kept (numbers read
with UseNumber), on both paths of claudeSavedDir (sign-in put in, or
there already) and in claudeTokenDir. A file that can't be read or
parsed, which Claude Code may be writing, is left as it is.

Run (mote, at this tree):
- go test -count=1 ./internal/provider ./internal/claudetui: ok
- go vet ./internal/provider, GOOS=linux and GOOS=windows: ok
- gofmt -l: clean

Broken by hand: with claude_dirs.go and claude_setup_token.go at
origin/main, TestClaudeSavedDirMarksOnboardingDone and
TestClaudeTokenDirMarksOnboardingDone are red.

Not tried: a real Claude Code 2.1.316 run in a directory without the
mark; the reporter's own fix (the same mark) is the evidence. Not the
full suite.
2026-11-02 11:48:26 +08:00

113 lines
3.7 KiB
Go

package provider
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
// Jaxxxxnn on X (magpie 0.1.2669, Claude Code 2.1.316): a saved account's
// config directory had a .claude.json without hasCompletedOnboarding, so
// Claude Code's interactive mode asked for a login method beside a sign-in
// that works, and every run and usage read there ended. The mark is added
// to the file that is there, every other key kept, both where the sign-in
// is put in and where it is there already.
func TestClaudeSavedDirMarksOnboardingDone(t *testing.T) {
claudeThree(t)
var users []string
for _, l := range Logins("claude") {
if !l.Active {
users = append(users, l.User)
}
}
if len(users) < 2 {
t.Fatalf("%d saved accounts beside the active one, want 2", len(users))
}
read := func(dir string) map[string]any {
t.Helper()
b, err := os.ReadFile(filepath.Join(dir, ".claude.json"))
if err != nil {
t.Fatal(err)
}
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
t.Fatalf("%v: %s", err, b)
}
return m
}
// a .claude.json without the mark, as -p or an older magpie left it
stale := `{"numStartups": 3, "firstStartTime": "2026-10-30T01:02:03.000Z", "userID": "abc", "big": 12345678901234567890}`
// the sign-in not put in yet: the profile file there is kept
fresh := claudeAccountDir(users[0])
if err := os.MkdirAll(fresh, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(fresh, ".claude.json"), []byte(stale), 0o600); err != nil {
t.Fatal(err)
}
// the sign-in there already, put in by an earlier run
had, err := claudeSavedDir(users[1])
if err != nil {
t.Fatal(err)
}
if m := read(had); m["hasCompletedOnboarding"] != true || m["oauthAccount"] == nil {
t.Fatalf("a new directory's .claude.json is %v, want the mark and the profile", m)
}
if err := os.WriteFile(filepath.Join(had, ".claude.json"), []byte(stale), 0o600); err != nil {
t.Fatal(err)
}
for _, u := range users[:2] {
dir, err := claudeSavedDir(u)
if err != nil {
t.Fatal(err)
}
m := read(dir)
if m["hasCompletedOnboarding"] != true {
t.Fatalf("%s: onboarding not marked done: %v", u, m)
}
if m["numStartups"] != float64(3) || m["userID"] != "abc" || m["firstStartTime"] != "2026-10-30T01:02:03.000Z" {
t.Fatalf("%s: Claude Code's own keys not kept: %v", u, m)
}
if b, _ := os.ReadFile(filepath.Join(dir, ".claude.json")); !strings.Contains(string(b), "12345678901234567890") {
t.Fatalf("%s: a number Claude Code wrote was rewritten: %s", u, b)
}
}
// a file being written (not JSON yet) is left as it is
half := `{"numStartups": 3, "userI`
if err := os.WriteFile(filepath.Join(had, ".claude.json"), []byte(half), 0o600); err != nil {
t.Fatal(err)
}
if _, err := claudeSavedDir(users[1]); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(filepath.Join(had, ".claude.json")); string(b) != half {
t.Fatalf("a half-written .claude.json was written over: %s", b)
}
}
// A setup-token account's directory made before the mark was written, or
// one -p ran in, gets the mark too.
func TestClaudeTokenDirMarksOnboardingDone(t *testing.T) {
claudeHome(t)
dir := claudeAccountDir("token@x.com")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, ".claude.json"), []byte(`{"numStartups": 1}`), 0o600); err != nil {
t.Fatal(err)
}
if _, err := claudeTokenDir("token@x.com"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(filepath.Join(dir, ".claude.json"))
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil || m["hasCompletedOnboarding"] != true || m["numStartups"] != float64(1) {
t.Fatalf(".claude.json is %s (%v), want the mark beside numStartups", b, err)
}
}