Files
magpie/internal/provider/plugin_icon_test.go
T
yetone 49caff3caf provider tests: a no-wait return is asserted by holding the slow thing, not a wall-clock bound (#1617)
The siblings of #1586 that 7b151cf54 left alone. Each said "comes back
without waiting" by measuring time.Since(start) against a bound (50ms,
500ms, 1s, 2s, 3s) that a loaded runner can pass and so make red without
the guarantee breaking. Each now holds the slow thing until it has its
answer, so a call that comes back at all came back without waiting for it.
A deadline is kept only so a call that did wait fails as a FAIL rather
than a hang, and each is short of the slow thing's own timeout, so a call
that waited cannot come back in time by having it time out:

- cli_identity_test TestCLIIdentityFirstAskBounded: both looks run while
  the ask is held (it was already). The second look is run with firstAsk
  set to an hour, so a second look that waited again (not only waited the
  whole ask) is caught; 10s guard.
- claude_status_test TestClaudeIdentityServedWhileAsked: the stand-in CLI
  now waits for a file instead of `sleep 1`, and writes another when
  reached. The stale look must come back while it is held, with the ask
  still in flight (claudeStatusBusy), and the CLI must have been asked
  again. The guard is 4s, short of askClaudeStatus' own 5s kill. Its
  cleanup lets the CLI go and waits for the ask to end.
