#2451 drew a plan key's month of credits as a Credits meter beside the 5-hour and weekly windows. Those two say when they start again; the Credits one said nothing, though cmdKeyPlanID already asks billing/subscriptions for the plan id and threw its currentPeriodEnd away. cmdKeyPlanID now keeps the period end beside the plan id (cached the same ten minutes, and the pair read before kept while the endpoint can't be read), and cmdQuotaOf puts it on the Credits window's ResetsAt. No new request. The signed-in subscription's card is the plugin's since be62bcf79; the plugin sets the same reset from commandcode-auth 0.1.13. Patch by ggbdpq (Discord gua2100), as rebased in #2454. Added in review: a period end already past is dropped, so a pair kept while the endpoint fails, or a renewal its reply doesn't show yet, never puts a reset in the past on the card; the test has a case for it. Run (sandbox HOME, at origin/main85b45b4d7plus this): - go test -tags nogui -run 'CommandCode|KeyQuotas' ./internal/provider/: ok - Broken by hand: ResetsAt: until taken out -> "the key card's credits have no reset" (ResetsAt nil); the past-end guard taken out -> "the key card's credits reset in the past". Each restored: ok. - go test -tags nogui ./internal/provider/: two known reds on main, both filed and both red at85b45b4d7without this change: TestUnreadLoginsWriteErrorsKeepCredentials crashes the binary (#3632), and with it skipped the rest pass except TestFetchAsksASharedBaseOnce and two siblings (#3854). - gofmt -l internal/provider: clean. go vet ./internal/provider/ on darwin, GOOS=windows, GOOS=linux: ok. Not tried: a real Command Code plan key (none here), the GUI card, a period that renews while the card is open; go test ./... (one suite per machine). The key card only learns the period end when billing/credits leaves the plan id out, which is what it does today; if it starts sending the plan id, the reset is not read.
130 lines
5.2 KiB
Go
130 lines
5.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// #2451 (tianxia3111): a Command Code plan's key, on the keyed preset,
|
|
// showed its 5-hour and weekly windows as meters and the month's credits
|
|
// only as dollars left ($5.27). billing/credits leaves the plan out, so
|
|
// the key's card asks billing/subscriptions for it, as the signed-in
|
|
// subscription's card does, and the month is a meter beside the windows.
|
|
// The reply is the reporter's own.
|
|
func TestCommandCodeKeyCardHasCredits(t *testing.T) {
|
|
var subs atomic.Int32
|
|
var subsDown atomic.Bool
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Header.Get("X-Host") + r.URL.Path {
|
|
case "api.commandcode.ai/alpha/billing/credits":
|
|
w.Write([]byte(`{"credits":{"belowThreshold":false,"creditThreshold":0,"monthlyCredits":5.2207933742,"purchasedCredits":0,"freeCredits":0},
|
|
"windowLimits":{"limited":true,"exceeded":null,
|
|
"fiveHour":{"used":0.0866816244,"cap":14,"exceeded":false,"resetAt":4102444800000},
|
|
"weekly":{"used":0.8971969678,"cap":35,"exceeded":false,"resetAt":4102444800000}},
|
|
"sandboxAccess":true}`))
|
|
case "api.commandcode.ai/alpha/billing/subscriptions":
|
|
subs.Add(1)
|
|
if subsDown.Load() {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"success":true,"data":{"planId":"individual-go-monthly","status":"active"}}`))
|
|
default:
|
|
t.Errorf("asked %s%s", r.Header.Get("X-Host"), r.URL.Path)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
old := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = rewrite{srv}
|
|
t.Cleanup(func() { http.DefaultClient.Transport = old })
|
|
|
|
p := Provider{ID: "commandcode", Key: "cmd-2451", Chat: "https://api.commandcode.ai/provider/v1"}
|
|
src, _ := planQuotaSourceOf(p)
|
|
plan, ws, _, err := planKeyWindows(context.Background(), p, src, p.Key)
|
|
if err != nil || plan != "Go" || len(ws) != 3 {
|
|
t.Fatalf("%v %q %+v", err, plan, ws)
|
|
}
|
|
if ws[0].Name != "5 hours" || ws[1].Name != "Weekly" {
|
|
t.Errorf("windows: %+v", ws)
|
|
}
|
|
// Go gives $10 a month; $5.22 of it is left
|
|
if c := ws[2]; c.Name != "Credits" || c.Display != "$4.78 / $10.00" || c.Used < 47.7 || c.Used > 47.8 {
|
|
t.Errorf("credits: %+v", c)
|
|
}
|
|
|
|
// the plan is asked again only after ten minutes, and the one read
|
|
// before stands while it can't be read
|
|
subsDown.Store(true)
|
|
if _, ws, _, _ := planKeyWindows(context.Background(), p, src, p.Key); len(ws) != 3 || subs.Load() != 1 {
|
|
t.Errorf("read again: %d asks, %+v", subs.Load(), ws)
|
|
}
|
|
cmdKeyPlans.Lock()
|
|
seen := cmdKeyPlans.m[p.Key]
|
|
seen.at = seen.at.Add(-11 * 60e9)
|
|
cmdKeyPlans.m[p.Key] = seen
|
|
cmdKeyPlans.Unlock()
|
|
if plan, ws, _, _ := planKeyWindows(context.Background(), p, src, p.Key); len(ws) != 3 || plan != "Go" || subs.Load() != 2 {
|
|
t.Errorf("plan unreadable: %d asks, %q %+v", subs.Load(), plan, ws)
|
|
}
|
|
|
|
// a key whose plan was never read has its windows alone
|
|
q := p
|
|
q.Key = "cmd-2451-new"
|
|
if plan, ws, _, err := planKeyWindows(context.Background(), q, src, q.Key); err != nil || plan != "" || len(ws) != 2 {
|
|
t.Errorf("plan never read: %v %q %+v", err, plan, ws)
|
|
}
|
|
}
|
|
|
|
// The Credits window #2451 added renews when the plan's period does: the
|
|
// end is in the same billing/subscriptions reply the plan id is read
|
|
// from, and the card's other two windows each say when they start again.
|
|
func TestCommandCodeKeyCreditsRenewAtPeriodEnd(t *testing.T) {
|
|
const end = int64(4102444800000) // 2100-01-01, in ms as cmdTime reads it
|
|
periodEnd := end
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Header.Get("X-Host") + r.URL.Path {
|
|
case "api.commandcode.ai/alpha/billing/credits":
|
|
w.Write([]byte(`{"credits":{"monthlyCredits":5.22,"purchasedCredits":0,"freeCredits":0},
|
|
"windowLimits":{"limited":true,
|
|
"fiveHour":{"used":1,"cap":14,"resetAt":4102444800000},
|
|
"weekly":{"used":2,"cap":35,"resetAt":4102444800000}}}`))
|
|
case "api.commandcode.ai/alpha/billing/subscriptions":
|
|
w.Write([]byte(`{"success":true,"data":{"planId":"individual-go-monthly","status":"active","currentPeriodEnd":` + strconv.FormatInt(periodEnd, 10) + `}}`))
|
|
default:
|
|
t.Errorf("asked %s%s", r.Header.Get("X-Host"), r.URL.Path)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
old := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = rewrite{srv}
|
|
t.Cleanup(func() { http.DefaultClient.Transport = old })
|
|
|
|
p := Provider{ID: "commandcode", Key: "cmd-reset", Chat: "https://api.commandcode.ai/provider/v1"}
|
|
src, _ := planQuotaSourceOf(p)
|
|
_, ws, _, err := planKeyWindows(context.Background(), p, src, p.Key)
|
|
if err != nil || len(ws) != 3 {
|
|
t.Fatalf("%v %+v", err, ws)
|
|
}
|
|
if c := ws[2]; c.Name != "Credits" || c.ResetsAt == nil || c.ResetsAt.UnixMilli() != end {
|
|
t.Errorf("the key card's credits have no reset: %+v", c)
|
|
}
|
|
|
|
// a period end already past says nothing about when they come back
|
|
periodEnd = time.Now().Add(-time.Hour).UnixMilli()
|
|
p.Key = "cmd-reset-past"
|
|
_, ws, _, err = planKeyWindows(context.Background(), p, src, p.Key)
|
|
if err != nil || len(ws) != 3 {
|
|
t.Fatalf("%v %+v", err, ws)
|
|
}
|
|
if c := ws[2]; c.Name != "Credits" || c.ResetsAt != nil {
|
|
t.Errorf("the key card's credits reset in the past: %+v", c)
|
|
}
|
|
}
|