Files
magpie/internal/cli/providers_balance_method_cli_test.go
T
yetone dc8dcc85d1 provider: a custom Balance URL can be asked with POST, for an endpoint that answers only a POST (#2368, yaofeng)
A custom provider's Balance URL was always asked with GET, so a balance
endpoint that takes only a POST answered 405 and the card never showed.

- Provider.BalanceMethod ("" or GET, or POST with no body). It applies to
  the user's own Balance URL only; a vendor magpie knows is asked as
  before. balanceMethodOf refuses any other value before anything is sent.
- CLI: `magpie provider set <id> balance.method=POST` (GET clears it; any
  other value is refused and the saved one kept); `magpie provider <id>`
  shows "from POST <url>".
- GUI: a GET/POST segmented control (segs, not a <select>) beside the
  Balance URL in More endpoints, aria-labelled and titled in en, zh, zh-TW,
  ja and de. Check balance and Save carry it; a preset's save keeps it.
- No vendor preset or name is added. A string amount ("0.94306040") was
  already read by number(), so `$usd_balance` reads as any field does.
- docs/subsystems/providers-accounts.md says it.

Run:
- go test -run TestBalance ./internal/provider/: ok. New TestBalanceMethod:
  a POST-only httptest server, POST (any case) reads $0.94 from a string
  usd_balance, "" and GET are still GET (405 is an error), PUT, DELETE and
  a CRLF value are refused with nothing sent.
- go test -run TestProviderSetBalanceMethod ./internal/cli/: ok.
- Broken by hand: the request always GET turns TestBalanceMethod red
  ("asked GET, want POST"); balanceMethodOf passing any method turns it red
  (PUT and DELETE sent); the CLI taking any value turns
  TestProviderSetBalanceMethod red; the Check balance body without
  balanceMethod turns 20 of 21 balance-method.test.cjs cases red, and
  origin/main's app.js all of them.
- GUI: balance-method.test.cjs 42/42 (Chromium and WebKit, 1100 and 420,
  en, zh, zh-TW, ja, de, saved GET and POST: opens on the saved method,
  named and titled in the language, fits beside the URL with no page
  scroll, the other pick goes in the check and save bodies). Siblings:
  balance-parts, balance-field-default, balance-fix, catalog-hint,
  duplicate-pause, key-balance-card, local-address, provider-base-api,
  provider-auth-header, provider-gemini, provider-search,
  provider-url-each-api, gui-de, gui-ja, gui-zh-tw, i18n-keys: 250/250.
- Real use: the built binary in a sandbox HOME, a custom provider at a
  local server that answers GET 405 and POST {"usd_balance":"0.94306040"}.
  Default: "unavailable 405 Method Not Allowed"; balance.method=post:
  "$0.94 from POST …/api/check-balance $usd_balance", the server saw one
  POST with Content-Length 0 and the Bearer key; balance.method=PATCH
  refused. NanoGPT itself was not asked (no key here).
- gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows
  go vet -tags nogui ./..., GOOS=windows go vet ./...: ok.

Suite run here: HOME=<sandbox> go test -tags nogui -timeout 45m ./...: 62
ok, internal/gui FAIL only on TestSetAnswersWhileDevinAndProvidersHang
(a state took 11.97s, load ~180), open as #1375; it passes -count=2 alone.

Full suite: SUITE_LINE
2026-10-15 23:08:05 +08:00

38 lines
1.3 KiB
Go

package cli
import (
"strings"
"testing"
)
// magpie provider set balance.method= takes GET or POST, in any case, and
// keeps GET as the unset default; any other method is refused and leaves
// the saved one alone (#2368).
func TestProviderSetBalanceMethod(t *testing.T) {
groupsHome(t)
for _, c := range []struct{ v, want string }{
{"POST", "POST"}, {"get", ""}, {" post ", "POST"}, {"", ""},
} {
if _, err := printed(t, func() error {
return providerCmd([]string{"provider", "set", "a", "balance=https://relay.example.com/api/check-balance", "balance.method=" + c.v})
}); err != nil {
t.Fatalf("%q: %v", c.v, err)
}
if got := mustFind(t, "a").BalanceMethod; got != c.want {
t.Fatalf("balance.method=%q kept %q, want %q", c.v, got, c.want)
}
}
if _, err := printed(t, func() error { return providerCmd([]string{"provider", "set", "a", "balance.method=POST"}) }); err != nil {
t.Fatal(err)
}
for _, v := range []string{"PUT", "DELETE", "POST\r\nX: y"} {
err := providerCmd([]string{"provider", "set", "a", "balance.method=" + v})
if err == nil || !strings.Contains(err.Error(), "GET|POST") {
t.Errorf("balance.method=%q: %v", v, err)
}
if got := mustFind(t, "a").BalanceMethod; got != "POST" {
t.Errorf("a refused %q left %q", v, got)
}
}
}