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).
81 lines
3.1 KiB
Go
81 lines
3.1 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
// #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)
|
|
}
|
|
}
|