Files
magpie/internal/plugin/dist_unread_test.go
T
yetone e0eb74987c plugins: a dist branch not read is unknown, not "not on npm", and its read isn't cut off by one ask
ferock on Discord (v0.1.2399): the Plugins page showed the Chat page
plugin (@magpie-community/page-chat, on the community repo's dist
branch only, never on npm) as Coming soon.

Cause: Info asks each package under npmEach (6s). npm said 404, and
npmAsk turned to distInfo -> distPkg, which read the ~1.6MB index.json
(GitHub and the jsDelivr mirror) under that same 6s context, holding
distMu. On a slow link the read was cut off, distPkg returned nil as if
the branch had no such package, and npmInfo kept "not on npm" (ok=true,
empty NPM) for an hour, saved to plugin-npm.json. distWait then held a
new read back for a minute.

Fix:
- distPkg returns (*distPackage, error): nil, nil only when the branch
  was read and lacks the package (or it is out of scope); an error
  wrapping errDistUnread when the branch couldn't be read (last read
  failed or not finished) and the kept index doesn't have it.
- The index is read in its own goroutine (readDist), apart from the
  asker's context, with its own distReadFor (2m). One read at a time
  (distReading); askers wait for it or their own ctx, distMu is not held
  across the network read. A read that ends after its asker stopped
  waiting is kept for the next ask.
- npmAsk: npm's errNotFound with the dist branch unknown becomes the
  dist error, so every caller that keeps "missing" (Info/npmInfo,
  newestOf in store.go, the update checks in updates.go) treats it as
  unknown and asks again. Readme says the branch couldn't be read instead
  of npm's not found. installDist returns the unread error rather than
  "has no".
- mendDist was checked: it reads only local files, no dist read.

Old data: a "not on npm" already kept in plugin-npm.json by an older
build is asked again once it is an hour old, as before.

Tests: TestDistReadSlowerThanAnAskIsntMissing (index.json held past
npmEach: not kept as missing, and the next Info after the read ends has
0.1.7) and TestDistReadFailedIsntMissing (index.json 503: unknown, asked
again; a read branch without the package is still errNotFound).
fakeDist's reset waits for a read under way (distSettled).

Run (mote-run; vet at base 2cfb8351d, tests again at this commit rebased
on origin/main 484b45d82, which changed no Go in plugin or gui; pushed on 7dc8a18d0, which
touches only internal/provider and internal/gateway):
- gofmt -l: nothing
- go vet ./internal/plugin/..., GOOS=windows go vet ./...,
  GOOS=linux go vet -tags nogui ./...: ok
- go test -count=1 -timeout 45m ./internal/plugin/... ./internal/gui/...:
  internal/plugin ok (103.6s). internal/gui FAIL, two tests only, both
  red the same on a fresh origin/main 484b45d82 run alone and filed:
  TestSettingsSaveKeepsWhatItDoesNotSend (SearchAll, #4514) and
  TestMainWindowCorners/macOS_26_SDK on mote's macOS 27 (#3949).

Broken by hand (mote-run, -run DistRead):
- npmAsk's errNotFound -> dist error override removed: both new tests
  red (the market kept an empty entry).
- readDist given the asker's ctx (the old cut-off): the slow test red
  (after the dist answered, still no version).

Not tried: a real slow link or ferock's machine; the real index.json
from the dist branch; the GUI page itself (actionFor already shows a
spinner for a package with no npm entry yet).
2026-10-31 22:22:12 +08:00

123 lines
4.0 KiB
Go

package plugin
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
)
// gatedDist serves the dist branch with pkg's versions, its index.json
// answered as fail says: 503 while it is true, and only once open is
// closed. Its address is distBase for the test.
func gatedDist(t *testing.T, pkg string, versions []string, fail *atomic.Bool) (open func()) {
t.Helper()
fakeDist(t, nil, false)
inner := distServer(t, map[string][]string{pkg: versions}, false)
gate := make(chan struct{})
var once sync.Once
open = func() { once.Do(func() { close(gate) }) }
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/index.json" {
select {
case <-gate:
case <-r.Context().Done():
return
}
if fail != nil && fail.Load() {
http.Error(w, "unavailable", http.StatusServiceUnavailable)
return
}
}
res, err := http.Get(inner + r.URL.Path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer res.Body.Close()
w.WriteHeader(res.StatusCode)
io.Copy(w, res.Body)
}))
t.Cleanup(srv.Close)
t.Cleanup(func() { open(); distSettled(t) })
was := distMirror
distBase = srv.URL
distMirror = func(string) string { return "" }
t.Cleanup(func() { distMirror = was })
return open
}
// A package only the dist branch has (the Chat page, @magpie-community/
// page-chat, never on npm) whose index.json comes slower than one
// package's ask (npmEach) isn't "not on npm" for the next hour: it is
// unknown, kept as nothing, and once the read that went on ends, the
// next ask has it (ferock on Discord, v0.1.2399: Coming soon).
func TestDistReadSlowerThanAnAskIsntMissing(t *testing.T) {
sandbox(t)
const pkg = "@magpie-community/page-chat"
npmHasNothing(t)
open := gatedDist(t, pkg, []string{"0.1.6", "0.1.7"}, nil)
npmEach, npmAll = 200*time.Millisecond, time.Second
if info, ok := Info(context.Background(), []string{pkg})[pkg]; ok {
t.Fatalf("with the dist branch not read yet the market says %+v; want it unknown", info)
}
npmMu.Lock()
e, kept := npmCached()[pkg]
npmMu.Unlock()
if kept {
t.Fatalf("kept %+v as what npm said, with the dist branch unread", e)
}
c, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
_, err := npmAsk(c, pkg)
cancel()
if errors.Is(err, errNotFound) || !errors.Is(err, errDistUnread) {
t.Fatalf("npmAsk with the dist branch unread: %v; want errDistUnread, not errNotFound", err)
}
// the read wasn't cut off with the ask: it ends once the branch
// answers, and the next ask has its 0.1.7
open()
distSettled(t)
if info := Info(context.Background(), []string{pkg})[pkg]; info.Version != "0.1.7" {
t.Fatalf("once the dist branch answered the market says %+v; want its 0.1.7", info)
}
}
// A dist branch that answers with an error is unknown too, never "not on
// npm", and is asked again once distWait has passed.
func TestDistReadFailedIsntMissing(t *testing.T) {
sandbox(t)
const pkg = "@magpie-community/page-chat"
npmHasNothing(t)
var fail atomic.Bool
fail.Store(true)
open := gatedDist(t, pkg, []string{"0.1.7"}, &fail)
open()
wait := distWait
distWait = 0
t.Cleanup(func() { distWait = wait })
npmEach, npmAll = 5*time.Second, 10*time.Second
if info, ok := Info(context.Background(), []string{pkg})[pkg]; ok {
t.Fatalf("with the dist branch failing the market says %+v; want it unknown", info)
}
if _, err := npmAsk(context.Background(), pkg); errors.Is(err, errNotFound) {
t.Fatalf("npmAsk with the dist branch failing: %v, said missing", err)
}
fail.Store(false)
if info := Info(context.Background(), []string{pkg})[pkg]; info.Version != "0.1.7" {
t.Fatalf("once the dist branch answered the market says %+v; want its 0.1.7", info)
}
// and a branch read that has no such package is missing, as before
const gone = "@magpie-community/opencode-nothing-auth"
if _, err := npmAsk(context.Background(), gone); !errors.Is(err, errNotFound) {
t.Fatalf("npmAsk of a package neither has: %v; want errNotFound", err)
}
}