The reporter clicked Update all. npm 11.19.0 ran
`npm install -g --prefix /opt/homebrew @anthropic-ai/claude-code@latest`,
logged "reify failed optional dependency …claude-code-darwin-arm64" and
exited 0. Claude Code 2.1.299's postinstall soft-fails without its native
package, so bin/claude.exe was left as the stub that says "Error: claude
native binary not installed." and exits 1. UpdateCLI saw npm succeed and
read the new version from package.json (proc.Version never runs an npm
CLI), so the update was reported as done.
Now, after an update that succeeds, UpdateCLI runs `<cli> --version` once
(runsBroken). It uses a 30s timeout, the update's env (npm's folder first
on PATH) and no stdin. Only a run that exits non-zero and prints no version
counts as broken. A run that didn't start, timed out, or exited 127
(interpreter not found) is unknown, not broken.
- npm installs that magpie runs as the user: the version just installed is
installed once more with --include=optional, which is how the reporter
mended theirs, and the check runs again.
- npm installs needing administrator rights get no second prompt.
- If the CLI still fails, or it was installed some other way (bun, pnpm,
brew, self), the update fails. The message carries what the CLI said and
how to reinstall it, e.g. `npm install -g --prefix /opt/homebrew
@anthropic-ai/claude-code@2.1.299 --include=optional`.
Both Update all and a row's Update pill go through UpdateCLI, so both are
covered. The GUI already shows an update's error, so it is unchanged.
What was run:
- TestUpdateCLIChecksItRuns: a fake Homebrew prefix with Claude Code's
package and the stub as the reporter found them. It covers the update
mended by the second install, a working update that runs nothing more,
and an update left broken that fails with the reinstall command. With
the check disabled it fails ("not mended: <nil>").
- TestRunsBrokenOnlyWhenSure: a version said with a non-zero exit, exit 127
and a silent exit 0 are not broken.
- gofmt -l; go vet for darwin, linux (nogui) and windows.
- go test ./internal/agent/ -timeout 45m passes (856s). The first run hit
the default 10m timeout at load ~290 while in TestOmo, with no failure.
- go test ./internal/proc/ passes, TestNoCommandBypassesProc included.
Not tried: a real npm install that drops the native package, and Windows,
where the new tests skip because their CLIs are shell scripts.
131 lines
5.2 KiB
Go
131 lines
5.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// An npm update that exits 0 but leaves Claude Code without its native
|
|
// package (#2128): the package says 2.1.299, and bin/claude.exe is the
|
|
// stub that only says the native binary isn't installed. magpie installs
|
|
// that version again with its optional packages, and when that doesn't
|
|
// mend it either, the update fails, saying how to reinstall it.
|
|
func TestUpdateCLIChecksItRuns(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("shell scripts as the CLI")
|
|
}
|
|
tmp, _ := filepath.EvalSymlinks(t.TempDir())
|
|
prefix := filepath.Join(tmp, "homebrew")
|
|
pkgDir := filepath.Join(prefix, "lib/node_modules/@anthropic-ai/claude-code")
|
|
pkgJSON := filepath.Join(pkgDir, "package.json")
|
|
exeFile := filepath.Join(pkgDir, "bin/claude.exe")
|
|
// the stub as Claude Code's postinstall leaves it without the native package
|
|
stub := "#!/bin/sh\necho 'Error: claude native binary not installed.' >&2\nexit 1\n"
|
|
works := "#!/bin/sh\necho '2.1.299 (Claude Code)'\n"
|
|
file(t, pkgJSON, `{"name":"@anthropic-ai/claude-code","version":"2.1.298"}`)
|
|
file(t, exeFile, works)
|
|
npm := file(t, filepath.Join(prefix, "bin/npm"), "#!/bin/sh\nexit 1\n")
|
|
link(t, "../lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe", filepath.Join(prefix, "bin/claude"))
|
|
t.Setenv("PATH", filepath.Join(prefix, "bin"))
|
|
t.Setenv("HOME", filepath.Join(tmp, "home"))
|
|
|
|
oldRun := runUpdate
|
|
t.Cleanup(func() { runUpdate = oldRun; versions, latests = memo{}, memo{} })
|
|
versions, latests = memo{}, memo{}
|
|
latests.get("npm:@anthropic-ai/claude-code", "", 1<<40, 1<<40, func() (string, error) { return "2.1.299", nil })
|
|
var ran []string
|
|
mends := true
|
|
runUpdate = func(_ context.Context, u *updater) ([]byte, error) {
|
|
ran = append(ran, strings.Join(u.cmd, " "))
|
|
// npm 11.19.0's log: "reify failed optional dependency …-darwin-arm64", exit 0
|
|
os.WriteFile(pkgJSON, []byte(`{"name":"@anthropic-ai/claude-code","version":"2.1.299"}`), 0o644)
|
|
body := stub
|
|
if mends && strings.HasSuffix(u.cmd[len(u.cmd)-1], "--include=optional") {
|
|
body = works
|
|
}
|
|
testenv.WriteProgram(exeFile, body)
|
|
return []byte("changed 1 package in 3s"), nil
|
|
}
|
|
a := &Agent{ID: "claude", Name: "Claude Code", Bin: "claude"}
|
|
if c, ok := a.CLI(); !ok || c.Via != "npm" || !c.Update || c.Version != "2.1.298" {
|
|
t.Fatalf("before: %+v", c)
|
|
}
|
|
|
|
// mended by installing 2.1.299 again with its optional packages
|
|
c, err := a.UpdateCLI()
|
|
if err != nil || c.Version != "2.1.299" {
|
|
t.Fatalf("mended: %+v, %v", c, err)
|
|
}
|
|
want := []string{
|
|
npm + " install -g --prefix " + prefix + " @anthropic-ai/claude-code@latest",
|
|
npm + " install -g --prefix " + prefix + " @anthropic-ai/claude-code@2.1.299 --include=optional",
|
|
}
|
|
if strings.Join(ran, "\n") != strings.Join(want, "\n") {
|
|
t.Errorf("ran:\n%s\nwant:\n%s", strings.Join(ran, "\n"), strings.Join(want, "\n"))
|
|
}
|
|
|
|
// a working update runs nothing more
|
|
ran = nil
|
|
os.WriteFile(pkgJSON, []byte(`{"name":"@anthropic-ai/claude-code","version":"2.1.298"}`), 0o644)
|
|
versions = memo{}
|
|
runUpdate = func(_ context.Context, u *updater) ([]byte, error) {
|
|
ran = append(ran, strings.Join(u.cmd, " "))
|
|
os.WriteFile(pkgJSON, []byte(`{"name":"@anthropic-ai/claude-code","version":"2.1.299"}`), 0o644)
|
|
return nil, nil
|
|
}
|
|
if _, err := a.UpdateCLI(); err != nil || len(ran) != 1 {
|
|
t.Fatalf("a working update: %v, ran %q", err, ran)
|
|
}
|
|
|
|
// not mended: the update fails, saying what claude said and the reinstall
|
|
ran = nil
|
|
mends = false
|
|
os.WriteFile(pkgJSON, []byte(`{"name":"@anthropic-ai/claude-code","version":"2.1.298"}`), 0o644)
|
|
versions = memo{}
|
|
runUpdate = func(_ context.Context, u *updater) ([]byte, error) {
|
|
ran = append(ran, strings.Join(u.cmd, " "))
|
|
os.WriteFile(pkgJSON, []byte(`{"name":"@anthropic-ai/claude-code","version":"2.1.299"}`), 0o644)
|
|
testenv.WriteProgram(exeFile, stub)
|
|
return nil, nil
|
|
}
|
|
_, err = a.UpdateCLI()
|
|
if err == nil || !strings.Contains(err.Error(), "claude native binary not installed") ||
|
|
!strings.Contains(err.Error(), "npm install -g --prefix "+prefix+" @anthropic-ai/claude-code@2.1.299 --include=optional") || len(ran) != 2 {
|
|
t.Fatalf("not mended: %v, ran %q", err, ran)
|
|
}
|
|
}
|
|
|
|
// What can't be told isn't taken for broken: a CLI that says its version
|
|
// and exits non-zero, one whose interpreter isn't found (127), one that
|
|
// prints nothing and exits 0.
|
|
func TestRunsBrokenOnlyWhenSure(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("shell scripts as the CLI")
|
|
}
|
|
dir := t.TempDir()
|
|
u := &updater{via: "npm", cmd: []string{"npm"}}
|
|
n := 0
|
|
for body, broken := range map[string]bool{
|
|
"#!/bin/sh\necho 'Error: claude native binary not installed.'\nexit 1\n": true,
|
|
"#!/bin/sh\necho 'tool 1.2.3'\nexit 2\n": false,
|
|
"#!/bin/sh\necho 'env: node: No such file or directory' >&2\nexit 127\n": false,
|
|
"#!/bin/sh\n": false,
|
|
} {
|
|
n++
|
|
bin := file(t, filepath.Join(dir, fmt.Sprint("cli", n)), body)
|
|
if got := runsBroken(context.Background(), bin, u); (got != "") != broken {
|
|
t.Errorf("%q: broken %q, want %v", body, got, broken)
|
|
}
|
|
}
|
|
if got := runsBroken(context.Background(), filepath.Join(dir, "missing"), u); got != "" {
|
|
t.Errorf("a CLI that didn't start: %q", got)
|
|
}
|
|
}
|