refactor(providers): cover art and tags share one registry and settings service (#8002)
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>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// Package providers is the frame the pluggable outside sources hang on:
|
||||
// cover-art sources (internal/coverart) and tag sources (internal/tags).
|
||||
//
|
||||
// Each family keeps its own tables, provider set and capabilities. What they
|
||||
// share lives here: the compiled-in registry, the settings service that
|
||||
// reconciles it with the family's settings table (enabled flags, sealed API
|
||||
// keys, the sources version the enricher stamps), and the admin views of
|
||||
// it. A fix to key sealing or to the version bump is made once.
|
||||
package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Provider is the base shape every source implements.
|
||||
type Provider interface {
|
||||
// ID is a stable lowercase token: persisted as the row's source, keyed
|
||||
// in the family's provider_settings table, and shown in the admin UI.
|
||||
ID() string
|
||||
|
||||
// DisplayName is the label on the admin Settings card.
|
||||
DisplayName() string
|
||||
|
||||
// RequiresAPIKey decides whether the admin UI shows a key field. It
|
||||
// does not gate the enabled toggle: a source with a usable default
|
||||
// ships working.
|
||||
RequiresAPIKey() bool
|
||||
|
||||
// DefaultEnabled is whether a fresh install starts with the source on.
|
||||
// Sources that need a key ship off.
|
||||
DefaultEnabled() bool
|
||||
|
||||
// Configure applies the admin's settings. It is called at boot and on
|
||||
// every admin change, and must be idempotent and safe to call while the
|
||||
// enricher is reading.
|
||||
Configure(Settings) error
|
||||
}
|
||||
|
||||
// Settings is what a provider receives on Configure. An empty APIKey is
|
||||
// the provider's to interpret: a public default key, or "not configured".
|
||||
type Settings struct {
|
||||
Enabled bool
|
||||
APIKey string
|
||||
}
|
||||
|
||||
// Testable providers can answer "is my config working?" without a full
|
||||
// enrichment cycle, for the admin Test button.
|
||||
type Testable interface {
|
||||
Provider
|
||||
TestConnection(ctx context.Context) error
|
||||
}
|
||||
|
||||
// ErrProviderNotFound is returned for an ID no provider registered.
|
||||
var ErrProviderNotFound = errors.New("provider not registered")
|
||||
|
||||
// ErrNotTestable is returned when testing a provider that isn't Testable.
|
||||
var ErrNotTestable = errors.New("provider does not support test connection")
|
||||
|
||||
// Registry is one family's compiled-in providers, in registration order.
|
||||
// Providers register from their file's init().
|
||||
type Registry struct {
|
||||
family string
|
||||
mu sync.Mutex
|
||||
providers []Provider
|
||||
}
|
||||
|
||||
// NewRegistry returns an empty registry; family names it in panics.
|
||||
func NewRegistry(family string) *Registry { return &Registry{family: family} }
|
||||
|
||||
// Register adds p. A duplicate ID panics: providers are compiled in, so a
|
||||
// duplicate is a build bug.
|
||||
func (r *Registry) Register(p Provider) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for _, existing := range r.providers {
|
||||
if existing.ID() == p.ID() {
|
||||
panic(fmt.Sprintf("%s: duplicate provider ID %q", r.family, p.ID()))
|
||||
}
|
||||
}
|
||||
r.providers = append(r.providers, p)
|
||||
}
|
||||
|
||||
// All returns a snapshot in registration order.
|
||||
func (r *Registry) All() []Provider {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
out := make([]Provider, len(r.providers))
|
||||
copy(out, r.providers)
|
||||
return out
|
||||
}
|
||||
|
||||
// ByID returns the registered provider with id, or ErrProviderNotFound.
|
||||
func (r *Registry) ByID(id string) (Provider, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for _, p := range r.providers {
|
||||
if p.ID() == id {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrProviderNotFound
|
||||
}
|
||||
|
||||
// ResetForTests empties the registry. Tests only.
|
||||
func (r *Registry) ResetForTests() {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.providers = nil
|
||||
}
|
||||
@@ -0,0 +1,307 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/secret"
|
||||
)
|
||||
|
||||
// Row is one provider's stored settings. APIKey is sealed, or nil.
|
||||
type Row struct {
|
||||
ProviderID string
|
||||
Enabled bool
|
||||
APIKey *string
|
||||
}
|
||||
|
||||
// Store is the tables one family keeps its settings in: a row per provider,
|
||||
// and a singleton holding the sources version and the hash of the
|
||||
// registered provider set.
|
||||
type Store interface {
|
||||
// EnsureRow inserts a default row for a provider that has none.
|
||||
EnsureRow(ctx context.Context, providerID string, enabled bool, order int32) error
|
||||
List(ctx context.Context) ([]Row, error)
|
||||
// Save writes enabled, and the sealed key only when keyChanged.
|
||||
Save(ctx context.Context, providerID string, enabled, keyChanged bool, sealedKey *string) error
|
||||
Version(ctx context.Context) (int32, error)
|
||||
// BumpVersion increments the version and returns it.
|
||||
BumpVersion(ctx context.Context) (int32, error)
|
||||
ProvidersHash(ctx context.Context) (string, error)
|
||||
// BumpVersionForProviders increments the version and stores hash as the
|
||||
// registered set it was bumped for.
|
||||
BumpVersionForProviders(ctx context.Context, hash string) (int32, error)
|
||||
}
|
||||
|
||||
// UpdatePatch is the admin PATCH body. A nil field is left as it is; an
|
||||
// empty APIKey clears the key.
|
||||
type UpdatePatch struct {
|
||||
Enabled *bool
|
||||
APIKey *string
|
||||
}
|
||||
|
||||
// Info is a provider as the admin GET handler shows it.
|
||||
type Info struct {
|
||||
ID string
|
||||
DisplayName string
|
||||
RequiresAPIKey bool
|
||||
Supports []string
|
||||
Enabled bool
|
||||
APIKeySet bool
|
||||
DisplayOrder int32
|
||||
Testable bool
|
||||
}
|
||||
|
||||
// Config is what a family hands NewService.
|
||||
type Config struct {
|
||||
Registry *Registry
|
||||
Store Store
|
||||
// KeyColumn is where the family's API keys are sealed.
|
||||
KeyColumn secret.Column
|
||||
// Supports names a provider's capabilities for the admin view, such as
|
||||
// "album_cover" or "track_tags".
|
||||
Supports func(Provider) []string
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
// Service reconciles a family's registry with its settings table, caches
|
||||
// each provider's enabled flag and key, and owns the sources version: the
|
||||
// stamp the enricher writes onto rows it has tried. The version moves
|
||||
// whenever the enabled set changes, so rows that found nothing get another
|
||||
// try through the new chain.
|
||||
type Service struct {
|
||||
cfg Config
|
||||
|
||||
mu sync.RWMutex
|
||||
enabledIDs map[string]bool
|
||||
apiKeys map[string]string
|
||||
version int32
|
||||
}
|
||||
|
||||
// NewService upserts a default row for every registered provider,
|
||||
// Configures each with its row, and loads the version.
|
||||
func NewService(ctx context.Context, cfg Config) (*Service, error) {
|
||||
s := &Service{cfg: cfg, enabledIDs: map[string]bool{}, apiKeys: map[string]string{}}
|
||||
registered := cfg.Registry.All()
|
||||
for i, p := range registered {
|
||||
if err := cfg.Store.EnsureRow(ctx, p.ID(), p.DefaultEnabled(), int32(i)); err != nil {
|
||||
return nil, fmt.Errorf("upsert provider %q: %w", p.ID(), err)
|
||||
}
|
||||
}
|
||||
rows, err := cfg.Store.List(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list provider settings: %w", err)
|
||||
}
|
||||
byID := map[string]Row{}
|
||||
for _, r := range rows {
|
||||
byID[r.ProviderID] = r
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, p := range registered {
|
||||
row, ok := byID[p.ID()]
|
||||
if !ok {
|
||||
cfg.Logger.Warn("provider settings: missing row after upsert", "provider", p.ID())
|
||||
continue
|
||||
}
|
||||
s.cache(row)
|
||||
s.configure(p)
|
||||
}
|
||||
if s.version, err = cfg.Store.Version(ctx); err != nil {
|
||||
return nil, fmt.Errorf("get sources version: %w", err)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// cache records row; the caller holds mu.
|
||||
func (s *Service) cache(row Row) {
|
||||
key := ""
|
||||
if row.APIKey != nil {
|
||||
key = secret.Reveal(s.cfg.KeyColumn.Purpose(), row.ProviderID, *row.APIKey)
|
||||
}
|
||||
s.enabledIDs[row.ProviderID] = row.Enabled
|
||||
s.apiKeys[row.ProviderID] = key
|
||||
}
|
||||
|
||||
// configure hands p its cached settings; the caller holds mu.
|
||||
func (s *Service) configure(p Provider) {
|
||||
if err := p.Configure(Settings{Enabled: s.enabledIDs[p.ID()], APIKey: s.apiKeys[p.ID()]}); err != nil {
|
||||
s.cfg.Logger.Warn("provider settings: Configure failed", "provider", p.ID(), "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled returns the enabled providers in registration order.
|
||||
func (s *Service) Enabled() []Provider {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
var out []Provider
|
||||
for _, p := range s.cfg.Registry.All() {
|
||||
if s.enabledIDs[p.ID()] {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// EnabledAs returns the enabled providers that implement capability T, in
|
||||
// registration order.
|
||||
func EnabledAs[T any](s *Service) []T {
|
||||
var out []T
|
||||
for _, p := range s.Enabled() {
|
||||
if c, ok := p.(T); ok {
|
||||
out = append(out, c)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// CurrentVersion is the version the enricher stamps onto rows it tries.
|
||||
func (s *Service) CurrentVersion() int32 {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.version
|
||||
}
|
||||
|
||||
// UpdateProvider applies an admin patch and re-Configures the provider.
|
||||
// versionBumped is true when the enabled set changed; a key change alone
|
||||
// doesn't move the version.
|
||||
func (s *Service) UpdateProvider(ctx context.Context, providerID string, patch UpdatePatch) (versionBumped bool, err error) {
|
||||
p, err := s.cfg.Registry.ByID(providerID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
s.mu.RLock()
|
||||
before := enabledSetSignature(s.enabledIDs)
|
||||
enabled := s.enabledIDs[providerID]
|
||||
s.mu.RUnlock()
|
||||
if patch.Enabled != nil {
|
||||
enabled = *patch.Enabled
|
||||
}
|
||||
sealed, err := secret.SealPtr(s.cfg.KeyColumn.Purpose(), providerID, patch.APIKey)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("seal provider api key: %w", err)
|
||||
}
|
||||
if err := s.cfg.Store.Save(ctx, providerID, enabled, patch.APIKey != nil, sealed); err != nil {
|
||||
return false, fmt.Errorf("update provider settings: %w", err)
|
||||
}
|
||||
rows, err := s.cfg.Store.List(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("re-list provider settings: %w", err)
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, r := range rows {
|
||||
s.cache(r)
|
||||
}
|
||||
s.configure(p)
|
||||
if enabledSetSignature(s.enabledIDs) == before {
|
||||
return false, nil
|
||||
}
|
||||
v, err := s.cfg.Store.BumpVersion(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("bump sources version: %w", err)
|
||||
}
|
||||
s.version = v
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// enabledSetSignature is the sorted, comma-joined enabled provider IDs.
|
||||
func enabledSetSignature(enabledIDs map[string]bool) string {
|
||||
ids := make([]string, 0, len(enabledIDs))
|
||||
for id, on := range enabledIDs {
|
||||
if on {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return strings.Join(ids, ",")
|
||||
}
|
||||
|
||||
// BumpVersionIfProvidersChanged moves the version when the registered
|
||||
// provider set differs from the one seen at the last boot, so rows that
|
||||
// found nothing are tried again through a chain an update widened or
|
||||
// narrowed. An unchanged set writes nothing.
|
||||
func (s *Service) BumpVersionIfProvidersChanged(ctx context.Context) (version int32, bumped bool, err error) {
|
||||
hash := s.registeredHash()
|
||||
stored, err := s.cfg.Store.ProvidersHash(ctx)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("get providers hash: %w", err)
|
||||
}
|
||||
if stored == hash {
|
||||
return s.CurrentVersion(), false, nil
|
||||
}
|
||||
v, err := s.BumpVersion(ctx)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return v, true, nil
|
||||
}
|
||||
|
||||
// BumpVersion always moves the version, for the admin's "search again for
|
||||
// everything that found nothing". It stores the current provider hash too,
|
||||
// as the boot-time bump would.
|
||||
func (s *Service) BumpVersion(ctx context.Context) (int32, error) {
|
||||
v, err := s.cfg.Store.BumpVersionForProviders(ctx, s.registeredHash())
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("bump version: %w", err)
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.version = v
|
||||
s.mu.Unlock()
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// registeredHash is the SHA-256 hex of the sorted, colon-joined registered
|
||||
// provider IDs.
|
||||
func (s *Service) registeredHash() string {
|
||||
provs := s.cfg.Registry.All()
|
||||
ids := make([]string, 0, len(provs))
|
||||
for _, p := range provs {
|
||||
ids = append(ids, p.ID())
|
||||
}
|
||||
sort.Strings(ids)
|
||||
sum := sha256.Sum256([]byte(strings.Join(ids, ":")))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// TestProvider runs the named provider's TestConnection. It returns
|
||||
// ErrNotTestable when the provider can't test itself.
|
||||
func (s *Service) TestProvider(ctx context.Context, providerID string) error {
|
||||
p, err := s.cfg.Registry.ByID(providerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tp, ok := p.(Testable)
|
||||
if !ok {
|
||||
return ErrNotTestable
|
||||
}
|
||||
return tp.TestConnection(ctx)
|
||||
}
|
||||
|
||||
// ListProviderInfo is every registered provider for the admin view, in
|
||||
// registration order.
|
||||
func (s *Service) ListProviderInfo() []Info {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
var out []Info
|
||||
for i, p := range s.cfg.Registry.All() {
|
||||
_, testable := p.(Testable)
|
||||
out = append(out, Info{
|
||||
ID: p.ID(),
|
||||
DisplayName: p.DisplayName(),
|
||||
RequiresAPIKey: p.RequiresAPIKey(),
|
||||
Supports: s.cfg.Supports(p),
|
||||
Enabled: s.enabledIDs[p.ID()],
|
||||
APIKeySet: s.apiKeys[p.ID()] != "",
|
||||
DisplayOrder: int32(i),
|
||||
Testable: testable,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user