- A pipeline over the facts about a list of songs (up to 5 genres, up to 8 artist names, how many songs, a few words about the rules of a smart playlist, the language). A local namer builds a name from the genres and the artists on this server and is always there; Claude is asked first only when an admin turned the new anthropic provider on with an API key. A failure or a refusal gives the local name. What Claude answers is kept under a hash of the facts, so the same question is not asked or paid twice.
- The anthropic provider: a write-only key (kept as typed, it is case-sensitive), a test that reads one model and sends no text, and a plain statement of what is sent. It uses the official Go SDK with nothing taken from the environment of the server (no key, profile, base URL or extra headers), no redirects, no system headers, and an answer that is cleaned and bounded.
- POST /playlists/{id}/suggest-name, POST /mixes/{id}/suggest-name and POST /smart-playlists/{id}/suggest-name answer with a name, a description and where it came from.
566 lines
19 KiB
Go
566 lines
19 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// memStore is an in-memory Store.
|
|
type memStore struct {
|
|
rows map[string]Settings
|
|
err error
|
|
}
|
|
|
|
func newMemStore() *memStore { return &memStore{rows: map[string]Settings{}} }
|
|
|
|
func (m *memStore) Get(_ context.Context, name string) (Settings, bool, error) {
|
|
s, ok := m.rows[name]
|
|
return s, ok, m.err
|
|
}
|
|
|
|
func (m *memStore) List(context.Context) ([]Settings, error) {
|
|
var out []Settings
|
|
for _, s := range m.rows {
|
|
out = append(out, s)
|
|
}
|
|
return out, m.err
|
|
}
|
|
|
|
func (m *memStore) Save(_ context.Context, s Settings) error {
|
|
if m.err != nil {
|
|
return m.err
|
|
}
|
|
m.rows[s.Provider] = s
|
|
return nil
|
|
}
|
|
|
|
const goodKey = "0123456789abcdef0123456789abcdef"
|
|
|
|
func boolp(b bool) *bool { return &b }
|
|
func strp(s string) *string { return &s }
|
|
func newSvc() (*Service, *memStore) {
|
|
st := newMemStore()
|
|
return NewService(st), st
|
|
}
|
|
|
|
func TestEveryProviderIsOffByDefault(t *testing.T) {
|
|
s, _ := newSvc()
|
|
infos, err := s.List(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(infos) != 7 || infos[6].Name != Anthropic || infos[0].Name != LastFM || infos[1].Name != MusicBrainz || infos[2].Name != Wikipedia || infos[3].Name != Commons || infos[4].Name != CoverArtArchive || infos[5].Name != Lidarr {
|
|
t.Fatalf("providers = %+v, want lastfm, musicbrainz, wikipedia, commons, coverartarchive, then lidarr", infos)
|
|
}
|
|
for _, in := range infos {
|
|
if in.Enabled || in.HasAPIKey {
|
|
t.Errorf("%s is on or has a key before anything was configured: %+v", in.Name, in)
|
|
}
|
|
if strings.TrimSpace(in.Sends) == "" || strings.TrimSpace(in.Notes) == "" {
|
|
t.Errorf("%s does not say what it sends or under which terms", in.Name)
|
|
}
|
|
}
|
|
if !infos[0].NeedsAPIKey || infos[1].NeedsAPIKey {
|
|
t.Errorf("needs_api_key: lastfm=%v musicbrainz=%v, want true/false", infos[0].NeedsAPIKey, infos[1].NeedsAPIKey)
|
|
}
|
|
}
|
|
|
|
func TestWhatEachProviderSendsNeverMentionsPrivateData(t *testing.T) {
|
|
s, _ := newSvc()
|
|
infos, _ := s.List(context.Background())
|
|
for _, in := range infos {
|
|
if !strings.Contains(in.Sends, "Never") {
|
|
t.Errorf("%s: the description of what is sent must say what is never sent: %q", in.Name, in.Sends)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestInfoNeverCarriesTheKey(t *testing.T) {
|
|
typ := reflect.TypeOf(Info{})
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
if name := strings.ToLower(typ.Field(i).Name); name == "apikey" || strings.Contains(name, "secret") || name == "key" {
|
|
t.Fatalf("Info has a field %q that could carry the key", typ.Field(i).Name)
|
|
}
|
|
}
|
|
s, _ := newSvc()
|
|
info, err := s.Update(context.Background(), LastFM, Patch{APIKey: strp(goodKey)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !info.HasAPIKey {
|
|
t.Error("HasAPIKey should be true after saving a key")
|
|
}
|
|
}
|
|
|
|
func TestSettingAndEnablingLastFM(t *testing.T) {
|
|
s, st := newSvc()
|
|
ctx := context.Background()
|
|
|
|
// Enabling without a key is refused.
|
|
if _, err := s.Update(ctx, LastFM, Patch{Enabled: boolp(true)}); !errors.Is(err, ErrInvalidSettings) {
|
|
t.Fatalf("enable without a key: err = %v", err)
|
|
}
|
|
if _, ok := st.rows[LastFM]; ok {
|
|
t.Error("a refused update left a row behind")
|
|
}
|
|
|
|
info, err := s.Update(ctx, LastFM, Patch{APIKey: strp(" " + strings.ToUpper(goodKey) + " ")})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !info.HasAPIKey || info.Enabled {
|
|
t.Errorf("after saving a key: %+v, want a key and still off", info)
|
|
}
|
|
if st.rows[LastFM].APIKey != goodKey {
|
|
t.Errorf("stored key = %q, want it trimmed and lower-cased", st.rows[LastFM].APIKey)
|
|
}
|
|
|
|
if info, err = s.Update(ctx, LastFM, Patch{Enabled: boolp(true)}); err != nil || !info.Enabled {
|
|
t.Fatalf("enable: %+v err=%v", info, err)
|
|
}
|
|
|
|
// Clearing the key turns the provider off with it.
|
|
info, err = s.Update(ctx, LastFM, Patch{APIKey: strp("")})
|
|
if err != nil || info.HasAPIKey || info.Enabled {
|
|
t.Fatalf("clear the key: %+v err=%v, want no key and off", info, err)
|
|
}
|
|
if st.rows[LastFM].APIKey != "" || st.rows[LastFM].Enabled {
|
|
t.Errorf("stored row after clearing: %+v", st.rows[LastFM])
|
|
}
|
|
}
|
|
|
|
func TestUpdateRejectsBadInput(t *testing.T) {
|
|
ctx := context.Background()
|
|
tests := []struct {
|
|
name string
|
|
provider string
|
|
patch Patch
|
|
}{
|
|
{"empty patch", LastFM, Patch{}},
|
|
{"key too short", LastFM, Patch{APIKey: strp("abc123")}},
|
|
{"key not hex", LastFM, Patch{APIKey: strp(strings.Repeat("z", 32))}},
|
|
{"key too long", LastFM, Patch{APIKey: strp(goodKey + "0")}},
|
|
{"a key for a provider without keys", MusicBrainz, Patch{APIKey: strp(goodKey)}},
|
|
{"contact with parentheses", MusicBrainz, Patch{Contact: strp("me (at) example.org")}},
|
|
{"contact with a line break", MusicBrainz, Patch{Contact: strp("a\nb")}},
|
|
{"contact too long", MusicBrainz, Patch{Contact: strp(strings.Repeat("x", 201))}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s, st := newSvc()
|
|
if _, err := s.Update(ctx, tt.provider, tt.patch); !errors.Is(err, ErrInvalidSettings) {
|
|
t.Fatalf("err = %v, want ErrInvalidSettings", err)
|
|
}
|
|
if len(st.rows) != 0 {
|
|
t.Error("a rejected update was stored")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnknownProvider(t *testing.T) {
|
|
s, _ := newSvc()
|
|
if _, err := s.Update(context.Background(), "spotify", Patch{Enabled: boolp(true)}); !errors.Is(err, ErrUnknownProvider) {
|
|
t.Fatalf("err = %v, want ErrUnknownProvider", err)
|
|
}
|
|
if _, _, err := s.Active(context.Background(), "spotify"); !errors.Is(err, ErrUnknownProvider) {
|
|
t.Fatalf("Active: err = %v, want ErrUnknownProvider", err)
|
|
}
|
|
}
|
|
|
|
func TestMusicBrainzNeedsNoKeyAndTakesAContact(t *testing.T) {
|
|
s, st := newSvc()
|
|
ctx := context.Background()
|
|
info, err := s.Update(ctx, MusicBrainz, Patch{Enabled: boolp(true), Contact: strp(" https://example.org/me ")})
|
|
if err != nil || !info.Enabled || info.Contact != "https://example.org/me" {
|
|
t.Fatalf("%+v err=%v", info, err)
|
|
}
|
|
if st.rows[MusicBrainz].Contact != "https://example.org/me" {
|
|
t.Errorf("stored contact = %q", st.rows[MusicBrainz].Contact)
|
|
}
|
|
// An empty contact goes back to the default (the project's address).
|
|
info, err = s.Update(ctx, MusicBrainz, Patch{Contact: strp("")})
|
|
if err != nil || info.Contact != "" || !info.Enabled {
|
|
t.Fatalf("clear the contact: %+v err=%v", info, err)
|
|
}
|
|
}
|
|
|
|
func TestActiveOnlyReturnsAUsableProvider(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
|
|
if _, ok, err := s.Active(ctx, LastFM); err != nil || ok {
|
|
t.Fatalf("nothing configured: ok=%v err=%v", ok, err)
|
|
}
|
|
_, _ = s.Update(ctx, LastFM, Patch{APIKey: strp(goodKey)})
|
|
if _, ok, _ := s.Active(ctx, LastFM); ok {
|
|
t.Error("a provider with a key but turned off was reported active")
|
|
}
|
|
_, _ = s.Update(ctx, LastFM, Patch{Enabled: boolp(true)})
|
|
cfg, ok, err := s.Active(ctx, LastFM)
|
|
if err != nil || !ok || cfg.APIKey != goodKey {
|
|
t.Fatalf("active: %+v ok=%v err=%v", cfg, ok, err)
|
|
}
|
|
_, _ = s.Update(ctx, LastFM, Patch{Enabled: boolp(false)})
|
|
if _, ok, _ := s.Active(ctx, LastFM); ok {
|
|
t.Error("a provider turned off was reported active")
|
|
}
|
|
|
|
// MusicBrainz is usable as soon as it is on.
|
|
_, _ = s.Update(ctx, MusicBrainz, Patch{Enabled: boolp(true)})
|
|
if _, ok, _ := s.Active(ctx, MusicBrainz); !ok {
|
|
t.Error("musicbrainz on, no key needed: should be active")
|
|
}
|
|
}
|
|
|
|
func TestListMergesStoredRowsInAFixedOrder(t *testing.T) {
|
|
s, st := newSvc()
|
|
st.rows[MusicBrainz] = Settings{Provider: MusicBrainz, Enabled: true, Contact: "c", UpdatedAt: time.Unix(1, 0)}
|
|
st.rows["stale-unknown"] = Settings{Provider: "stale-unknown", Enabled: true} // a row of a provider that no longer exists
|
|
infos, err := s.List(context.Background())
|
|
if err != nil || len(infos) != 7 {
|
|
t.Fatalf("infos = %+v err=%v, want exactly the known providers", infos, err)
|
|
}
|
|
if infos[0].Name != LastFM || infos[1].Name != MusicBrainz || !infos[1].Enabled || infos[1].Contact != "c" {
|
|
t.Errorf("infos = %+v", infos)
|
|
}
|
|
}
|
|
|
|
func TestStoreErrorsPassThrough(t *testing.T) {
|
|
boom := errors.New("db down")
|
|
s := NewService(&memStore{rows: map[string]Settings{}, err: boom})
|
|
if _, err := s.List(context.Background()); !errors.Is(err, boom) {
|
|
t.Errorf("List: %v", err)
|
|
}
|
|
if _, err := s.Update(context.Background(), LastFM, Patch{APIKey: strp(goodKey)}); !errors.Is(err, boom) {
|
|
t.Errorf("Update: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUserAgentIsIdentifiableAndSafe(t *testing.T) {
|
|
if got := UserAgent("1.2.3", ""); got != "Sonda/1.2.3 ( https://codeberg.org/squidd/sonda )" {
|
|
t.Errorf("default contact: %q", got)
|
|
}
|
|
if got := UserAgent("1.2.3", " https://example.org/me "); got != "Sonda/1.2.3 ( https://example.org/me )" {
|
|
t.Errorf("with a contact: %q", got)
|
|
}
|
|
if got := UserAgent("", ""); !strings.HasPrefix(got, "Sonda/dev ( ") {
|
|
t.Errorf("no version: %q", got)
|
|
}
|
|
// Whatever is passed in, the header stays one line and keeps its shape.
|
|
for _, evil := range []string{"a\r\nX-Evil: 1", "a ) b ( c", "\x00\x07"} {
|
|
ua := UserAgent("1.0\r\nX: y", evil)
|
|
if strings.ContainsAny(ua, "\r\n\x00") || strings.Count(ua, "(") != 1 || strings.Count(ua, ")") != 1 {
|
|
t.Errorf("UserAgent(%q) = %q is not a safe single-line header", evil, ua)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An empty api_key on a provider that has no keys must not switch it off.
|
|
func TestAnEmptyKeyDoesNotTurnOffAProviderThatNeedsNone(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
if _, err := s.Update(ctx, MusicBrainz, Patch{Enabled: boolp(true)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := s.Update(ctx, MusicBrainz, Patch{Contact: strp("https://example.org/me"), APIKey: strp("")})
|
|
if err != nil || !info.Enabled {
|
|
t.Fatalf("%+v err=%v: musicbrainz needs no key, so an empty one must leave it on", info, err)
|
|
}
|
|
if _, ok, _ := s.Active(ctx, MusicBrainz); !ok {
|
|
t.Error("musicbrainz stopped being active")
|
|
}
|
|
}
|
|
|
|
// ---- testing a provider ----
|
|
|
|
func TestAProviderCanBeTestedBeforeItIsSwitchedOn(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
var gotKey string
|
|
s.SetChecker(LastFM, func(_ context.Context, key string) error { gotKey = key; return nil })
|
|
|
|
// No key yet: say so, and ask nobody.
|
|
res, err := s.Test(ctx, LastFM)
|
|
if err != nil || res.OK || res.Status != TestNoKey || gotKey != "" {
|
|
t.Fatalf("without a key: %+v err=%v key=%q", res, err, gotKey)
|
|
}
|
|
|
|
if _, err := s.Update(ctx, LastFM, Patch{APIKey: strp(goodKey)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res, err = s.Test(ctx, LastFM) // still off: an explicit test is allowed
|
|
if err != nil || !res.OK || res.Status != TestOK || gotKey != goodKey {
|
|
t.Fatalf("with a key: %+v err=%v key=%q", res, err, gotKey)
|
|
}
|
|
}
|
|
|
|
func TestTestMapsWhatWentWrong(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
err error
|
|
want string
|
|
}{
|
|
{ErrKeyRefused, TestKeyRefused},
|
|
{ErrRateLimited, TestRateLimited},
|
|
{ErrUnreachable, TestUnreachable},
|
|
{errors.New("something else"), TestFailed},
|
|
} {
|
|
s, _ := newSvc()
|
|
_, _ = s.Update(context.Background(), LastFM, Patch{APIKey: strp(goodKey)})
|
|
s.SetChecker(LastFM, func(context.Context, string) error { return tt.err })
|
|
res, err := s.Test(context.Background(), LastFM)
|
|
if err != nil || res.OK || res.Status != tt.want {
|
|
t.Errorf("%v: %+v err=%v, want status %s", tt.err, res, err, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTestErrors(t *testing.T) {
|
|
s, st := newSvc()
|
|
ctx := context.Background()
|
|
if _, err := s.Test(ctx, "spotify"); !errors.Is(err, ErrUnknownProvider) {
|
|
t.Errorf("unknown provider: %v", err)
|
|
}
|
|
if _, err := s.Test(ctx, MusicBrainz); !errors.Is(err, ErrNoTest) {
|
|
t.Errorf("a provider without a test: %v", err)
|
|
}
|
|
s.SetChecker(LastFM, func(context.Context, string) error { return nil })
|
|
boom := errors.New("db down")
|
|
st.err = boom
|
|
if _, err := s.Test(ctx, LastFM); !errors.Is(err, boom) {
|
|
t.Errorf("a storage error: %v", err)
|
|
}
|
|
}
|
|
|
|
// A checker must be handed the stored key and nothing in the answer may carry it.
|
|
func TestTestResultNeverCarriesTheKey(t *testing.T) {
|
|
s, _ := newSvc()
|
|
_, _ = s.Update(context.Background(), LastFM, Patch{APIKey: strp(goodKey)})
|
|
s.SetChecker(LastFM, func(context.Context, string) error { return errors.New("failed with " + goodKey) })
|
|
res, _ := s.Test(context.Background(), LastFM)
|
|
if strings.Contains(res.Status, goodKey) {
|
|
t.Errorf("the result repeats the key: %+v", res)
|
|
}
|
|
}
|
|
|
|
// MusicBrainz asks for a contact, which an admin can change: the User-Agent
|
|
// follows the stored value, falls back to the project address, and never fails.
|
|
func TestUserAgentForFollowsTheStoredContact(t *testing.T) {
|
|
s, st := newSvc()
|
|
ua := s.UserAgentFor(MusicBrainz, "1.2.3")
|
|
if got := ua(); got != "Sonda/1.2.3 ( "+ProjectURL+" )" {
|
|
t.Errorf("default = %q", got)
|
|
}
|
|
if _, err := s.Update(context.Background(), MusicBrainz, Patch{Contact: strp("https://example.org/me")}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ua(); got != "Sonda/1.2.3 ( https://example.org/me )" {
|
|
t.Errorf("after the change = %q", got)
|
|
}
|
|
st.err = errors.New("db down")
|
|
if got := ua(); got != "Sonda/1.2.3 ( "+ProjectURL+" )" {
|
|
t.Errorf("with the store failing = %q, want the project address", got)
|
|
}
|
|
}
|
|
|
|
// ---- purge ----
|
|
|
|
func TestPurgeRemovesAProvidersData(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
var asked string
|
|
s.SetPurger(func(_ context.Context, name string) (Purged, error) {
|
|
asked = name
|
|
return Purged{SimilarArtists: 3, Tags: 7, Artists: 2}, nil
|
|
})
|
|
got, err := s.Purge(ctx, LastFM)
|
|
if err != nil || got != (Purged{SimilarArtists: 3, Tags: 7, Artists: 2}) || asked != LastFM {
|
|
t.Fatalf("purge = %+v err=%v asked=%q", got, err, asked)
|
|
}
|
|
// It works whether the provider is on or off, and does not change that.
|
|
if _, err := s.Update(ctx, LastFM, Patch{APIKey: strp(goodKey), Enabled: boolp(true)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.Purge(ctx, LastFM); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok, _ := s.Active(ctx, LastFM); !ok {
|
|
t.Error("a purge switched the provider off")
|
|
}
|
|
}
|
|
|
|
func TestPurgeErrors(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
if _, err := s.Purge(ctx, "spotify"); !errors.Is(err, ErrUnknownProvider) {
|
|
t.Errorf("unknown provider: %v", err)
|
|
}
|
|
if _, err := s.Purge(ctx, LastFM); err == nil {
|
|
t.Error("a purge with nothing to purge with did not fail")
|
|
}
|
|
boom := errors.New("db down")
|
|
s.SetPurger(func(context.Context, string) (Purged, error) { return Purged{}, boom })
|
|
if _, err := s.Purge(ctx, LastFM); !errors.Is(err, boom) {
|
|
t.Errorf("a storage error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestWikipediaIsAProviderThatNeedsNoKey(t *testing.T) {
|
|
svc, _ := newSvc()
|
|
infos, err := svc.List(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var wp *Info
|
|
for i := range infos {
|
|
if infos[i].Name == Wikipedia {
|
|
wp = &infos[i]
|
|
}
|
|
}
|
|
if wp == nil {
|
|
t.Fatalf("providers = %+v, want wikipedia among them", infos)
|
|
}
|
|
if wp.Enabled || wp.NeedsAPIKey || wp.Sends == "" || wp.Notes == "" {
|
|
t.Errorf("wikipedia = %+v: off by default, no key, and it says what is sent and under which licence", wp)
|
|
}
|
|
// Off until an admin switches it on; no key needed to do so.
|
|
if _, active, _ := svc.Active(context.Background(), Wikipedia); active {
|
|
t.Error("active before anyone switched it on")
|
|
}
|
|
on := true
|
|
if _, err := svc.Update(context.Background(), Wikipedia, Patch{Enabled: &on}); err != nil {
|
|
t.Fatalf("enable: %v", err)
|
|
}
|
|
if _, active, _ := svc.Active(context.Background(), Wikipedia); !active {
|
|
t.Error("not active after being switched on")
|
|
}
|
|
key := "0123456789abcdef0123456789abcdef"
|
|
if _, err := svc.Update(context.Background(), Wikipedia, Patch{APIKey: &key}); !errors.Is(err, ErrInvalidSettings) {
|
|
t.Errorf("a key for a provider that has none: err = %v", err)
|
|
}
|
|
contact := "https://example.org/me"
|
|
if info, err := svc.Update(context.Background(), Wikipedia, Patch{Contact: &contact}); err != nil || info.Contact != contact {
|
|
t.Errorf("contact: %+v err=%v", info, err)
|
|
}
|
|
}
|
|
|
|
func TestCommonsIsAProviderThatNeedsNoKeyAndSaysWhatItSendsAndKeeps(t *testing.T) {
|
|
svc, _ := newSvc()
|
|
infos, _ := svc.List(context.Background())
|
|
var c *Info
|
|
for i := range infos {
|
|
if infos[i].Name == Commons {
|
|
c = &infos[i]
|
|
}
|
|
}
|
|
if c == nil {
|
|
t.Fatalf("providers = %+v, want commons among them", infos)
|
|
}
|
|
if c.Enabled || c.NeedsAPIKey || c.Sends == "" || c.Notes == "" {
|
|
t.Errorf("commons = %+v: off by default, no key, and it says what is sent and under which licences", c)
|
|
}
|
|
for _, want := range []string{"MusicBrainz", "free licences"} {
|
|
if !strings.Contains(c.Sends+c.Notes, want) {
|
|
t.Errorf("the text does not say %q: %q %q", want, c.Sends, c.Notes)
|
|
}
|
|
}
|
|
on := true
|
|
if _, err := svc.Update(context.Background(), Commons, Patch{Enabled: &on}); err != nil {
|
|
t.Fatalf("enable: %v", err)
|
|
}
|
|
if _, active, _ := svc.Active(context.Background(), Commons); !active {
|
|
t.Error("not active after being switched on")
|
|
}
|
|
}
|
|
|
|
func TestMusicBrainzSaysItIsToldTheArtistAndTheTitleOfAnAlbumWithNoId(t *testing.T) {
|
|
svc, _ := newSvc()
|
|
infos, _ := svc.List(context.Background())
|
|
for _, in := range infos {
|
|
if in.Name != MusicBrainz {
|
|
continue
|
|
}
|
|
for _, want := range []string{"name of the album's artist and its title", "never an artist name"} {
|
|
if !strings.Contains(in.Sends, want) {
|
|
t.Errorf("the text does not say %q: %q", want, in.Sends)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
t.Fatal("no musicbrainz provider")
|
|
}
|
|
|
|
func TestCoverArtArchiveIsAProviderThatNeedsNoKeyAndSaysWhatItSends(t *testing.T) {
|
|
svc, _ := newSvc()
|
|
infos, _ := svc.List(context.Background())
|
|
var c *Info
|
|
for i := range infos {
|
|
if infos[i].Name == CoverArtArchive {
|
|
c = &infos[i]
|
|
}
|
|
}
|
|
if c == nil {
|
|
t.Fatalf("providers = %+v", infos)
|
|
}
|
|
if c.Enabled || c.NeedsAPIKey {
|
|
t.Errorf("coverartarchive = %+v: off by default, no key", c)
|
|
}
|
|
for _, want := range []string{"release group", "album", "its title go to MusicBrainz"} {
|
|
if !strings.Contains(c.Sends+" "+c.Notes, want) {
|
|
t.Errorf("the text does not say %q: %q %q", want, c.Sends, c.Notes)
|
|
}
|
|
}
|
|
on := true
|
|
if _, err := svc.Update(context.Background(), CoverArtArchive, Patch{Enabled: &on}); err != nil {
|
|
t.Fatalf("enable: %v", err)
|
|
}
|
|
if _, active, _ := svc.Active(context.Background(), CoverArtArchive); !active {
|
|
t.Error("not active after being switched on")
|
|
}
|
|
}
|
|
|
|
func TestAnthropicKeyKeepsItsCaseAndNeedsToLookLikeOne(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
key := "sk-ant-api03-AbCdEfGhIjKlMnOpQrStUv_wxYZ-0123456789"
|
|
info, err := s.Update(ctx, Anthropic, Patch{APIKey: &key})
|
|
if err != nil || !info.HasAPIKey || !info.NeedsAPIKey {
|
|
t.Fatalf("%+v, %v", info, err)
|
|
}
|
|
stored, err := s.Stored(ctx, Anthropic)
|
|
if err != nil || stored.APIKey != key {
|
|
t.Errorf("stored key = %q, %v: it is case-sensitive and must be kept as typed", stored.APIKey, err)
|
|
}
|
|
for _, bad := range []string{"hello", "sk-ant-", "sk-ant-short", "sk-proj-" + strings.Repeat("a", 40), "sk-ant-" + strings.Repeat("a", 30) + " x"} {
|
|
if _, err := s.Update(ctx, Anthropic, Patch{APIKey: &bad}); !errors.Is(err, ErrInvalidSettings) {
|
|
t.Errorf("%q: err = %v, want ErrInvalidSettings", bad, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnthropicIsOffUntilItHasAKeyAndSaysWhatItSends(t *testing.T) {
|
|
s, _ := newSvc()
|
|
ctx := context.Background()
|
|
if _, err := s.Update(ctx, Anthropic, Patch{Enabled: boolp(true)}); !errors.Is(err, ErrInvalidSettings) {
|
|
t.Errorf("enabled with no key: %v", err)
|
|
}
|
|
infos, _ := s.List(ctx)
|
|
var a Info
|
|
for _, i := range infos {
|
|
if i.Name == Anthropic {
|
|
a = i
|
|
}
|
|
}
|
|
if a.Enabled || a.URL != "" {
|
|
t.Errorf("anthropic = %+v", a)
|
|
}
|
|
for _, want := range []string{"genres", "artist names", "language", "API key", "Never your users, your listens, track titles", "nothing leaves"} {
|
|
if !strings.Contains(a.Sends, want) {
|
|
t.Errorf("the text of what is sent lacks %q: %s", want, a.Sends)
|
|
}
|
|
}
|
|
}
|