Files
magpie/internal/plugin/bunupdate_test.go
T
yetone e484b4ff26 update: a slow download still moving is never ended by a total time; Bun, Claude Code and pf-serve too
#5572: since 80a47abba a silent download is broken off after 30s and
tried again, but the update's download still had a 10-minute total
(the update client's Timeout, the GUI run's ctx, `magpie update`'s ctx).
A download under ~140 KB/s, still receiving bytes, failed the 83 MB
Windows exe at 10 minutes part way. The stall timer is what ends a dead
one now; nothing ends one that is still moving.

- internal/update: fetch downloads through fileClient, the client without
  its Timeout. The feed and notes keep the 10-minute client. fetch's stall
  timer runs from the ask to the last byte, so a server that never answers
  still ends at stallAfter (30s), and download's retries stay bounded
  (len(retryWaits)+1 = 6 tries), so a connection that keeps going silent
  ends in about 4 minutes.
- internal/gui (Mac Stage, Windows/Linux StageBinary): run's 10 minutes
  (checkWait, a var for tests) bound only LatestIn; the download runs
  under a ctx with no deadline.
- internal/cli `magpie update` (Mac Stage, Windows/Linux ReplaceBinary):
  the same split.
- Siblings with the same fixed cap:
  - new internal/stall: Start(ctx, after) gives a ctx that ends with
    ErrStalled once `after` goes by with no byte read through w.Reader.
  - Bun (internal/plugin getURLOnce): watched with a 60s stall;
    KeepBunUpdated's 10-minute ctx around CheckBun is gone.
  - Claude Code (internal/claudecode downloadOnce, ~200 MB): watched with
    a 60s stall; keepOnce's 30-minute ctx around install is gone.
  - pf-serve (internal/pii upgrade): its 10-minute ctx is gone; get
    already had a 60s stall and the client a 60s header timeout, as the
    model's download (no total) does.
- Left as they are: plugin installs and npm updates (KeepUpdated's 10
  minutes) run `bun install`, whose network is Bun's own, small tarballs;
  rtk (5 min client), skills (2 min client), skilltree and market reads
  are small files; Grok CLI install has no cap of its own (its caller's
  ctx). None of them carries a large download behind a wall-clock total.

Run on mote (mote-run), at 49f207c78 + this change; the targeted
set again after rebasing on d4a221f79 (ok):
- go test ./internal/stall ./internal/update ./internal/claudecode
  ./internal/pii -run 'Stall|Slow|Silen|TestDownload|TestInstall|TestKeep';
  go test ./internal/plugin -run Bun; go test ./internal/gui -run
  TestUpdate: ok
- new: TestSlowButMovingIsReadWhole, TestSilenceIsAStall (no answer, part
  way), TestDownloadSlowButMovingOutlastsTheClientTimeout (client Timeout
  300ms, body ~1.2s: one try, no retry), TestUpdateSlowDownloadOutlastsTheCheckBound
  (checkWait 200ms, body ~600ms: ready), TestDownloadThatGoesSilentIsAStall
  (claudecode), TestBunDownloadThatGoesSilentIsAStall. The silent server
  after its retries stays covered by TestDownloadStallEveryTimeSaysSo
  (6 tries, errStalled).
- go test -race -count=3 ./internal/stall ./internal/update -run
  'Stall|Slow|Silen': ok
- win-run (Windows 11): ./internal/stall ./internal/update
  ./internal/claudecode ./internal/plugin with the same -run, and
  ./internal/gui -run 'TestUpdateSlowDownload|TestUpdateDownloadSaysStepAndSpeed': ok
- wsl-run (Ubuntu 24.04), -tags nogui: the same packages and ./internal/gui
  -run TestUpdate: ok
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build; GOOS=linux go build -tags nogui;
  GOOS=windows go build: clean
- Full suite: run after the push under the suite lock.

Broken by hand: fetch back on `client` fails
TestDownloadSlowButMovingOutlastsTheClientTimeout ("broke off ...
Client.Timeout or context cancellation while reading body"); the GUI's
download back under the check's ctx fails
TestUpdateSlowDownloadOutlastsTheCheckBound (state error, "context
deadline exceeded"); stall's reader not putting the timer off fails
TestSlowButMovingIsReadWhole (50 of 120 bytes); Bun's and Claude Code's
downloads unwatched fail their silent tests at the 20s bound ("context
deadline exceeded").

Not tried: a real update over a slow or throttled link, on any OS; a real
Bun or Claude Code download over one. The Mac's unpack and signature
check now run with no deadline (they had the 10 minutes before).

Fixes #5572
2026-11-07 06:09:42 +08:00

371 lines
12 KiB
Go

package plugin
import (
"archive/zip"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/stall"
"github.com/yetone/magpie/internal/testenv"
)
// bunReleases serves Bun's releases as GitHub does: each version's zip for
// this machine, holding the bun given, and its SHASUMS256.txt. It counts
// the zips asked for.
func bunReleases(t *testing.T, exe []byte) *atomic.Int32 {
t.Helper()
target, err := bunTarget()
if err != nil {
t.Skip(err)
}
var zb bytes.Buffer
zw := zip.NewWriter(&zb)
h := &zip.FileHeader{Name: target + "/" + bunExe(), Method: zip.Deflate}
h.SetMode(0o755)
w, _ := zw.CreateHeader(h)
w.Write(exe)
zw.Close()
sum := sha256.Sum256(zb.Bytes())
oldSum, hadSum := bunSums[target+".zip"]
bunSums[target+".zip"] = hex.EncodeToString(sum[:])
t.Cleanup(func() {
if hadSum {
bunSums[target+".zip"] = oldSum
} else {
delete(bunSums, target+".zip")
}
})
var asked atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasSuffix(r.URL.Path, "/SHASUMS256.txt"):
fmt.Fprintf(w, "%s %s.zip\n", hex.EncodeToString(sum[:]), target)
case strings.HasSuffix(r.URL.Path, "/"+target+".zip"):
asked.Add(1)
w.Write(zb.Bytes())
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
old := bunRelease
bunRelease = srv.URL
t.Cleanup(func() { bunRelease = old })
return &asked
}
// bunHome gives the test its own magpie folders, no $MAGPIE_BUN, and Bun's
// newest release as given.
func bunHome(t *testing.T, latest string, published time.Time) {
t.Helper()
dir := t.TempDir()
t.Setenv("HOME", dir)
t.Setenv("USERPROFILE", dir)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(dir, "cache"))
t.Setenv("LOCALAPPDATA", filepath.Join(dir, "local"))
t.Setenv("MAGPIE_BUN", "")
old := bunLatest
bunLatest = func(context.Context) (string, time.Time, error) { return latest, published, nil }
t.Cleanup(func() { bunLatest = old })
}
func stubTry(t *testing.T, f func(exe, v string) error) {
old := tryBun
tryBun = func(_ context.Context, exe, v string) error { return f(exe, v) }
t.Cleanup(func() { tryBun = old })
}
// bunSums must be checked against Bun's official SHASUMS256.txt when
// BunVersion changes; this catches a missing or malformed value.
func TestBunDefaultSums(t *testing.T) {
for _, target := range []string{
"bun-darwin-aarch64",
"bun-darwin-x64",
"bun-linux-aarch64",
"bun-linux-x64-baseline",
"bun-linux-aarch64-musl",
"bun-linux-x64-musl-baseline",
"bun-windows-x64-baseline",
"bun-windows-aarch64",
"bun-linux-aarch64-android",
"bun-linux-x64-android-baseline",
} {
got := bunChecksum(BunVersion, target)
if len(got) != 64 {
t.Fatalf("%s checksum = %q", target, got)
}
if _, err := hex.DecodeString(got); err != nil {
t.Fatalf("%s checksum = %q: %v", target, got, err)
}
}
if got := bunChecksum("9.9.9", "bun-linux-x64-baseline"); got != "" {
t.Fatalf("a newer Bun used the built-in checksum: %q", got)
}
}
// The tests' oldest Bun (testenv.Bun skips an older one on PATH) is the
// oldest magpie runs plugins on. testenv can't import this package, whose
// own tests use it, so it keeps a copy (#1600).
func TestMinBunIsBunVersion(t *testing.T) {
if testenv.MinBun != BunVersion {
t.Fatalf("testenv.MinBun is %s, BunVersion %s: change both", testenv.MinBun, BunVersion)
}
}
func TestBunTarget(t *testing.T) {
want := map[string]string{
"darwin/arm64": "bun-darwin-aarch64",
"darwin/amd64": "bun-darwin-x64",
"linux/arm64": "bun-linux-aarch64",
"linux/amd64": "bun-linux-x64-baseline",
"windows/amd64": "bun-windows-x64-baseline",
"windows/arm64": "bun-windows-aarch64",
"android/arm64": "bun-linux-aarch64-android",
"android/amd64": "bun-linux-x64-android-baseline",
}[runtime.GOOS+"/"+runtime.GOARCH]
got, err := bunTarget()
if want == "" {
if err == nil {
t.Fatalf("unsupported platform selected %q", got)
}
return
}
if err != nil || got != want {
t.Fatalf("bunTarget = %q, %v; want %q", got, err, want)
}
}
// Windows on ARM downloads Bun's native bun-windows-aarch64 build, with
// the checksum magpie carries for it, and runs plugins on it: it was
// turned away as having no build (#2183, EdwinDigital).
func TestWindowsOnArmGetsNativeBun(t *testing.T) {
bunHome(t, BunVersion, time.Now())
stubPlatform(t, "windows", "arm64", false)
if got, err := bunTarget(); err != nil || got != "bun-windows-aarch64" {
t.Fatalf("bunTarget = %q, %v; want bun-windows-aarch64", got, err)
}
if got := bunChecksum(BunVersion, "bun-windows-aarch64"); got != "a7a16b876a305fd1029c66dbd27007b4f6112ae896532f675878731a21e50cfd" {
t.Fatalf("bun-windows-aarch64 checksum = %q, not Bun %s's SHASUMS256.txt", got, BunVersion)
}
asked := bunReleases(t, []byte("windows arm64 build"))
exe, err := Bun(context.Background())
if err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(exe); string(b) != "windows arm64 build" || asked.Load() != 1 {
t.Fatalf("Bun = %s holding %q after %d downloads", exe, b, asked.Load())
}
}
// A Bun out two days is taken: downloaded, its checksum checked, tried,
// and run from then on; the one before it is kept to fall back on.
func TestCheckBunTakesASettledRelease(t *testing.T) {
bunHome(t, "9.9.9", time.Now().Add(-49*time.Hour))
asked := bunReleases(t, []byte("new bun"))
var tried []string
stubTry(t, func(exe, v string) error { tried = append(tried, v); return nil })
ctx := context.Background()
old, err := Bun(ctx) // the first download, BunVersion
if err != nil || BunInUse() != BunVersion {
t.Fatalf("first Bun %s, %v, in use %s", old, err, BunInUse())
}
got, err := CheckBun(ctx)
if err != nil || got != "9.9.9" {
t.Fatalf("CheckBun = %q, %v", got, err)
}
if BunInUse() != "9.9.9" || len(tried) != 1 || asked.Load() != 2 {
t.Fatalf("in use %s, tried %v, %d downloads", BunInUse(), tried, asked.Load())
}
exe, err := Bun(ctx)
if err != nil || filepath.Base(filepath.Dir(exe)) != "9.9.9" {
t.Fatalf("Bun = %s, %v", exe, err)
}
if b, _ := os.ReadFile(exe); string(b) != "new bun" {
t.Fatalf("the new bun is %q", b)
}
if _, err := os.Stat(old); err != nil {
t.Fatalf("the Bun before wasn't kept: %v", err)
}
if s := readBunState(); s.Previous != BunVersion || s.Checked.IsZero() {
t.Fatalf("state %+v", s)
}
// nothing newer: nothing downloaded
if got, err := CheckBun(ctx); got != "" || err != nil || asked.Load() != 2 {
t.Fatalf("again: %q, %v, %d downloads", got, err, asked.Load())
}
}
// A release out less than two days, or older than the floor, isn't taken.
func TestCheckBunWaitsAndKeepsTheFloor(t *testing.T) {
for _, c := range []struct {
v string
ago time.Duration
}{{"9.9.9", 47 * time.Hour}, {"0.0.1", 300 * time.Hour}} {
bunHome(t, c.v, time.Now().Add(-c.ago))
asked := bunReleases(t, []byte("bun"))
stubTry(t, func(string, string) error { return nil })
if got, err := CheckBun(context.Background()); got != "" || err != nil || asked.Load() != 0 || BunInUse() != BunVersion {
t.Fatalf("%s out %s: took %q, %v, %d downloads", c.v, c.ago, got, err, asked.Load())
}
}
}
// A Bun that fails its try is set aside: removed, never downloaded again,
// the one in use kept.
func TestCheckBunSetsAsideOneThatFailsItsTry(t *testing.T) {
bunHome(t, "9.9.9", time.Now().Add(-72*time.Hour))
asked := bunReleases(t, []byte("broken bun"))
stubTry(t, func(string, string) error { return errors.New("it says it is \"\"") })
if _, err := CheckBun(context.Background()); err == nil {
t.Fatal("a Bun that failed its try was taken")
}
if BunInUse() != BunVersion || asked.Load() != 1 {
t.Fatalf("in use %s, %d downloads", BunInUse(), asked.Load())
}
if _, err := os.Stat(bunDirOf("9.9.9")); !os.IsNotExist(err) {
t.Fatalf("the failed Bun was left: %v", err)
}
if got, err := CheckBun(context.Background()); got != "" || err != nil || asked.Load() != 1 {
t.Fatalf("tried again: %q, %v, %d downloads", got, err, asked.Load())
}
}
// The real try: the Bun on PATH passes it, as its own version.
func TestTryBunOnRealBun(t *testing.T) {
bun := testenv.Bun(t)
v := bunReported(bun)
if err := tryBun(context.Background(), bun, v); err != nil {
t.Fatalf("bun %s: %v", v, err)
}
if err := tryBun(context.Background(), bun, "0.0.1"); err == nil {
t.Fatal("a Bun saying another version passed")
}
}
// A Bun the host dies on is left for the one before it, and set aside:
// the plugins load on the old one.
func TestHostFallsBackWhenTheNewBunDies(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the dying bun is a shell script")
}
bun := testenv.Bun(t)
bunHome(t, "9.9.9", time.Now().Add(-72*time.Hour))
t.Cleanup(Settle)
if err := os.MkdirAll(bunDirOf(BunVersion), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(bun, bunExeOf(BunVersion)); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(bunDirOf("9.9.9"), 0o755); err != nil {
t.Fatal(err)
}
testenv.Program(t, bunExeOf("9.9.9"), "#!/bin/sh\nexit 3\n")
if err := writeBunState(bunState{Current: "9.9.9", Previous: BunVersion}); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
ps, err := Providers(ctx)
if err != nil || len(ps) != 1 {
t.Fatalf("Providers = %+v, %v", ps, err)
}
if s := readBunState(); BunInUse() != BunVersion || len(s.Bad) != 1 || s.Bad[0] != "9.9.9" {
t.Fatalf("in use %s, state %+v", BunInUse(), s)
}
}
func TestDownloadBunDefaultSkipsOfficialSums(t *testing.T) {
target, err := bunTarget()
if err != nil {
t.Skip(err)
}
var zb bytes.Buffer
zw := zip.NewWriter(&zb)
h := &zip.FileHeader{Name: target + "/" + bunExe(), Method: zip.Deflate}
h.SetMode(0o755)
w, _ := zw.CreateHeader(h)
w.Write([]byte("bun"))
zw.Close()
sum := sha256.Sum256(zb.Bytes())
var sumsAsked atomic.Int32
var zipAsked atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasSuffix(r.URL.Path, "/SHASUMS256.txt"):
sumsAsked.Add(1)
http.Error(w, "must not ask", http.StatusInternalServerError)
case strings.HasSuffix(r.URL.Path, "/"+target+".zip"):
zipAsked.Add(1)
w.Write(zb.Bytes())
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
oldRelease := bunRelease
bunRelease = srv.URL
defer func() { bunRelease = oldRelease }()
oldSum, hadSum := bunSums[target+".zip"]
bunSums[target+".zip"] = hex.EncodeToString(sum[:])
defer func() {
if hadSum {
bunSums[target+".zip"] = oldSum
} else {
delete(bunSums, target+".zip")
}
}()
exe := filepath.Join(t.TempDir(), bunExe())
if err := downloadBun(context.Background(), BunVersion, exe); err != nil {
t.Fatal(err)
}
if sumsAsked.Load() != 0 {
t.Fatalf("SHASUMS256.txt asked %d times", sumsAsked.Load())
}
if zipAsked.Load() != 1 {
t.Fatalf("zip asked %d times", zipAsked.Load())
}
}
// Bun's download has no bound on the whole (#5572); a server that goes
// silent part way is ended by the stall, said as one.
func TestBunDownloadThatGoesSilentIsAStall(t *testing.T) {
old := downloadStall
downloadStall = 100 * time.Millisecond
t.Cleanup(func() { downloadStall = old })
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1000")
w.Write([]byte("PK"))
w.(http.Flusher).Flush()
<-r.Context().Done()
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
_, err := getURL(ctx, srv.URL+"/bun.zip", 1<<20)
if !errors.Is(err, stall.ErrStalled) || ctx.Err() != nil {
t.Fatalf("err = %v, want a stall", err)
}
}