Files
silo-server/internal/api/handlers/settings_device_test.go
T
9b111649f3 perf+fix(audiobooks): detail page, browse, sessions & scanner (#169)
* perf(catalog): fix audiobook detail N+1 + slow people facets

Audiobook detail pages were slow in proportion to track count (up to 433
files/book). Root causes, found by EXPLAIN ANALYZE on the live DB:

- effectiveAudioSelection ran 3-4 user-store queries (profile, audio pref,
  library pref) per file inside buildPlaybackInfo's loop, though the results
  are invariant across a request. Introduce a request-scoped audioPrefResolver
  that memoizes the store lookups (library prefs keyed by folder); a 400-file
  audiobook now issues each query once instead of per file. Selection logic is
  unchanged (audioPreference returns a copy so the original-language sentinel
  is still resolved per file).
- buildAudiobookExtension ran its four independent related-content queries
  serially; run them concurrently so latency is the slowest, not the sum.
- author/narrator browse facets did a full people-table scan; add a
  (kind, content_id, person_id) index so the facet resolves from an index-only
  scan of just that kind's credits (~112ms -> ~49ms on the live library).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf(catalog): cache audiobook author/narrator group browse

The Authors/Narrators audiobook pages were slow on cold load and slow again
after a hard refresh (fast only while the React Query client cache was warm).

Root cause (EXPLAIN ANALYZE on live, 31K-audiobook library): the grouped
browse query is ~234ms/page, there are ~13K distinct authors, and the client
pages through the entire list on every load (sequential 500-row requests). With
no server-side cache each of the ~20 pages re-ran the full aggregation
(COUNT(*) OVER() forces it), so a cold load was ~20x234ms. The client's 60s
staleTime was the only thing making a warm revisit fast; a refresh wiped it.

Fix: AudiobookGroupsCache caches the full sorted group list per (library,
group_by, sort, viewer) for 60s (matching the client staleTime, so no extra
staleness) and serves every page as an in-memory slice — one aggregation per
window instead of one per page, and a refresh is a cache hit. Also raise the
client page size 500->2000 so fewer sequential round-trips are needed now that
a larger page is a cheap slice.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf(settings): throttle per-request device last_seen upserts

Device-setting reads (HandleGetDeviceSetting, HandleGetEffectiveSettings,
HandleGetEffectiveSubtitleAppearance) each registered the request's device — an
INSERT ... ON CONFLICT upsert of last_seen_at on a single per-device row. A page
that fetches many settings fired hundreds of these concurrently; they serialized
on that row's lock (observed 100-237ms each, ~250 per page load in the slow
query log), taxing every settings fetch.

Throttle device registration to one upsert per (profile, device) per 5 minutes
via an in-process TTL cache, marking the device seen before the upsert so a
concurrent burst collapses to a single write. last_seen_at stays fresh to within
the window. Reads no longer issue a contended write on the hot path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf+fix(audiobooks): probe-repair, resume position, cache storm, groups reveal, hot-row + stats index

From the full audiobook code review (EXPLAIN + slow-query trace on live):

- #1 (P0, detail-page killer): NeedsCriticalProbeRepair required video codec/
  resolution/tracks, which audio-only files never have, so PlaybackProbeEnsurer
  re-ran ffprobe per file on every detail/watch load (up to N serial spawns for
  an N-track book) and never converged. Gate video-field checks on the file
  actually having a video stream. TDD.
- #3 (P0): abs session-sync rewound the resume cursor — UpdateProgressPosition
  did an unconditional SET with no monotonic guard, ignored its error, and
  no-op'd when no row existed (first-listen resume lost). Now a finish-preserving
  GREATEST upsert; caller logs failures.
- #4 (P0 perf): progress reports fired every ~10s invalidated all of
  catalogKeys.all → refetched every active browse/detail query incl the 13k
  audiobook group lists. Scope invalidation to the reported item's detail.
- #6 (P1 perf): Authors/Narrators page rendered all ~13k groups + cover images
  at once (main-thread freeze). Incremental reveal: render a capped window, grow
  on scroll via IntersectionObserver.
- #10: throttle abs TouchToken last_seen upsert (one per token per 5min) — same
  hot-row contention class as the device fix.
