Files
magpie/internal/plugin/host_proxy_boot_test.go
T
yetone e9221e96dd provider, netproxy, gui: allowances can be read only when asked, and a system proxy set after start is taken within seconds (#1518, RooobinYe)
The reporter starts magpie at login with the proxy on "follow system
proxy". magpie comes up before the proxy app. With no system proxy set
yet, netproxy.For gives none, so magpie's own allowance reads went out
direct, from the user's real IP. That reading was then kept for up to
15s. A system proxy that is set but not listening fails the request
("proxyconnect ... connection refused"); it never falls back to direct.

Proxy freshness:
- netproxy.System re-reads a "none" reading after 2s (noneKeep) instead
  of 15s (systemKeep, unchanged for a set proxy).
- The plugin host looks again every 2s while it has no proxy
  (proxyLookNone), so a host started before the proxy app takes it soon.
- Catalog sync, the update check, plugin updates and token refresh all go
  through netproxy.Func / Dispatch, so they use the current proxy too.

Settings -> Allowances -> "Allowance reads": Automatic (default, as
before) or When I ask (settings.QuotaReads = "asked",
POST /api/settings/quota-reads):
- These count as asking (provider.Asked): the Usage page opened or
  refreshed (?asked=1), a card's refresh (RefreshUsage), magpie quota,
  magpie accounts, magpie quota wait and the TUI's quotas.
- Every other read (quotaReading -> holdUnasked) carries netproxy.Hold.
  netproxy.Func, Dispatch's choice proxies and the plugin host's calls
  refuse a held request before it leaves.
- The readers return the last reading, dated (AsOf), or "not read yet".
  That covers routing's Allowances / AwaitAllowance, the used-up switch,
  the menu bar, alerts, Quotas/KeyBalances/PlanQuotas, Codex/Claude/
  Cursor/Grok/Kiro usage and KeyAllowance.
- An account refused for its quota (StaleAllowance) or a key refused
  (StaleKeyAllowance) is still read, once.
- Warm-up and check-in are unchanged.
- Strings are added in zh, zh-TW, ja and de. The docs are updated:
  providers-accounts (Allowance row), provider-plugins (host proxy).

Run:
- go test -tags nogui ./... (sandbox HOME), gofmt -l, and go vet -tags
  nogui on GOOS=linux/windows/darwin for ., netproxy, plugin, settings,
  provider, gui and tui.
- New tests, each seen red by breaking the change by hand:
  - TestSystemProxySetAfterStartIsTakenSoon and TestHeldRequestNeverLeaves
    (netproxy).
  - TestHostWithNoProxyLooksAgainSoon (plugin; red with 15s).
  - TestAllowancesReadOnlyWhenAsked (red without the hold and without
    markStaleRead), TestAllowancesReadByThemselvesByDefault and
    TestKeyReadsOnlyWhenAsked (red without holdUnasked and without
    unheld for a refused key).
- GUI quota-reads.test.cjs in Chromium and WebKit, at 900 and 360px, in
  en/zh/zh-TW/ja/de.
- i18n-keys, gui-ja, gui-de, settings-sections, settings-groups,
  currency, number-units and tray-bird.
2026-10-11 00:20:25 +08:00

30 lines
1.0 KiB
Go

package plugin
import (
"testing"
"time"
)
// #1518: a host started at login before the proxy app had set the
// system's proxy asks again within seconds, not after the 15 a host with
// a proxy waits: until then every request a plugin made went direct.
func TestHostWithNoProxyLooksAgainSoon(t *testing.T) {
for _, k := range []string{"HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY", "https_proxy", "http_proxy", "all_proxy"} {
t.Setenv(k, "")
}
h := &host{proxy: hostProxy(env()), proxyAt: time.Now().Add(-3 * time.Second)}
if proxied(h.proxy) {
t.Skip("this machine's system proxy is set; the case needs none")
}
t.Setenv("HTTPS_PROXY", "http://127.0.0.1:7891")
if !h.proxyMoved() {
t.Fatal("the proxy was set 3s after a host with none started, and the host kept going direct")
}
// A host with a proxy keeps its 15 seconds.
h = &host{proxy: hostProxy(env()), proxyAt: time.Now().Add(-3 * time.Second)}
t.Setenv("HTTPS_PROXY", "http://127.0.0.1:7892")
if h.proxyMoved() {
t.Fatal("a host with a proxy looked again before its 15 seconds")
}
}