Fix bug where error occurs when there are no artists in the library

This commit is contained in:
simojenki
2021-07-31 15:56:29 +10:00
parent 4aa154baa8
commit f036b39524
2 changed files with 77 additions and 2 deletions

View File

@@ -342,6 +342,8 @@ export class Navidrome implements MusicService {
"subsonic-response.album.song", "subsonic-response.album.song",
"subsonic-response.albumList.album", "subsonic-response.albumList.album",
"subsonic-response.artist.album", "subsonic-response.artist.album",
"subsonic-response.artists.index",
"subsonic-response.artists.index.artist",
"subsonic-response.artistInfo.similarArtist", "subsonic-response.artistInfo.similarArtist",
"subsonic-response.genres.genre", "subsonic-response.genres.genre",
"subsonic-response.playlist.entry", "subsonic-response.playlist.entry",
@@ -378,7 +380,7 @@ export class Navidrome implements MusicService {
getArtists = (credentials: Credentials): Promise<IdName[]> => getArtists = (credentials: Credentials): Promise<IdName[]> =>
this.getJSON<GetArtistsResponse>(credentials, "/rest/getArtists") this.getJSON<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) => ({ artists.map((artist) => ({
id: artist._id, id: artist._id,

View File

@@ -908,7 +908,7 @@ describe("Navidrome", () => {
}); });
describe("getting artists", () => { describe("getting artists", () => {
describe("when there are no results", () => { describe("when there are indexes, but no artists", () => {
beforeEach(() => { beforeEach(() => {
mockGET mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK))) .mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
@@ -942,6 +942,79 @@ describe("Navidrome", () => {
}); });
}); });
describe("when there no indexes and no artists", () => {
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">
</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 there is one index and one artist", () => {
const artist1 = anArtist();
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>
</index>
</artists>
</subsonic-response>`;
describe("when it all fits on one page", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(getArtistsXml)));
});
it("should return the single artist", 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 }));
const expectedResults = [artist1].map(
(it) => ({
id: it.id,
name: it.name,
})
);
expect(artists).toEqual({
results: expectedResults,
total: 1,
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParams),
headers,
});
});
});
});
describe("when there are artists", () => { describe("when there are artists", () => {
const artist1 = anArtist(); const artist1 = anArtist();
const artist2 = anArtist(); const artist2 = anArtist();