package subsonic import ( "encoding/xml" "slices" "strings" "testing" ) // The XML forms an app reads: a genre's name is the element's text, and a // folder's entries are elements. func TestGenreAndDirectoryXML(t *testing.T) { out, err := xml.Marshal(GenresResponse{ Envelope: NewEnvelope("ok"), Genres: GenresContainer{Genres: []Genre{{Value: "Jazz", SongCount: 3, AlbumCount: 2}}}, }) if err != nil { t.Fatalf("marshal genres: %v", err) } if want := `Jazz`; !strings.Contains(string(out), want) { t.Errorf("genres XML = %s, want it to hold %s", out, want) } out, err = xml.Marshal(DirectoryResponse{ Envelope: NewEnvelope("ok"), Directory: Directory{ID: "a", Name: "Tin Orchard", Children: []SongRef{ {ID: "b", Parent: "a", Title: "Paper Boats", IsDir: true}, }}, }) if err != nil { t.Fatalf("marshal directory: %v", err) } if want := ``; !strings.Contains(string(out), want) { t.Errorf("directory XML = %s, want it to hold %s", out, want) } } func (f picksFixture) browse() *browseHandlers { return &browseHandlers{pool: f.pool} } // A tag of several genres counts toward each; the missing file counts toward // none; the genres with the most songs come first, then by name. func TestGetGenres_CountsEachGenreOfASplitTag(t *testing.T) { f := seedPicks(t) var got struct { Genres GenresContainer `json:"genres"` } if code := call(t, f.browse().getGenres, f.user, "", &got); code != 0 { t.Fatalf("getGenres: error %d", code) } want := []Genre{ {Value: "Jazz", SongCount: 3, AlbumCount: 2}, {Value: "Rock", SongCount: 3, AlbumCount: 2}, {Value: "Funk", SongCount: 1, AlbumCount: 1}, } if !slices.Equal(got.Genres.Genres, want) { t.Errorf("genres = %+v, want %+v", got.Genres.Genres, want) } } func (f picksFixture) songsByGenre(t *testing.T, query string) ([]string, int) { t.Helper() var got struct { SongsByGenre struct { Songs *[]SongRef `json:"song"` } `json:"songsByGenre"` } code := call(t, f.browse().getSongsByGenre, f.user, query, &got) if code != 0 { return nil, code } if got.SongsByGenre.Songs == nil { t.Fatalf("getSongsByGenre %s: song is null or absent, want a list", query) } return titles(*got.SongsByGenre.Songs), 0 } // A genre's songs are its playable ones, without the listener's quarantined // song, album by album in track order, a page at a time. func TestGetSongsByGenre_PagesTheGenresPlayableSongs(t *testing.T) { f := seedPicks(t) cases := []struct { query string want []string }{ {"genre=Jazz", []string{"Dusk", "Harbour"}}, {"genre=Rock", []string{"Flint", "Gale", "Inlet"}}, {"genre=Rock&count=2&offset=1", []string{"Gale", "Inlet"}}, // Ember is the only Funk song, and the listener quarantined it. {"genre=Funk", []string{}}, } for _, c := range cases { got, code := f.songsByGenre(t, c.query) if code != 0 { t.Fatalf("%s: error %d", c.query, code) } if !slices.Equal(got, c.want) { t.Errorf("%s = %v, want %v", c.query, got, c.want) } } if _, code := f.songsByGenre(t, ""); code != ErrMissingParameter { t.Errorf("no genre: code %d, want %d", code, ErrMissingParameter) } } func (f picksFixture) directory(t *testing.T, id string) (Directory, int) { t.Helper() var got struct { Directory Directory `json:"directory"` } code := call(t, f.browse().getMusicDirectory, f.user, "id="+id, &got) return got.Directory, code } // An artist's folder holds its albums as folders; an album's holds its songs. func TestGetMusicDirectory_OpensAnArtistToAlbumsAndAnAlbumToSongs(t *testing.T) { f := seedPicks(t) artistID := uuidToID(f.artist.ID) dir, code := f.directory(t, artistID) if code != 0 { t.Fatalf("artist folder: error %d", code) } if dir.ID != artistID || dir.Name != "Tin Orchard" { t.Errorf("artist folder = %q %q, want %q Tin Orchard", dir.ID, dir.Name, artistID) } // By release date, then title, as getArtist lists them. if got, want := titles(dir.Children), []string{"Lantern Hours", "Gone Tide", "Paper Boats"}; !slices.Equal(got, want) { t.Errorf("artist folder holds %v, want %v", got, want) } for _, c := range dir.Children { if !c.IsDir || c.Parent != artistID || c.Type != "" { t.Errorf("album %s: isDir=%v parent=%q type=%q, want a folder under the artist", c.Title, c.IsDir, c.Parent, c.Type) } } paper := uuidToID(f.albums["Paper Boats"].ID) if dir.Children[2].ID != paper || dir.Children[2].Year != 2012 { t.Errorf("Paper Boats folder = %+v, want id %s and year 2012", dir.Children[2], paper) } dir, code = f.directory(t, paper) if code != 0 { t.Fatalf("album folder: error %d", code) } if dir.ID != paper || dir.Parent != artistID || dir.Name != "Paper Boats" { t.Errorf("album folder = %q parent %q %q, want %q under %q", dir.ID, dir.Parent, dir.Name, paper, artistID) } if got, want := titles(dir.Children), []string{"Gale", "Harbour", "Inlet"}; !slices.Equal(got, want) { t.Errorf("album folder holds %v, want %v", got, want) } for _, c := range dir.Children { if c.IsDir || c.Parent != paper || c.Type != "music" { t.Errorf("song %s: isDir=%v parent=%q type=%q, want a song in the album", c.Title, c.IsDir, c.Parent, c.Type) } } if _, code := f.directory(t, uuidToID(f.tracks["Gale"].ID)); code != ErrDataNotFound { t.Errorf("a song id: code %d, want %d", code, ErrDataNotFound) } if _, code := f.directory(t, ""); code != ErrMissingParameter { t.Errorf("no id: code %d, want %d", code, ErrMissingParameter) } } // getAlbumList offers getAlbumList2's lists, with each album a folder. func TestGetAlbumList_IsAlbumList2AsFolders(t *testing.T) { f := seedPicks(t) var got struct { AlbumList AlbumListContainer `json:"albumList"` } if code := call(t, f.browse().getAlbumList, f.user, "type=alphabeticalByName", &got); code != 0 { t.Fatalf("getAlbumList: error %d", code) } if names, want := titles(got.AlbumList.Albums), []string{"Gone Tide", "Lantern Hours", "Paper Boats"}; !slices.Equal(names, want) { t.Errorf("albums = %v, want %v", names, want) } for _, a := range got.AlbumList.Albums { if !a.IsDir || a.Artist != "Tin Orchard" || a.Parent != uuidToID(f.artist.ID) { t.Errorf("album %s: isDir=%v artist=%q parent=%q, want a folder under Tin Orchard", a.Title, a.IsDir, a.Artist, a.Parent) } } if code := call(t, f.browse().getAlbumList, f.user, "", nil); code != ErrMissingParameter { t.Errorf("no type: code %d, want %d", code, ErrMissingParameter) } } // search2 finds what search3 finds, with the albums as folders. func TestSearch2_FindsWhatSearch3FindsWithAlbumsAsFolders(t *testing.T) { f := seedPicks(t) var got struct { SearchResult2 SearchResult2Container `json:"searchResult2"` } if code := call(t, f.browse().search2, f.user, "query=Paper", &got); code != 0 { t.Fatalf("search2: error %d", code) } res := got.SearchResult2 if len(res.Albums) != 1 || res.Albums[0].Title != "Paper Boats" || !res.Albums[0].IsDir { t.Errorf("albums = %+v, want the Paper Boats folder", res.Albums) } if res.Artists == nil || len(res.Artists) != 0 || res.Songs == nil || len(res.Songs) != 0 { t.Errorf("artists = %v, songs = %v, want both empty lists", res.Artists, res.Songs) } if code := call(t, f.browse().search2, f.user, "query=Harbour", &got); code != 0 { t.Fatalf("search2: error %d", code) } if songs := titles(got.SearchResult2.Songs); !slices.Equal(songs, []string{"Harbour"}) { t.Errorf("songs = %v, want [Harbour]", songs) } }