Files
magpie/internal/library/rtk_install_bottle_test.go
T
yetone 053d0d37f8 library: Install RTK uses brew only when Homebrew has rtk's bottle, and keeps why it failed on the card (Scott__D on X)
Scott__D on X: "Magpie 里的这个 RTK 安装一直都不成功". The RTK card sat on
"Installing RTK… this can take a minute." over `brew install rtk`.

Cause: Homebrew bottles rtk only for Apple silicon on the newest macOS
(arm64_sequoia, arm64_tahoe, arm64_golden_gate, and Linux). On an Intel Mac
or macOS 14 and older, `brew install rtk` builds rtk from source, and first
the 19 formulae it builds with, rust, llvm and python among them: hours.
InstallRTK gives the installer five minutes, so each try was ended, said so
in a status line gone after ten seconds, and the next try started over.
Reproduced on mote with a sandbox Homebrew (HOME and prefix of its own)
and HOMEBREW_FAKE_MACOS=14.7: brew info rtk says "stable 1.1.0, HEAD" with
no "(bottled)", brew install rtk lists 19 dependencies to build, and
magpie's InstallRTK spent 3m43s in it before failing.

Fix:
- rtkInstaller asks `brew info rtk` and uses brew only when it says
  "(bottled)", brew's own word for pouring the bottle here. Otherwise, and
  when brew fails or doesn't answer in 30s, it is rtk's own script
  (install.sh from rtk-ai/rtk's README, which downloads rtk's release for
  darwin x86_64/aarch64 and Linux). The answer is kept an hour per brew,
  since ReadRTK asks on every read while rtk isn't installed.
- A failed brew says Homebrew's first Error: line and what it says to do,
  not the support-tier notice its output ends with.
