move some code

This commit is contained in:
simojenki
2025-02-15 11:34:59 +00:00
parent 0602e1f077
commit ddde55d02b
4 changed files with 125 additions and 26 deletions

View File

@@ -724,10 +724,22 @@ export class Subsonic {
)
);
// getStarred2 = (credentials: Credentials): Promise<{ albums: Album[] }> =>
// this.getJSON<GetStarredResponse>(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<SubsonicResponse>(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<SubsonicResponse>(credentials, `/rest/setRating`, {
id,
rating,
}).then(it =>
it.status == "ok"
);
}

View File

@@ -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);

View File

@@ -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);
});
});
});
});
});

View File

@@ -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