Artist images from getArtistInfo

This commit is contained in:
simojenki
2021-03-05 14:46:12 +11:00
parent 4dae907826
commit 5ad0da5168
7 changed files with 407 additions and 210 deletions

View File

@@ -22,15 +22,19 @@ export type AuthFailure = {
message: string; message: string;
}; };
export type ArtistSummary = {
export type Artist = {
id: string; id: string;
name: string; name: string;
image: { image: Images
small: string | undefined, }
medium: string | undefined,
large: string | undefined, export type Images = {
} small: string | undefined,
medium: string | undefined,
large: string | undefined,
}
export type Artist = ArtistSummary & {
}; };
export type Album = { export type Album = {
@@ -71,7 +75,7 @@ export interface MusicService {
} }
export interface MusicLibrary { export interface MusicLibrary {
artists(q: ArtistQuery): Promise<Result<Artist>>; artists(q: ArtistQuery): Promise<Result<ArtistSummary>>;
artist(id: string): Artist; artist(id: string): Promise<Artist>;
albums(q: AlbumQuery): Promise<Result<Album>>; albums(q: AlbumQuery): Promise<Result<Album>>;
} }

View File

@@ -4,11 +4,14 @@ import {
MusicService, MusicService,
Album, Album,
Artist, Artist,
ArtistSummary,
Result, Result,
slice2, slice2,
asResult, asResult,
AlbumQuery, AlbumQuery,
ArtistQuery, ArtistQuery,
MusicLibrary,
Images,
} from "./music_service"; } from "./music_service";
import X2JS from "x2js"; import X2JS from "x2js";
@@ -35,15 +38,17 @@ export type SubsonicResponse = {
_status: string; _status: string;
}; };
export type artist = {
_id: string;
_name: string;
_albumCount: string;
_artistImageUrl: string | undefined;
};
export type GetArtistsResponse = SubsonicResponse & { export type GetArtistsResponse = SubsonicResponse & {
artists: { artists: {
index: { index: {
artist: { artist: artist[];
_id: string;
_name: string;
_albumCount: string;
_artistImageUrl: string | undefined;
}[];
_name: string; _name: string;
}[]; }[];
}; };
@@ -65,16 +70,29 @@ export type artistInfo = {
largeImageUrl: string | undefined; largeImageUrl: string | undefined;
}; };
export type ArtistInfo = {
image: Images;
};
export type GetArtistInfoResponse = { export type GetArtistInfoResponse = {
artistInfo: artistInfo; artistInfo: artistInfo;
}; };
export type GetArtistResponse = {
artist: artist;
};
export function isError( export function isError(
subsonicResponse: SubsonicResponse subsonicResponse: SubsonicResponse
): subsonicResponse is SubsonicError { ): subsonicResponse is SubsonicError {
return (subsonicResponse as SubsonicError).error !== undefined; return (subsonicResponse as SubsonicError).error !== undefined;
} }
export type IdName = {
id: string;
name: string;
};
export class Navidrome implements MusicService { export class Navidrome implements MusicService {
url: string; url: string;
encryption: Encryption; encryption: Encryption;
@@ -124,46 +142,80 @@ export class Navidrome implements MusicService {
) )
); );
artistInfo = (credentials: Credentials, id: string): Promise<ArtistInfo> =>
this.get<GetArtistInfoResponse>(credentials, "/rest/getArtistInfo", {
id,
}).then((it) => ({
image: {
small: it.artistInfo.smallImageUrl,
medium: it.artistInfo.mediumImageUrl,
large: it.artistInfo.largeImageUrl,
},
}));
async login(token: string) { async login(token: string) {
const navidrome = this; const navidrome = this;
const credentials: Credentials = this.parseToken(token); const credentials: Credentials = this.parseToken(token);
return Promise.resolve({
artists: (q: ArtistQuery): Promise<Result<Artist>> => const musicLibrary: MusicLibrary = {
artists: (q: ArtistQuery): Promise<Result<ArtistSummary>> =>
navidrome navidrome
.get<GetArtistsResponse>(credentials, "/rest/getArtists") .get<GetArtistsResponse>(credentials, "/rest/getArtists")
.then((it) => it.artists.index.flatMap((it) => it.artist)) .then((it) => it.artists.index.flatMap((it) => it.artist || []))
.then((artists) => .then((artists) =>
artists.map((artist) => ({
id: artist._id,
name: artist._name,
}))
)
.then(slice2(q))
.then(asResult)
.then((result) =>
Promise.all( Promise.all(
artists.map((artist) => result.results.map((idName: IdName) =>
navidrome navidrome.artistInfo(credentials, idName.id).then((artist) => ({
.get<GetArtistInfoResponse>( total: result.total,
credentials, result: {
"/rest/getArtistInfo", id: idName.id,
{ id: artist._id } name: idName.name,
) image: artist.image,
.then((it) => it.artistInfo) },
.then((artistInfo) => ({ }))
id: artist._id,
name: artist._name,
image: {
small: artistInfo.smallImageUrl,
medium: artistInfo.mediumImageUrl,
large: artistInfo.largeImageUrl,
},
}))
) )
) )
) )
.then(slice2(q)) .then((resultWithInfo) => {
.then(asResult), return {
artist: (id: string) => ({ total: resultWithInfo[0]?.total || 0,
id, results: resultWithInfo.map((it) => it.result),
name: id, };
image: { small: undefined, medium: undefined, large: undefined }, }),
}), artist: async (id: string): Promise<Artist> => {
return navidrome
.get<GetArtistResponse>(credentials, "/rest/getArtist", {
id,
})
.then(async (artist: GetArtistResponse) => {
return navidrome
.get<GetArtistInfoResponse>(credentials, "/rest/getArtistInfo", {
id,
})
.then((artistInfo: GetArtistInfoResponse) => ({
id: artist.artist._id,
name: artist.artist._name,
image: {
small: artistInfo.artistInfo.smallImageUrl,
medium: artistInfo.artistInfo.mediumImageUrl,
large: artistInfo.artistInfo.largeImageUrl,
},
}));
});
},
albums: (_: AlbumQuery): Promise<Result<Album>> => { albums: (_: AlbumQuery): Promise<Result<Album>> => {
return Promise.resolve({ results: [], total: 0 }); return Promise.resolve({ results: [], total: 0 });
}, },
}); };
return Promise.resolve(musicLibrary);
} }
} }

