Files
magpie/internal/agent/install_test.go
T
yetone d51d314ac5 agent: with no Node.js on the machine, Install another agent's npm commands install Node first, in the same line (#727, Sun1090 on v0.1.801: "没写curl的命令… curl xxx && npm xxxx", confirmed as option 1 — Pi, Copilot CLI, Crush and the rest had only npm install -g …, which a new machine without Node can't run) — installCommands now takes whether npm is here (nodeHere: exec.LookPath("npm"), then the folders proc.UserBinDirs knows — nvm's, fnm's, mise's, volta's, Homebrew's, the ~/.npmrc prefix — and on Windows also the registry's PATH (proc.LoginPath) and %ProgramFiles%\nodejs; npm.cmd there), and where it isn't, npmInstall gives on a Mac or Linux export NVM_DIR="$HOME/.nvm" && mkdir -p "$NVM_DIR" && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.8/install.sh | bash && . "$NVM_DIR/nvm.sh" && nvm install --lts && npm install -g <pkg> (nodejs.org's nvm route; NVM_DIR is set because the installer goes by $XDG_CONFIG_HOME/nvm when that is set, and nvm.sh is read in the same shell since the profile line only reaches new terminals; nvm's Node is the user's, so no sudo), and on Windows, for PowerShell, winget install -e --id OpenJS.NodeJS.LTS; $env:Path = <Machine + User Path from the registry>; npm.cmd install -g <pkg> (";" as Windows PowerShell 5.1 has no "&&", PATH re-read since the MSI only adds Node to new terminals, npm.cmd since npm.ps1 is refused under the default execution policy). Each such command carries node (nvm, nvm-mac or winget); the row labels it "Node.js + npm" and the note says the npm commands install Node with nvm/winget first — on a Mac that nvm's installer stops without Xcode's Command Line Tools (run xcode-select --install), on Windows to run them in PowerShell and open a new terminal if npm still isn't found. With Node here everything is as before. magpie still only shows commands to copy (the list never had a one-click install; the page's Update all/update pill run only for CLIs already installed, unchanged). The via column is 84px so "Node.js + npm" stays on one line. TestInstallCommandsWithoutNode (Pi on darwin/linux/windows, Claude's installer untouched, npm alone with Node), TestNpmIn and TestInstallWithoutNodeParses (bash/zsh/sh -n read the line) are new; agent-update-all.test.cjs gains "no Node.js here" (label, note with/without Xcode, the whole line copied, no sideways scroll, winget note in Chinese) and passes in WebKit (8 tests). go vet ./internal/..., builds (darwin, GOOS=linux -tags nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass but for TestCursorSignInJoinsAWrappedLink, which timed out under load and passes alone. Nothing was installed: the commands were only parsed, not run, and not tried on a machine without Node.
2026-10-04 10:13:55 +08:00

140 lines
6.1 KiB
Go

package agent
import (
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
)
// The install commands are the vendors' own (#727): the installer their
// docs give first, then npm's package, the one the update uses; per OS.
func TestInstallCommands(t *testing.T) {
cmds := func(id, goos string) []string {
var out []string
for _, c := range installCommands(id, goos, true) {
out = append(out, c.Via+": "+c.Command)
}
return out
}
for _, c := range []struct {
id, goos string
want []string
}{
{"claude", "darwin", []string{"script: curl -fsSL https://claude.ai/install.sh | bash", "npm: npm install -g @anthropic-ai/claude-code"}},
{"claude", "windows", []string{"powershell: irm https://claude.ai/install.ps1 | iex", "npm: npm install -g @anthropic-ai/claude-code"}},
{"codex", "darwin", []string{"script: curl -fsSL https://chatgpt.com/codex/install.sh | sh", "brew: brew install --cask codex", "npm: npm install -g @openai/codex"}},
{"codex", "linux", []string{"script: curl -fsSL https://chatgpt.com/codex/install.sh | sh", "npm: npm install -g @openai/codex"}},
{"gemini", "linux", []string{"brew: brew install gemini-cli", "npm: npm install -g @google/gemini-cli"}},
{"gemini", "windows", []string{"npm: npm install -g @google/gemini-cli"}},
{"opencode", "darwin", []string{"script: curl -fsSL https://opencode.ai/install | bash", "npm: npm install -g opencode-ai"}},
{"opencode", "windows", []string{"npm: npm install -g opencode-ai"}},
{"pi", "linux", []string{"npm: npm install -g @earendil-works/pi-coding-agent"}},
{"cursor", "darwin", nil}, // no command magpie knows
} {
if got := cmds(c.id, c.goos); !reflect.DeepEqual(got, c.want) {
t.Errorf("%s on %s: %q, want %q", c.id, c.goos, got, c.want)
}
}
}
// Only the agents not on this machine are offered, and not WSL's twins.
func TestInstallsOnlyMissing(t *testing.T) {
home := t.TempDir()
t.Setenv("PATH", t.TempDir())
here := &Agent{ID: "codex", Name: "Codex", Path: filepath.Join(home, "codex.toml"), Dir: home}
gone := &Agent{ID: "gemini", Name: "Gemini CLI", Icon: "gemini", Path: filepath.Join(home, "nope", "settings.json"), Bin: "gemini"}
wsl := &Agent{ID: "claude@wsl:Ubuntu", Name: "Claude Code", WSL: "Ubuntu", Path: filepath.Join(home, "nope", "x")}
none := &Agent{ID: "cursor", Name: "Cursor", Path: filepath.Join(home, "nope", "y")}
got := installsOf([]*Agent{here, gone, wsl, none}, "darwin", true)
if len(got) != 1 || got[0].ID != "gemini" || got[0].Name != "Gemini CLI" || got[0].Icon != "gemini" || len(got[0].Commands) != 2 {
t.Fatalf("installs: %+v", got)
}
if got := installsOf(nil, "linux", true); got == nil {
t.Error("none is [] for the page, not null")
}
}
// With no Node.js here, an npm command installs it first, in the same
// line (#727, Sun1090): nvm and its LTS Node on a Mac or Linux, winget's
// Node.js LTS on Windows; the vendors' own installers are as they were.
func TestInstallCommandsWithoutNode(t *testing.T) {
nvm := `export NVM_DIR="$HOME/.nvm" && mkdir -p "$NVM_DIR" && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/` + nvmVersion +
`/install.sh | bash && . "$NVM_DIR/nvm.sh" && nvm install --lts && npm install -g `
winget := "winget install -e --id OpenJS.NodeJS.LTS; $env:Path = [Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [Environment]::GetEnvironmentVariable('Path','User'); npm.cmd install -g "
for _, c := range []struct {
id, goos string
want []InstallCmd
}{
{"pi", "darwin", []InstallCmd{{Via: "npm", Node: "nvm-mac", Command: nvm + "@earendil-works/pi-coding-agent"}}},
{"pi", "linux", []InstallCmd{{Via: "npm", Node: "nvm", Command: nvm + "@earendil-works/pi-coding-agent"}}},
{"pi", "windows", []InstallCmd{{Via: "npm", Node: "winget", Command: winget + "@earendil-works/pi-coding-agent"}}},
{"claude", "linux", []InstallCmd{
{Via: "script", Command: "curl -fsSL https://claude.ai/install.sh | bash"},
{Via: "npm", Node: "nvm", Command: nvm + "@anthropic-ai/claude-code"},
}},
} {
if got := installCommands(c.id, c.goos, false); !reflect.DeepEqual(got, c.want) {
t.Errorf("%s on %s: %+v, want %+v", c.id, c.goos, got, c.want)
}
}
// with Node here, npm's alone, and no Node said
if got := installCommands("pi", "darwin", true); len(got) != 1 || got[0].Node != "" || got[0].Command != "npm install -g @earendil-works/pi-coding-agent" {
t.Errorf("with node: %+v", got)
}
// installsOf passes it on
home := t.TempDir()
t.Setenv("PATH", t.TempDir())
gone := &Agent{ID: "pi", Name: "Pi", Path: filepath.Join(home, "nope", "models.json"), Bin: "pi"}
if got := installsOf([]*Agent{gone}, "linux", false); len(got) != 1 || got[0].Commands[0].Node != "nvm" {
t.Errorf("installs without node: %+v", got)
}
}
// npm is found in a folder of a user's tools (nvm's, Homebrew's), as
// npm.cmd on Windows; a folder without it, or a folder named npm, isn't it.
func TestNpmIn(t *testing.T) {
empty, nvm, win, odd := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir()
if err := os.WriteFile(filepath.Join(nvm, "npm"), []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(win, "npm.cmd"), nil, 0o644); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(odd, "npm"), 0o755); err != nil {
t.Fatal(err)
}
for _, c := range []struct {
dirs []string
goos string
want bool
}{
{nil, "darwin", false},
{[]string{empty, odd, ""}, "linux", false},
{[]string{empty, nvm}, "darwin", true},
{[]string{nvm}, "windows", false},
{[]string{empty, win}, "windows", true},
{[]string{win}, "linux", false},
} {
if got := npmIn(c.dirs, c.goos); got != c.want {
t.Errorf("npmIn(%q, %s) = %v, want %v", c.dirs, c.goos, got, c.want)
}
}
}
// The command without Node is one line every shell it's run in reads:
// bash and zsh on a Mac or Linux.
func TestInstallWithoutNodeParses(t *testing.T) {
c := npmInstall("@earendil-works/pi-coding-agent", "linux", false).Command
for _, sh := range []string{"bash", "zsh", "sh"} {
p, err := exec.LookPath(sh)
if err != nil {
continue
}
if out, err := exec.Command(p, "-n", "-c", c).CombinedOutput(); err != nil {
t.Errorf("%s -n: %v %s", sh, err, out)
}
}
}