Files
localmodelroutex/backend/internal/api/stats_test.go
LovroG05 abecc4b888
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: export request statistics to InfluxDB 3
Add an optional exporter that writes one point per proxied request to
the InfluxDB 3 write endpoint. Points are batched in memory and flushed
by size or interval. The exporter is off until GATEWAY_INFLUX_URL is set.
2026-10-11 17:20:14 +02:00

43 lines
1.0 KiB
Go

package api
import (
"net/http"
"sync"
"testing"
"git.govekar.net/LovroG05/localmodelroutex/internal/store"
)
type fakeStats struct {
mu sync.Mutex
rows []store.RequestLog
}
func (f *fakeStats) Record(e store.RequestLog) {
f.mu.Lock()
defer f.mu.Unlock()
f.rows = append(f.rows, e)
}
func TestLogRequestForwardsRowToStats(t *testing.T) {
env := newTestEnv(t, chatReply, nil)
stats := &fakeStats{}
env.api.Stats = stats
rec := call(env.api.chatCompletions, allowAllKey(), http.MethodPost,
`{"model":"or/openai/gpt-4o","messages":[{"role":"user","content":"hi"}]}`)
if rec.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", rec.Code, rec.Body)
}
logged := env.logs.wait(t)
stats.mu.Lock()
defer stats.mu.Unlock()
if len(stats.rows) != 1 {
t.Fatalf("stats rows = %d, want 1", len(stats.rows))
}
got := stats.rows[0]
if got.ModelName != logged.ModelName || got.TotalTokens != logged.TotalTokens || got.Cost != logged.Cost || got.KeyPrefix != "sk-test" {
t.Errorf("stats row = %+v, log row = %+v", got, logged)
}
}