scoll indices based on ND sort name for artists

This commit is contained in:
simojenki
2021-12-24 17:22:10 +11:00
parent c7352aefa3
commit 00944a7a25
8 changed files with 607 additions and 203 deletions

View File

@@ -6,6 +6,7 @@ import {
MusicLibrary,
artistToArtistSummary,
albumToAlbumSummary,
Artist,
} from "../src/music_service";
import { v4 as uuid } from "uuid";
import {
@@ -78,6 +79,11 @@ describe("InMemoryMusicService", () => {
musicLibrary = (await service.login(token.serviceToken)) as MusicLibrary;
});
const artistToArtistSummaryWithSortName = (artist: Artist) => ({
...artistToArtistSummary(artist),
sortName: artist.name
})
describe("artists", () => {
const artist1 = anArtist();
const artist2 = anArtist();
@@ -95,11 +101,11 @@ describe("InMemoryMusicService", () => {
await musicLibrary.artists({ _index: 0, _count: 100 })
).toEqual({
results: [
artistToArtistSummary(artist1),
artistToArtistSummary(artist2),
artistToArtistSummary(artist3),
artistToArtistSummary(artist4),
artistToArtistSummary(artist5),
artistToArtistSummaryWithSortName(artist1),
artistToArtistSummaryWithSortName(artist2),
artistToArtistSummaryWithSortName(artist3),
artistToArtistSummaryWithSortName(artist4),
artistToArtistSummaryWithSortName(artist5),
],
total: 5,
});
@@ -110,8 +116,8 @@ describe("InMemoryMusicService", () => {
it("should provide an array of artists", async () => {
expect(await musicLibrary.artists({ _index: 2, _count: 2 })).toEqual({
results: [
artistToArtistSummary(artist3),
artistToArtistSummary(artist4),
artistToArtistSummaryWithSortName(artist3),
artistToArtistSummaryWithSortName(artist4),
],
total: 5,
});
@@ -121,7 +127,7 @@ describe("InMemoryMusicService", () => {
describe("fetching the last page", () => {
it("should provide an array of artists", async () => {
expect(await musicLibrary.artists({ _index: 4, _count: 2 })).toEqual({
results: [artistToArtistSummary(artist5)],
results: [artistToArtistSummaryWithSortName(artist5)],
total: 5,
});
});

View File

@@ -62,7 +62,7 @@ export class InMemoryMusicService implements MusicService {
return Promise.resolve({
artists: (q: ArtistQuery) =>
Promise.resolve(this.artists.map(artistToArtistSummary))
Promise.resolve(this.artists.map(artistToArtistSummary).map(it => ({ ...it, sortName: it.name })))
.then(slice2(q))
.then(asResult),
artist: (id: string) =>

View File

@@ -1,7 +1,57 @@
import { v4 as uuid } from "uuid";
import { anArtist } from "./builders";
import { artistToArtistSummary } from "../src/music_service";
import { artistToArtistSummary, slice2 } from "../src/music_service";
describe("slice2", () => {
const things = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
describe("when slice is a subset of the things", () => {
it("should return the page", () => {
expect(slice2({ _index: 3, _count: 4 })(things)).toEqual([
["d", "e", "f", "g"],
things.length
])
});
});
describe("when slice goes off the end of the things", () => {
it("should return the page", () => {
expect(slice2({ _index: 5, _count: 100 })(things)).toEqual([
["f", "g", "h", "i"],
things.length
])
});
});
describe("when no _count is provided", () => {
it("should return from the index", () => {
expect(slice2({ _index: 5 })(things)).toEqual([
["f", "g", "h", "i"],
things.length
])
});
});
describe("when no _index is provided", () => {
it("should assume from the start", () => {
expect(slice2({ _count: 3 })(things)).toEqual([
["a", "b", "c"],
things.length
])
});
});
describe("when no _index or _count is provided", () => {
it("should return all the things", () => {
expect(slice2()(things)).toEqual([
things,
things.length
])
});
});
});
describe("artistToArtistSummary", () => {
it("should map fields correctly", () => {

View File

@@ -863,24 +863,49 @@ describe("defaultArtistArtURI", () => {
describe("scrollIndicesFrom", () => {
describe("artists", () => {
it("should be scroll indicies", () => {
const artistNames = [
"10,000 Maniacs",
"99 Bacon Sandwiches",
"Aerosmith",
"Bob Marley",
"beatles", // intentionally lower case
"Cans",
"egg heads", // intentionally lower case
"Moon Cakes",
"Moon Boots",
"Numpty",
"Yellow brick road"
]
const scrollIndicies = scrollIndicesFrom(_.shuffle(artistNames).map(name => anArtist({ name })))
expect(scrollIndicies).toEqual("A,2,B,3,C,5,D,5,E,6,F,6,G,6,H,6,I,6,J,6,K,6,L,6,M,7,N,9,O,9,P,9,Q,9,R,9,S,9,T,9,U,9,V,9,W,9,X,9,Y,10,Z,10")
describe("when sortName is the same as name", () => {
it("should be scroll indicies", () => {
const artistNames = [
"10,000 Maniacs",
"99 Bacon Sandwiches",
"[something with square brackets]",
"Aerosmith",
"Bob Marley",
"beatles", // intentionally lower case
"Cans",
"egg heads", // intentionally lower case
"Moon Cakes",
"Moon Boots",
"Numpty",
"Yellow brick road"
]
const scrollIndicies = scrollIndicesFrom(artistNames.map(name => ({ name, sortName: name })))
expect(scrollIndicies).toEqual("A,3,B,4,C,6,D,6,E,7,F,7,G,7,H,7,I,7,J,7,K,7,L,7,M,8,N,10,O,10,P,10,Q,10,R,10,S,10,T,10,U,10,V,10,W,10,X,10,Y,11,Z,11")
});
});
describe("when sortName is different to the name name", () => {
it("should be scroll indicies", () => {
const artistSortNames = [
"10,000 Maniacs",
"99 Bacon Sandwiches",
"[something with square brackets]",
"Aerosmith",
"Bob Marley",
"beatles", // intentionally lower case
"Cans",
"egg heads", // intentionally lower case
"Moon Cakes",
"Moon Boots",
"Numpty",
"Yellow brick road"
]
const scrollIndicies = scrollIndicesFrom(artistSortNames.map(name => ({ name: uuid(), sortName: name })))
expect(scrollIndicies).toEqual("A,3,B,4,C,6,D,6,E,7,F,7,G,7,H,7,I,7,J,7,K,7,L,7,M,8,N,10,O,10,P,10,Q,10,R,10,S,10,T,10,U,10,V,10,W,10,X,10,Y,11,Z,11")
});
})
});
});
@@ -3152,6 +3177,9 @@ describe("wsdl api", () => {
const artist5 = anArtist({ name: "Metallica" });
const artist6 = anArtist({ name: "Yellow Brick Road" });
const artists = [artist1, artist2, artist3, artist4, artist5, artist6];
const artistsWithSortName = artists.map(it => ({ ...it, sortName: it.name }));
beforeEach(async () => {
ws = await createClientAsync(`${service.uri}?wsdl`, {
endpoint: service.uri,
@@ -3159,7 +3187,7 @@ describe("wsdl api", () => {
});
setupAuthenticatedRequest(ws);
musicLibrary.artists.mockResolvedValue({
results: [artist1, artist2, artist3, artist4, artist5, artist6],
results: artistsWithSortName,
total: 6
});
});
@@ -3170,11 +3198,11 @@ describe("wsdl api", () => {
});
expect(root[0]).toEqual({
getScrollIndicesResult: scrollIndicesFrom([artist1, artist2, artist3, artist4, artist5, artist6])
getScrollIndicesResult: scrollIndicesFrom(artistsWithSortName)
});
expect(musicService.login).toHaveBeenCalledWith(serviceToken);
expect(apiTokens.mint).toHaveBeenCalledWith(serviceToken);
expect(musicLibrary.artists).toHaveBeenCalledWith({ _index: 0, _count: 999999999 });
expect(musicLibrary.artists).toHaveBeenCalledWith({ _index: 0, _count: undefined });
});
});
});

View File

@@ -22,6 +22,8 @@ import {
PingResponse,
parseToken,
asToken,
artistSummaryFromNDArtist,
SubsonicCredentials,
} from "../src/subsonic";
import axios from "axios";
@@ -46,7 +48,6 @@ import {
Playlist,
SimilarArtist,
Rating,
Credentials,
AuthFailure,
} from "../src/music_service";
import {
@@ -576,6 +577,78 @@ const pingJson = (pingResponse: Partial<PingResponse> = {}) => ({
const PING_OK = pingJson({ status: "ok" });
describe("artistSummaryFromNDArtist", () => {
describe("when the orderArtistName is undefined", () => {
it("should use name", () => {
const artist = {
id: uuid(),
name: `name ${uuid()}`,
orderArtistName: undefined,
largeImageUrl: 'http://example.com/something.jpg'
}
expect(artistSummaryFromNDArtist(artist)).toEqual({
id: artist.id,
name: artist.name,
sortName: artist.name,
image: artistImageURN({ artistId: artist.id, artistImageURL: artist.largeImageUrl })
})
});
});
describe("when the artist image is valid", () => {
it("should create an ArtistSummary with Sortable", () => {
const artist = {
id: uuid(),
name: `name ${uuid()}`,
orderArtistName: `orderArtistName ${uuid()}`,
largeImageUrl: 'http://example.com/something.jpg'
}
expect(artistSummaryFromNDArtist(artist)).toEqual({
id: artist.id,
name: artist.name,
sortName: artist.orderArtistName,
image: artistImageURN({ artistId: artist.id, artistImageURL: artist.largeImageUrl })
})
});
});
describe("when the artist image is not valid", () => {
it("should create an ArtistSummary with Sortable", () => {
const artist = {
id: uuid(),
name: `name ${uuid()}`,
orderArtistName: `orderArtistName ${uuid()}`,
largeImageUrl: `http://example.com/${DODGY_IMAGE_NAME}`
}
expect(artistSummaryFromNDArtist(artist)).toEqual({
id: artist.id,
name: artist.name,
sortName: artist.orderArtistName,
image: artistImageURN({ artistId: artist.id, artistImageURL: artist.largeImageUrl })
});
});
});
describe("when the artist image is missing", () => {
it("should create an ArtistSummary with Sortable", () => {
const artist = {
id: uuid(),
name: `name ${uuid()}`,
orderArtistName: `orderArtistName ${uuid()}`,
largeImageUrl: undefined
}
expect(artistSummaryFromNDArtist(artist)).toEqual({
id: artist.id,
name: artist.name,
sortName: artist.orderArtistName,
image: artistImageURN({ artistId: artist.id, artistImageURL: artist.largeImageUrl })
});
});
});
});
describe("artistURN", () => {
describe("when artist URL is", () => {
describe("a valid external URL", () => {
@@ -731,13 +804,19 @@ describe("Subsonic", () => {
"User-Agent": "bonob",
};
const tokenFor = (credentials: Credentials) => pipe(
subsonic.generateToken(credentials),
const tokenFor = (credentials: Partial<SubsonicCredentials>) => pipe(
subsonic.generateToken({
username: "some username",
password: "some password",
bearer: undefined,
type: "subsonic",
...credentials
}),
TE.fold(e => { throw e }, T.of)
)
const login = (credentials: Credentials) => tokenFor(credentials)()
.then((it) => subsonic.login(it.serviceToken))
const login = (credentials: Partial<SubsonicCredentials> = {}) => tokenFor(credentials)()
.then((it) => subsonic.login(it.serviceToken))
describe("generateToken", () => {
describe("when the credentials are valid", () => {
@@ -1555,187 +1634,364 @@ describe("Subsonic", () => {
});
describe("getting artists", () => {
describe("when there are indexes, but no artists", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(
ok(
subsonicOK({
artists: {
index: [
{
name: "#",
},
{
name: "A",
},
{
name: "B",
},
],
},
})
)
)
);
});
it("should return empty", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 0, _count: 100 }));
expect(artists).toEqual({
results: [],
total: 0,
});
});
});
describe("when there no indexes and no artists", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(
ok(
subsonicOK({
artists: {},
})
)
)
);
});
it("should return empty", async () => {
const artists = await login({ username, password })
.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({albums:[anAlbum(), anAlbum(), anAlbum(), anAlbum()]});
const asArtistsJson = subsonicOK({
artists: {
index: [
{
name: "#",
artist: [
{
id: artist1.id,
name: artist1.name,
albumCount: artist1.albums.length,
},
],
},
],
},
});
describe("when it all fits on one page", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(asArtistsJson)));
});
it("should return the single artist", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 0, _count: 100 }));
const expectedResults = [{
id: artist1.id,
image: artist1.image,
name: artist1.name,
}];
expect(artists).toEqual({
results: expectedResults,
total: 1,
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
});
});
});
});
describe("when there are artists", () => {
const artist1 = anArtist({ name: "A Artist", albums:[anAlbum()] });
const artist2 = anArtist({ name: "B Artist" });
const artist3 = anArtist({ name: "C Artist" });
const artist4 = anArtist({ name: "D Artist" });
const artists = [artist1, artist2, artist3, artist4];
describe("when no paging is in effect", () => {
describe("when subsonic flavour is generic", () => {
describe("when there are indexes, but no artists", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(asArtistsJson(artists)))
Promise.resolve(
ok(
subsonicOK({
artists: {
index: [
{
name: "#",
},
{
name: "A",
},
{
name: "B",
},
],
},
})
)
)
);
});
it("should return all the artists", async () => {
it("should return empty", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 0, _count: 100 }));
const expectedResults = [artist1, artist2, artist3, artist4].map(
(it) => ({
expect(artists).toEqual({
results: [],
total: 0,
});
});
});
describe("when there no indexes and no artists", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(
ok(
subsonicOK({
artists: {},
})
)
)
);
});
it("should return empty", async () => {
const artists = await login({ username, password })
.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({albums:[anAlbum(), anAlbum(), anAlbum(), anAlbum()]});
const asArtistsJson = subsonicOK({
artists: {
index: [
{
name: "#",
artist: [
{
id: artist1.id,
name: artist1.name,
albumCount: artist1.albums.length,
},
],
},
],
},
});
describe("when it all fits on one page", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() => Promise.resolve(ok(asArtistsJson)));
});
it("should return the single artist", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 0, _count: 100 }));
const expectedResults = [{
id: artist1.id,
image: artist1.image,
name: artist1.name,
sortName: artist1.name
}];
expect(artists).toEqual({
results: expectedResults,
total: 1,
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
});
});
});
});
describe("when there are artists", () => {
const artist1 = anArtist({ name: "A Artist", albums:[anAlbum()] });
const artist2 = anArtist({ name: "B Artist" });
const artist3 = anArtist({ name: "C Artist" });
const artist4 = anArtist({ name: "D Artist" });
const artists = [artist1, artist2, artist3, artist4];
describe("when no paging is in effect", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(asArtistsJson(artists)))
);
});
it("should return all the artists", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 0, _count: 100 }));
const expectedResults = [artist1, artist2, artist3, artist4].map(
(it) => ({
id: it.id,
image: it.image,
name: it.name,
sortName: it.name,
})
);
expect(artists).toEqual({
results: expectedResults,
total: 4,
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
});
});
});
describe("when paging specified", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
.mockImplementationOnce(() =>
Promise.resolve(ok(asArtistsJson(artists)))
);
});
it("should return only the correct page of artists", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 1, _count: 2 }));
const expectedResults = [artist2, artist3].map((it) => ({
id: it.id,
image: it.image,
name: it.name,
})
);
sortName: it.name,
}));
expect(artists).toEqual({ results: expectedResults, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
});
});
});
});
});
describe("when the subsonic type is navidrome", () => {
const ndArtist1 = {
id: uuid(),
name: "Artist1",
orderArtistName: "Artist1",
largeImageUrl: "http://example.com/artist1/image.jpg"
};
const ndArtist2 = {
id: uuid(),
name: "Artist2",
orderArtistName: "The Artist2",
largeImageUrl: undefined
};
const ndArtist3 = {
id: uuid(),
name: "Artist3",
orderArtistName: "An Artist3",
largeImageUrl: `http://example.com/artist3/${DODGY_IMAGE_NAME}`
};
const ndArtist4 = {
id: uuid(),
name: "Artist4",
orderArtistName: "An Artist4",
largeImageUrl: `http://example.com/artist4/${DODGY_IMAGE_NAME}`
};
const bearer = `bearer-${uuid()}`;
describe("when no paging is specified", () => {
beforeEach(() => {
(axios.get as jest.Mock)
.mockImplementationOnce(() => Promise.resolve(ok(pingJson({ type: "navidrome" }))))
.mockImplementationOnce(() =>
Promise.resolve({
status: 200,
data: [
ndArtist1,
ndArtist2,
ndArtist3,
ndArtist4,
],
headers: {
"x-total-count": "4"
}
})
);
(axios.post as jest.Mock).mockResolvedValue(ok({ token: bearer }));
});
it("should fetch all artists", async () => {
const artists = await login({ username, password, bearer, type: "navidrome" })
.then((it) => it.artists({ _index: undefined, _count: undefined }));
expect(artists).toEqual({
results: expectedResults,
results: [
artistSummaryFromNDArtist(ndArtist1),
artistSummaryFromNDArtist(ndArtist2),
artistSummaryFromNDArtist(ndArtist3),
artistSummaryFromNDArtist(ndArtist4),
],
total: 4,
});
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
expect(axios.get).toHaveBeenCalledWith(`${url}/api/artist`, {
params: asURLSearchParams({
_sort: "name",
_order: "ASC",
_start: "0"
}),
headers: {
"User-Agent": "bonob",
"x-nd-authorization": `Bearer ${bearer}`,
},
});
});
});
describe("when paging specified", () => {
describe("when start index is specified", () => {
beforeEach(() => {
mockGET
.mockImplementationOnce(() => Promise.resolve(ok(PING_OK)))
(axios.get as jest.Mock)
.mockImplementationOnce(() => Promise.resolve(ok(pingJson({ type: "navidrome" }))))
.mockImplementationOnce(() =>
Promise.resolve(ok(asArtistsJson(artists)))
Promise.resolve({
status: 200,
data: [
ndArtist3,
ndArtist4,
],
headers: {
"x-total-count": "5"
}
})
);
(axios.post as jest.Mock).mockResolvedValue(ok({ token: bearer }));
});
it("should fetch all artists", async () => {
const artists = await login({ username, password, bearer, type: "navidrome" })
.then((it) => it.artists({ _index: 2, _count: undefined }));
expect(artists).toEqual({
results: [
artistSummaryFromNDArtist(ndArtist3),
artistSummaryFromNDArtist(ndArtist4),
],
total: 5,
});
it("should return only the correct page of artists", async () => {
const artists = await login({ username, password })
.then((it) => it.artists({ _index: 1, _count: 2 }));
const expectedResults = [artist2, artist3].map((it) => ({
id: it.id,
image: it.image,
name: it.name,
}));
expect(artists).toEqual({ results: expectedResults, total: 4 });
expect(axios.get).toHaveBeenCalledWith(`${url}/rest/getArtists`, {
params: asURLSearchParams(authParamsPlusJson),
headers,
expect(axios.get).toHaveBeenCalledWith(`${url}/api/artist`, {
params: asURLSearchParams({
_sort: "name",
_order: "ASC",
_start: "2"
}),
headers: {
"User-Agent": "bonob",
"x-nd-authorization": `Bearer ${bearer}`,
},
});
});
});
describe("when start index and count is specified", () => {
beforeEach(() => {
(axios.get as jest.Mock)
.mockImplementationOnce(() => Promise.resolve(ok(pingJson({ type: "navidrome" }))))
.mockImplementationOnce(() =>
Promise.resolve({
status: 200,
data: [
ndArtist3,
ndArtist4,
],
headers: {
"x-total-count": "5"
}
})
);
(axios.post as jest.Mock).mockResolvedValue(ok({ token: bearer }));
});
it("should fetch all artists", async () => {
const artists = await login({ username, password, bearer, type: "navidrome" })
.then((it) => it.artists({ _index: 2, _count: 23 }));
expect(artists).toEqual({
results: [
artistSummaryFromNDArtist(ndArtist3),
artistSummaryFromNDArtist(ndArtist4),
],
total: 5,
});
expect(axios.get).toHaveBeenCalledWith(`${url}/api/artist`, {
params: asURLSearchParams({
_sort: "name",
_order: "ASC",
_start: "2",
_end: "25"
}),
headers: {
"User-Agent": "bonob",
"x-nd-authorization": `Bearer ${bearer}`,
},
});
});
});
});
});