Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
126 lines
5 KiB
Go
126 lines
5 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/naming"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/anthropic"
|
|
)
|
|
|
|
func TestNamingRepoKeepsAndReadsANameAndKeepsTheFirstOneWhenAskedTwice(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
r := NewNamingRepo(db)
|
|
if _, ok, err := r.Get(ctx, "k1"); err != nil || ok {
|
|
t.Fatalf("an unknown key: ok = %v, err = %v", ok, err)
|
|
}
|
|
in := naming.Suggestion{Name: "Riff senza sosta", Description: "Hard rock.", Source: naming.SourceClaude}
|
|
if err := r.Put(ctx, "k1", in, time.Unix(1_700_000_000, 0)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, ok, err := r.Get(ctx, "k1")
|
|
if err != nil || !ok || got != in {
|
|
t.Fatalf("got %+v, %v, %v", got, ok, err)
|
|
}
|
|
// Two requests that asked at once both put: the second changes nothing and is no error.
|
|
if err := r.Put(ctx, "k1", naming.Suggestion{Name: "Another", Source: naming.SourceClaude}, time.Unix(1_700_000_100, 0)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ := r.Get(ctx, "k1"); got.Name != "Riff senza sosta" {
|
|
t.Errorf("the kept name changed: %+v", got)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestNamingRepoCountsTheGenresOfSongs(t *testing.T) {
|
|
songs := []mixSong{
|
|
{"r1", "Queen", []string{"rock", "classic rock"}}, {"r2", "Queen", []string{"rock"}}, {"m1", "Slayer", []string{"metal"}}, {"n1", "Nobody", nil},
|
|
}
|
|
mixBackends(t, songs, func(t *testing.T, e *mixEnv) {
|
|
got, err := NewNamingRepo(e.db).GenreCounts(context.Background(), []int64{e.tracks["r1"], e.tracks["r2"], e.tracks["n1"]})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got["rock"] != 2 || got["classic rock"] != 1 || got["metal"] != 0 || len(got) != 2 {
|
|
t.Errorf("counts = %v", got)
|
|
}
|
|
if got, err := NewNamingRepo(e.db).GenreCounts(context.Background(), nil); err != nil || len(got) != 0 {
|
|
t.Errorf("no songs: %v, %v", got, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestUsageRepoSumsEachDayAndModelAndForgetsWhatIsOlderThanAYear(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
r := NewNamingRepo(db)
|
|
day := func(n int) time.Time { return time.Date(2026, 11, n, 15, 30, 0, 0, time.UTC) }
|
|
rec := func(at time.Time, kind, model, outcome, reason string, in, out int64) {
|
|
t.Helper()
|
|
err := r.Record(ctx, naming.Call{At: at, Kind: kind, Model: model, Outcome: outcome, Reason: reason, Usage: anthropic.Usage{Input: in, Output: out, CacheRead: 1, CacheWrite: 2}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
rec(day(12), "mix", "claude-a", naming.OutcomeMade, "", 100, 40)
|
|
rec(day(12), "smart", "claude-a", naming.OutcomeMade, "", 200, 60)
|
|
rec(day(12), "mix", "claude-a", naming.OutcomeKept, "", 0, 0)
|
|
rec(day(12), "mix", "claude-a", naming.OutcomeFailed, naming.ReasonNoName, 50, 300)
|
|
rec(day(12), "mix", "claude-b", naming.OutcomeMade, "", 10, 10)
|
|
rec(day(10), "playlist", "claude-a", naming.OutcomeFailed, naming.ReasonKey, 0, 0)
|
|
rec(day(1), "mix", "claude-a", naming.OutcomeMade, "", 7, 7)
|
|
|
|
rows, err := r.Since(ctx, day(5))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rows) != 3 {
|
|
t.Fatalf("rows = %+v, want 3: the 12th with two models and the 10th; the 1st is before", rows)
|
|
}
|
|
first := rows[0]
|
|
if !first.Day.Equal(time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC)) || first.Model != "claude-a" || first.Made != 2 || first.Kept != 1 || first.Failed != 1 ||
|
|
first.Usage != (anthropic.Usage{Input: 350, Output: 400, CacheRead: 4, CacheWrite: 8}) {
|
|
t.Errorf("first = %+v", first)
|
|
}
|
|
if rows[1].Model != "claude-b" || rows[1].Made != 1 || rows[1].Usage.Input != 10 {
|
|
t.Errorf("second = %+v", rows[1])
|
|
}
|
|
if !rows[2].Day.Equal(time.Date(2026, 11, 10, 0, 0, 0, 0, time.UTC)) || rows[2].Failed != 1 {
|
|
t.Errorf("third = %+v", rows[2])
|
|
}
|
|
if none, _ := r.Since(ctx, day(30)); len(none) != 0 {
|
|
t.Errorf("a period with no calls = %+v", none)
|
|
}
|
|
|
|
// A call more than a year after the oldest one makes the oldest go.
|
|
if err := r.Record(ctx, naming.Call{At: day(1).Add(366 * 24 * time.Hour), Kind: "mix", Model: "claude-a", Outcome: naming.OutcomeMade}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var n int
|
|
if err := db.QueryRow("SELECT COUNT(*) FROM claude_usage WHERE at < ?", day(1).Unix()+1).Scan(&n); err != nil || n != 0 {
|
|
t.Errorf("%d records older than a year are left, %v", n, err)
|
|
}
|
|
if err := db.QueryRow("SELECT COUNT(*) FROM claude_usage").Scan(&n); err != nil || n == 0 {
|
|
t.Errorf("the recent ones went too: %d, %v", n, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestUsageRepoKeepsNothingOfTheFacts(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
cols, err := db.Query("SELECT * FROM claude_usage LIMIT 0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cols.Close()
|
|
names, _ := cols.Columns()
|
|
want := []string{"id", "at", "kind", "model", "outcome", "reason", "input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens"}
|
|
if len(names) != len(want) {
|
|
t.Errorf("columns = %v, want %v: nothing but the account", names, want)
|
|
}
|
|
})
|
|
}
|