sonda/internal/naming/naming_test.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
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.
2026-11-15 17:30:29 +01:00

459 lines
17 KiB
Go

package naming
import (
"context"
"errors"
"slices"
"strings"
"testing"
"time"
"git.kmalloc.org/squid/sonda/internal/catalog"
"git.kmalloc.org/squid/sonda/internal/providers"
"git.kmalloc.org/squid/sonda/internal/providers/anthropic"
)
// ---- fakes ----
type fakeSettings struct {
on bool
key string
err error
}
func (f fakeSettings) Active(_ context.Context, name string) (providers.Settings, bool, error) {
if name != providers.Anthropic {
return providers.Settings{}, false, providers.ErrUnknownProvider
}
return providers.Settings{Provider: name, Enabled: f.on, APIKey: f.key}, f.on && f.err == nil, f.err
}
type fakeClaude struct {
calls []anthropic.Request
keys []string
res anthropic.Result
err error
}
func (f *fakeClaude) Name(_ context.Context, key string, r anthropic.Request) (anthropic.Result, error) {
f.calls = append(f.calls, r)
f.keys = append(f.keys, key)
return f.res, f.err
}
type fakeStore struct {
names map[string]Suggestion
genres map[int64][]string // a song's genres, as the files spell them
puts int
}
func newStore() *fakeStore {
return &fakeStore{names: map[string]Suggestion{}, genres: map[int64][]string{}}
}
func (f *fakeStore) Get(_ context.Context, key string) (Suggestion, bool, error) {
s, ok := f.names[key]
return s, ok, nil
}
func (f *fakeStore) Put(_ context.Context, key string, s Suggestion, _ time.Time) error {
f.puts++
f.names[key] = s
return nil
}
func (f *fakeStore) GenreCounts(_ context.Context, ids []int64) (map[string]int, error) {
out := map[string]int{}
for _, id := range ids {
for _, g := range f.genres[id] {
out[g]++
}
}
return out, nil
}
type fakeMeter struct {
calls []Call
err error
}
func (f *fakeMeter) Record(_ context.Context, c Call) error {
f.calls = append(f.calls, c)
return f.err
}
func newService(set fakeSettings, c *fakeClaude, st *fakeStore) *Service {
s := NewService(set, c, st, nil)
s.now = func() time.Time { return time.Unix(1_700_000_000, 0) }
return s
}
var rock = Facts{Kind: "mix", Genres: []string{"hard rock", "rock"}, Artists: []string{"Kiss", "AC/DC", "Van Halen"}, Tracks: 50}
// ---- the local namer ----
func TestTheLocalNameComesFromTheGenresAndTheArtists(t *testing.T) {
for name, tc := range map[string]struct {
f Facts
lang string
want, desc string
}{
"a mix, in English": {rock, "en", "Hard Rock Mix", "With Kiss, AC/DC, Van Halen."},
"a mix, in Italian": {rock, "it", "Mix Hard Rock", "Con Kiss, AC/DC, Van Halen."},
"a playlist with two genres": {Facts{Kind: "playlist", Genres: []string{"rock", "metal", "pop"}, Artists: []string{"Queen"}, Tracks: 12}, "en", "Rock & Metal", "With Queen."},
"more than three artists": {Facts{Kind: "smart", Genres: []string{"jazz"}, Artists: []string{"A", "B", "C", "D", "E"}, Tracks: 9}, "en", "Jazz", "With A, B, C and more."},
"in Italian, more than three": {Facts{Kind: "smart", Genres: []string{"jazz"}, Artists: []string{"A", "B", "C", "D"}, Tracks: 9}, "it", "Jazz", "Con A, B, C e altri."},
"no genre, two artists": {Facts{Kind: "playlist", Artists: []string{"Queen", "Kiss"}, Tracks: 4}, "en", "Queen & Kiss", "With Queen, Kiss."},
"no genre, one artist": {Facts{Kind: "playlist", Artists: []string{"Queen"}, Tracks: 4}, "en", "Queen", "With Queen."},
"no genre, many artists": {Facts{Kind: "playlist", Artists: []string{"A", "B", "C", "D"}, Tracks: 4}, "en", "A, B & more", "With A, B, C and more."},
"nothing known": {Facts{Kind: "playlist", Tracks: 3}, "en", "New playlist", "3 songs."},
"nothing known, Italian": {Facts{Kind: "playlist", Tracks: 1}, "it", "Nuova playlist", "1 brano."},
"a language that is not known": {Facts{Kind: "playlist", Tracks: 2}, "xx", "New playlist", "2 songs."},
} {
t.Run(name, func(t *testing.T) {
got := local(tc.f, tc.lang)
if got.Name != tc.want || got.Description != tc.desc || got.Source != SourceLocal {
t.Errorf("got %+v, want %q / %q", got, tc.want, tc.desc)
}
})
}
}
func TestWithoutTheProviderNothingLeavesAndAFreeNameIsGiven(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "should not be asked"}}
s := newService(fakeSettings{on: false}, c, newStore())
got, err := s.Suggest(context.Background(), rock, "en")
if err != nil || got.Source != SourceLocal || got.Name != "Hard Rock Mix" {
t.Fatalf("%+v, %v", got, err)
}
if len(c.calls) != 0 {
t.Errorf("Claude was asked with the provider off: %v", c.calls)
}
}
// ---- Claude ----
func TestClaudeIsAskedForTheFactsAndItsNameIsKept(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "Riff senza sosta", Description: "Hard rock."}}
st := newStore()
s := newService(fakeSettings{on: true, key: "sk-ant-key"}, c, st)
got, err := s.Suggest(context.Background(), rock, "it")
if err != nil || got.Source != SourceClaude || got.Name != "Riff senza sosta" || got.Description != "Hard rock." {
t.Fatalf("%+v, %v", got, err)
}
if len(c.calls) != 1 || c.keys[0] != "sk-ant-key" {
t.Fatalf("calls = %v keys = %v", c.calls, c.keys)
}
r := c.calls[0]
if r.Kind != "mix" || r.Language != "it" || r.Tracks != 50 || !slices.Equal(r.Genres, rock.Genres) || !slices.Equal(r.Artists, rock.Artists) {
t.Errorf("request = %+v", r)
}
// The same facts in the same language are not asked again.
again, err := s.Suggest(context.Background(), rock, "it")
if err != nil || again.Name != "Riff senza sosta" || again.Source != SourceClaude {
t.Fatalf("again = %+v, %v", again, err)
}
if len(c.calls) != 1 || st.puts != 1 {
t.Errorf("%d calls and %d puts for the same facts", len(c.calls), st.puts)
}
// Another language, or other facts, are another question.
if _, err := s.Suggest(context.Background(), rock, "en"); err != nil {
t.Fatal(err)
}
other := rock
other.Artists = []string{"Kiss"}
if _, err := s.Suggest(context.Background(), other, "it"); err != nil {
t.Fatal(err)
}
if len(c.calls) != 3 {
t.Errorf("%d calls, want 3", len(c.calls))
}
}
func TestTheSongCountAloneDoesNotMakeANewQuestion(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "N"}}
s := newService(fakeSettings{on: true, key: "sk-ant-key"}, c, newStore())
a, b := rock, rock
b.Tracks = 37
_, _ = s.Suggest(context.Background(), a, "en")
_, _ = s.Suggest(context.Background(), b, "en")
if len(c.calls) != 1 {
t.Errorf("%d calls: a playlist that gains a song keeps its name", len(c.calls))
}
}
func TestWhenClaudeFailsTheFreeNameIsGivenAndNothingIsKept(t *testing.T) {
for name, err := range map[string]error{
"no name": anthropic.ErrNoName,
"key refused": providers.ErrKeyRefused,
"rate limited": providers.ErrRateLimited,
"unreachable": providers.ErrUnreachable,
"deadline": context.DeadlineExceeded,
} {
t.Run(name, func(t *testing.T) {
c := &fakeClaude{err: err}
st := newStore()
s := newService(fakeSettings{on: true, key: "sk-ant-key"}, c, st)
got, e := s.Suggest(context.Background(), rock, "en")
if e != nil || got.Source != SourceLocal || got.Name != "Hard Rock Mix" {
t.Fatalf("%+v, %v", got, e)
}
if st.puts != 0 {
t.Error("a failed answer was kept")
}
})
}
}
func TestAProviderThatCannotBeReadDoesNotStopAName(t *testing.T) {
s := newService(fakeSettings{err: errors.New("db down")}, &fakeClaude{}, newStore())
got, err := s.Suggest(context.Background(), rock, "en")
if err != nil || got.Source != SourceLocal {
t.Errorf("%+v, %v", got, err)
}
}
func TestFactsAreCutToWhatIsSent(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "N"}}
s := newService(fakeSettings{on: true, key: "sk-ant-key"}, c, newStore())
many := Facts{Kind: "playlist", Tracks: 500, Hint: strings.Repeat("h", 1000)}
for i := 0; i < 20; i++ {
many.Genres = append(many.Genres, "genre "+string(rune('a'+i)))
many.Artists = append(many.Artists, "Artist "+string(rune('a'+i)))
}
if _, err := s.Suggest(context.Background(), many, "en"); err != nil {
t.Fatal(err)
}
r := c.calls[0]
if len(r.Genres) != anthropic.MaxGenres || len(r.Artists) != anthropic.MaxArtists || len(r.Hint) > anthropic.MaxHintLen {
t.Errorf("sent %d genres, %d artists, a hint of %d", len(r.Genres), len(r.Artists), len(r.Hint))
}
}
func TestAnUnknownLanguageIsEnglishAndLanguageCodesAreNormalised(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "N"}}
s := newService(fakeSettings{on: true, key: "sk-ant-key"}, c, newStore())
for _, lang := range []string{"IT", " it ", "it-IT"} {
_, _ = s.Suggest(context.Background(), rock, lang)
}
_, _ = s.Suggest(context.Background(), rock, "klingon")
if len(c.calls) != 2 || c.calls[0].Language != "it" || c.calls[1].Language != "en" {
t.Errorf("languages asked = %+v", c.calls)
}
}
// ---- the facts of a list of songs ----
func track(id int64, artists ...string) catalog.Track {
t := catalog.Track{ID: id, Title: "t"}
for i, a := range artists {
t.Artists = append(t.Artists, catalog.ArtistRef{ID: int64(i + 1), Name: a})
}
return t
}
func TestFactsOfSongsAreTheMostCommonArtistsAndGenres(t *testing.T) {
st := newStore()
st.genres = map[int64][]string{1: {"Rock", "rock"}, 2: {"Hard Rock"}, 3: {"jazz+funk"}, 4: {"jazz-funk"}, 5: {"other"}, 6: {"hard rock"}}
songs := []catalog.Track{
track(1, "Kiss"), track(2, "Kiss"), track(3, "AC/DC"), track(4, "Unknown Artist"), track(5, "Various Artists"), track(6, "Kiss", "AC/DC"),
}
s := newService(fakeSettings{}, &fakeClaude{}, st)
f, err := s.FactsOf(context.Background(), "playlist", songs, "never played")
if err != nil {
t.Fatal(err)
}
if !slices.Equal(f.Artists, []string{"Kiss", "AC/DC"}) {
t.Errorf("artists = %v: the commonest first, and not the names that stand for nobody", f.Artists)
}
if want := []string{"hard rock", "jazz funk", "rock"}; !slices.Equal(f.Genres, want) {
t.Errorf("genres = %v, want %v: spellings are one, a tag that is no genre is out, and a tie goes to the name", f.Genres, want)
}
if f.Kind != "playlist" || f.Tracks != 6 || f.Hint != "never played" {
t.Errorf("facts = %+v", f)
}
}
func TestFactsOfNoSongs(t *testing.T) {
s := newService(fakeSettings{}, &fakeClaude{}, newStore())
f, err := s.FactsOf(context.Background(), "playlist", nil, "")
if err != nil || f.Tracks != 0 || len(f.Genres) != 0 || len(f.Artists) != 0 {
t.Errorf("%+v, %v", f, err)
}
}
// ---- what is asked of Claude is recorded ----
func metered(set fakeSettings, c *fakeClaude) (*Service, *fakeMeter) {
m := &fakeMeter{}
s := newService(set, c, newStore())
s.SetMeter(m)
return s, m
}
func TestEveryCallToClaudeIsRecordedWithItsTokens(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "N", Usage: anthropic.Usage{Input: 120, Output: 45}}}
s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c)
if _, err := s.Suggest(context.Background(), rock, "en"); err != nil {
t.Fatal(err)
}
if len(m.calls) != 1 {
t.Fatalf("calls recorded = %+v", m.calls)
}
got := m.calls[0]
if got.Outcome != OutcomeMade || got.Kind != "mix" || got.Model != anthropic.Model || got.Usage != (anthropic.Usage{Input: 120, Output: 45}) || got.Reason != "" {
t.Errorf("recorded %+v", got)
}
if !got.At.Equal(time.Unix(1_700_000_000, 0)) {
t.Errorf("at = %v", got.At)
}
}
func TestANameTakenFromTheKeptOnesIsRecordedAsSavedAndCostsNothing(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "N", Usage: anthropic.Usage{Input: 100, Output: 40}}}
s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c)
_, _ = s.Suggest(context.Background(), rock, "en")
_, _ = s.Suggest(context.Background(), rock, "en")
if len(m.calls) != 2 || m.calls[1].Outcome != OutcomeKept || m.calls[1].Usage != (anthropic.Usage{}) {
t.Errorf("recorded %+v", m.calls)
}
}
func TestAFailureIsRecordedWithItsReasonAndTheTokensThatWereBilled(t *testing.T) {
for name, tc := range map[string]struct {
err error
reason string
usage anthropic.Usage
}{
"no name, but billed": {anthropic.ErrNoName, ReasonNoName, anthropic.Usage{Input: 90, Output: 300}},
"key refused": {providers.ErrKeyRefused, ReasonKey, anthropic.Usage{}},
"rate limited": {providers.ErrRateLimited, ReasonRate, anthropic.Usage{}},
"unreachable": {providers.ErrUnreachable, ReasonUnreachable, anthropic.Usage{}},
"anything else": {context.DeadlineExceeded, ReasonOther, anthropic.Usage{}},
} {
t.Run(name, func(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Usage: tc.usage}, err: tc.err}
s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c)
if got, err := s.Suggest(context.Background(), rock, "en"); err != nil || got.Source != SourceLocal {
t.Fatalf("%+v, %v", got, err)
}
if len(m.calls) != 1 || m.calls[0].Outcome != OutcomeFailed || m.calls[0].Reason != tc.reason || m.calls[0].Usage != tc.usage {
t.Errorf("recorded %+v, want a failure for %q with %+v", m.calls, tc.reason, tc.usage)
}
})
}
}
func TestNothingIsRecordedWhenClaudeIsNotAsked(t *testing.T) {
s, m := metered(fakeSettings{on: false}, &fakeClaude{})
_, _ = s.Suggest(context.Background(), rock, "en")
_, _ = s.Suggest(context.Background(), Facts{Kind: "playlist"}, "en") // nothing to say, nothing asked
if len(m.calls) != 0 {
t.Errorf("recorded %+v", m.calls)
}
}
func TestAMeterThatFailsDoesNotStopTheName(t *testing.T) {
c := &fakeClaude{res: anthropic.Result{Name: "N"}}
s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c)
m.err = errors.New("db down")
got, err := s.Suggest(context.Background(), rock, "en")
if err != nil || got.Name != "N" || got.Source != SourceClaude {
t.Errorf("%+v, %v", got, err)
}
}
// ---- the report of what it costs ----
func TestTheReportSumsTheDaysAndPricesTheTokens(t *testing.T) {
d := func(day int) time.Time { return time.Date(2026, 11, day, 0, 0, 0, 0, time.UTC) }
rows := []DayRow{
{Day: d(12), Model: anthropic.Model, Made: 3, Kept: 5, Failed: 1, Usage: anthropic.Usage{Input: 3000, Output: 1500}},
{Day: d(10), Model: anthropic.Model, Made: 1, Usage: anthropic.Usage{Input: 1000, Output: 500}},
}
r := Summarize(rows, 30, anthropic.Model)
if r.Days != 30 || r.Model != anthropic.Model || !r.Estimate {
t.Errorf("report = %+v", r)
}
if r.Totals.Made != 4 || r.Totals.Kept != 5 || r.Totals.Failed != 1 || r.Totals.Usage.Input != 4000 || r.Totals.Usage.Output != 2000 {
t.Errorf("totals = %+v", r.Totals)
}
// 4000 tokens in and 2000 out at $4 and $20 per million: $0.016 + $0.040.
if r.Totals.CostUSD < 0.05599 || r.Totals.CostUSD > 0.05601 || r.Unpriced {
t.Errorf("cost = %v unpriced = %v", r.Totals.CostUSD, r.Unpriced)
}
if len(r.Daily) != 2 || r.Daily[0].Date != "2026-11-12" || r.Daily[1].Date != "2026-11-10" || r.Daily[0].Made != 3 {
t.Errorf("daily = %+v: the newest day first", r.Daily)
}
if got := r.Daily[1].CostUSD; got < 0.01399 || got > 0.01401 {
t.Errorf("a day's cost = %v", got)
}
}
func TestTwoModelsOnTheSameDayAreOneDayAndAModelWithNoPriceIsSaidSo(t *testing.T) {
day := time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC)
r := Summarize([]DayRow{
{Day: day, Model: anthropic.Model, Made: 1, Usage: anthropic.Usage{Input: 1000, Output: 500}},
{Day: day, Model: "claude-future", Made: 2, Usage: anthropic.Usage{Input: 9000, Output: 9000}},
}, 7, anthropic.Model)
if len(r.Daily) != 1 || r.Daily[0].Made != 3 || r.Daily[0].Usage.Input != 10000 {
t.Fatalf("daily = %+v", r.Daily)
}
if !r.Unpriced || r.Totals.CostUSD < 0.01399 || r.Totals.CostUSD > 0.01401 {
t.Errorf("cost = %v unpriced = %v: only the model that has a price is counted, and the report says there is another", r.Totals.CostUSD, r.Unpriced)
}
}
func TestAReportWithNoCallsIsEmptyNotMissing(t *testing.T) {
r := Summarize(nil, 30, anthropic.Model)
if r.Daily == nil || len(r.Daily) != 0 || r.Totals.CostUSD != 0 || r.Unpriced {
t.Errorf("report = %+v", r)
}
}
type fakeLedger struct {
fakeMeter
rows []DayRow
since time.Time
}
func (f *fakeLedger) Since(_ context.Context, since time.Time) ([]DayRow, error) {
f.since = since
return f.rows, nil
}
func TestTheServiceReadsTheLedgerFromTheStartOfThePeriod(t *testing.T) {
l := &fakeLedger{rows: []DayRow{{Day: time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC), Model: anthropic.Model, Made: 2}}}
s := newService(fakeSettings{}, &fakeClaude{}, newStore())
s.SetLedger(l)
r, err := s.Report(context.Background(), 30)
if err != nil || r.Totals.Made != 2 {
t.Fatalf("%+v, %v", r, err)
}
if want := s.now().Add(-30 * 24 * time.Hour); !l.since.Equal(want) {
t.Errorf("since = %v, want %v", l.since, want)
}
// The ledger is also what records.
s.SetLedger(l)
_, _ = newServiceWithLedger(l).Suggest(context.Background(), rock, "en")
if _, err := s.Report(context.Background(), 0); !errors.Is(err, ErrInvalidDays) {
t.Errorf("0 days: %v", err)
}
if _, err := s.Report(context.Background(), MaxReportDays+1); !errors.Is(err, ErrInvalidDays) {
t.Errorf("too many days: %v", err)
}
}
func newServiceWithLedger(l *fakeLedger) *Service {
s := newService(fakeSettings{on: true, key: "sk-ant-key"}, &fakeClaude{res: anthropic.Result{Name: "N"}}, newStore())
s.SetLedger(l)
return s
}
func TestAServiceWithNoLedgerGivesAnEmptyReport(t *testing.T) {
s := newService(fakeSettings{}, &fakeClaude{}, newStore())
r, err := s.Report(context.Background(), 30)
if err != nil || len(r.Daily) != 0 {
t.Errorf("%+v, %v", r, err)
}
}