Files
magpie/internal/gui/provider_save_tencent_test.go
T
yetone 4062cda781 provider: a custom provider at a Token Plan's or Ark's address is asked for no access key (ysm119)
ysm119 on Discord saved a custom provider (no preset) at
https://tokenhub.tencentmaas.com/plan/v3 with a plain sk- key on the
OpenAI-compatible tab, and its editor asked for a SecretId and
SecretKey. AccessKeyVendor decides by host alone, so any provider at a
Token Plan's /plan address (or an Ark plan's) got the access-key fields,
and a Singapore Token Plan or Ark one also got a Usage card asking for
them.

Now AsksAccessKey(p) gates the two places a user is asked: the editor's
listing (gui/providers.go) and the keyless Usage cards
(tencentPlanQuotas, volcPlanQuotas). A preset's provider is asked as
before; a custom one only once it has an access key saved (so a key the
user did give is still shown and still used for its usage). The host
rules themselves are unchanged, since presetAccessKey and the presets
read them on providers with no preset of their own.

The key itself was already sent as typed: no Tencent signing exists on
chat, Responses, the model list or a probe; the access key only reads
plan usage. TestCustomTokenHubIsSentAsTyped holds that (Authorization:
Bearer <key>, on Chat and Responses).

Run on mote (go1.26.8):
- go test ./internal/provider ./internal/gui: provider ok; gui red only
  in TestMainWindowCorners (AppKit, #3949, macOS 27 on mote).
- TestPlanCardWithoutAccessKey, TestCustomTokenHubIsSentAsTyped,
  TestProviderListTellsTencentAccessKey ok.
- GUI (node --test, Chromium and WebKit, 5 languages): tencent-access-key,
  tencent-cloud, volc-access-key, usage-access-key-card, i18n-keys and
  the provider editor's key files ok, except tencent-cloud's "a region
  clicked right after the strip scrolled" (#4321, open, same on main).
- gofmt -l clean; go vet, GOOS=linux go vet -tags nogui, GOOS=windows
  go vet on provider, gateway, gui ok.

Broken by hand: AsksAccessKey returning AccessKeyVendor(p) always turns
TestPlanCardWithoutAccessKey (my-ark gets a card),
TestCustomTokenHubIsSentAsTyped and TestProviderListTellsTencentAccessKey
(my-tokenhub, my-intl, my-ark asked) red.

Not tried: the reporter's real tokenhub key; the real app's editor.
2026-11-04 21:21:32 +08:00

136 lines
4.5 KiB
Go

package gui
// Tencent Cloud's Token Plan 企业版 is read with the account's SecretId and
// SecretKey (#1574): the listing tells the editor the Tencent Cloud preset
// takes one at its plan regions alone (China's, which Token Plan 个人版
// shares with 企业版 since Tencent moved it there, ysm119; and 企业版's
// in Singapore), not at pay as you go, and whose it is, and a
// provider of it, at whatever region, its SecretId and whether a
// SecretKey is saved, never the SecretKey. Volcengine Ark's take one at
// all of theirs; another vendor's at none.
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
func TestProviderListTellsTencentAccessKey(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)
at := func(region string) provider.Provider {
t.Helper()
p, err := provider.FromPreset("tencent-cloud")
if err != nil {
t.Fatal(err)
}
pr := provider.Preset("tencent-cloud")
for _, r := range pr.Regions {
if r.ID == region {
p.Chat, p.Responses, p.Anthropic = r.Chat, r.Responses, r.Anthropic
}
}
return p
}
team := at("enterprise-intl")
team.Key, team.AccessKeyID, team.SecretAccessKey = "sk-tp-1", "AKID-1", "SK-1"
if err := provider.Save(team); err != nil {
t.Fatal(err)
}
personal := at("plan")
personal.ID, personal.Name, personal.Key = "tc-personal", "Tencent personal", "sk-tp-2"
if err := provider.Save(personal); err != nil {
t.Fatal(err)
}
// a custom provider is the user's own address and key: one at the
// plan's address, or at Ark's, is asked for no access key (ysm119),
// and one given one anyway keeps it shown
for _, c := range []provider.Provider{
{ID: "my-tokenhub", Name: "My TokenHub", Chat: "https://tokenhub.tencentmaas.com/plan/v3", Key: "sk-tp-own"},
{ID: "my-intl", Name: "My TokenHub intl", Chat: "https://tokenhub-intl.tencentmaas.com/plan/v3", Key: "sk-tp-own2"},
{ID: "my-ark", Name: "My Ark", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", Key: "ark-own"},
{ID: "my-keyed", Name: "My keyed", Chat: "https://tokenhub-intl.tencentmaas.com/plan/v3", Key: "sk-tp-own3", AccessKeyID: "AKID-own", SecretAccessKey: "SK-own"},
} {
if err := provider.Save(c); err != nil {
t.Fatal(err)
}
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/providers", nil))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
if strings.Contains(w.Body.String(), "SK-1") {
t.Error("the listing told the SecretKey")
}
var got struct {
Providers []struct {
ID string `json:"id"`
AccessKey *accessKeyJSON `json:"accessKey"`
} `json:"providers"`
Presets []struct {
ID string `json:"id"`
AccessKey bool `json:"accessKey"`
AccessKeyVendor string `json:"accessKeyVendor"`
AccessKeyRegions []string `json:"accessKeyRegions"`
} `json:"presets"`
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
ps := map[string]*accessKeyJSON{}
for _, x := range got.Providers {
ps[x.ID] = x.AccessKey
}
if a := ps["tencent-cloud"]; a == nil || a.Vendor != "tencent" || a.ID != "AKID-1" || !a.SecretSet {
t.Errorf("enterprise: %+v", a)
}
// at China's plan, 个人版's and 企业版's both, it takes one too
if a := ps["tc-personal"]; a == nil || a.Vendor != "tencent" || a.ID != "" || a.SecretSet {
t.Errorf("personal: %+v", a)
}
for _, id := range []string{"my-tokenhub", "my-intl", "my-ark"} {
if a, ok := ps[id]; !ok || a != nil {
t.Errorf("custom %s: asked for an access key: %+v (listed %v)", id, a, ok)
}
}
if a := ps["my-keyed"]; a == nil || a.Vendor != "tencent" || a.ID != "AKID-own" || !a.SecretSet {
t.Errorf("custom with an access key saved: %+v", a)
}
seen := 0
for _, pr := range got.Presets {
switch pr.ID {
case "tencent-cloud":
seen++
if !pr.AccessKey || pr.AccessKeyVendor != "tencent" || !slices.Equal(pr.AccessKeyRegions, []string{"plan", "enterprise-intl"}) {
t.Errorf("tencent-cloud preset: %+v", pr)
}
case "volcengine":
seen++
if !pr.AccessKey || pr.AccessKeyVendor != "volcengine" || pr.AccessKeyRegions != nil {
t.Errorf("volcengine preset: %+v", pr)
}
case "deepseek":
seen++
if pr.AccessKey || pr.AccessKeyVendor != "" {
t.Errorf("deepseek preset: %+v", pr)
}
}
}
if seen != 3 {
t.Errorf("presets seen: %d", seen)
}
}