- partners_test TestPartnersDoesNotWait: the five Partners() calls run
  while the site is held; 10s guard (the fetch's own is 20s).
- partners_test TestPartnersNow and plugin_icon_test
  TestRepoIconsWaitForFirstFetch had a floor and a ceiling. The floor is
  the guarantee that the call waited for its bound, and a busy machine only
  lengthens it, so it stays (100ms, and 250ms for a 300ms wait as before).
  The ceiling was the "doesn't wait past its bound" half: it is now that
  the call comes back while the site/fetch is held; 10s guard (the fetch's
  own are 20s and 30s).
- sub2api_usage_test TestKeyAllowanceNeverWaits: the ten looks run while
  sub2api is held and report back over a channel, not t.Errorf from a
  goroutine that could outlive a failed test; 10s guard (the read's own is
  30s).

What was run (temp HOME, caches pinned):
  go test -tags nogui -race -count=10 -run '<the six tests>' ./internal/provider  ok
  go test -tags nogui -count=1 ./internal/provider                                ok 395.6s
  gofmt -l; go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider  clean

Broken by hand, each red at the new assertion:
  cli_identity.go, drop c.waited = true (every look waits):
    :124 a second look waited
  cli_identity.go, drop the firstAsk case (the first look waits the ask):
    :116 the first look waited for the held ask
  account.go, askClaudeStatus() in the stale case under the lock:
    claude_status_test.go:111 a stale look waited for the held CLI
  partners.go, Partners runs refreshPartners in line:
    :246 Partners waited for the held site
  partners.go, PartnersNow without its time.After(wait):
    :316 PartnersNow waited past its bound for the held site
  key_allowance.go, KeyAllowance reads in line (lock let go around it):
    sub2api_usage_test.go:279 a look waited for the held read
  plugin_icon.go, RepoIcons without its timer case:
    :168 RepoIcons waited past its 300ms for the held fetch
2026-10-11 22:02:50 +08:00

171 lines
5.6 KiB
Go

package provider
import (
"context"
"encoding/base64"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// Lemon on Discord: can a plugin give its provider its own icon? The auth
// hook's icon (or package.json's magpie.icon) is a data:image URI, kept
// at once as a picture given by hand is, or an https URL, fetched in the
// background through FetchIcon's checks; it comes before the market's,
// and anything else is refused.
func TestPluginOwnIcon(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("MAGPIE_PLUGIN_MARKET", "off")
png := []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
var fetched atomic.Int32
was := fetchPluginIcon
t.Cleanup(func() { fetchPluginIcon = was })
fetchPluginIcon = func(_ context.Context, u string) (string, error) {
fetched.Add(1)
if u != "https://lemon.example/icon.png" {
t.Errorf("fetched %q", u)
}
return StoreIcon(png)
}
// a data URI: kept at once, over the market's icon for its provider
data := "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
ic := PluginIcon(plugin.Provider{ID: "google", Spec: "lemon-plugin", Icon: data})
if !strings.HasPrefix(ic, "file:") {
t.Fatalf("data URI icon = %q", ic)
}
if f := IconFile(strings.TrimPrefix(ic, "file:")); f == "" {
t.Fatalf("%q isn't a stored picture", ic)
}
// none given: the market's, as before
if ic := PluginIcon(plugin.Provider{ID: "google", Spec: "lemon-plugin"}); ic != "gemini-color" {
t.Errorf("no icon given = %q", ic)
}
// an https URL: the market's (none here) until it is fetched, then its
pp := plugin.Provider{ID: "lemon", Spec: "lemon-plugin", Icon: "https://lemon.example/icon.png"}
if ic := PluginIcon(pp); ic != "" {
t.Errorf("before the fetch = %q", ic)
}
var got string
for i := 0; i < 100 && !strings.HasPrefix(got, "file:"); i++ {
time.Sleep(20 * time.Millisecond)
got = PluginIcon(pp)
}
if !strings.HasPrefix(got, "file:") || fetched.Load() != 1 {
t.Fatalf("after the fetch = %q, fetched %d times", got, fetched.Load())
}
// refused: not https, not a picture, a private host, a picture over 1 MB
big := "data:image/png;base64," + base64.StdEncoding.EncodeToString(append(png, make([]byte, MaxIcon)...))
for _, bad := range []string{"http://lemon.example/icon.png", "javascript:alert(1)", "data:text/html,<b>x</b>", "https://127.0.0.1/icon.png", "https://localhost/icon.png", "file:///etc/passwd", big} {
if ic := PluginIcon(plugin.Provider{ID: "lemon", Spec: "lemon-plugin", Icon: bad}); ic != "" {
t.Errorf("%.40q taken as %q", bad, ic)
}
}
time.Sleep(50 * time.Millisecond)
if fetched.Load() != 1 {
t.Errorf("fetched %d times", fetched.Load())
}
// kept across a restart, and not pruned as a picture no provider has
plugIcons.Lock()
plugIcons.from = ""
plugIcons.Unlock()
old := time.Now().Add(-2 * time.Hour)
for _, name := range keptPluginIcons() {
os.Chtimes(IconFile(name), old, old)
}
pruneIcons(file{})
if ic := PluginIcon(pp); ic != got {
t.Errorf("after a restart and a prune = %q, want %q", ic, got)
}
if fetched.Load() != 1 {
t.Errorf("fetched again: %d", fetched.Load())
}
}
// yetone: 非官方插件显示不出来 logo. A repository's https picture not fetched
// before is waited for, so the GitHub list's first answer has it; one slower
// than the wait shows the next time, and the wait ends when it does.
func TestRepoIconsWaitForFirstFetch(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
png := []byte("\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
slow := make(chan struct{})
// the fetch left waiting ends with the test
t.Cleanup(func() {
close(slow)
for i := 0; i < 200; i++ {
plugIcons.Lock()
n := len(plugIcons.busy)
plugIcons.Unlock()
if n == 0 {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Error("the slow fetch didn't end")
})
was := fetchPluginIcon
t.Cleanup(func() { fetchPluginIcon = was })
fetchPluginIcon = func(ctx context.Context, u string) (string, error) {
if strings.Contains(u, "slow") {
select {
case <-slow:
case <-ctx.Done():
}
return "", ctx.Err()
}
time.Sleep(100 * time.Millisecond)
return StoreIcon(png)
}
data := "data:image/png;base64," + base64.StdEncoding.EncodeToString(png)
said := []string{"https://gaccode.example/favicon.png", "", data}
got := RepoIcons(said, 5*time.Second)
if !strings.HasPrefix(got[0], "file:") || got[1] != "" || !strings.HasPrefix(got[2], "file:") {
t.Fatalf("first answer = %q", got)
}
// The slow fetch is held until the test ends, so RepoIcons coming back at
// all is its wait ending it (#1617); the floor is that it did wait, which
// a busy machine only lengthens. The deadline only turns a wait past the
// 300ms into a FAIL, and is short of the fetch's own 30s.
type answer struct {
icon string
took time.Duration
}
answered := make(chan answer, 1)
go func() {
start := time.Now()
got := RepoIcons([]string{"https://slow.example/icon.png"}, 300*time.Millisecond)
answered <- answer{got[0], time.Since(start)}
}()
select {
case a := <-answered:
if a.icon != "" {
t.Fatalf("a fetch never finished = %q", a.icon)
}
if a.took < 250*time.Millisecond {
t.Fatalf("waited %v for a 300ms wait", a.took)
}
case <-time.After(10 * time.Second):
t.Fatal("RepoIcons waited past its 300ms for the held fetch")
}
}