- #8: index abs_playback_sessions (user_id, profile_id, started_at) for the
  listening-stats aggregations.

Deferred (need contract/validation): listening-time idempotency (client delta-vs-
cumulative), scanner deleted-file reconcile, abs session retention job, abs list-
handler batch fetch, scanner-output P2s (need re-backfill).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* perf(audiobooks): batch-fetch abs list/shelf handlers (kill N+1)

handleSimilarItems, handleItemsInProgress, and handleGetMyProgress called
MediaStore.GetAudiobookByID once per row — up to ~500 single fetches (each a
few queries) on app open. Add GetAudiobooksByIDs (one access-scoped fetch +
people/series hydrated once for the whole set) and look results up from the
returned map, preserving order. Underlying primitives (GetByIDsWithAccess,
hydratePeople/Series) were already batch-capable.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(audiobooks): reconcile deleted files on scan + prune session history

#5: ScanAudiobookFolder only ever upserted — deleted/renamed books leaked
media_items/media_files/memberships forever. Mirror the ebook reconcile: collect
seenPaths during the walk, MarkMissing files no longer on disk, then
reconcileLibraryMemberships. Safety mirrors ebooks/video: an inaccessible root
(unmounted source) is skipped entirely, and a walk that saw zero files while the
DB has rows only reconciles after operator cleanup confirmation
(ebookEmptyCleanupAllowed) — so a flapping mount can't wipe the catalog. Soft
mark only; the existing grace-period purge hard-deletes later. Reconcile runs
only on a fully-completed (non-cancelled) scan. (#9 coarse case already handled:
audiobookFolderShouldSkip skips unchanged folders; per-file reuse deferred.)

#8-retention: abs_playback_sessions grew unbounded (one row per play-start, never
deleted) and fed every listening-stats scan. Add an hourly sweep in
SessionCleaner: close abandoned open sessions (no /close, stopped syncing >24h)
and delete closed sessions older than 90 days. Mirrors the recommendation_cache /
missing-files prune pattern.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(audiobooks): address max-effort code-review findings

From /code-review max on the pre-PR diff:
- DATA RACE (P0): SessionCleaner.lastABSSessionPrune is read+written by both the
  15s ticker goroutine and the shutdown-path CleanStale call (main.go defers
  Stop() to after that call). Guard the prune-due gate with a mutex. (CleanStale
  was stateless before this branch, so concurrent calls were previously safe.)
- ScanAudiobookFolder hardcoded fullScan=true into the empty-walk cleanup guard,
  but it's also called from ScanSubtree (incremental scans). An empty subtree
  scan would wrongly consume the operator's one-shot empty-cleanup allowance and
  warn. Thread a real fullScan flag (true from ScanFolder, false from the two
  subtree call sites), mirroring the ebook path.
- Revert UpdateProgressPosition to UPDATE-only (drop the INSERT-on-missing):
  keep the monotonic GREATEST + finish guard that fixes the resume rewind, but
  restore the no-op-on-missing contract so a stray sync tick can't resurrect
  just-cleared progress or create a zero-duration continue-listening row.
- Clamp the audiobook-groups handler limit (paging moved into the cache, leaving
  the old 500/page bound stranded); also gofmt the Scanner struct.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(audiobooks): address review feedback for scanner and stats

* fix(audiobooks): address review feedback

* fix(audiobooks): retry failed session prune

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com>
2026-06-17 09:51:43 -04:00

643 lines
22 KiB
Go

