Files
magpie/internal/provider/fetch_new_soon_test.go
T
yetone b7ceb53fe4 provider: WorkBuddy's built-in is retired; its plugin serves it
workbuddy and workbuddy-ai join retiredBuiltins, as Zed did in c0605abc2
and Qoder in 43cc3171d. @magpie-community/opencode-workbuddy-auth serves
both, moved or not.

Removed (built-in only):
- provider: workbuddy_models.go (the model list); in workbuddy.go the
  sign-in, token renewal, usage/credits reading, request signing and the
  site's model fetch; the WorkBuddy cases in account.go (Accounts),
  logins_on.go, logins.go (list/switch/forget), login_usage.go,
  subscription_usage.go and signin.go; WorkBuddyBaseForTest, now unused.
- The built-in tests: workbuddy_test, workbuddy_signin_test,
  workbuddy_accounts_test, workbuddy_ai_test, workbuddy_body_test,
  workbuddy_enterprise_test, workbuddy_free_test, workbuddy_names_test,
  workbuddy_spare_free_test, workbuddy_refused_test (provider), and
  gateway/workbuddy_refused_test.

Kept, and why:
- migrate_workbuddy.go (the mover) with what it reads: wbSite, wbCreds,
  wbOwn/wbAuthPath (the desktop's file, sent as a marker), wbLogins,
  wbTokens (back drops a renewed pair).
- workbuddy_checkin.go: the daily check-in now walks the plugin's
  accounts only and sends through the plugin's fetch (a.via); wbCallVia,
  wbNum and the site's api() serve it, and plugin_usage's host.
- WBRestricted/WBRestrictedHint: the gateway reads the plugin's 11140 by
  them. textOnly: applied to the plugin's lists.
- draw_workbuddy and search_workbuddy in the gateway: they serve the
  plugin's provider. The PLUGIN-SERVED notice in workbuddy.go.
- The WorkBuddy agent target (internal/agent) is untouched.

Tests changed in meaning:
- TestFetchNewAccount, TestFetchNewRetriedSoon and
  TestFetchingNewOnceStarted used WorkBuddy as the built-in signed in;
  they use Codex now (codexSignedIn, codexListedAt), same assertions.
- TestFetchPlanWithoutList uses local models; TestWorkBuddyFetchModels,
  TestWorkBuddyAIGLMIsTextOnly and TestOwnStoreSignInAgainDropsTheReading
  (WorkBuddy case) are gone with the code.
- Check-in: TestWorkBuddyCheckinSignedIn is gone; the fixture's account
  is a plugin account signing through via; PluginUnderItsOwnID became
  TestWorkBuddyCheckinPluginAccounts (plugin accounts listed, a built-in
  login left out). TestCheckinAccountsListed reads a moved plugin.
- TestWorkBuddyMover seeds the desktop's logins.json row: retired, the
  built-in no longer writes it, and an existing user's row is what moves
  (red without the row).
- Gateway: the WorkBuddy draw and search tests run through the fake
  plugin (besideFake), without the built-in's header checks;
  TestWorkBuddySearchesForItsModels folded into
  TestWorkBuddyPluginSearches (refusal and search-off checks kept);
  TestLoopEndsWorkBuddysReply is the plugin route only;
  TestWorkBuddyRestrictedRestsHours lost its built-in translation case.

Run:
- gofmt -l internal: empty
- go vet -tags nogui ./...; GOOS=linux and GOOS=windows the same: clean
- go test -tags nogui -count=1 -timeout 45m -run 'WorkBuddy|workbuddy|Checkin|CheckIn|FetchNew|FetchingNew|FetchPlan|Migrat|migrat|Mover|Moved|Retired|Retire|SignIn|Plugin|Draw|Search|Loop|Redact|Restricted|Images|TextOnly|Qoder|BuiltinsSay|AsksOwn|Loopback' ./internal/provider ./internal/gateway ./internal/cli ./internal/gui ./internal/qoder
  first run: gateway, cli, gui ok; provider red on TestWorkBuddyMover
  (above); after the fix provider ok (99s).
- Not run: whole packages, the full suite, the GUI node suite.
2026-10-17 23:11:00 +08:00

120 lines
3.0 KiB
Go

package provider
import (
"net/http"
"net/http/httptest"
"path/filepath"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// An account whose list couldn't be had is asked again a minute on, and
// opening the panel asks without the page waiting (#422: after an update
// Kiro offered Auto alone until magpie was restarted by hand — its one try
// had failed, and it wasn't asked again for ten minutes, and then only by
// the Providers page).
func TestFetchNewRetriedSoon(t *testing.T) {
isolate(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"))
newFetches.Lock()
newFetches.m = map[string]time.Time{}
newFetches.Unlock()
newSoonAt.Store(0)
t.Cleanup(func() {
waitNewSoon()
newFetches.Lock()
newFetches.m = map[string]time.Time{}
newFetches.Unlock()
newSoonAt.Store(0)
forgetAccountCaches()
})
var asked atomic.Int32
var down atomic.Bool
down.Store(true)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/backend-api/codex/models" {
w.WriteHeader(404)
return
}
asked.Add(1)
if down.Load() {
w.WriteHeader(502)
return
}
w.Write(codexListOf("gpt-6-luna", "gpt-6-sol"))
}))
defer srv.Close()
codexListedAt(t, srv.URL)
codexSignedIn(t, home)
listed := func() bool {
p, err := Find("codex")
if err != nil {
t.Fatal(err)
}
for _, m := range p.Available() {
if m.ID == "gpt-6-luna" {
return true
}
}
return false
}
// the try at start-up fails
FetchNew(5 * time.Second)
if n := asked.Load(); n != 1 {
t.Fatalf("asked %d times at start-up", n)
}
down.Store(false)
FetchNew(5 * time.Second)
if n := asked.Load(); n != 1 {
t.Fatalf("a failed list asked again at once: %d", n)
}
// opened again straight away: nothing more is started
FetchNewSoon(5 * time.Second)
newSoonAt.Store(0)
waitNewSoon()
if n := asked.Load(); n != 1 {
t.Fatalf("a failed list asked again at once by the panel: %d", n)
}
// a minute and a bit later the panel is opened: asked again, without
// the page waiting for it
newFetches.Lock()
newFetches.m["codex"] = time.Now().Add(-61 * time.Second)
newFetches.Unlock()
FetchNewSoon(5 * time.Second)
waitNewSoon()
if !listed() || asked.Load() != 2 {
t.Fatalf("not asked again a minute on: asked %d times, listed %v", asked.Load(), listed())
}
// and the panel opened again within newSoonEvery starts nothing
if err := catalog.SaveLive("codex", "", nil); err != nil {
t.Fatal(err)
}
newFetches.Lock()
newFetches.m = map[string]time.Time{}
newFetches.Unlock()
FetchNewSoon(5 * time.Second)
waitNewSoon()
if n := asked.Load(); n != 2 {
t.Fatalf("the panel asked again within %v: %d", newSoonEvery, n)
}
}
// waitNewSoon waits for a FetchNewSoon under way to end.
func waitNewSoon() {
for deadline := time.Now().Add(5 * time.Second); fetchingNew.Load() && time.Now().Before(deadline); {
time.Sleep(10 * time.Millisecond)
}
}