From ddde55d02be16c01028e762836d5e4079eec7312 Mon Sep 17 00:00:00 2001 From: simojenki Date: Sat, 15 Feb 2025 11:34:59 +0000 Subject: [PATCH] move some code --- src/subsonic.ts | 24 ++++-- src/subsonic_music_library.ts | 13 +-- tests/subsonic.test.ts | 113 ++++++++++++++++++++++++--- tests/subsonic_music_library.test.ts | 1 + 4 files changed, 125 insertions(+), 26 deletions(-) diff --git a/src/subsonic.ts b/src/subsonic.ts index 2eeeb5a..548f67a 100644 --- a/src/subsonic.ts +++ b/src/subsonic.ts @@ -724,10 +724,22 @@ export class Subsonic { ) ); - // getStarred2 = (credentials: Credentials): Promise<{ albums: Album[] }> => - // this.getJSON(credentials, "/rest/getStarred2") - // .then((it) => it.starred2) - // .then((it) => ({ - // albums: it.album.map(asAlbum), - // })); + private st4r = (credentials: Credentials, action: string, { id } : { id: string }) => + this.getJSON(credentials, `/rest/${action}`, { id }).then(it => + it.status == "ok" + ); + + star = (credentials: Credentials, ids : { id: string }) => + this.st4r(credentials, "star", ids) + + unstar = (credentials: Credentials, ids : { id: string }) => + this.st4r(credentials, "unstar", ids) + + setRating = (credentials: Credentials, id: string, rating: number) => + this.getJSON(credentials, `/rest/setRating`, { + id, + rating, + }).then(it => + it.status == "ok" + ); } diff --git a/src/subsonic_music_library.ts b/src/subsonic_music_library.ts index 80aef49..228d71d 100644 --- a/src/subsonic_music_library.ts +++ b/src/subsonic_music_library.ts @@ -191,21 +191,12 @@ export class SubsonicMusicLibrary implements MusicLibrary { const thingsToUpdate = []; if (track.rating.love != rating.love) { thingsToUpdate.push( - this.subsonic.getJSON( - this.credentials, - `/rest/${rating.love ? "star" : "unstar"}`, - { - id: trackId, - } - ) + (rating.love ? this.subsonic.star : this.subsonic.unstar)(this.credentials,{ id: trackId }) ); } if (track.rating.stars != rating.stars) { thingsToUpdate.push( - this.subsonic.getJSON(this.credentials, `/rest/setRating`, { - id: trackId, - rating: rating.stars, - }) + this.subsonic.setRating(this.credentials, trackId, rating.stars) ); } return Promise.all(thingsToUpdate); diff --git a/tests/subsonic.test.ts b/tests/subsonic.test.ts index 939d89b..704e2a1 100644 --- a/tests/subsonic.test.ts +++ b/tests/subsonic.test.ts @@ -532,15 +532,21 @@ describe("asTrack", () => { }); }); -const subsonicOK = (body: any = {}) => ({ - "subsonic-response": { - status: "ok", - version: "1.16.1", - type: "subsonic", - serverVersion: "0.45.1 (c55e6590)", - ...body, - }, -}); +const subsonicResponse = (response : Partial<{ status: string, body: any }> = { }) => { + const status = response.status || "ok" + const body = response.body || {} + return { + "subsonic-response": { + status, + version: "1.16.1", + type: "subsonic", + serverVersion: "0.45.1 (c55e6590)", + ...body, + }, + }; +}; + +const subsonicOK = (body: any = {}) => subsonicResponse({ status: "ok", body }); const asGenreJson = (genre: { name: string; albumCount: number }) => ({ songCount: 1475, @@ -1008,4 +1014,93 @@ describe("subsonic", () => { }); }); }); + + describe("stars and unstars", () => { + const id = uuid(); + + describe("staring a track", () => { + describe("when ok", () => { + beforeEach(() => { + mockGET.mockImplementationOnce(() => + Promise.resolve(ok(subsonicResponse({ status: "ok" }))) + ); + }); + + it("should return true", async () => { + const result = await subsonic.star(credentials, { id }); + + expect(result).toEqual(true); + expect(axios.get).toHaveBeenCalledWith( + url.append({ pathname: "/rest/star" }).href(), + { + params: asURLSearchParams({ + ...authParamsPlusJson, + id + }), + headers, + } + ); + }); + }); + + describe("when not ok", () => { + beforeEach(() => { + mockGET.mockImplementationOnce(() => + Promise.resolve(ok(subsonicResponse({ status: "not-ok" }))) + ); + }); + + it("should return false", async () => { + const result = await subsonic.star(credentials, { id }); + + expect(result).toEqual(false); + }); + }); + }); + }); + + describe("setting ratings", () => { + const id = uuid(); + + describe("when the rating is valid", () => { + describe("when response is ok", () => { + beforeEach(() => { + mockGET.mockImplementationOnce(() => + Promise.resolve(ok(subsonicResponse({ status: "ok" }))) + ); + }); + + it("should return true", async () => { + const result = await subsonic.setRating(credentials, id, 4); + + expect(result).toEqual(true); + expect(axios.get).toHaveBeenCalledWith( + url.append({ pathname: "/rest/setRating" }).href(), + { + params: asURLSearchParams({ + ...authParamsPlusJson, + id, + rating: 4 + }), + headers, + } + ); + }); + }); + + describe("when response is not ok", () => { + beforeEach(() => { + mockGET.mockImplementationOnce(() => + Promise.resolve(ok(subsonicResponse({ status: "not-ok" }))) + ); + }); + + it("should return false", async () => { + const result = await subsonic.setRating(credentials, id, 2); + + expect(result).toEqual(false); + }); + }); + }); + }); }); diff --git a/tests/subsonic_music_library.test.ts b/tests/subsonic_music_library.test.ts index 66faeb1..01a921e 100644 --- a/tests/subsonic_music_library.test.ts +++ b/tests/subsonic_music_library.test.ts @@ -833,6 +833,7 @@ describe("SubsonicMusicLibrary", () => { }; const subsonic = new SubsonicMusicLibrary( + // todo: this should be a mock... new Subsonic(url, customPlayers), { username, password }, customPlayers as unknown as CustomPlayers