package handlers
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/go-chi/chi/v5"
apimw "github.com/Silo-Server/silo-server/internal/api/middleware"
"github.com/Silo-Server/silo-server/internal/auth"
"github.com/Silo-Server/silo-server/internal/models"
"github.com/Silo-Server/silo-server/internal/userdb"
"github.com/Silo-Server/silo-server/internal/userstore"
)
type testAdminUserRepo struct {
users map[int]*models.User
}
func (r testAdminUserRepo) List(context.Context) ([]*models.User, error) {
out := make([]*models.User, 0, len(r.users))
for _, user := range r.users {
out = append(out, user)
}
return out, nil
}
func (r testAdminUserRepo) Create(context.Context, models.CreateUserInput) (*models.User, error) {
panic("unexpected Create call")
}
func (r testAdminUserRepo) Update(context.Context, int, models.UpdateUserInput) error {
panic("unexpected Update call")
}
func (r testAdminUserRepo) Delete(context.Context, int) error {
panic("unexpected Delete call")
}
func (r testAdminUserRepo) GetByID(_ context.Context, id int) (*models.User, error) {
return r.users[id], nil
}
func TestRegisterRequestDeviceNilStore(t *testing.T) {
h := NewSettingsHandler(nil)
h.registerRequestDevice(context.Background(), nil, "profile-1", requestDeviceMetadata{
DeviceID: "device-1",
DeviceName: "Living Room",
DevicePlatform: "web",
})
}
type mappedTestUserStoreProvider struct {
stores map[int]userstore.UserStore
}
func (p mappedTestUserStoreProvider) ForUser(_ context.Context, userID int) (userstore.UserStore, error) {
return p.stores[userID], nil
}
func (p mappedTestUserStoreProvider) Close() error { return nil }
func newIsolatedProfileTestStore(t *testing.T, suffix string) userstore.UserStore {
t.Helper()
dsn := "file:" + strings.NewReplacer("/", "_", " ", "_").Replace(t.Name()+"-"+suffix) + "?mode=memory&cache=shared"
db, err := sql.Open("sqlite3", dsn)
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
t.Cleanup(func() {
_ = db.Close()
})
if err := userdb.InitSchema(db); err != nil {
t.Fatalf("init schema: %v", err)
}
store := userdb.NewSQLiteUserStore(db)
if err := store.CreateProfile(context.Background(), userstore.Profile{ID: "profile-1", Name: "Main"}); err != nil {
t.Fatalf("create profile: %v", err)
}
return store
}
func TestEffectiveSubtitleAppearancePrefersDeviceOverride(t *testing.T) {
store := newProfileTestStore(t)
if err := store.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "device-1",
DeviceName: "Living Room",
DevicePlatform: "tvOS",
Key: subtitleAppearanceSettingKey,
Value: `{"fontSize":"small"}`,
}); err != nil {
t.Fatalf("SetDeviceSetting: %v", err)
}
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(http.MethodGet, "/settings/subtitle_appearance/effective", nil)
req.Header.Set(deviceIDHeader, "device-1")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec := httptest.NewRecorder()
handler.HandleGetEffectiveSubtitleAppearance(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
var resp effectiveSubtitleAppearanceResponse
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if !resp.HasDeviceOverride || resp.EffectiveValue != `{"fontSize":"small"}` || resp.GlobalValue != "" {
t.Fatalf("response = %#v", resp)
}
}
func TestSubtitleAppearanceDeviceOverrideRoundTrip(t *testing.T) {
store := newProfileTestStore(t)
handler := NewSettingsHandler(testUserStoreProvider{store: store})
body := bytes.NewBufferString(`{"value":"{\"fontSize\":\"xxlarge\"}"}`)
req := httptest.NewRequest(http.MethodPut, "/settings/device/subtitle_appearance", body)
req.Header.Set(deviceIDHeader, "iphone")
req.Header.Set(deviceNameHeader, "Example iPhone")
req.Header.Set(devicePlatformHeader, "iOS")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec := httptest.NewRecorder()
handler.HandleSetSubtitleAppearanceDeviceOverride(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("set status = %d body=%s", rec.Code, rec.Body.String())
}
entry, err := store.GetDeviceSetting(context.Background(), "profile-1", "iphone", subtitleAppearanceSettingKey)
if err != nil {
t.Fatalf("GetDeviceSetting: %v", err)
}
if entry == nil || entry.Value != `{"fontSize":"xxlarge"}` || entry.DevicePlatform != "iOS" {
t.Fatalf("entry = %#v", entry)
}
req = httptest.NewRequest(http.MethodDelete, "/settings/device/subtitle_appearance", nil)
req.Header.Set(deviceIDHeader, "iphone")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec = httptest.NewRecorder()
handler.HandleDeleteSubtitleAppearanceDeviceOverride(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("delete status = %d body=%s", rec.Code, rec.Body.String())
}
entry, err = store.GetDeviceSetting(context.Background(), "profile-1", "iphone", subtitleAppearanceSettingKey)
if err != nil {
t.Fatalf("GetDeviceSetting after delete: %v", err)
}
if entry != nil {
t.Fatalf("entry after delete = %#v, want nil", entry)
}
}
func TestGetEffectiveSettingsResolvesUserDeviceAndDefaultSources(t *testing.T) {
store := newProfileTestStore(t)
if err := store.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "apple-tv",
Key: "playback.preferred_quality",
Value: "1080p",
}); err != nil {
t.Fatalf("SetDeviceSetting(preferred_quality): %v", err)
}
if err := store.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "apple-tv",
Key: "player.playback_speed",
Value: "1.25",
}); err != nil {
t.Fatalf("SetDeviceSetting: %v", err)
}
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(
http.MethodGet,
"/settings/effective?keys=playback.preferred_quality,player.playback_speed,player.hdr_enabled,ui.remember_library_page_state",
nil,
)
req.Header.Set(deviceIDHeader, "apple-tv")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec := httptest.NewRecorder()
handler.HandleGetEffectiveSettings(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
var resp effectiveSettingsResponse
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if len(resp.Settings) != 4 {
t.Fatalf("settings len = %d", len(resp.Settings))
}
byKey := make(map[string]effectiveSettingResponse, len(resp.Settings))
for _, entry := range resp.Settings {
byKey[entry.Key] = entry
}
if got := byKey["playback.preferred_quality"]; got.EffectiveValue != "1080p" || got.Source != "device" || !got.HasDeviceOverride {
t.Fatalf("preferred_quality = %#v", got)
}
if got := byKey["player.playback_speed"]; got.EffectiveValue != "1.25" || got.Source != "device" || !got.HasDeviceOverride {
t.Fatalf("playback_speed = %#v", got)
}
if got := byKey["player.hdr_enabled"]; got.EffectiveValue != "true" || got.Source != "default" {
t.Fatalf("hdr_enabled = %#v", got)
}
if got := byKey[rememberLibraryPageStateSettingKey]; got.EffectiveValue != "true" || got.Source != "default" {
t.Fatalf("remember_library_page_state = %#v", got)
}
}
func TestGenericSettingsRejectInvalidRegisteredValues(t *testing.T) {
store := newProfileTestStore(t)
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(
http.MethodPut,
"/settings/playback.auto_skip_intro",
bytes.NewBufferString(`{"value":"maybe"}`),
)
req = withRouteParams(req, map[string]string{"key": "playback.auto_skip_intro"})
req = req.WithContext(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}))
rec := httptest.NewRecorder()
handler.HandleSetSetting(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
}
func TestLibraryPageStateIsDeviceScopedJSONSetting(t *testing.T) {
store := newProfileTestStore(t)
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(
http.MethodPut,
"/settings/device/ui.library_page_state",
bytes.NewBufferString(`{"value":"{\"version\":1,\"libraries\":{\"7\":{\"search\":\"tab=library\"}}}"}`),
)
req = withRouteParams(req, map[string]string{"key": libraryPageStateSettingKey})
req.Header.Set(deviceIDHeader, "browser")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec := httptest.NewRecorder()
handler.HandleSetDeviceSetting(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
entry, err := store.GetDeviceSetting(context.Background(), "profile-1", "browser", libraryPageStateSettingKey)
if err != nil {
t.Fatalf("GetDeviceSetting: %v", err)
}
if entry == nil || !strings.Contains(entry.Value, `"tab=library"`) {
t.Fatalf("entry = %#v", entry)
}
}
func TestLibraryPageStateRejectsInvalidJSONAndUserScope(t *testing.T) {
store := newProfileTestStore(t)
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(
http.MethodPut,
"/settings/device/ui.library_page_state",
bytes.NewBufferString(`{"value":"not json"}`),
)
req = withRouteParams(req, map[string]string{"key": libraryPageStateSettingKey})
req.Header.Set(deviceIDHeader, "browser")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec := httptest.NewRecorder()
handler.HandleSetDeviceSetting(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("invalid JSON status = %d body=%s", rec.Code, rec.Body.String())
}
req = httptest.NewRequest(
http.MethodPut,
"/settings/ui.library_page_state",
bytes.NewBufferString(`{"value":"{\"version\":1,\"libraries\":{}}"}`),
)
req = withRouteParams(req, map[string]string{"key": libraryPageStateSettingKey})
req = req.WithContext(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}))
rec = httptest.NewRecorder()
handler.HandleSetSetting(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("user-scope status = %d body=%s", rec.Code, rec.Body.String())
}
}
func TestRememberLibraryPageStateIsDeviceScopedBoolSetting(t *testing.T) {
store := newProfileTestStore(t)
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(
http.MethodPut,
"/settings/device/ui.remember_library_page_state",
bytes.NewBufferString(`{"value":"false"}`),
)
req = withRouteParams(req, map[string]string{"key": rememberLibraryPageStateSettingKey})
req.Header.Set(deviceIDHeader, "browser")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec := httptest.NewRecorder()
handler.HandleSetDeviceSetting(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
entry, err := store.GetDeviceSetting(context.Background(), "profile-1", "browser", rememberLibraryPageStateSettingKey)
if err != nil {
t.Fatalf("GetDeviceSetting: %v", err)
}
if entry == nil || entry.Value != "false" {
t.Fatalf("entry = %#v", entry)
}
req = httptest.NewRequest(
http.MethodPut,
"/settings/device/ui.remember_library_page_state",
bytes.NewBufferString(`{"value":"maybe"}`),
)
req = withRouteParams(req, map[string]string{"key": rememberLibraryPageStateSettingKey})
req.Header.Set(deviceIDHeader, "browser")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-1"))
rec = httptest.NewRecorder()
handler.HandleSetDeviceSetting(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("invalid bool status = %d body=%s", rec.Code, rec.Body.String())
}
req = httptest.NewRequest(
http.MethodPut,
"/settings/ui.remember_library_page_state",
bytes.NewBufferString(`{"value":"false"}`),
)
req = withRouteParams(req, map[string]string{"key": rememberLibraryPageStateSettingKey})
req = req.WithContext(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}))
rec = httptest.NewRecorder()
handler.HandleSetSetting(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("user-scope status = %d body=%s", rec.Code, rec.Body.String())
}
}
func TestAdminCanResetSubtitleAppearanceDeviceOverrides(t *testing.T) {
store := newProfileTestStore(t)
for _, deviceID := range []string{"apple-tv", "iphone"} {
if err := store.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: deviceID,
Key: subtitleAppearanceSettingKey,
Value: `{"fontSize":"small"}`,
}); err != nil {
t.Fatalf("SetDeviceSetting(%s): %v", deviceID, err)
}
}
handler := &AdminHandler{storeProv: testUserStoreProvider{store: store}}
req := httptest.NewRequest(http.MethodDelete, "/admin/users/7/profiles/profile-1/device-settings/subtitle_appearance/apple-tv", nil)
req = withRouteParams(req, map[string]string{
"id": "7", "profile_id": "profile-1", "key": subtitleAppearanceSettingKey, "device_id": "apple-tv",
})
rec := httptest.NewRecorder()
handler.HandleDeleteUserDeviceSetting(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("delete one status = %d body=%s", rec.Code, rec.Body.String())
}
remaining, err := store.ListDeviceSettings(context.Background(), subtitleAppearanceSettingKey)
if err != nil {
t.Fatalf("ListDeviceSettings: %v", err)
}
if len(remaining) != 1 || remaining[0].DeviceID != "iphone" {
t.Fatalf("remaining = %#v", remaining)
}
req = httptest.NewRequest(http.MethodDelete, "/admin/users/7/device-settings/subtitle_appearance", nil)
req = withRouteParams(req, map[string]string{"id": "7", "key": subtitleAppearanceSettingKey})
rec = httptest.NewRecorder()
handler.HandleDeleteUserDeviceSettingsByKey(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("delete all status = %d body=%s", rec.Code, rec.Body.String())
}
remaining, err = store.ListDeviceSettings(context.Background(), subtitleAppearanceSettingKey)
if err != nil {
t.Fatalf("ListDeviceSettings after delete all: %v", err)
}
if len(remaining) != 0 {
t.Fatalf("remaining after delete all = %#v", remaining)
}
}
func TestEffectiveSettingsAreIsolatedPerProfileOnSameDevice(t *testing.T) {
store := newProfileTestStore(t)
if err := store.CreateProfile(context.Background(), userstore.Profile{ID: "profile-2", Name: "Guest"}); err != nil {
t.Fatalf("CreateProfile: %v", err)
}
if err := store.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "shared-tv",
Key: "player.playback_speed",
Value: "1.5",
}); err != nil {
t.Fatalf("SetDeviceSetting profile-1: %v", err)
}
if err := store.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-2",
DeviceID: "shared-tv",
Key: "player.playback_speed",
Value: "0.75",
}); err != nil {
t.Fatalf("SetDeviceSetting profile-2: %v", err)
}
handler := NewSettingsHandler(testUserStoreProvider{store: store})
req := httptest.NewRequest(http.MethodGet, "/settings/effective?keys=player.playback_speed", nil)
req.Header.Set(deviceIDHeader, "shared-tv")
req = req.WithContext(apimw.SetProfileID(apimw.SetClaims(req.Context(), &auth.Claims{UserID: 7}), "profile-2"))
rec := httptest.NewRecorder()
handler.HandleGetEffectiveSettings(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
var resp effectiveSettingsResponse
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if len(resp.Settings) != 1 {
t.Fatalf("settings len = %d", len(resp.Settings))
}
if got := resp.Settings[0]; got.ProfileID != "profile-2" || got.EffectiveValue != "0.75" {
t.Fatalf("effective setting = %#v", got)
}
}
func TestAdminCanListAndInspectDevicesAcrossUsers(t *testing.T) {
store1 := newIsolatedProfileTestStore(t, "one")
store2 := newIsolatedProfileTestStore(t, "two")
if err := store1.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "living-room",
DeviceName: "Living Room TV",
DevicePlatform: "tvOS",
Key: "player.playback_speed",
Value: "1.25",
}); err != nil {
t.Fatalf("SetDeviceSetting store1: %v", err)
}
if err := store1.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "living-room",
DeviceName: "Living Room TV",
DevicePlatform: "tvOS",
Key: "player.audio_sync_ms",
Value: "120",
}); err != nil {
t.Fatalf("SetDeviceSetting store1 second: %v", err)
}
store1Registry, ok := store1.(userstore.DeviceRegistry)
if !ok {
t.Fatalf("store1 does not support device registry")
}
if err := store1Registry.RegisterDevice(context.Background(), userstore.DeviceEntry{
ProfileID: "profile-1",
DeviceID: "bedroom",
DeviceName: "Bedroom TV",
DevicePlatform: "Android TV",
}); err != nil {
t.Fatalf("RegisterDevice store1: %v", err)
}
if err := store2.SetDeviceSetting(context.Background(), userstore.DeviceSettingEntry{
ProfileID: "profile-1",
DeviceID: "phone",
DeviceName: "Travel Phone",
DevicePlatform: "iOS",
Key: "player.hdr_enabled",
Value: "false",
}); err != nil {
t.Fatalf("SetDeviceSetting store2: %v", err)
}
handler := &AdminHandler{
userRepo: testAdminUserRepo{
users: map[int]*models.User{
7: {ID: 7, Username: "alice", Email: "alice@example.com"},
11: {ID: 11, Username: "bob", Email: "bob@example.com"},
},
},
storeProv: mappedTestUserStoreProvider{
stores: map[int]userstore.UserStore{
7: store1,
11: store2,
},
},
}
req := httptest.NewRequest(http.MethodGet, "/admin/devices", nil)
rec := httptest.NewRecorder()
handler.HandleListDevices(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("list status = %d body=%s", rec.Code, rec.Body.String())
}
var listResp adminDevicesListResponse
if err := json.NewDecoder(rec.Body).Decode(&listResp); err != nil {
t.Fatalf("decode list: %v", err)
}
if len(listResp.Devices) != 3 {
t.Fatalf("devices len = %d, want 3", len(listResp.Devices))
}
var bedroom *adminDeviceSummaryResponse
for i := range listResp.Devices {
if listResp.Devices[i].DeviceID == "bedroom" {
bedroom = &listResp.Devices[i]
break
}
}
if bedroom == nil {
t.Fatalf("registered device without overrides missing: %#v", listResp.Devices)
}
if bedroom.OverrideCount != 0 || bedroom.ProfileCount != 1 || bedroom.DeviceName != "Bedroom TV" {
t.Fatalf("registered device summary = %#v", bedroom)
}
req = httptest.NewRequest(http.MethodGet, "/admin/devices/7/living-room", nil)
req = withRouteParams(req, map[string]string{"user_id": "7", "device_id": "living-room"})
rec = httptest.NewRecorder()
handler.HandleGetDevice(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("detail status = %d body=%s", rec.Code, rec.Body.String())
}
var detailResp adminDeviceDetailResponse
if err := json.NewDecoder(rec.Body).Decode(&detailResp); err != nil {
t.Fatalf("decode detail: %v", err)
}
if detailResp.Username != "alice" || detailResp.DeviceName != "Living Room TV" {
t.Fatalf("detail response = %#v", detailResp)
}
if len(detailResp.Settings) != 2 {
t.Fatalf("detail settings len = %d, want 2", len(detailResp.Settings))
}
req = httptest.NewRequest(http.MethodGet, "/admin/devices/7/bedroom", nil)
req = withRouteParams(req, map[string]string{"user_id": "7", "device_id": "bedroom"})
rec = httptest.NewRecorder()
handler.HandleGetDevice(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("registered detail status = %d body=%s", rec.Code, rec.Body.String())
}
if err := json.NewDecoder(rec.Body).Decode(&detailResp); err != nil {
t.Fatalf("decode registered detail: %v", err)
}
if detailResp.DeviceName != "Bedroom TV" || detailResp.OverrideCount != 0 {
t.Fatalf("registered detail response = %#v", detailResp)
}
if len(detailResp.Settings) != 0 {
t.Fatalf("registered detail settings len = %d, want 0", len(detailResp.Settings))
}
if len(detailResp.Profiles) != 1 || detailResp.Profiles[0].ProfileID != "profile-1" {
t.Fatalf("registered detail profiles = %#v", detailResp.Profiles)
}
}
func TestAdminCanResetAllOverridesForOneDevice(t *testing.T) {
store := newProfileTestStore(t)
for _, entry := range []userstore.DeviceSettingEntry{
{ProfileID: "profile-1", DeviceID: "living-room", Key: "player.playback_speed", Value: "1.25"},
{ProfileID: "profile-1", DeviceID: "living-room", Key: "player.audio_sync_ms", Value: "120"},
{ProfileID: "profile-1", DeviceID: "phone", Key: "player.hdr_enabled", Value: "false"},
} {
if err := store.SetDeviceSetting(context.Background(), entry); err != nil {
t.Fatalf("SetDeviceSetting: %v", err)
}
}
handler := &AdminHandler{storeProv: testUserStoreProvider{store: store}}
req := httptest.NewRequest(http.MethodDelete, "/admin/users/7/profiles/profile-1/devices/living-room/settings", nil)
req = withRouteParams(req, map[string]string{"id": "7", "profile_id": "profile-1", "device_id": "living-room"})
rec := httptest.NewRecorder()
handler.HandleDeleteAllUserDeviceSettings(rec, req)
if rec.Code != http.StatusNoContent {
t.Fatalf("delete status = %d body=%s", rec.Code, rec.Body.String())
}
entries, err := store.ListAllDeviceSettings(context.Background())
if err != nil {
t.Fatalf("ListAllDeviceSettings: %v", err)
}
if len(entries) != 1 || entries[0].DeviceID != "phone" {
t.Fatalf("entries after delete = %#v", entries)
}
registry, ok := store.(userstore.DeviceRegistry)
if !ok {
t.Fatalf("store does not support device registry")
}
devices, err := registry.ListDevices(context.Background())
if err != nil {
t.Fatalf("ListDevices: %v", err)
}
foundLivingRoom := false
for _, device := range devices {
if device.ProfileID == "profile-1" && device.DeviceID == "living-room" {
foundLivingRoom = true
break
}
}
if !foundLivingRoom {
t.Fatalf("registry devices after delete = %#v", devices)
}
}
func withRouteParams(req *http.Request, params map[string]string) *http.Request {
routeCtx := chi.NewRouteContext()
for key, value := range params {
routeCtx.URLParams.Add(key, value)
}
return req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, routeCtx))
}