From 86a2411f212eb6f124a0450909324ff302ae12de Mon Sep 17 00:00:00 2001 From: simojenki Date: Sun, 7 Mar 2021 11:37:13 +1100 Subject: [PATCH] Genres on main menu --- src/smapi.ts | 20 ++++++++++++- tests/smapi.test.ts | 72 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/smapi.ts b/src/smapi.ts index d67e419..67bd2af 100644 --- a/src/smapi.ts +++ b/src/smapi.ts @@ -164,6 +164,12 @@ const container = ({ title, }); +const genre = (genre: string) => ({ + itemType: "container", + id: `genre:${genre}`, + title: genre, +}) + const album = (album: AlbumSummary) => ({ itemType: "album", id: `album:${album.id}`, @@ -237,9 +243,10 @@ function bindSmapiSoapServiceToExpress( mediaCollection: [ container({ id: "artists", title: "Artists" }), container({ id: "albums", title: "Albums" }), + container({ id: "genres", title: "Genres" }), ], index: 0, - total: 2, + total: 3, }); case "artists": return await musicLibrary.artists(paging).then((result) => @@ -263,6 +270,17 @@ function bindSmapiSoapServiceToExpress( total: result.total, }) ); + case "genres": + return await musicLibrary + .genres() + .then(slice2(paging)) + .then(([page, total]) => + getMetadataResult({ + mediaCollection: page.map(genre), + index: paging._index, + total, + }) + ); case "artist": return await musicLibrary .artist(typeId!) diff --git a/tests/smapi.test.ts b/tests/smapi.test.ts index 552d297..1c824be 100644 --- a/tests/smapi.test.ts +++ b/tests/smapi.test.ts @@ -353,23 +353,79 @@ describe("api", () => { mediaCollection: [ { itemType: "container", id: "artists", title: "Artists" }, { itemType: "container", id: "albums", title: "Albums" }, + { itemType: "container", id: "genres", title: "Genres" }, ], index: 0, - total: 2, + total: 3, }) ); }); }); + describe("asking for a genres", () => { + const artist1 = anArtist({ + albums: [anAlbum({ genre: "Pop" }), anAlbum({ genre: "Rock" })], + }); + const artist2 = anArtist({ + albums: [ + anAlbum({ genre: "Trip-Hop" }), + anAlbum({ genre: "Punk" }), + anAlbum({ genre: "Pop" }), + ], + }); + + const expectedGenres = ["Pop", "Punk", "Rock", "Trip-Hop"]; + + beforeEach(() => { + musicService.hasArtists(artist1, artist2); + }); + + describe("asking for all genres", () => { + it("should return a collection of genres", async () => { + const result = await ws.getMetadataAsync({ + id: `genres`, + index: 0, + count: 100, + }); + expect(result[0]).toEqual( + getMetadataResult({ + mediaCollection: expectedGenres.map((genre) => ({ + itemType: "container", + id: `genre:${genre}`, + title: genre, + })), + index: 0, + total: expectedGenres.length, + }) + ); + }); + }); + + describe("asking for a page of genres", () => { + it("should return just that page", async () => { + const result = await ws.getMetadataAsync({ + id: `genres`, + index: 1, + count: 2, + }); + expect(result[0]).toEqual( + getMetadataResult({ + mediaCollection: ["Punk", "Rock"].map((genre) => ({ + itemType: "container", + id: `genre:${genre}`, + title: genre, + })), + index: 1, + total: expectedGenres.length, + }) + ); + }); + }); + }); + describe("asking for a single artist", () => { const artistWithManyAlbums = anArtist({ - albums: [ - anAlbum(), - anAlbum(), - anAlbum(), - anAlbum(), - anAlbum(), - ], + albums: [anAlbum(), anAlbum(), anAlbum(), anAlbum(), anAlbum()], }); beforeEach(() => {