package e2e import ( "context" "net/http" "testing" "time" "gitea.mynodes.xyz/ilker/llmgw/internal/config" "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) // paidConfig turns on auth with a price attached to the test model, so spend // is a number rather than always zero. func paidConfig(path string) func(*config.Server) { return func(c *config.Server) { c.Auth = config.Auth{ Enabled: true, Store: path, CacheTTL: 50 * time.Millisecond, FlushInterval: 20 * time.Millisecond, BatchSize: 8, Pricing: map[string]config.Price{ testModel: {PromptPer1k: 1_000_000, CompletionPer1k: 1_000_000}, }, } } } func usageRows(t *testing.T, path string) []store.Usage { t.Helper() s, err := store.OpenSQLite(context.Background(), path) if err != nil { t.Fatal(err) } defer s.Close() rows, err := s.SpendSince(context.Background(), time.Now().Add(-time.Hour)) if err != nil { t.Fatal(err) } out := make([]store.Usage, 0, len(rows)) for key, micros := range rows { out = append(out, store.Usage{KeyID: key, CostMicros: micros}) } return out } func TestUsageReachesTheStoreWithoutBlockingRequests(t *testing.T) { path, mint := provision(t) key := mint(t, "spender") h := start(t, 2, paidConfig(path)) for range 12 { if code, msg := h.call(t, key, testModel); code != http.StatusOK { t.Fatalf("%d %s", code, msg) } } // The writer is asynchronous, so this is the one place a wait is correct. deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if rows := usageRows(t, path); len(rows) > 0 && rows[0].CostMicros > 0 { return } time.Sleep(25 * time.Millisecond) } t.Fatal("no usage reached the store") } func TestRequestRateLimitsBindThroughTheGateway(t *testing.T) { path, mint := provision(t) key := mint(t, "burst") withStore(t, path, func(s *store.SQLite) { rpm := 5 if err := s.PutLimits(context.Background(), store.Limits{ Scope: store.ScopeKey, ScopeID: "key-burst", RPM: &rpm, }); err != nil { t.Fatal(err) } }) h := start(t, 2, paidConfig(path)) ok, limited := 0, 0 for range 20 { switch code, _ := h.call(t, key, testModel); code { case http.StatusOK: ok++ case http.StatusTooManyRequests: limited++ default: t.Fatalf("unexpected status %d", code) } } if ok != 5 { t.Errorf("a limit of 5 let %d requests through", ok) } if limited != 15 { t.Errorf("%d requests were refused, expected 15", limited) } } func TestASpentBudgetRefusesAndDoesNotSuggestRetrying(t *testing.T) { path, mint := provision(t) key := mint(t, "broke") withStore(t, path, func(s *store.SQLite) { // One micro-unit: the first request exhausts it. budget := int64(1) if err := s.PutLimits(context.Background(), store.Limits{ Scope: store.ScopeKey, ScopeID: "key-broke", Budget: &budget, BudgetPeriod: store.PeriodMonth, }); err != nil { t.Fatal(err) } }) h := start(t, 2, paidConfig(path)) if code, msg := h.call(t, key, testModel); code != http.StatusOK { t.Fatalf("the first request should go through: %d %s", code, msg) } deadline := time.Now().Add(3 * time.Second) for time.Now().Before(deadline) { if code, _ := h.call(t, key, testModel); code == http.StatusTooManyRequests { return } time.Sleep(25 * time.Millisecond) } t.Fatal("a key kept spending past its budget") } func TestBudgetSurvivesAGatewayRestart(t *testing.T) { // The counters live in memory. If a restart reset them, a budget would be a // suggestion that anyone could clear by bouncing the process. path, mint := provision(t) key := mint(t, "persistent") h := start(t, 2, paidConfig(path)) for range 3 { if code, msg := h.call(t, key, testModel); code != http.StatusOK { t.Fatalf("%d %s", code, msg) } } // Let the batch writer land before the process goes away. deadline := time.Now().Add(3 * time.Second) for time.Now().Before(deadline) { if rows := usageRows(t, path); len(rows) > 0 && rows[0].CostMicros > 0 { break } time.Sleep(25 * time.Millisecond) } spent := usageRows(t, path) if len(spent) == 0 || spent[0].CostMicros == 0 { t.Fatal("nothing was recorded to restore from") } // A second gateway over the same store, with a budget just under what was // already spent: it must refuse immediately rather than starting fresh. withStore(t, path, func(s *store.SQLite) { budget := spent[0].CostMicros if err := s.PutLimits(context.Background(), store.Limits{ Scope: store.ScopeKey, ScopeID: "key-persistent", Budget: &budget, BudgetPeriod: store.PeriodMonth, }); err != nil { t.Fatal(err) } }) h2 := start(t, 2, paidConfig(path)) if code, _ := h2.call(t, key, testModel); code != http.StatusTooManyRequests { t.Errorf("the restarted gateway gave the key a fresh budget: %d", code) } } func TestLimitsAreNotAppliedWithoutAuthentication(t *testing.T) { // Accounting only exists where there is an identity to charge. h := start(t, 2) for range 30 { if code, msg := h.call(t, "", testModel); code != http.StatusOK { t.Fatalf("an unauthenticated gateway rate-limited a request: %d %s", code, msg) } } }