View File

@@ -2,17 +2,18 @@ import express, { Express } from "express";
import * as Eta from "eta"; import * as Eta from "eta";
import morgan from "morgan"; import morgan from "morgan";
import { Sonos, Service } from "./sonos";
import { import {
Sonos, SOAP_PATH,
Service, STRINGS_ROUTE,
} from "./sonos"; PRESENTATION_MAP_ROUTE,
import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE, LOGIN_ROUTE } from './smapi'; LOGIN_ROUTE,
} from "./smapi";
import { LinkCodes, InMemoryLinkCodes } from "./link_codes"; import { LinkCodes, InMemoryLinkCodes } from "./link_codes";
import { MusicService, isSuccess } from "./music_service"; import { MusicService, isSuccess } from "./music_service";
// import logger from "./logger"; // import logger from "./logger";
import bindSmapiSoapServiceToExpress from "./smapi"; import bindSmapiSoapServiceToExpress from "./smapi";
function server( function server(
sonos: Sonos, sonos: Sonos,
bonobService: Service, bonobService: Service,
@@ -65,7 +66,7 @@ function server(
res.render("login", { res.render("login", {
bonobService, bonobService,
linkCode: req.query.linkCode, linkCode: req.query.linkCode,
loginRoute: LOGIN_ROUTE loginRoute: LOGIN_ROUTE,
}); });
}); });
@@ -85,7 +86,7 @@ function server(
res.render("success", { res.render("success", {
message: `Login successful!`, message: `Login successful!`,
}); });
} else { } else {
res.status(403).render("failure", { res.status(403).render("failure", {
message: `Login failed! ${authResult.message}!`, message: `Login failed! ${authResult.message}!`,
}); });
@@ -112,7 +113,33 @@ function server(
res.send(""); res.send("");
}); });
bindSmapiSoapServiceToExpress(app, SOAP_PATH, webAddress, linkCodes, musicService); // app.get("/artist/:artistId/image", (req, res) => {
// console.log(`Trying to load image for ${req.params["artistId"]}, token ${JSON.stringify(req.cookies)}`)
// const authToken = req.headers["X-AuthToken"]! as string;
// const artistId = req.params["artistId"]!;
// musicService
// .login(authToken)
// .then((it) => it.artist(artistId))
// .then(artist => artist.image.small)
// .then((url) => {
// if (url) {
// console.log(`${artistId} sending 307 -> ${url}`)
// res.setHeader("Location", url);
// res.status(307).send();
// } else {
// console.log(`${artistId} sending 404`)
// res.status(404).send();
// }
// });
// });
bindSmapiSoapServiceToExpress(
app,
SOAP_PATH,
webAddress,
linkCodes,
musicService
);
return app; return app;
} }

