release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every import and the Dockerfile's version ldflags. - buildinfo.ProjectURL, the contact outside services see in Minstrel's User-Agent: https://github.com/Fabled-Sword/minstrel. - README's compose example pulls ghcr.io/fabled-sword/minstrel:latest. - docs/security.md sends problem reports to the GitHub issues. - The demo-library stack builds Minstrel from the checkout, since :dev is not published to GHCR. - hygiene.go's same-site example uses example.com names. The workbench CI (.gitea/, ci/, export-ignored) still builds and publishes :dev to the private registry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
6.0 KiB
Go
192 lines
6.0 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/secret"
|
|
)
|
|
|
|
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 a key that was never set changes nothing a search uses. (An
|
|
// empty key is stored as NULL without sealing.)
|
|
key := ""
|
|
bumped, err = s.UpdateProvider(ctx, "p", UpdatePatch{APIKey: &key})
|
|
if err != nil || bumped {
|
|
t.Fatalf("clearing an unset key: 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)
|
|
}
|
|
}
|
|
|
|
// A source switched on before its key was entered searched without it, so
|
|
// the key arriving moves the version and the gaps it left are tried again.
|
|
// Swapping one key for another doesn't.
|
|
func TestUpdateProvider_AKeyArrivingOnAnEnabledSourceBumpsTheVersion(t *testing.T) {
|
|
secret.Install([]byte("providers tests: not a deployment key"))
|
|
p := &fakeProvider{id: "p"}
|
|
s, _ := newTestService(t, p)
|
|
ctx := context.Background()
|
|
on := true
|
|
if _, err := s.UpdateProvider(ctx, "p", UpdatePatch{Enabled: &on}); err != nil {
|
|
t.Fatalf("enable: %v", err)
|
|
}
|
|
for _, step := range []struct {
|
|
key string
|
|
want bool
|
|
}{
|
|
{"first-key", true},
|
|
{"second-key", false},
|
|
{"", true},
|
|
} {
|
|
key := step.key
|
|
bumped, err := s.UpdateProvider(ctx, "p", UpdatePatch{APIKey: &key})
|
|
if err != nil || bumped != step.want {
|
|
t.Fatalf("key %q: bumped=%v err=%v, want %v", step.key, bumped, err, step.want)
|
|
}
|
|
}
|
|
}
|