Add albums to Artist

This commit is contained in:
simojenki
2021-03-05 18:36:38 +11:00
parent 17d48434c0
commit 979f72206e
6 changed files with 309 additions and 206 deletions

View File

@@ -35,11 +35,14 @@ export type Images = {
} }
export type Artist = ArtistSummary & { export type Artist = ArtistSummary & {
albums: Album[]
}; };
export type Album = { export type Album = {
id: string; id: string;
name: string; name: string;
year: string | undefined;
genre: string | undefined;
}; };
export type Paging = { export type Paging = {

View File

@@ -30,6 +30,9 @@ export const t_and_s = (password: string) => {
}; };
}; };
export const isDodgyImage = (url: string) =>
url.endsWith("2a96cbd8b46e442fc41c2b86b821562f.png");
export type SubconicEnvelope = { export type SubconicEnvelope = {
"subsonic-response": SubsonicResponse; "subsonic-response": SubsonicResponse;
}; };
@@ -38,11 +41,20 @@ export type SubsonicResponse = {
_status: string; _status: string;
}; };
export type album = {
_id: string;
_name: string;
_genre: string | undefined;
_year: string | undefined;
_coverArt: string;
};
export type artist = { export type artist = {
_id: string; _id: string;
_name: string; _name: string;
_albumCount: string; _albumCount: string;
_artistImageUrl: string | undefined; _artistImageUrl: string | undefined;
album: album[];
}; };
export type GetArtistsResponse = SubsonicResponse & { export type GetArtistsResponse = SubsonicResponse & {
@@ -163,10 +175,24 @@ export class Navidrome implements MusicService {
}, },
})); }));
getArtist = (credentials: Credentials, id: string): Promise<IdName> => getArtist = (
credentials: Credentials,
id: string
): Promise<IdName & { albums: Album[] }> =>
this.get<GetArtistResponse>(credentials, "/rest/getArtist", { this.get<GetArtistResponse>(credentials, "/rest/getArtist", {
id, id,
}).then((it) => ({ id: it.artist._id, name: it.artist._name })); })
.then((it) => it.artist)
.then((it) => ({
id: it._id,
name: it._name,
albums: it.album.map((album) => ({
id: album._id,
name: album._name,
year: album._year,
genre: album._genre,
})),
}));
async login(token: string) { async login(token: string) {
const navidrome = this; const navidrome = this;
@@ -208,6 +234,7 @@ export class Navidrome implements MusicService {
id: artist.id, id: artist.id,
name: artist.name, name: artist.name,
image: artistInfo.image, image: artistInfo.image,
albums: artist.albums,
})), })),
albums: (_: AlbumQuery): Promise<Result<Album>> => { albums: (_: AlbumQuery): Promise<Result<Album>> => {
return Promise.resolve({ results: [], total: 0 }); return Promise.resolve({ results: [], total: 0 });

View File

@@ -3,7 +3,7 @@ import { v4 as uuid } from "uuid";
import { Credentials } from "../src/smapi"; import { Credentials } from "../src/smapi";
import { Service, Device } from "../src/sonos"; import { Service, Device } from "../src/sonos";
import { Album, Artist } from "../src/music_service"; import { Artist } from "../src/music_service";
const randomInt = (max: number) => Math.floor(Math.random() * max); const randomInt = (max: number) => Math.floor(Math.random() * max);
const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`; const randomIpAddress = () => `127.0.${randomInt(255)}.${randomInt(255)}`;
@@ -68,40 +68,46 @@ export function someCredentials(token: string): Credentials {
}; };
} }
export type ArtistWithAlbums = Artist & { export const BOB_MARLEY: Artist = {
albums: Album[];
};
export const BOB_MARLEY: ArtistWithAlbums = {
id: uuid(), id: uuid(),
name: "Bob Marley", name: "Bob Marley",
albums: [ albums: [
{ id: uuid(), name: "Burin'" }, { id: uuid(), name: "Burin'", year: "1973", genre: "Reggae" },
{ id: uuid(), name: "Exodus" }, { id: uuid(), name: "Exodus", year: "1977", genre: "Reggae" },
{ id: uuid(), name: "Kaya" }, { id: uuid(), name: "Kaya", year: "1978", genre: "Ska" },
], ],
image: { image: {
small: "http://localhost/BOB_MARLEY/sml", small: "http://localhost/BOB_MARLEY/sml",
medium: "http://localhost/BOB_MARLEY/med", medium: "http://localhost/BOB_MARLEY/med",
large: "http://localhost/BOB_MARLEY/lge", large: "http://localhost/BOB_MARLEY/lge",
} },
}; };
export const BLONDIE: ArtistWithAlbums = { export const BLONDIE: Artist = {
id: uuid(), id: uuid(),
name: "Blondie", name: "Blondie",
albums: [ albums: [
{ id: uuid(), name: "Blondie" }, {
{ id: uuid(), name: "Parallel Lines" }, id: uuid(),
name: "Blondie",
year: "1976",
genre: "New Wave",
},
{
id: uuid(),
name: "Parallel Lines",
year: "1978",
genre: "Pop Rock",
},
], ],
image: { image: {
small: undefined, small: undefined,
medium: undefined, medium: undefined,
large: undefined, large: undefined,
} },
}; };
export const MADONNA: ArtistWithAlbums = { export const MADONNA: Artist = {
id: uuid(), id: uuid(),
name: "Madonna", name: "Madonna",
albums: [], albums: [],
@@ -109,27 +115,31 @@ export const MADONNA: ArtistWithAlbums = {
small: "http://localhost/MADONNA/sml", small: "http://localhost/MADONNA/sml",
medium: undefined, medium: undefined,
large: "http://localhost/MADONNA/lge", large: "http://localhost/MADONNA/lge",
} },
}; };
export const METALLICA: ArtistWithAlbums = { export const METALLICA: Artist = {
id: uuid(), id: uuid(),
name: "Metallica", name: "Metallica",
albums: [ albums: [
{ {
id: uuid(), id: uuid(),
name: "Ride the Lightening", name: "Ride the Lightening",
year: "1984",
genre: "Heavy Metal",
}, },
{ {
id: uuid(), id: uuid(),
name: "Master of Puppets", name: "Master of Puppets",
year: "1986",
genre: "Heavy Metal",
}, },
], ],
image: { image: {
small: "http://localhost/METALLICA/sml", small: "http://localhost/METALLICA/sml",
medium: "http://localhost/METALLICA/med", medium: "http://localhost/METALLICA/med",
large: "http://localhost/METALLICA/lge", large: "http://localhost/METALLICA/lge",
} },
}; };
export const ALL_ALBUMS = [ export const ALL_ALBUMS = [

View File

@@ -1,7 +1,6 @@
import { import {
InMemoryMusicService, InMemoryMusicService,
artistWithAlbumsToArtist, artistToArtistSummary,
artistWithAlbumsToArtistSummary,
} from "./in_memory_music_service"; } from "./in_memory_music_service";
import { AuthSuccess, MusicLibrary } from "../src/music_service"; import { AuthSuccess, MusicLibrary } from "../src/music_service";
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
@@ -47,19 +46,9 @@ describe("InMemoryMusicService", () => {
}); });
}); });
describe("artistWithAlbumsToArtist", () => { describe("artistToArtistSummary", () => {
it("should map fields correctly", () => { it("should map fields correctly", () => {
expect(artistWithAlbumsToArtist(BOB_MARLEY)).toEqual({ expect(artistToArtistSummary(BOB_MARLEY)).toEqual({
id: BOB_MARLEY.id,
name: BOB_MARLEY.name,
image: BOB_MARLEY.image,
});
});
});
describe("artistWithAlbumsToArtistSummary", () => {
it("should map fields correctly", () => {
expect(artistWithAlbumsToArtistSummary(BOB_MARLEY)).toEqual({
id: BOB_MARLEY.id, id: BOB_MARLEY.id,
name: BOB_MARLEY.name, name: BOB_MARLEY.name,
image: BOB_MARLEY.image, image: BOB_MARLEY.image,
@@ -85,10 +74,10 @@ describe("InMemoryMusicService", () => {
describe("fetching all", () => { describe("fetching all", () => {
it("should provide an array of artists", async () => { it("should provide an array of artists", async () => {
const artists = [ const artists = [
artistWithAlbumsToArtistSummary(BOB_MARLEY), artistToArtistSummary(BOB_MARLEY),
artistWithAlbumsToArtistSummary(MADONNA), artistToArtistSummary(MADONNA),
artistWithAlbumsToArtistSummary(BLONDIE), artistToArtistSummary(BLONDIE),
artistWithAlbumsToArtistSummary(METALLICA), artistToArtistSummary(METALLICA),
]; ];
expect( expect(
await musicLibrary.artists({ _index: 0, _count: 100 }) await musicLibrary.artists({ _index: 0, _count: 100 })
@@ -102,8 +91,8 @@ describe("InMemoryMusicService", () => {
describe("fetching the second page", () => { describe("fetching the second page", () => {
it("should provide an array of artists", async () => { it("should provide an array of artists", async () => {
const artists = [ const artists = [
artistWithAlbumsToArtistSummary(BLONDIE), artistToArtistSummary(BLONDIE),
artistWithAlbumsToArtistSummary(METALLICA), artistToArtistSummary(METALLICA),
]; ];
expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual({ expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual({
results: artists, results: artists,
@@ -115,9 +104,9 @@ describe("InMemoryMusicService", () => {
describe("fetching the more items than fit on the second page", () => { describe("fetching the more items than fit on the second page", () => {
it("should provide an array of artists", async () => { it("should provide an array of artists", async () => {
const artists = [ const artists = [
artistWithAlbumsToArtistSummary(MADONNA), artistToArtistSummary(MADONNA),
artistWithAlbumsToArtistSummary(BLONDIE), artistToArtistSummary(BLONDIE),
artistWithAlbumsToArtistSummary(METALLICA), artistToArtistSummary(METALLICA),
]; ];
expect( expect(
await musicLibrary.artists({ _index: 1, _count: 50 }) await musicLibrary.artists({ _index: 1, _count: 50 })
@@ -130,10 +119,10 @@ describe("InMemoryMusicService", () => {
describe("when it exists", () => { describe("when it exists", () => {
it("should provide an artist", async () => { it("should provide an artist", async () => {
expect(await musicLibrary.artist(MADONNA.id)).toEqual( expect(await musicLibrary.artist(MADONNA.id)).toEqual(
artistWithAlbumsToArtist(MADONNA) MADONNA
); );
expect(await musicLibrary.artist(BLONDIE.id)).toEqual( expect(await musicLibrary.artist(BLONDIE.id)).toEqual(
artistWithAlbumsToArtist(BLONDIE) BLONDIE
); );
}); });
}); });

View File

@@ -1,7 +1,7 @@
import { option as O } from "fp-ts"; import { option as O } from "fp-ts";
import { pipe } from "fp-ts/lib/function"; import { pipe } from "fp-ts/lib/function";
import { ArtistWithAlbums } from "./builders";
import { import {
MusicService, MusicService,
Credentials, Credentials,
@@ -16,18 +16,14 @@ import {
ArtistSummary, ArtistSummary,
} from "../src/music_service"; } from "../src/music_service";
export const artistWithAlbumsToArtistSummary = ( export const artistToArtistSummary = (
it: ArtistWithAlbums it: Artist
): ArtistSummary => ({ ): ArtistSummary => ({
id: it.id, id: it.id,
name: it.name, name: it.name,
image: it.image, image: it.image,
}); });
export const artistWithAlbumsToArtist = (it: ArtistWithAlbums): Artist => ({
...artistWithAlbumsToArtistSummary(it),
});
type P<T> = (t: T) => boolean; type P<T> = (t: T) => boolean;
const all: P<any> = (_: any) => true; const all: P<any> = (_: any) => true;
const artistWithId = (id: string): P<Artist> => (artist: Artist) => const artistWithId = (id: string): P<Artist> => (artist: Artist) =>
@@ -35,7 +31,7 @@ const artistWithId = (id: string): P<Artist> => (artist: Artist) =>
export class InMemoryMusicService implements MusicService { export class InMemoryMusicService implements MusicService {
users: Record<string, string> = {}; users: Record<string, string> = {};
artists: ArtistWithAlbums[] = []; artists: Artist[] = [];
generateToken({ generateToken({
username, username,
@@ -62,14 +58,13 @@ export class InMemoryMusicService implements MusicService {
return Promise.reject("Invalid auth token"); return Promise.reject("Invalid auth token");
return Promise.resolve({ return Promise.resolve({
artists: (q: ArtistQuery) => artists: (q: ArtistQuery) =>
Promise.resolve(this.artists.map(artistWithAlbumsToArtistSummary)) Promise.resolve(this.artists.map(artistToArtistSummary))
.then(slice2(q)) .then(slice2(q))
.then(asResult), .then(asResult),
artist: (id: string) => artist: (id: string) =>
pipe( pipe(
this.artists.find((it) => it.id === id), this.artists.find((it) => it.id === id),
O.fromNullable, O.fromNullable,
O.map(artistWithAlbumsToArtist),
O.map(it => Promise.resolve(it)), O.map(it => Promise.resolve(it)),
O.getOrElse(() => Promise.reject(`No artist with id '${id}'`)) O.getOrElse(() => Promise.reject(`No artist with id '${id}'`))
), ),
@@ -99,7 +94,7 @@ export class InMemoryMusicService implements MusicService {
return this; return this;
} }
hasArtists(...newArtists: ArtistWithAlbums[]) { hasArtists(...newArtists: Artist[]) {
this.artists = [...this.artists, ...newArtists]; this.artists = [...this.artists, ...newArtists];
return this; return this;
} }

View File

@@ -1,13 +1,13 @@
import { Md5 } from "ts-md5/dist/md5"; import { Md5 } from "ts-md5/dist/md5";
import { Navidrome, t } from "../src/navidrome"; import { isDodgyImage, Navidrome, t } from "../src/navidrome";
import encryption from "../src/encryption"; import encryption from "../src/encryption";
import axios from "axios"; import axios from "axios";
jest.mock("axios"); jest.mock("axios");
import randomString from "../src/random_string"; import randomString from "../src/random_string";
import { Artist, AuthSuccess, Images } from "../src/music_service"; import { Album, Artist, AuthSuccess, Images } from "../src/music_service";
jest.mock("../src/random_string"); jest.mock("../src/random_string");
describe("t", () => { describe("t", () => {
@@ -18,6 +18,26 @@ describe("t", () => {
}); });
}); });
describe("isDodgyImage", () => {
describe("when ends with 2a96cbd8b46e442fc41c2b86b821562f.png", () => {
it("is dodgy", () => {
expect(
isDodgyImage("http://something/2a96cbd8b46e442fc41c2b86b821562f.png")
).toEqual(true);
});
});
describe("when does not end with 2a96cbd8b46e442fc41c2b86b821562f.png", () => {
it("is dodgy", () => {
expect(isDodgyImage("http://something/somethingelse.png")).toEqual(false);
expect(
isDodgyImage(
"http://something/2a96cbd8b46e442fc41c2b86b821562f.png?withsomequerystring=true"
)
).toEqual(false);
});
});
});
const ok = (data: string) => ({ const ok = (data: string) => ({
status: 200, status: 200,
data, data,
@@ -36,6 +56,31 @@ const artistInfoXml = (
</artistInfo> </artistInfo>
</subsonic-response>`; </subsonic-response>`;
const albumXml = (artist: Artist, album: Album) => `<album id="${album.id}"
parent="${artist.id}"
isDir="true"
title="${album.name}" name="${album.name}" album="${album.name}"
artist="${artist.name}"
genre="${album.genre}"
coverArt="foo"
duration="123"
playCount="4"
year="${album.year}"
created="2021-01-07T08:19:55.834207205Z"
artistId="${artist.id}"
songCount="19"
isVideo="false"></album>`;
const artistXml = (
artist: Artist
) => `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)">
<artist id="${artist.id}" name="${artist.name}" albumCount="${
artist.albums.length
}" artistImageUrl="....">
${artist.albums.map((album) => albumXml(artist, album))}
</artist>
</subsonic-response>`;
const PING_OK = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"></subsonic-response>`; const PING_OK = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"></subsonic-response>`;
describe("Navidrome", () => { describe("Navidrome", () => {
@@ -101,50 +146,64 @@ describe("Navidrome", () => {
}); });
describe("getArtist", () => { describe("getArtist", () => {
const artistId = "someUUID_123"; const album1: Album = {
const artistName = "BananaMan"; id: "album1",
name: "super album",
year: "2001",
genre: "Pop",
};
const artistXml = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"> const album2: Album = {
<artist id="${artistId}" name="${artistName}" albumCount="9" artistImageUrl="...."> id: "album2",
</artist> name: "bad album",
</subsonic-response>`; year: "2002",
genre: "Rock",
};
const getArtistInfoXml = artistInfoXml({ const artist: Artist = {
id: "someUUID_123",
name: "BananaMan",
image: {
small: "sml1", small: "sml1",
medium: "med1", medium: "med1",
large: "lge1", large: "lge1",
}); },
albums: [album1, album2],
};
beforeEach(() => { beforeEach(() => {
mockGET mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK))) .mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(artistXml))) .mockImplementationOnce(() => Promise.resolve(ok(artistXml(artist))))
.mockImplementationOnce(() => Promise.resolve(ok(getArtistInfoXml))); .mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist.image)))
);
}); });
it("should do it", async () => { it.only("should do it", async () => {
const artist = await navidrome const result: Artist = await navidrome
.generateToken({ username, password }) .generateToken({ username, password })
.then((it) => it as AuthSuccess) .then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.artist(artistId)); .then((it) => it.artist(artist.id));
expect(artist).toEqual({ expect(result).toEqual({
id: artistId, id: artist.id,
name: artistName, name: artist.name,
image: { small: "sml1", medium: "med1", large: "lge1" }, image: { small: "sml1", medium: "med1", large: "lge1" },
albums: [album1, album2]
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtist`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtist`, {
params: { params: {
id: artistId, id: artist.id,
...authParams, ...authParams,
}, },
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: artistId, id: artist.id,
...authParams, ...authParams,
}, },
}); });
@@ -152,42 +211,6 @@ describe("Navidrome", () => {
}); });
describe("getArtists", () => { describe("getArtists", () => {
const artist1: Artist = {
id: "artist1.id",
name: "artist1.name",
image: { small: "s1", medium: "m1", large: "l1" },
};
const artist2: Artist = {
id: "artist2.id",
name: "artist2.name",
image: { small: "s2", medium: "m2", large: "l2" },
};
const artist3: Artist = {
id: "artist3.id",
name: "artist3.name",
image: { small: "s3", medium: "m3", large: "l3" },
};
const artist4: Artist = {
id: "artist4.id",
name: "artist4.name",
image: { small: "s4", medium: "m4", large: "l4" },
};
const getArtistsXml = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)">
<artists lastModified="1614586749000" ignoredArticles="The El La Los Las Le Les Os As O A">
<index name="#">
<artist id="${artist1.id}" name="${artist1.name}" albumCount="22"></artist>
<artist id="${artist2.id}" name="${artist2.name}" albumCount="9"></artist>
</index>
<index name="A">
<artist id="${artist3.id}" name="${artist3.name}" albumCount="2"></artist>
</index>
<index name="B">
<artist id="${artist4.id}" name="${artist4.name}" albumCount="2"></artist>
</index>
</artists>
</subsonic-response>`;
describe("when there are no results", () => { describe("when there are no results", () => {
beforeEach(() => { beforeEach(() => {
mockGET mockGET
@@ -222,6 +245,47 @@ describe("Navidrome", () => {
}); });
}); });
describe("when there are artists", () => {
const artist1: Artist = {
id: "artist1.id",
name: "artist1.name",
image: { small: "s1", medium: "m1", large: "l1" },
albums: [],
};
const artist2: Artist = {
id: "artist2.id",
name: "artist2.name",
image: { small: "s2", medium: "m2", large: "l2" },
albums: [],
};
const artist3: Artist = {
id: "artist3.id",
name: "artist3.name",
image: { small: "s3", medium: "m3", large: "l3" },
albums: [],
};
const artist4: Artist = {
id: "artist4.id",
name: "artist4.name",
image: { small: "s4", medium: "m4", large: "l4" },
albums: [],
};
const getArtistsXml = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)">
<artists lastModified="1614586749000" ignoredArticles="The El La Los Las Le Les Os As O A">
<index name="#">
<artist id="${artist1.id}" name="${artist1.name}" albumCount="22"></artist>
<artist id="${artist2.id}" name="${artist2.name}" albumCount="9"></artist>
</index>
<index name="A">
<artist id="${artist3.id}" name="${artist3.name}" albumCount="2"></artist>
</index>
<index name="B">
<artist id="${artist4.id}" name="${artist4.name}" albumCount="2"></artist>
</index>
</artists>
</subsonic-response>`;
describe("when no paging is in effect", () => { describe("when no paging is in effect", () => {
beforeEach(() => { beforeEach(() => {
mockGET mockGET
@@ -248,8 +312,16 @@ describe("Navidrome", () => {
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.artists({ _index: 0, _count: 100 })); .then((it) => it.artists({ _index: 0, _count: 100 }));
const expectedResults = [artist1, artist2, artist3, artist4].map(
(it) => ({
id: it.id,
name: it.name,
image: it.image,
})
);
expect(artists).toEqual({ expect(artists).toEqual({
results: [artist1, artist2, artist3, artist4], results: expectedResults,
total: 4, total: 4,
}); });
@@ -303,7 +375,13 @@ describe("Navidrome", () => {
.then((it) => navidrome.login(it.authToken)) .then((it) => navidrome.login(it.authToken))
.then((it) => it.artists({ _index: 1, _count: 2 })); .then((it) => it.artists({ _index: 1, _count: 2 }));
expect(artists).toEqual({ results: [artist2, artist3], total: 4 }); const expectedResults = [artist2, artist3].map((it) => ({
id: it.id,
name: it.name,
image: it.image,
}));
expect(artists).toEqual({ results: expectedResults, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams, params: authParams,
@@ -323,4 +401,5 @@ describe("Navidrome", () => {
}); });
}); });
}); });
});
}); });