View File

@@ -6,7 +6,12 @@ import path from "path";
import logger from "./logger"; import logger from "./logger";
import { LinkCodes } from "./link_codes"; import { LinkCodes } from "./link_codes";
import { Album, Artist, MusicLibrary, MusicService } from "./music_service"; import {
Album,
ArtistSummary,
MusicLibrary,
MusicService,
} from "./music_service";
export const LOGIN_ROUTE = "/login"; export const LOGIN_ROUTE = "/login";
export const SOAP_PATH = "/ws/sonos"; export const SOAP_PATH = "/ws/sonos";
@@ -227,35 +232,41 @@ function bindSmapiSoapServiceToExpress(
case "artists": case "artists":
return await musicLibrary return await musicLibrary
.artists(paging) .artists(paging)
.then(({ results, total }: { results: Artist[], total: number}) => .then(
getMetadataResult({ ({
mediaCollection: results.map((it) => results,
({ total,
}: {
results: ArtistSummary[];
total: number;
}) =>
getMetadataResult({
mediaCollection: results.map((it) => ({
itemType: "artist", itemType: "artist",
id: `artist:${it.id}`, id: `artist:${it.id}`,
artistId: it.id, artistId: it.id,
title: it.name, title: it.name,
albumArtURI: it.image.small albumArtURI: it.image.small,
}) })),
), index: paging._index,
index: paging._index, total,
total, })
})
); );
case "albums": case "albums":
return await musicLibrary return await musicLibrary
.albums(paging) .albums(paging)
.then(({ results, total }: { results: Album[], total: number}) => .then(
getMetadataResult({ ({ results, total }: { results: Album[]; total: number }) =>
mediaCollection: results.map((it) => getMetadataResult({
container({ mediaCollection: results.map((it) =>
id: `album:${it.id}`, container({
title: it.name, id: `album:${it.id}`,
}) title: it.name,
), })
index: paging._index, ),
total, index: paging._index,
}) total,
})
); );
default: default:
throw `Unsupported id:${id}`; throw `Unsupported id:${id}`;

View File

@@ -1,4 +1,8 @@
import { InMemoryMusicService, artistWithAlbumsToArtist } from "./in_memory_music_service"; import {
InMemoryMusicService,
artistWithAlbumsToArtist,
artistWithAlbumsToArtistSummary,
} 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";
import { import {
@@ -48,8 +52,18 @@ describe("InMemoryMusicService", () => {
expect(artistWithAlbumsToArtist(BOB_MARLEY)).toEqual({ expect(artistWithAlbumsToArtist(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,
}) });
});
});
describe("artistWithAlbumsToArtistSummary", () => {
it("should map fields correctly", () => {
expect(artistWithAlbumsToArtistSummary(BOB_MARLEY)).toEqual({
id: BOB_MARLEY.id,
name: BOB_MARLEY.name,
image: BOB_MARLEY.image,
});
}); });
}); });
@@ -71,12 +85,14 @@ 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 = [
artistWithAlbumsToArtist(BOB_MARLEY), artistWithAlbumsToArtistSummary(BOB_MARLEY),
artistWithAlbumsToArtist(MADONNA), artistWithAlbumsToArtistSummary(MADONNA),
artistWithAlbumsToArtist(BLONDIE), artistWithAlbumsToArtistSummary(BLONDIE),
artistWithAlbumsToArtist(METALLICA), artistWithAlbumsToArtistSummary(METALLICA),
]; ];
expect(await musicLibrary.artists({ _index: 0, _count: 100 })).toEqual({ expect(
await musicLibrary.artists({ _index: 0, _count: 100 })
).toEqual({
results: artists, results: artists,
total: 4, total: 4,
}); });
@@ -86,8 +102,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 = [
artistWithAlbumsToArtist(BLONDIE), artistWithAlbumsToArtistSummary(BLONDIE),
artistWithAlbumsToArtist(METALLICA), artistWithAlbumsToArtistSummary(METALLICA),
]; ];
expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual({ expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual({
results: artists, results: artists,
@@ -99,9 +115,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 = [
artistWithAlbumsToArtist(MADONNA), artistWithAlbumsToArtistSummary(MADONNA),
artistWithAlbumsToArtist(BLONDIE), artistWithAlbumsToArtistSummary(BLONDIE),
artistWithAlbumsToArtist(METALLICA), artistWithAlbumsToArtistSummary(METALLICA),
]; ];
expect( expect(
await musicLibrary.artists({ _index: 1, _count: 50 }) await musicLibrary.artists({ _index: 1, _count: 50 })
@@ -112,15 +128,19 @@ describe("InMemoryMusicService", () => {
describe("artist", () => { describe("artist", () => {
describe("when it exists", () => { describe("when it exists", () => {
it("should provide an artist", () => { it("should provide an artist", async () => {
expect(musicLibrary.artist(MADONNA.id)).toEqual(artistWithAlbumsToArtist(MADONNA)); expect(await musicLibrary.artist(MADONNA.id)).toEqual(
expect(musicLibrary.artist(BLONDIE.id)).toEqual(artistWithAlbumsToArtist(BLONDIE)); artistWithAlbumsToArtist(MADONNA)
);
expect(await musicLibrary.artist(BLONDIE.id)).toEqual(
artistWithAlbumsToArtist(BLONDIE)
);
}); });
}); });
describe("when it doesnt exist", () => { describe("when it doesnt exist", () => {
it("should provide an artist", () => { it("should blow up", async () => {
expect(() => musicLibrary.artist("-1")).toThrow( return expect(musicLibrary.artist("-1")).rejects.toEqual(
"No artist with id '-1'" "No artist with id '-1'"
); );
}); });
@@ -130,30 +150,50 @@ describe("InMemoryMusicService", () => {
describe("albums", () => { describe("albums", () => {
describe("fetching with no filtering", () => { describe("fetching with no filtering", () => {
it("should return all the albums for all the artists", async () => { it("should return all the albums for all the artists", async () => {
expect(await musicLibrary.albums({ _index: 0, _count: 100 })).toEqual({ expect(await musicLibrary.albums({ _index: 0, _count: 100 })).toEqual(
results: ALL_ALBUMS, {
total: ALL_ALBUMS.length, results: ALL_ALBUMS,
}); total: ALL_ALBUMS.length,
}
);
}); });
}); });
describe("fetching for a single artist", () => { describe("fetching for a single artist", () => {
it("should return them all if the artist has some", async () => { it("should return them all if the artist has some", async () => {
expect(await musicLibrary.albums({ artistId: BLONDIE.id, _index: 0, _count: 100 })).toEqual({ expect(
await musicLibrary.albums({
artistId: BLONDIE.id,
_index: 0,
_count: 100,
})
).toEqual({
results: BLONDIE.albums, results: BLONDIE.albums,
total: BLONDIE.albums.length, total: BLONDIE.albums.length,
}); });
}); });
it("should return empty list of the artists does not have any", async () => { it("should return empty list of the artists does not have any", async () => {
expect(await musicLibrary.albums({ artistId: MADONNA.id, _index: 0, _count: 100 })).toEqual({ expect(
await musicLibrary.albums({
artistId: MADONNA.id,
_index: 0,
_count: 100,
})
).toEqual({
results: [], results: [],
total: 0, total: 0,
}); });
}); });
it("should return empty list if the artist id is not valid", async () => { it("should return empty list if the artist id is not valid", async () => {
expect(await musicLibrary.albums({ artistId: uuid(), _index: 0, _count: 100 })).toEqual({ expect(
await musicLibrary.albums({
artistId: uuid(),
_index: 0,
_count: 100,
})
).toEqual({
results: [], results: [],
total: 0, total: 0,
}); });

View File

@@ -13,18 +13,20 @@ import {
AlbumQuery, AlbumQuery,
slice2, slice2,
asResult, asResult,
ArtistSummary,
} from "../src/music_service"; } from "../src/music_service";
export const artistWithAlbumsToArtist = (it: ArtistWithAlbums): Artist => ({ export const artistWithAlbumsToArtistSummary = (
it: ArtistWithAlbums
): ArtistSummary => ({
id: it.id, id: it.id,
name: it.name, name: it.name,
image: it.image image: it.image,
}); });
const getOrThrow = (message: string) => export const artistWithAlbumsToArtist = (it: ArtistWithAlbums): Artist => ({
O.getOrElseW(() => { ...artistWithAlbumsToArtistSummary(it),
throw message; });
});
type P<T> = (t: T) => boolean; type P<T> = (t: T) => boolean;
const all: P<any> = (_: any) => true; const all: P<any> = (_: any) => true;
@@ -60,7 +62,7 @@ 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(artistWithAlbumsToArtist)) Promise.resolve(this.artists.map(artistWithAlbumsToArtistSummary))
.then(slice2(q)) .then(slice2(q))
.then(asResult), .then(asResult),
artist: (id: string) => artist: (id: string) =>
@@ -68,7 +70,8 @@ export class InMemoryMusicService implements MusicService {
this.artists.find((it) => it.id === id), this.artists.find((it) => it.id === id),
O.fromNullable, O.fromNullable,
O.map(artistWithAlbumsToArtist), O.map(artistWithAlbumsToArtist),
getOrThrow(`No artist with id '${id}'`) O.map(it => Promise.resolve(it)),
O.getOrElse(() => Promise.reject(`No artist with id '${id}'`))
), ),
albums: (q: AlbumQuery) => albums: (q: AlbumQuery) =>
Promise.resolve( Promise.resolve(

View File

@@ -1,13 +1,13 @@
import { Md5 } from "ts-md5/dist/md5"; import { Md5 } from "ts-md5/dist/md5";
import { Navidrome, t, artistInfo } from "../src/navidrome"; import { 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 { AuthSuccess } from "../src/music_service"; import { Artist, AuthSuccess, Images } from "../src/music_service";
jest.mock("../src/random_string"); jest.mock("../src/random_string");
describe("t", () => { describe("t", () => {
@@ -24,23 +24,21 @@ const ok = (data: string) => ({
}); });
const artistInfoXml = ( const artistInfoXml = (
artistInfo: Partial<artistInfo> images: Images
) => `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"> ) => `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)">
<artistInfo> <artistInfo>
<biography></biography> <biography></biography>
<musicBrainzId></musicBrainzId> <musicBrainzId></musicBrainzId>
<lastFmUrl></lastFmUrl> <lastFmUrl></lastFmUrl>
<smallImageUrl>${artistInfo.smallImageUrl || ""}</smallImageUrl> <smallImageUrl>${images.small || ""}</smallImageUrl>
<mediumImageUrl>${ <mediumImageUrl>${images.medium || ""}</mediumImageUrl>
artistInfo.mediumImageUrl || "" <largeImageUrl>${images.large || ""}</largeImageUrl>
}</mediumImageUrl>
<largeImageUrl>${artistInfo.largeImageUrl || ""}</largeImageUrl>
</artistInfo> </artistInfo>
</subsonic-response>`; </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", () => {
const url = "http://127.0.0.22:4567"; const url = "http://127.0.0.22:4567";
const username = "user1"; const username = "user1";
const password = "pass1"; const password = "pass1";
@@ -49,7 +47,7 @@ describe("navidrome", () => {
const navidrome = new Navidrome(url, encryption("secret")); const navidrome = new Navidrome(url, encryption("secret"));
const mockedRandomString = (randomString as unknown) as jest.Mock; const mockedRandomString = (randomString as unknown) as jest.Mock;
const mockGET = jest.fn() const mockGET = jest.fn();
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
@@ -102,54 +100,147 @@ describe("navidrome", () => {
}); });
}); });
describe("getArtists", () => { describe("getArtist", () => {
const getArtistsXml = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)"> const artistId = "someUUID_123";
<artists lastModified="1614586749000" ignoredArticles="The El La Los Las Le Les Os As O A"> const artistName = "BananaMan";
<index name="#">
<artist id="2911b2d67a6b11eb804dd360a6225680" name="artist1" albumCount="22"></artist>
<artist id="3c0b9d7a7a6b11eb9773f398e6236ad6" name="artist2" albumCount="9"></artist>
</index>
<index name="A">
<artist id="3c5113007a6b11eb87173bfb9b07f9b1" name="artist3" albumCount="2"></artist>
</index>
<index name="B">
<artist id="3ca781c27a6b11eb897ebbb5773603ad" name="artist4" albumCount="2"></artist>
</index>
</artists>
</subsonic-response>`;
const artist1_getArtistInfoXml = artistInfoXml({ const artistXml = `<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.16.1" type="navidrome" serverVersion="0.40.0 (8799358a)">
smallImageUrl: "sml1", <artist id="${artistId}" name="${artistName}" albumCount="9" artistImageUrl="....">
mediumImageUrl: "med1", </artist>
largeImageUrl: "lge1", </subsonic-response>`;
});
const artist2_getArtistInfoXml = artistInfoXml({ const getArtistInfoXml = artistInfoXml({
smallImageUrl: "sml2", small: "sml1",
mediumImageUrl: undefined, medium: "med1",
largeImageUrl: "lge2", large: "lge1",
});
const artist3_getArtistInfoXml = artistInfoXml({
smallImageUrl: undefined,
mediumImageUrl: "med3",
largeImageUrl: undefined,
});
const artist4_getArtistInfoXml = artistInfoXml({
smallImageUrl: "sml4",
mediumImageUrl: "med4",
largeImageUrl: "lge4",
}); });
beforeEach(() => { beforeEach(() => {
mockGET mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK))) .mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(getArtistsXml))) .mockImplementationOnce(() => Promise.resolve(ok(artistXml)))
.mockImplementationOnce(() => Promise.resolve(ok(artist1_getArtistInfoXml))) .mockImplementationOnce(() => Promise.resolve(ok(getArtistInfoXml)));
.mockImplementationOnce(() => Promise.resolve(ok(artist2_getArtistInfoXml))) });
.mockImplementationOnce(() => Promise.resolve(ok(artist3_getArtistInfoXml)))
.mockImplementationOnce(() => Promise.resolve(ok(artist4_getArtistInfoXml))); it("should do it", async () => {
const artist = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.artist(artistId));
expect(artist).toEqual({
id: artistId,
name: artistName,
image: { small: "sml1", medium: "med1", large: "lge1" },
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtist`, {
params: {
id: artistId,
...authParams,
},
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: {
id: artistId,
...authParams,
},
});
});
});
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", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(
ok(`<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="#">
</index>
<index name="A">
</index>
<index name="B">
</index>
</artists>
</subsonic-response>`)
)
);
});
it("should return empty", async () => {
const artists = await navidrome
.generateToken({ username, password })
.then((it) => it as AuthSuccess)
.then((it) => navidrome.login(it.authToken))
.then((it) => it.artists({ _index: 0, _count: 100 }));
expect(artists).toEqual({
results: [],
total: 0,
});
});
}); });
describe("when no paging is in effect", () => { describe("when no paging is in effect", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(getArtistsXml)))
.mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist1.image)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist2.image)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist3.image)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist4.image)))
);
});
it("should return all the artists", async () => { it("should return all the artists", async () => {
const artists = await navidrome const artists = await navidrome
.generateToken({ username, password }) .generateToken({ username, password })
@@ -157,54 +248,35 @@ 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 expectedArtists = [ expect(artists).toEqual({
{ results: [artist1, artist2, artist3, artist4],
id: "2911b2d67a6b11eb804dd360a6225680", total: 4,
name: "artist1", });
image: { small: "sml1", medium: "med1", large: "lge1" },
},
{
id: "3c0b9d7a7a6b11eb9773f398e6236ad6",
name: "artist2",
image: { small: "sml2", medium: "", large: "lge2" },
},
{
id: "3c5113007a6b11eb87173bfb9b07f9b1",
name: "artist3",
image: { small: "", medium: "med3", large: "" },
},
{
id: "3ca781c27a6b11eb897ebbb5773603ad",
name: "artist4",
image: { small: "sml4", medium: "med4", large: "lge4" },
},
];
expect(artists).toEqual({ results: expectedArtists, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams, params: authParams,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: "2911b2d67a6b11eb804dd360a6225680", id: artist1.id,
...authParams, ...authParams,
}, },
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: "3c0b9d7a7a6b11eb9773f398e6236ad6", id: artist2.id,
...authParams, ...authParams,
}, },
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: "3c5113007a6b11eb87173bfb9b07f9b1", id: artist3.id,
...authParams, ...authParams,
}, },
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: "3ca781c27a6b11eb897ebbb5773603ad", id: artist4.id,
...authParams, ...authParams,
}, },
}); });
@@ -212,6 +284,18 @@ describe("navidrome", () => {
}); });
describe("when paging specified", () => { describe("when paging specified", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(getArtistsXml)))
.mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist2.image)))
)
.mockImplementationOnce(() =>
Promise.resolve(ok(artistInfoXml(artist3.image)))
);
});
it("should return only the correct page of artists", async () => { it("should return only the correct page of artists", async () => {
const artists = await navidrome const artists = await navidrome
.generateToken({ username, password }) .generateToken({ username, password })
@@ -219,44 +303,20 @@ 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 }));
const expectedArtists = [ expect(artists).toEqual({ results: [artist2, artist3], total: 4 });
{
id: "3c0b9d7a7a6b11eb9773f398e6236ad6",
name: "artist2",
image: { small: "sml2", medium: "", large: "lge2" },
},
{
id: "3c5113007a6b11eb87173bfb9b07f9b1",
name: "artist3",
image: { small: "", medium: "med3", large: "" },
},
];
expect(artists).toEqual({ results: expectedArtists, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: authParams, params: authParams,
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: "2911b2d67a6b11eb804dd360a6225680", id: artist2.id,
...authParams, ...authParams,
}, },
}); });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, { expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: { params: {
id: "3c0b9d7a7a6b11eb9773f398e6236ad6", id: artist3.id,
...authParams,
},
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: {
id: "3c5113007a6b11eb87173bfb9b07f9b1",
...authParams,
},
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtistInfo`, {
params: {
id: "3ca781c27a6b11eb897ebbb5773603ad",
...authParams, ...authParams,
}, },
}); });