package api import ( "testing" "github.com/ponzischeme89/memby/server/internal/store" ) func TestNormaliseGenreAffinityScalesToTheTopGenre(t *testing.T) { response := normaliseGenreAffinity(store.GenreAffinity{ Sessions: 40, Genres: []store.GenreWeight{ {Genre: "Crime", Score: 20}, {Genre: "Drama", Score: 10}, {Genre: "Western", Score: 1}, }, }) if response.Sessions != 40 { t.Fatalf("sessions = %d, want 40", response.Sessions) } if len(response.Genres) != 3 { t.Fatalf("genres = %d, want 3", len(response.Genres)) } if response.Genres[0].Genre != "Crime" || response.Genres[0].Weight != 1 { t.Fatalf("top = %+v, want Crime at 1", response.Genres[0]) } if response.Genres[1].Weight != 0.5 { t.Fatalf("Drama = %v, want 0.5", response.Genres[1].Weight) } } // A share is the whole point: two households with wildly different amounts of viewing must // produce the same ordering from the same balance of it. func TestNormaliseGenreAffinityIsScaleFree(t *testing.T) { light := normaliseGenreAffinity(store.GenreAffinity{Sessions: 12, Genres: []store.GenreWeight{ {Genre: "Comedy", Score: 3}, {Genre: "Horror", Score: 1}, }}) heavy := normaliseGenreAffinity(store.GenreAffinity{Sessions: 900, Genres: []store.GenreWeight{ {Genre: "Comedy", Score: 300}, {Genre: "Horror", Score: 100}, }}) for i := range light.Genres { if light.Genres[i] != heavy.Genres[i] { t.Fatalf("entry %d: %+v vs %+v", i, light.Genres[i], heavy.Genres[i]) } } } // An empty or nonsensical reading has to come back as an empty *list* rather than as null, // because the television reads "no genres" as "use the default order" and a null would be // one more shape for it to have an opinion about. func TestNormaliseGenreAffinityRefusesToInventWeights(t *testing.T) { for name, affinity := range map[string]store.GenreAffinity{ "nothing watched": {}, "all zero": {Sessions: 5, Genres: []store.GenreWeight{{Genre: "Drama"}}}, "negative": {Sessions: 5, Genres: []store.GenreWeight{{Genre: "Drama", Score: -2}}}, } { response := normaliseGenreAffinity(affinity) if response.Genres == nil { t.Fatalf("%s: genres is nil, want an empty list", name) } if len(response.Genres) != 0 { t.Fatalf("%s: genres = %+v, want none", name, response.Genres) } } } // A blank label cannot be matched against anything on the television, so it is dropped // rather than sent as a row with a weight the set would have to know to ignore. func TestNormaliseGenreAffinityDropsBlankLabels(t *testing.T) { response := normaliseGenreAffinity(store.GenreAffinity{Sessions: 20, Genres: []store.GenreWeight{ {Genre: "Drama", Score: 4}, {Genre: "", Score: 2}, }}) if len(response.Genres) != 1 || response.Genres[0].Genre != "Drama" { t.Fatalf("genres = %+v, want Drama alone", response.Genres) } }