Files
magpie/internal/agent/claude_detect_windows_test.go
T
yetone 0b904a3e60 agent: on Windows an agent CLI in ~\.local\bin, npm's, Volta's, bun's folder or on the registry's PATH now is listed, though magpie started before it was installed (#1482, edussss)
Detected and cliBin asked exec.LookPath against magpie's own PATH only.
On Windows that is the PATH magpie started with (proc.UserPath does
nothing there; a self-update restarts with the same one), so Claude
Code's native installer, which puts claude.exe in ~\.local\bin and adds
that to the user's PATH in the registry, and an npm -g install, weren't
seen by a running magpie. The sign-in already found the CLI through
proc.FindTool (#839); binAt now does the same for Detected and cliBin, so
the Agents page lists it and shows its version. On the Windows box the
ssh session's PATH lacks ~\.local\bin although the registry has it.

Run: TestClaudeFoundWhereWindowsInstallsPutIt (Windows-only) on the
Windows box: passes with this change, all five cases red with the
fallback disabled. go test of internal/agent on macOS, the full suite,
gofmt -l, go vet -tags nogui on darwin, linux and windows.
2026-10-11 08:18:27 +08:00

85 lines
2.9 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/testenv"
)
// claudeBinHome is a Windows home with no Claude Code folder, no
// CLAUDE_CONFIG_DIR and a PATH of magpie's own that has no claude on it:
// a magpie started before Claude Code was installed, or restarted by a
// self-update with the PATH it had then.
func claudeBinHome(t *testing.T) string {
t.Helper()
home := t.TempDir()
t.Setenv("USERPROFILE", home)
t.Setenv("HOME", home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("PATH", t.TempDir())
if a := claude(home); a.Detected() {
t.Fatal("Claude Code listed with nothing of it here")
}
return home
}
// #1482 (edussss, Windows 11, magpie v0.1.1175): Claude Code wasn't listed
// on the Agents page. Detected asked magpie's own PATH only, which on
// Windows is the one magpie started with: Claude Code's installer (irm
// https://claude.ai/install.ps1 | iex) puts claude.exe in ~\.local\bin and
// adds that to the user's PATH in the registry, which a running magpie
// never sees, and npm's global folder is the same. The sign-in already
// found it there through proc.FindTool (#839); now the Agents page does
// too, with the CLI's path.
func TestClaudeFoundWhereWindowsInstallsPutIt(t *testing.T) {
for _, tc := range []struct{ name, rel string }{
{"the native installer's ~/.local/bin", `.local\bin\claude.exe`},
{"npm's global folder", `AppData\Roaming\npm\claude.cmd`},
{"Volta's", `AppData\Local\Volta\bin\claude.exe`},
{"bun's", `.bun\bin\claude.exe`},
} {
t.Run(tc.name, func(t *testing.T) {
home := claudeBinHome(t)
bin := filepath.Join(home, tc.rel)
writeFile(t, bin, "")
a := claude(home)
if !a.Detected() {
t.Fatalf("Claude Code at %s isn't listed", bin)
}
if got, _ := a.cliBin(); !sameFile(got, bin) {
t.Fatalf("its CLI is %q, want %s", got, bin)
}
})
}
// installed after magpie started, in a folder the registry's PATH has
// now (winget's Links, scoop's shims, an nvm-windows Node): a test's
// registry folder lies under its sandbox (proc.SystemDirs)
t.Run("the registry's PATH now", func(t *testing.T) {
if os.Getenv(testenv.Marker) == "" {
t.Skip("not in a sandbox: the registry's folders would be the machine's own")
}
home := claudeBinHome(t)
dirs := proc.SystemDirs(proc.LoginPath()...)
if len(dirs) == 0 {
t.Skip("no PATH in this machine's registry")
}
bin := filepath.Join(dirs[len(dirs)-1], "claude.exe")
writeFile(t, bin, "")
t.Cleanup(func() { os.Remove(bin) })
if !claude(home).Detected() {
t.Fatalf("Claude Code at %s, on the registry's PATH, isn't listed", bin)
}
})
}
func sameFile(a, b string) bool {
x, err1 := os.Stat(a)
y, err2 := os.Stat(b)
return err1 == nil && err2 == nil && os.SameFile(x, y)
}