Files
magpie/internal/cli/providers_billing_cli_test.go
T
yetone 07af9f86a6 providers: mark a provider and a routing group on a plan or pay as you go (EV_John_Liu on X)
EV_John_Liu on X asked for pay-as-you-go and token plans to be marked on
providers and routing groups. A provider row now carries a quiet grey
badge, Plan, Pay as you go, or Plan + pay as you go when its keys are
on both (Volcengine's Coding Plan beside a pay-as-you-go key). It is
read from what magpie knows: a signed-in subscription is a plan; a
plan preset (Kimi Code, Qwen/Bailian token plans, OpenCode Go,
ClinePass) is a plan; a preset region's own name (Coding Plan, Token
Plan, Pay as you go) decides; other vendor and relay presets are pay as
you go. Presets whose billing magpie can't tell (MiniMax, Ollama Cloud,
ModelScope and others) and custom endpoints show nothing until the
user says: the provider editor's Advanced has Billing (Auto / Plan /
Pay as you go), and `magpie providers set <id> billing=plan|payg|auto`.
The user's word always wins. A routing group gets the same tag when
every on member is known; its title lists each member.

Run on mote with these changes on origin/main ccc96b4a7; pushed rebased
on 901e641f2, no conflict, where the core scripts weigh 1,531,382 of
their 1,533,802 (at ccc96b4a7 they were 1,934 over; main has since
moved code out, so no raise):
- go test -run 'Billing|CountFree|KeyPlan|GroupModelsCarry|ProviderSet'
  ./internal/provider ./internal/gui ./internal/cli: ok.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows.
- provider-billing.test.cjs: 5 languages, 900 and 440px, chromium and
  webkit, green (54 of 54 together with the ticks file); i18n-keys and
  i18n-en-no-cjk green.

Broken by hand: BillingKind returning "" turns TestBillingKind,
TestBillingKindMixedKeys, TestBillingEveryRegion and
TestProviderBilling red; dropping the badge turns
provider-billing.test.cjs red in every run.

Not tried: real accounts of each vendor (the marker reads saved
settings only, nothing goes upstream); the real app window. The whole
GUI suite was started after this record.
2026-11-10 18:58:03 +08:00

50 lines
1.7 KiB
Go

package cli
import (
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider set billing=plan marks a provider on a plan (EV_John_Liu
// on X), show says so, billing=auto hands it back to magpie's reading, and
// any other value is refused and leaves the saved one alone.
func TestProviderSetBilling(t *testing.T) {
groupsHome(t)
if _, err := printed(t, func() error { return providerCmd([]string{"provider", "set", "a", "billing=plan"}) }); err != nil {
t.Fatal(err)
}
if p := mustFind(t, "a"); p.Billing != provider.BillingPlan || p.BillingKind() != provider.BillingPlan {
t.Fatalf("billing=plan: %q", p.Billing)
}
out, err := printed(t, func() error { return providerCmd([]string{"provider", "a"}) })
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "billing") || !strings.Contains(out, "by your word") {
t.Fatalf("show doesn't say how it bills:\n%s", out)
}
for _, v := range []string{"maybe", "mixed", "--help"} {
err := providerCmd([]string{"provider", "set", "a", "billing=" + v})
if err == nil || !strings.Contains(err.Error(), "billing=plan|payg|auto") {
t.Errorf("billing=%q: %v", v, err)
}
if mustFind(t, "a").Billing != provider.BillingPlan {
t.Errorf("a refused billing=%q changed it", v)
}
}
if _, err := printed(t, func() error { return providerCmd([]string{"provider", "set", "a", "billing=payg"}) }); err != nil {
t.Fatal(err)
}
if mustFind(t, "a").Billing != provider.BillingPAYG {
t.Fatal("billing=payg not saved")
}
if _, err := printed(t, func() error { return providerCmd([]string{"provider", "set", "a", "billing=auto"}) }); err != nil {
t.Fatal(err)
}
if mustFind(t, "a").Billing != "" {
t.Fatal("billing=auto left the user's word")
}
}