Files
memby/server/internal/api/home_row_layout_test.go
2026-08-28 23:00:02 +12:00

62 lines
2.1 KiB
Go

package api
import (
"encoding/json"
"testing"
"github.com/ponzischeme89/memby/server/internal/recommend"
"github.com/ponzischeme89/memby/server/internal/store"
)
func TestRowLayoutOverridesReadsEveryPage(t *testing.T) {
policy := store.FeaturePolicy{Values: map[string]json.RawMessage{
"home.sectionDefinitions": json.RawMessage(`[
{"id":"favorites","type":"favorites","title":"Favourites","component":"mediaRow","layout":"thumb"},
{"id":"continue","type":"continueWatching","title":"Continue","component":"mediaRow"},
{"id":"broken","type":"custom","title":"x","component":"mediaRow","layout":"sideways"}
]`),
"movies.sectionDefinitions": json.RawMessage(`[
{"id":"library","type":"library","title":"Library","component":"mediaGrid","layout":"poster"}
]`),
}}
got := rowLayoutOverrides(policy)
if got["favorites"] != "thumb" {
t.Fatalf("favorites layout = %q, want thumb", got["favorites"])
}
if got["library"] != "poster" {
t.Fatalf("library layout = %q, want poster", got["library"])
}
if _, ok := got["continue"]; ok {
t.Fatalf("a row with no layout must not appear as an override")
}
if _, ok := got["broken"]; ok {
t.Fatalf("an invalid layout must not appear as an override")
}
}
func TestApplyRowLayoutOverridesMatchesExactAndChildRows(t *testing.T) {
rows := []recommend.Row{
{ID: "favorites"},
{ID: "for-you"},
{ID: "for-you:home:evening"},
{ID: "latest-movies", Layout: "poster"},
}
out := applyRowLayoutOverrides(rows, map[string]string{"favorites": "thumb", "for-you": "poster"})
if out[0].Layout != "thumb" {
t.Fatalf("exact match not applied: %q", out[0].Layout)
}
if out[1].Layout != "poster" || out[2].Layout != "poster" {
t.Fatalf("a section id must pin its child rows too: %q / %q", out[1].Layout, out[2].Layout)
}
if out[3].Layout != "poster" {
t.Fatalf("a row that already carries a layout must be left alone: %q", out[3].Layout)
}
}
func TestApplyRowLayoutOverridesNoOpWithoutOverrides(t *testing.T) {
rows := []recommend.Row{{ID: "favorites"}}
if applyRowLayoutOverrides(rows, nil)[0].Layout != "" {
t.Fatalf("no overrides must leave rows untouched")
}
}