Files
magpie/internal/agent/claude_dirs_test.go
T
yetone f3cdb03c8a agents: a Claude Code kept in another config folder is a row of its own (BieJian21872 on X)
Only the folder CLAUDE_CONFIG_DIR (else ~/.claude) names was found. Every
other ~/.claude… folder holding Claude Code's files (settings.json,
.credentials.json or projects/) is now claude#<name>, "Claude Code · <name>",
made by claudeIn at that folder (place.dir, as claude#launch is): connected,
model picked and checked in its own settings.json. With the variable
elsewhere, ~/.claude is claude#default. ~/.claude.json, another tool's
.claude-… folder and the launch file's folder are not rows. A folder
outside the home folder is not found (documented in agent-wiring.md).

Run at the pushed head's parent 16ed1a952 plus this change, on mote:
go test ./internal/agent (ok), ./internal/library (ok), ./internal/cli
(one red, TestEveryRouteHasACommandOrIsNamed on POST
/api/plugin-pages/arrange, not this change; #6162). gofmt clean; go vet
darwin, linux nogui and windows on internal/agent. Broken by hand:
dropping claudeDirAgents from onMachineAll turns
TestClaudeCodeInMoreThanOneFolder red.
Not tried: a real Claude Code against a second folder; GUI suite (no GUI
file changed); internal/gateway and internal/sessions were still running.
2026-11-11 21:02:21 +08:00

77 lines
2.8 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// BieJian21872 on X: a second Claude Code (CLAUDE_CONFIG_DIR=~/.claude-work)
// was never found; only ~/.claude was. Each extra folder holding Claude
// Code's files is a row of its own, connected in its own settings.json.
func TestClaudeCodeInMoreThanOneFolder(t *testing.T) {
home, _ := claudeDottedHome(t, provider.Provider{ID: "relay", Name: "Relay", Anthropic: "https://example.test", Key: "k",
Models: []string{"claude-sonnet-4.5", "glm-4.6"}}, `{}`)
work := filepath.Join(home, ".claude-work")
writeFile(t, filepath.Join(work, "settings.json"), `{"theme": "dark"}`)
writeFile(t, filepath.Join(home, ".claude2", "projects", "p", "s.jsonl"), `{}`)
// not Claude Code's: another tool's folder, a file, a backup, the launch file's name
writeFile(t, filepath.Join(home, ".claude-code-router", "config.json"), `{}`)
writeFile(t, filepath.Join(home, ".claude.json"), `{}`)
writeFile(t, filepath.Join(home, ".claude.json.backup"), `{}`)
writeFile(t, filepath.Join(home, ".claude-launch", "settings.json"), `{}`)
t.Setenv("PATH", t.TempDir())
got := map[string]*Agent{}
for _, a := range claudeDirAgents(home) {
got[a.ID] = a
}
if len(got) != 2 || got["claude#work"] == nil || got["claude#2"] == nil {
t.Fatalf("rows %v, want claude#work and claude#2 only", got)
}
// listed on the Agents page with the others
listed := false
for _, b := range onMachine(home, filepath.Join(home, ".config")) {
listed = listed || b.ID == "claude#work"
}
if !listed {
t.Fatal("claude#work isn't among the agents magpie lists")
}
a := got["claude#work"]
if a.Dir != work || !a.Detected() || a.Name != "Claude Code · work" {
t.Fatalf("row %q at %s detected %v", a.Name, a.Dir, a.Detected())
}
if err := a.Field("model").Set("relay/glm-4.6"); err != nil {
t.Fatal(err)
}
path := filepath.Join(work, "settings.json")
if v, _ := edit.GetJSON(path, "env.ANTHROPIC_BASE_URL"); v != gateway.URL() {
b, _ := os.ReadFile(path)
t.Fatalf("%s doesn't point at magpie:\n%s", path, b)
}
if v, _ := edit.GetJSON(path, "theme"); v != "dark" {
t.Fatalf("the user's theme is %q", v)
}
// the default row is untouched
if v, _ := edit.GetJSON(filepath.Join(home, ".claude", "settings.json"), "env.ANTHROPIC_BASE_URL"); v != "" {
t.Fatalf("~/.claude was written: %q", v)
}
if !claude(home).Detected() {
t.Fatal("the default row is gone")
}
// with CLAUDE_CONFIG_DIR at one of them, the others (~/.claude too) are rows
t.Setenv("CLAUDE_CONFIG_DIR", work)
ids := map[string]bool{}
for _, a := range claudeDirAgents(home) {
ids[a.ID] = true
}
if ids["claude#work"] || !ids["claude#default"] || !ids["claude#2"] {
t.Fatalf("rows with CLAUDE_CONFIG_DIR at .claude-work: %v", ids)
}
}