sonda/internal/usersettings/usersettings_test.go
tgiachi ed32aac228 feat: the radio stations are in the apps only when the person asks
A new setting, radio_in_apps, off until the person turns it on (Settings, Apps and
devices, or PATCH /me/settings). With it off, the list of playlists of an OpenSubsonic
app is only the playlists of the person, and a station asked for by id is not found.
With it on, the stations come as playlists called Radio · Rock.

- migration 0036: user_settings.radio_in_apps
- usersettings, the settings API and the OpenAPI spec have the new field; the PATCH
  now keeps each setting apart (naming one no longer sets the other)
- Settings has a switch
2026-11-16 12:33:09 +01:00

93 lines
2.4 KiB
Go

package usersettings
import (
"context"
"errors"
"testing"
)
type fakeStore struct {
byUser map[int64]Settings
gone map[int64]bool
err error
}
func (f *fakeStore) Get(_ context.Context, id int64) (Settings, error) {
if f.err != nil {
return Settings{}, f.err
}
if f.gone[id] {
return Settings{}, ErrUserNotFound
}
return f.byUser[id], nil
}
func (f *fakeStore) Update(_ context.Context, id int64, p Patch) (Settings, error) {
if f.err != nil {
return Settings{}, f.err
}
if f.gone[id] {
return Settings{}, ErrUserNotFound
}
s := f.byUser[id]
if p.DiscoverFromListening != nil {
s.DiscoverFromListening = *p.DiscoverFromListening
}
if p.RadioInApps != nil {
s.RadioInApps = *p.RadioInApps
}
f.byUser[id] = s
return s, nil
}
func newFake() *fakeStore { return &fakeStore{byUser: map[int64]Settings{}, gone: map[int64]bool{}} }
func TestEverythingIsOffUntilTheUserTurnsItOn(t *testing.T) {
s := NewService(newFake())
got, err := s.Get(context.Background(), 1)
if err != nil || got.DiscoverFromListening {
t.Errorf("new user: %+v err=%v, want everything off", got, err)
}
}
func TestPatchChangesOnlyWhatItNames(t *testing.T) {
f := newFake()
s := NewService(f)
on, off := true, false
got, err := s.Update(context.Background(), 1, Patch{DiscoverFromListening: &on})
if err != nil || !got.DiscoverFromListening {
t.Fatalf("turn on: %+v err=%v", got, err)
}
if got, _ = s.Update(context.Background(), 1, Patch{}); !got.DiscoverFromListening {
t.Errorf("an empty patch changed the setting: %+v", got)
}
if got, _ = s.Update(context.Background(), 1, Patch{DiscoverFromListening: &off}); got.DiscoverFromListening {
t.Errorf("turn off: %+v", got)
}
if other, _ := s.Get(context.Background(), 2); other.DiscoverFromListening {
t.Error("a setting of one user leaked to another")
}
}
func TestADeletedAccountIsReported(t *testing.T) {
f := newFake()
f.gone[7] = true
s := NewService(f)
on := true
if _, err := s.Get(context.Background(), 7); !errors.Is(err, ErrUserNotFound) {
t.Errorf("Get: err = %v", err)
}
if _, err := s.Update(context.Background(), 7, Patch{DiscoverFromListening: &on}); !errors.Is(err, ErrUserNotFound) {
t.Errorf("Update: err = %v", err)
}
}
func TestStoreErrorsPassThrough(t *testing.T) {
boom := errors.New("boom")
f := newFake()
f.err = boom
if _, err := NewService(f).Get(context.Background(), 1); !errors.Is(err, boom) {
t.Errorf("err = %v", err)
}
}