Files
magpie/internal/plugin/models_refused_test.go
yetone 16dcfa504a plugin: a models hook that refused a sign-in isn't asked again for it at once (JZGiVvkSOA75376 on X)
Two dead Zed sign-ins were listed ~30 times a minute, each listing
asking Zed for a model token and getting 401 again. A models hook that
throws with signIn "expired" is now held for that sign-in (its access,
refresh or key): the listing falls back as it did, with the same error,
and the hook isn't asked until the same backoff as a failed renewal
(30s doubling to 30 minutes) has passed. Signing in again asks at once.
This is in the host, so it holds for every plugin, not only Zed.

Run: go test -run 'TestRefusedListIsNotAskedAgain|TestFetchBuildsInfoOnce|
TestPluginRenewsSignIns|TestRenewRetryBacksOff' ./internal/plugin on
mote: ok.
Broken by hand: skipping the hold makes TestRefusedListIsNotAskedAgain
red (3 listings asked 3 more times).
2026-11-10 09:38:45 +08:00

60 lines
1.7 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A models hook that said its vendor refused the sign-in (signIn
// "expired") isn't asked again for that sign-in at once: two dead Zed
// sign-ins were listed, and refused by Zed, ~30 times a minute
// (JZGiVvkSOA75376 on X). A new sign-in is asked at once.
func TestRefusedListIsNotAskedAgain(t *testing.T) {
sandbox(t)
log := filepath.Join(t.TempDir(), "models.log")
t.Setenv("FAKE_MODELS_LOG", log)
asked := func() int {
b, _ := os.ReadFile(log)
return strings.Count(string(b), "asked r-models-gone")
}
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)
}
k, err := Import(ctx, "fakeco", map[string]any{"type": "oauth", "refresh": "r-models-gone", "access": "a", "expires": 9e15, "accountId": "gone@fake"})
if err != nil {
t.Fatal(err)
}
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
first := asked()
if first == 0 {
t.Fatal("the models hook wasn't asked for the account")
}
for range 3 {
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
}
if got := asked(); got != first {
t.Fatalf("3 more listings asked the hook of a refused sign-in %d more times, want 0", got-first)
}
// signed in again: asked at once
if err := Restore(ctx, "fakeco", k, map[string]any{"type": "oauth", "refresh": "r-models-gone", "access": "a2", "expires": 9e15, "accountId": "gone@fake"}); err != nil {
t.Fatal(err)
}
if _, err := Providers(ctx); err != nil {
t.Fatal(err)
}
if got := asked(); got <= first {
t.Fatal("a new sign-in's models weren't asked")
}
}