Files
magpie/internal/gui/provider_save_tencent_test.go
T
yetone 1d016ee3f3 provider, gui: Token Plan 企业版 shows its usage on the Usage page, read with the account's SecretId and SecretKey (#1574, undirectlookable)
#1574 asks for Tencent Cloud Token Plan's usage on the Usage page through
TokenHub's cloud API (api/1823/132270). That API covers the enterprise
plan only (Token Plan 企业版; the previous commit added its regions). The
personal plan (api.lkeap.cloud.tencent.com/plan) has no usage API, only
the console, so it has no card. The API takes the account's API key, a
SecretId and SecretKey signed TC3-HMAC-SHA256, never the plan's sk- key.

provider (tencent_usage.go):
- A provider at /plan on tokenhub.tencentmaas.com (ap-guangzhou) or
  tokenhub-intl.tencentmaas.com (ap-singapore) with a SecretId and
  SecretKey gets a card per team plan in PlanQuotas
  (tencentPlanQuotas), each key and region asked once.
- It asks DescribeTokenPlanList, then DescribeTokenPlan per plan
  (version 2026-03-22), all at tokenhub.tencentcloudapi.com. Where a
  reply leaves a part out, the list's copy of it is kept.
- The window is TotalUsed of CycleQuota (else TotalQuota), in credits
  (专业套餐) or tokens (轻享套餐, enterprise-auto). It resets at
  TokenSummary.CycleEndTime. Without that, it counts months from
  StartTime and stops at ExpireTime.
- The docs don't say whether TotalUsed is the cycle's or the package's.
  This reads it as the cycle's.
- A plan that isn't enable shows its status beside the plan's name.
- An AuthFailure or UnauthorizedOperation reply says to check the key and
  its CAM rights. No plan visible to the key says so (a sub-user sees
  only the plans it made).
- The SecretId and SecretKey sit in the AccessKeyID and SecretAccessKey
  fields Volcengine's access key uses (#1427). They are stored, kept out
  of key-less backups and dropped from cached cards the same way.
  AccessKeyVendor tells the two vendors apart.

gui:
- The listing tells whose access key a provider or preset takes.
  presetAccessKey lists the regions that take one: Tencent Cloud's two
  Enterprise regions.
- The editor names the fields SecretId and SecretKey, with Tencent's
  hint, and shows them only at those regions (or at an endpoint none of
  the regions has).
- A SecretId with no SecretKey is refused only where the fields are
  shown. The new strings are in zh, zh-TW, ja and de.
- docs/subsystems/providers-accounts.md and the GUI tests README describe
  it.

Run:
- TestTC3SignMatchesDoc pins the signing doc's worked example (payload
  hash 35e9c5b0…, Signature 10b1a37a…).
- TestTencentPlanRegion, TestTencentPlanWindow and TestTencentPlanQuotas
  use the API docs' own response examples. The last is an httptest fake
  that rechecks each TC3 signature and the X-TC-* headers, and fails if
  the plan's key is ever sent.
- TestProviderListTellsTencentAccessKey covers the listing, alongside
  TestVolc* and TestPlanQuotas.
- Breaking the action's lowercasing in the signature, or dropping
  TokenSummary, turns them red.
- GUI: tencent-access-key.test.cjs (new) 12/12 in Chromium and WebKit at
  420px, en and zh. It goes red with the region hook or the vendor
  labels taken out. volc-access-key, tencent-cloud, i18n-keys, gui-ja
  and gui-de pass.
- gofmt; go vet on darwin, linux and windows; the full go test ./...

Not tried against the real API: we have no Tencent Cloud account. What
is read is what the docs show, and the reporter is asked to try it.
2026-10-11 13:06:28 +08:00

113 lines
3.3 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 two Enterprise regions alone, 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")
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)
}
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 the personal plan it still takes one, for when it is moved
if a := ps["tc-personal"]; a == nil || a.Vendor != "tencent" || a.ID != "" || a.SecretSet {
t.Errorf("personal: %+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{"enterprise", "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)
}
}