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 on901e641f2, no conflict, where the core scripts weigh 1,531,382 of their 1,533,802 (atccc96b4a7they 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.
73 lines
3.1 KiB
Go
73 lines
3.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// EV_John_Liu on X: whether a provider is on a plan or pay as you go is
|
|
// marked on Providers and on a routing group's members. /api/providers
|
|
// gives magpie's reading (billingKind) and the user's word (billing),
|
|
// the editor's save keeps the word on a preset and on a typed-in
|
|
// provider, refuses a value that is neither, and a group's member says
|
|
// its provider's billing.
|
|
func TestProviderBilling(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
do := func(method, path, body string, code int) string {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
if w.Code != code {
|
|
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
|
|
}
|
|
return w.Body.String()
|
|
}
|
|
do("POST", "/api/provider/save", `{"id":"openrouter","name":"OpenRouter","preset":"openrouter","key":"k","new":true}`, 200)
|
|
if !strings.Contains(do("GET", "/api/providers", "", 200), `"billingKind":"payg"`) {
|
|
t.Fatal("/api/providers doesn't say OpenRouter is pay as you go")
|
|
}
|
|
do("POST", "/api/provider/save", `{"id":"openrouter","from":"openrouter","name":"OpenRouter","preset":"openrouter","key":"","billing":"plan"}`, 200)
|
|
if p, err := provider.Find("openrouter"); err != nil || p.Billing != provider.BillingPlan || p.BillingKind() != provider.BillingPlan {
|
|
t.Fatalf("the preset's word wasn't kept: %+v %v", p.Billing, err)
|
|
}
|
|
if out := do("GET", "/api/providers", "", 200); !strings.Contains(out, `"billing":"plan"`) || !strings.Contains(out, `"billingKind":"plan"`) {
|
|
t.Fatal("/api/providers doesn't give the word back")
|
|
}
|
|
do("POST", "/api/provider/save", `{"id":"openrouter","from":"openrouter","name":"OpenRouter","preset":"openrouter","key":"","billing":"mixed"}`, 400)
|
|
if p, _ := provider.Find("openrouter"); p.Billing != provider.BillingPlan {
|
|
t.Fatalf("a refused value changed it: %q", p.Billing)
|
|
}
|
|
do("POST", "/api/provider/save", `{"id":"openrouter","from":"openrouter","name":"OpenRouter","preset":"openrouter","key":"","billing":""}`, 200)
|
|
if p, _ := provider.Find("openrouter"); p.Billing != "" {
|
|
t.Fatalf("Auto left the word: %q", p.Billing)
|
|
}
|
|
|
|
do("POST", "/api/provider/save", `{"id":"box","name":"Box","chat":"http://127.0.0.1:9/v1","key":"k","models":["m"],"billing":"payg","new":true}`, 200)
|
|
if p, err := provider.Find("box"); err != nil || p.Billing != provider.BillingPAYG {
|
|
t.Fatalf("a typed-in provider's word wasn't kept: %+v %v", p.Billing, err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{ID: "g", Name: "G", Members: []string{"box/m"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, g := range groupsState().Groups {
|
|
if g.ID != "g" {
|
|
continue
|
|
}
|
|
if len(g.Info) != 1 || g.Info[0].Billing != provider.BillingPAYG {
|
|
t.Fatalf("the member doesn't say its provider's billing: %+v", g.Info)
|
|
}
|
|
return
|
|
}
|
|
t.Fatal("group g not listed")
|
|
}
|