coverart and tags each carried a line-for-line copy of the provider registry and the settings service: reconcile at boot, seal and reveal API keys, bump the sources version when the enabled set or the registered set changes. A fix to either had to be made twice. internal/providers now holds Registry, Service (over a small Store interface) and EnabledAs[T]. Each family keeps its own tables, provider set and capabilities: a 40-line Store adapter over its sqlc queries and its capability views (EnabledAlbumProviders, EnabledTrackTagProviders). Provider, ProviderSettings, TestableProvider, ProviderInfo and ProviderUpdatePatch stay as the families' names for the shared types. The service has unit tests over an in-memory store. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
5.1 KiB
Go
162 lines
5.1 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
)
|
|
|
|
type fakeProvider struct {
|
|
id string
|
|
defaultOn bool
|
|
configured Settings
|
|
}
|
|
|
|
func (p *fakeProvider) ID() string { return p.id }
|
|
func (p *fakeProvider) DisplayName() string { return p.id }
|
|
func (p *fakeProvider) RequiresAPIKey() bool { return false }
|
|
func (p *fakeProvider) DefaultEnabled() bool { return p.defaultOn }
|
|
func (p *fakeProvider) Configure(s Settings) error { p.configured = s; return nil }
|
|
|
|
// fetcher stands in for a family capability such as AlbumCoverProvider.
|
|
type fetcher interface {
|
|
Provider
|
|
Fetch()
|
|
}
|
|
|
|
type fetchingProvider struct{ fakeProvider }
|
|
|
|
func (*fetchingProvider) Fetch() {}
|
|
|
|
// memStore is a Store in memory, standing in for a family's tables.
|
|
type memStore struct {
|
|
rows map[string]Row
|
|
order []string
|
|
version int32
|
|
hash string
|
|
}
|
|
|
|
func newMemStore() *memStore { return &memStore{rows: map[string]Row{}} }
|
|
|
|
func (m *memStore) EnsureRow(_ context.Context, id string, enabled bool, _ int32) error {
|
|
if _, ok := m.rows[id]; !ok {
|
|
m.rows[id] = Row{ProviderID: id, Enabled: enabled}
|
|
m.order = append(m.order, id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *memStore) List(context.Context) ([]Row, error) {
|
|
out := make([]Row, 0, len(m.order))
|
|
for _, id := range m.order {
|
|
out = append(out, m.rows[id])
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *memStore) Save(_ context.Context, id string, enabled, keyChanged bool, key *string) error {
|
|
r := m.rows[id]
|
|
r.Enabled = enabled
|
|
if keyChanged {
|
|
r.APIKey = key
|
|
}
|
|
m.rows[id] = r
|
|
return nil
|
|
}
|
|
|
|
func (m *memStore) Version(context.Context) (int32, error) { return m.version, nil }
|
|
func (m *memStore) BumpVersion(context.Context) (int32, error) { m.version++; return m.version, nil }
|
|
func (m *memStore) ProvidersHash(context.Context) (string, error) { return m.hash, nil }
|
|
func (m *memStore) BumpVersionForProviders(_ context.Context, h string) (int32, error) {
|
|
m.hash = h
|
|
m.version++
|
|
return m.version, nil
|
|
}
|
|
|
|
func newTestService(t *testing.T, ps ...Provider) (*Service, *memStore) {
|
|
t.Helper()
|
|
reg := NewRegistry("test")
|
|
for _, p := range ps {
|
|
reg.Register(p)
|
|
}
|
|
store := newMemStore()
|
|
s, err := NewService(context.Background(), Config{
|
|
Registry: reg, Store: store,
|
|
Supports: func(Provider) []string { return nil },
|
|
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("NewService: %v", err)
|
|
}
|
|
return s, store
|
|
}
|
|
|
|
func TestNewService_ConfiguresEachProviderWithItsDefault(t *testing.T) {
|
|
on := &fakeProvider{id: "on", defaultOn: true}
|
|
off := &fakeProvider{id: "off"}
|
|
s, _ := newTestService(t, on, off)
|
|
if !on.configured.Enabled || off.configured.Enabled {
|
|
t.Fatalf("configured on=%v off=%v, want true false", on.configured.Enabled, off.configured.Enabled)
|
|
}
|
|
if got := s.Enabled(); len(got) != 1 || got[0].ID() != "on" {
|
|
t.Fatalf("Enabled() = %v, want [on]", got)
|
|
}
|
|
}
|
|
|
|
func TestEnabledAs_KeepsOnlyTheCapability(t *testing.T) {
|
|
plain := &fakeProvider{id: "plain", defaultOn: true}
|
|
fetching := &fetchingProvider{fakeProvider{id: "fetching", defaultOn: true}}
|
|
s, _ := newTestService(t, plain, fetching)
|
|
got := EnabledAs[fetcher](s)
|
|
if len(got) != 1 || got[0].ID() != "fetching" {
|
|
t.Fatalf("EnabledAs[fetcher] = %v, want [fetching]", got)
|
|
}
|
|
}
|
|
|
|
func TestUpdateProvider_BumpsTheVersionOnlyWhenTheEnabledSetChanges(t *testing.T) {
|
|
p := &fakeProvider{id: "p"}
|
|
s, store := newTestService(t, p)
|
|
ctx := context.Background()
|
|
on := true
|
|
bumped, err := s.UpdateProvider(ctx, "p", UpdatePatch{Enabled: &on})
|
|
if err != nil || !bumped {
|
|
t.Fatalf("enable: bumped=%v err=%v, want true nil", bumped, err)
|
|
}
|
|
if !p.configured.Enabled || s.CurrentVersion() != store.version {
|
|
t.Fatalf("after enable: configured=%v version=%d store=%d", p.configured.Enabled, s.CurrentVersion(), store.version)
|
|
}
|
|
// Clearing the key: a key-only change. (An empty key is stored as
|
|
// NULL without sealing, so the test needs no sealing key.)
|
|
key := ""
|
|
bumped, err = s.UpdateProvider(ctx, "p", UpdatePatch{APIKey: &key})
|
|
if err != nil || bumped {
|
|
t.Fatalf("key only: bumped=%v err=%v, want false nil", bumped, err)
|
|
}
|
|
if _, err := s.UpdateProvider(ctx, "nope", UpdatePatch{}); err != ErrProviderNotFound {
|
|
t.Fatalf("unknown provider: err=%v, want ErrProviderNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestRegisteredHash_IgnoresRegistrationOrder(t *testing.T) {
|
|
a, b := &fakeProvider{id: "a"}, &fakeProvider{id: "b"}
|
|
s1, _ := newTestService(t, a, b)
|
|
s2, _ := newTestService(t, b, a)
|
|
h1, h2 := s1.registeredHash(), s2.registeredHash()
|
|
if h1 != h2 || len(h1) != 64 {
|
|
t.Fatalf("hashes %q and %q: want equal SHA-256 hex", h1, h2)
|
|
}
|
|
}
|
|
|
|
func TestBumpVersionIfProvidersChanged_WritesOnlyOnAChange(t *testing.T) {
|
|
s, store := newTestService(t, &fakeProvider{id: "a"})
|
|
ctx := context.Background()
|
|
if _, bumped, err := s.BumpVersionIfProvidersChanged(ctx); err != nil || !bumped {
|
|
t.Fatalf("first boot: bumped=%v err=%v, want true", bumped, err)
|
|
}
|
|
before := store.version
|
|
if _, bumped, err := s.BumpVersionIfProvidersChanged(ctx); err != nil || bumped || store.version != before {
|
|
t.Fatalf("same set: bumped=%v version %d→%d, want no write", bumped, before, store.version)
|
|
}
|
|
}
|