- The card keeps "The install failed: {why}" beside Get RTK until the
  next try (as the upgrade's error already did), in every language.
- The five-minute bound on the installer was already there and stays.

Run on mote (macOS 27.2, arm64):
- real InstallRTK, sandbox HOME, sandbox Homebrew, HOMEBREW_FAKE_MACOS=14.7:
  chose rtk's script, installed rtk 1.1.0 into the sandbox ~/.local/bin in
  12.2s (`rtk --version`: rtk 1.1.0); the card then offers Put RTK on PATH.
- real InstallRTK, same sandbox, macOS 27 as it is: chose brew (bottled),
  installed rtk 1.1.0 in 10.7s.
- go test ./internal/library (TestRTK*): ok. New tests:
  TestRTKInstallsWithBrewOnlyFromItsBottle (brew info output read from the
  real brew, bottled and not), TestRTKBrewInfoThatHangs,
  TestRTKBrewInstallSaysBrewsError (brew's real output on mote).
- GUI: library-rtk-install.test.cjs, 5 languages x 1100/420, Chromium and
  WebKit: 22/22. All library-*, gui-de/ja/zh-tw and i18n-keys files: 770 of
  771; the one red is i18n-keys "a plugin's words are its own" on
  plugins/routing "masking…", red on origin/main too (#4053).
- gofmt -l: nothing. go vet ./internal/library ./internal/gui;
  GOOS=linux and GOOS=windows go vet ./internal/library: ok.
- Full suite on mote, go test -tags nogui -count=1 -timeout 45m ./...
  at base df9376592: 77 packages ok, exit 0. Rebased on 874b86653 (no
  change under internal/library or the library plugin); at the pushed
  tree: go test ./internal/library ok, GOOS=linux go vet -tags nogui ./...
  ok, library-rtk-install + i18n-keys 25 of 26 (the one red is #4053).

Broken by hand: rtkInstaller back to "brew when there is one": all four
bottle cases, the hang test and (with brewError off) the error test red.
The kept answer dropped: "brew asked 2 times". The card's error line
dropped: library-rtk-install red in every language.

Not tried: a real Intel Mac or a real macOS 14 (simulated with
HOMEBREW_FAKE_MACOS); Scott__D's own Mac, macOS version and network; a
network where GitHub downloads fail (the script's error then shows on the
card).
2026-10-28 19:28:44 +08:00

157 lines
5.2 KiB
Go

package library
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/proc"
"github.com/yetone/magpie/internal/testenv"
)
// What brew info rtk printed on an Apple silicon Mac (macOS 27), and with
// HOMEBREW_FAKE_MACOS=14.7, where Homebrew has no rtk bottle: read from a
// real brew 5 on mote.
const (
brewInfoBottled = `==> rtk: stable 1.1.0 (bottled), HEAD
CLI proxy to minimize LLM token consumption
https://www.rtk-ai.app/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/r/rtk.rb
License: Apache-2.0
==> Dependencies
Build (1): rust
`
brewInfoSource = `==> rtk: stable 1.1.0, HEAD
CLI proxy to minimize LLM token consumption
https://www.rtk-ai.app/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/r/rtk.rb
License: Apache-2.0
==> Dependencies
Build (1): rust
`
)
// fakeBrew puts a brew in the sandbox's bin answering info rtk with info
// (and exit code), counting each ask in a file; install rtk prints
// install and fails. The kept answer is dropped before and after.
func fakeBrew(t *testing.T, h, info string, code int, install string) (asked func() int) {
t.Helper()
bin := filepath.Join(h, "bin")
write(t, filepath.Join(h, "info.txt"), info)
write(t, filepath.Join(h, "install.txt"), install)
count := filepath.Join(h, "asked")
testenv.Program(t, filepath.Join(bin, "brew"), `#!/bin/sh
case "$*" in
"info rtk") echo x >> "`+count+`"; /bin/cat "`+filepath.Join(h, "info.txt")+`"; exit `+itoa(code)+` ;;
"install rtk") /bin/cat "`+filepath.Join(h, "install.txt")+`"; exit 1 ;;
esac
exit 2
`)
testenv.Program(t, filepath.Join(bin, "curl"), "#!/bin/sh\nexit 1\n")
forgetBottled := func() {
bottled.Lock()
bottled.brew, bottled.ok, bottled.at = "", false, time.Time{}
bottled.Unlock()
}
forgetBottled()
t.Cleanup(forgetBottled)
proc.Forget()
t.Cleanup(proc.Forget)
return func() int {
b, _ := os.ReadFile(count)
return strings.Count(string(b), "x")
}
}
func itoa(n int) string {
if n == 0 {
return "0"
}
return "1"
}
// Scott__D on X: Install RTK ran brew install rtk on a Mac Homebrew has no
// rtk bottle for, so brew set out to build rust, llvm and python from
// source, and the five-minute wait ended it every time. Such a brew is
// passed over for rtk's own script; one with the bottle is still used.
func TestRTKInstallsWithBrewOnlyFromItsBottle(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("winget installs rtk on Windows; the fakes are shell scripts")
}
script := "curl -fsSL " + rtkScript + " | sh"
for _, c := range []struct {
name, info string
code int
want string
}{
{"bottled", brewInfoBottled, 0, "brew install rtk"},
{"built from source", brewInfoSource, 0, script},
// mote's brew, which can't run git: it says nothing of rtk
{"brew that fails", "Error: Unable to automatically install a working Git.\n", 1, script},
{"brew that says nothing", "", 0, script},
} {
t.Run(c.name, func(t *testing.T) {
h := sandbox(t)
asked := fakeBrew(t, h, c.info, c.code, "")
if v := ReadRTK(); v.Path != "" || v.Install != c.want {
t.Fatalf("install %q, want %q (path %q)", v.Install, c.want, v.Path)
}
// the page reads it again: brew is asked once
if v := ReadRTK(); v.Install != c.want || asked() != 1 {
t.Fatalf("read again: install %q, brew asked %d times", v.Install, asked())
}
})
}
}
// A brew info that doesn't answer is given brewInfoWait, then rtk's own
// script is used.
func TestRTKBrewInfoThatHangs(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fakes are shell scripts")
}
h := sandbox(t)
fakeBrew(t, h, "", 0, "")
testenv.Program(t, filepath.Join(h, "bin", "brew"), "#!/bin/sh\nexec /bin/sleep 60\n")
w := brewInfoWait
brewInfoWait = 300 * time.Millisecond
t.Cleanup(func() { brewInfoWait = w })
start := time.Now()
if v := ReadRTK(); !strings.HasPrefix(v.Install, "curl ") {
t.Fatalf("install %q", v.Install)
}
if d := time.Since(start); d > 20*time.Second {
t.Fatalf("waited %s for brew", d)
}
}
// A failed brew install says Homebrew's error and what to do, not the
// support-tier notice its output ends with. The output is brew 5's on mote
// (macOS without the Command Line Tools for its Homebrew), from its last
// download on.
func TestRTKBrewInstallSaysBrewsError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the fakes are shell scripts")
}
h := sandbox(t)
fakeBrew(t, h, brewInfoBottled, 0, "✔︎ Formula rtk (1.1.0)\n✔︎ Formula rust (1.99.1)\n"+
"Error: No developer tools installed.\n\nInstall the Command Line Tools:\n xcode-select --install\n"+
"Error: Xcode alone is not sufficient on Sonoma.\n\nInstall the Command Line Tools:\n xcode-select --install\n\n"+
"This is a Tier 3 configuration:\n https://docs.brew.sh/Support-Tiers#tier-3\n"+
"You can report issues with Tier 3 configurations to Homebrew/* repositories!\n \n"+
"Read the above document before opening any issues or PRs.\n")
_, err := InstallRTK()
if err == nil {
t.Fatal("brew failed, and InstallRTK didn't")
}
want := "brew failed (exit code 1) — Error: No developer tools installed. Install the Command Line Tools: xcode-select --install"
if err.Error() != want {
t.Fatalf("error %q\nwant %q", err, want)
}
}