Skip to content

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

1.6 KB
Raw Blame
package gateway

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

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

// GET /v1/magpie/quotas/history is each account's windows over time
// (#651): the points of the last ?days=, ?provider= and ?user= picking
// one account's, and another machine without the shared key gets nothing.
func TestQuotasHistory(t *testing.T) {
	fresh(t)
	t.Setenv("MAGPIE_ADDR", "")
	now := time.Now().UTC().Truncate(time.Second)
	ago := func(h int) string { return now.Add(-time.Duration(h) * time.Hour).Format(time.RFC3339) }
	seed := fmt.Sprintf(`{"codex|[email protected]":{"5 hours":[{"at":%q,"left":90},{"at":%q,"left":70}]},
		"claude|":{"Weekly":[{"at":%q,"left":40}]}}`, ago(100), ago(1), ago(2))
	if err := provider.MergeQuotaHistory([]byte(seed), now); err != nil {
		t.Fatal(err)
	}
	h := lanGuard(New().Handler())
	get := func(from, q string) (int, []provider.QuotaHistory) {
		r := httptest.NewRequest("GET", "/v1/magpie/quotas/history"+q, nil)
		r.RemoteAddr = from
		w := httptest.NewRecorder()
		h.ServeHTTP(w, r)
		var out struct{ Data []provider.QuotaHistory }
		json.Unmarshal(w.Body.Bytes(), &out)
		return w.Code, out.Data
	}
	c, all := get("127.0.0.1:5000", "")
	if c != 200 || len(all) != 2 {
		t.Fatalf("all: %d %+v", c, all)
	}
	_, codex := get("127.0.0.1:5000", "?provider=codex&[email protected]&days=2")
	if len(codex) != 1 || codex[0].User != "[email protected]" || len(codex[0].Lines[0].Points) != 2 {
		t.Fatalf("codex, 2 days (the point the range begins in, and the last): %+v", codex)
	}
	if c, _ := get("192.168.1.9:5000", ""); c == http.StatusOK {
		t.Fatal("another machine without a key got the history")
	}
}