* feat: add DimAgent subscription support Add OAuth PKCE login with automatic and pasted callbacks, account management, model discovery, quota reporting and gateway integration. Safely rotate credentials during concurrent requests and preserve fresh tokens when updating model caches. Cover protocol, provider, GUI and callback behavior with regression tests. * dimagent: a refresh hiccup inside the day's lead keeps the token that still runs The fallback checked !dimagentNearExpiry, which is false whenever a refresh runs at all, so a 5xx/timeout on /oauth/token failed every request for the last 24h of a still-valid token. A 401/403 still lapses the account. * dimagent: review tests (sign forwarding, parallel refresh once, hiccup in lead, LoginUsage) * dimagent: signing in again takes the callback port from the sign-in it replaces StartSignIn begins the new flow before it cancels the old one, so the new flow's listen on 54321 failed (ignored) and the old listener then shut down a second later: the browser's callback was refused and only pasting could finish. The older DimAgent flow is now ended and its server closed first, and the listen retried briefly while the port is let go. * dimagent: the fake-upstream tests get a name of their own --------- Co-authored-by: yetone <yetoneful@gmail.com>
103 lines
3.7 KiB
Go
103 lines
3.7 KiB
Go
package dimagent
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A reply as the live endpoint answers one today, its shapes kept and its
|
|
// figures and names invented. Its account_id is a number, not a string — read as a
|
|
// string the whole allowance was lost for every account. A plan that has
|
|
// ended names no current term, so its paid time ended with its last term; and
|
|
// it holds no bucket of the plan's own besides its add-ons, whose total is
|
|
// those added up and must not stand as a second ring.
|
|
const usageLive = `{"success":true,"data":{
|
|
"account_id":7,
|
|
"subscription":{"product":{"name":"Example Lite"},
|
|
"subscription":{"status":"canceled","cancel_at_period_end":true},
|
|
"current_term_id":null,
|
|
"last_term":{"end_at":"2026-09-21T00:00:00Z","status":"ended"}},
|
|
"credits":{
|
|
"subscription_bucket":null,
|
|
"addon_buckets":[{"bucket_kind":"addon","total_units":300,"used_units":10,
|
|
"remaining_units":290,"unlimited":false,"expires_at":"2026-10-21T00:00:00Z",
|
|
"status":"active","window_token_cap":0,"window_duration_hours":0}],
|
|
"total_units":300,"used_units":10,"remaining_units":290},
|
|
"feature_meters":[],
|
|
"resets":{"window":{"available_count":0},"monthly_full":{"available_count":0}},
|
|
"credits_display":{"enabled":true,"credit_name":"Credits","decimals":0}}}`
|
|
|
|
func TestParseUsageLiveShape(t *testing.T) {
|
|
u, err := ParseUsage([]byte(usageLive))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.AccountID != "7" {
|
|
t.Fatalf("account id read from a number: %q", u.AccountID)
|
|
}
|
|
if u.Plan.Name != "Example Lite" || u.Plan.Status != "canceled" {
|
|
t.Fatalf("plan: %+v", u.Plan)
|
|
}
|
|
if got := u.Plan.EndsAt.Format("2006-01-02"); got != "2026-09-21" {
|
|
t.Fatalf("an ended plan's paid term: %s", got)
|
|
}
|
|
if !u.Plan.RenewKnown || u.Plan.Renew {
|
|
t.Fatalf("renew: %+v", u.Plan)
|
|
}
|
|
// its stated total is its one add-on's, so it is left out rather than
|
|
// shown as the same credits twice
|
|
if u.Total.holds() {
|
|
t.Fatalf("the buckets' own sum shown again: %+v", u.Total)
|
|
}
|
|
if len(u.AddOns) != 1 {
|
|
t.Fatalf("add-ons: %+v", u.AddOns)
|
|
}
|
|
a := u.AddOns[0]
|
|
if a.Total != 300 || a.Used != 10 || a.Remaining != 290 {
|
|
t.Fatalf("add-on: %+v", a)
|
|
}
|
|
if a.Units != "Credits" {
|
|
t.Fatalf("units read from credits_display: %+v", a)
|
|
}
|
|
if len(a.Windows) != 0 {
|
|
t.Fatalf("a window of no cap kept as a rate window: %+v", a.Windows)
|
|
}
|
|
}
|
|
|
|
// A reply that names no buckets at all, leaving the account's credits as the
|
|
// one figure it states, is read from that figure — otherwise an account with
|
|
// credits would seem to have none.
|
|
func TestParseUsageFlatTotalAlone(t *testing.T) {
|
|
const body = `{"data":{"account_id":7,"credits":{
|
|
"subscription_bucket":null,"addon_buckets":[],
|
|
"total_units":900,"used_units":20,"remaining_units":880},
|
|
"credits_display":{"enabled":true,"credit_name":"Credits"}}}`
|
|
u, err := ParseUsage([]byte(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.Total.Total != 900 || u.Total.Used != 20 || u.Total.Remaining != 880 {
|
|
t.Fatalf("tally: %+v", u.Total)
|
|
}
|
|
if u.Total.Units != "Credits" {
|
|
t.Fatalf("tally units: %+v", u.Total)
|
|
}
|
|
if len(u.AddOns) != 0 || u.Subscription.holds() {
|
|
t.Fatalf("buckets invented out of the tally: %+v", u)
|
|
}
|
|
}
|
|
|
|
// An add-on bucket whose units the reply doesn't name still reads: a vendor
|
|
// that shows no currency of its own leaves the reader to say credits.
|
|
func TestParseUsageNoCreditName(t *testing.T) {
|
|
body := strings.Replace(usageLive, `"credits_display":{"enabled":true,"credit_name":"Credits","decimals":0}`,
|
|
`"credits_display":{"enabled":false,"credit_name":"Credits"}`, 1)
|
|
u, err := ParseUsage([]byte(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.AddOns[0].Units != "" {
|
|
t.Fatalf("a currency the vendor doesn't show: %+v", u.AddOns[0])
|
|
}
|
|
}
|