provider: a Command Code plan key's card shows the month's credits as a meter (#2451)

tianxia3111 runs Command Code on the keyed preset with a plan's key. Its
card showed the 5-hour and weekly windows as meters, but the month's
credits only as dollars left ($5.27), with no share of the month. The
cause: billing/credits leaves out the planId, so the key's card couldn't
know the plan's monthly pool. The signed-in subscription's card gets
that pool from billing/subscriptions.

The key's card (planKeyWindows -> cmdKeyWindows) now asks
billing/subscriptions with the same key for the plan. It then makes the
month a "Credits" window beside the other two, through the same
cmdQuotaOf the subscription's card uses ("$4.78 / $10.00" on a Go plan).

- The plan id is kept per key for ten minutes, since
  billing/subscriptions takes 15-20 s at times.
- The id read before still counts while the plan can't be read.
- A key whose plan was never read keeps its two windows alone.
- A pay-as-you-go key, with no windows, still gets no card.

planWindows' request and status handling move into planBody, so that
cmdKeyWindows can decode the reply itself; other vendors are unchanged.

Run:
- New TestCommandCodeKeyCardHasCredits uses the reporter's own
  billing/credits reply. Its billing/subscriptions reply has the shape
  the other Command Code tests use (individual-go-monthly), because the
  reporter's plan id isn't known.
- With the new branch disabled by hand, the test went red: two windows,
  no plan. With it, green.
- TestKeyQuotasKeepLast now answers billing/subscriptions with 503 and
  still sees its two windows.
- gofmt; go vet -tags nogui for darwin, linux and windows.
- Not tried against Command Code's real API: there is no plan key here.
- Full suite (go test -timeout 45m ./..., at f1c91a99c plus this change):
  every package ok except internal/gui, red only on
  TestPanelJoinsFullScreenSpaces, which is #2450 (macOS 26.2, red on
  main before this change).
This commit is contained in:
yetone
2026-10-16 06:32:41 +08:00
parent f1c91a99c0
commit ba17c4f90d
5 changed files with 161 additions and 15 deletions
+48
View File
@@ -644,6 +644,54 @@ func cmdSubscription(ctx context.Context, a cmdAuth) (id, plan string, until *ti
return "", cmdNoPlan, nil, "", true
}
// cmdKeyWindows is a Command Code key's plan card (#2451): the plan's
// 5-hour and weekly windows, and its month of credits as one more, as
// the signed-in subscription's card has them. billing/credits leaves the
// plan out, so it is the key's subscription's, read at most every ten
// minutes (billing/subscriptions takes 15–20 seconds at times), and the
// one read before while it can't be read. A key with no windows is a
// pay-as-you-go key: no plan, and no card.
func cmdKeyWindows(ctx context.Context, src planQuotaSource, key string) (string, []QuotaWindow, error) {
b, err := planBody(ctx, src, key)
if err != nil {
return "", nil, err
}
var c cmdCredits
if err := json.Unmarshal(b, &c); err != nil {
return "", nil, err
}
if len(cmdWindows(c)) == 0 {
return cmdPlanName(c.Credits.PlanID), []QuotaWindow{}, nil
}
if c.Credits.PlanID == "" {
c.Credits.PlanID = cmdKeyPlanID(ctx, key)
}
q := cmdQuotaOf(SubscriptionQuota{}, c)
return q.Plan, q.Windows, nil
}
var cmdKeyPlans = struct {
sync.Mutex
m map[string]cmdSeen // plan is the subscription's id here
}{m: map[string]cmdSeen{}}
func cmdKeyPlanID(ctx context.Context, key string) string {
cmdKeyPlans.Lock()
seen, had := cmdKeyPlans.m[key]
cmdKeyPlans.Unlock()
if had && time.Since(seen.at) < 10*time.Minute {
return seen.plan
}
id, _, _, _, ok := cmdSubscription(ctx, cmdAuth{APIKey: key})
if !ok {
return seen.plan
}
cmdKeyPlans.Lock()
cmdKeyPlans.m[key] = cmdSeen{id, time.Now()}
cmdKeyPlans.Unlock()
return id
}
func cmdLoginQuota(ctx context.Context, l Login) SubscriptionQuota {
for _, c := range cmdLogins() {
if strings.EqualFold(c.User, l.User) {