Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

1.7 KB
Raw Blame
package gateway

import (
	"net/http"
	"testing"
	"time"

	"github.com/yetone/magpie/internal/provider"
)

// A subscription that fails with a window full rests until the window
// renews, but no longer than one out of quota would: a reset read a year
// off (Claude Code's /usage date with no year, taken for next year) once
// benched the account for that year.
func TestRoutingHoldsAWindowRestToLongestQuota(t *testing.T) {
	forgetRouting()
	old := allowances
	defer func() { allowances = old }()
	now := time.Now()
	allowances = func(string) map[string]provider.Allowance {
		return map[string]provider.Allowance{
			"[email protected]":  {{Used: 100, Resets: now.AddDate(1, 0, 0), Span: 7 * 24 * time.Hour}},
			"[email protected]": {{Used: 100, Resets: now.Add(3 * time.Hour), Span: 5 * time.Hour}},
		}
	}
	acct := func(user string) candidate {
		return candidate{p: provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: user}}, model: "claude-sonnet-4-5", rest: "claude#" + user}
	}
	s := &Server{}
	defer s.Unrest(acct("[email protected]").restKey())
	defer s.Unrest(acct("[email protected]").restKey())
	near := func(d, want time.Duration) bool { return d > want-time.Minute && d <= want }

	r := s.restAfter(acct("[email protected]"), 502, http.Header{}, []byte("bad gateway"))
	if r.Why != failOther || r.By != "window" || !near(time.Until(r.Until), longestQuota) {
		t.Fatalf("window read a year off: rests %v by %s (%s), want at most %v", time.Until(r.Until), r.By, r.Why, longestQuota)
	}
	r = s.restAfter(acct("[email protected]"), 502, http.Header{}, []byte("bad gateway"))
	if r.Why != failOther || r.By != "window" || !near(time.Until(r.Until), 3*time.Hour) {
		t.Fatalf("window renewing in 3h: rests %v by %s (%s)", time.Until(r.Until), r.By, r.Why)
	}
}