diff --git a/src/navidrome.ts b/src/navidrome.ts index b7e39d5..9916b53 100644 --- a/src/navidrome.ts +++ b/src/navidrome.ts @@ -342,6 +342,8 @@ export class Navidrome implements MusicService { "subsonic-response.album.song", "subsonic-response.albumList.album", "subsonic-response.artist.album", + "subsonic-response.artists.index", + "subsonic-response.artists.index.artist", "subsonic-response.artistInfo.similarArtist", "subsonic-response.genres.genre", "subsonic-response.playlist.entry", @@ -378,7 +380,7 @@ export class Navidrome implements MusicService { getArtists = (credentials: Credentials): Promise => this.getJSON(credentials, "/rest/getArtists") - .then((it) => it.artists.index.flatMap((it) => it.artist || [])) + .then((it) => (it.artists.index || []).flatMap((it) => it.artist || [])) .then((artists) => artists.map((artist) => ({ id: artist._id, diff --git a/tests/navidrome.test.ts b/tests/navidrome.test.ts index 0a72ec2..f51adbe 100644 --- a/tests/navidrome.test.ts +++ b/tests/navidrome.test.ts @@ -908,7 +908,7 @@ describe("Navidrome", () => { }); describe("getting artists", () => { - describe("when there are no results", () => { + describe("when there are indexes, but no artists", () => { beforeEach(() => { mockGET .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(` + + + `) + ) + ); + }); + + 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 = ` + + + + + + `; + + 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", () => { const artist1 = anArtist(); const artist2 = anArtist();