Skip to content

Commit

Permalink
Add a test for /api/playlists?contains=:media_id
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Nov 28, 2024
1 parent 888ef62 commit de18a81
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/controllers/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ async function getPlaylists(req) {
playlists = await uw.playlists.getUserPlaylists(user);
}

playlists.sort((a, b) => {
return a.name.localeCompare(b.name);
});

return toListResponse(
playlists.map(serializePlaylist),
{ url: req.fullUrl },
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class PlaylistsRepository {

/**
* Get playlists that contain any of the given medias. If multiple medias are in a single
* playlist, that playlist will be returned multiple times, keyed on the media's unique ObjectId.
* playlist, that playlist will be returned multiple times, keyed on the media ID.
*
* @param {MediaID[]} mediaIDs
* @param {{ author?: UserID }} options
Expand Down
40 changes: 40 additions & 0 deletions test/playlists.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,46 @@ describe('Playlists', () => {
sinon.match({ name: 'Playlist B' }),
]);
});

it('shows all playlists containing a specific media', async () => {
const token = await uw.test.createTestSessionToken(user);

const { playlist: playlistA } = await uw.playlists.createPlaylist(user, { name: 'Playlist A' });
const { playlist: playlistB } = await uw.playlists.createPlaylist(user, { name: 'Playlist B' });

const [onlyA, onlyB, both] = await uw.source('test-source').get(user, ['ONLY_A', 'ONLY_B', 'BOTH']);
const a = await uw.playlists.addPlaylistItems(playlistA, [onlyA, both]);
const b = await uw.playlists.addPlaylistItems(playlistB, [onlyB, both]);

const mediaIDOnlyA = a.added[0].media.id;
const mediaIDOnlyB = b.added[0].media.id;
const mediaIDBoth = a.added[1].media.id;

const resOnlyA = await supertest(uw.server)
.get(`/api/playlists?contains=${mediaIDOnlyA}`)
.set('Cookie', `uwsession=${token}`)
.expect(200);
sinon.assert.match(resOnlyA.body.data, [
sinon.match({ name: 'Playlist A' }),
]);

const resOnlyB = await supertest(uw.server)
.get(`/api/playlists?contains=${mediaIDOnlyB}`)
.set('Cookie', `uwsession=${token}`)
.expect(200);
sinon.assert.match(resOnlyB.body.data, [
sinon.match({ name: 'Playlist B' }),
]);

const resBoth = await supertest(uw.server)
.get(`/api/playlists?contains=${mediaIDBoth}`)
.set('Cookie', `uwsession=${token}`)
.expect(200);
sinon.assert.match(resBoth.body.data, [
sinon.match({ name: 'Playlist A' }),
sinon.match({ name: 'Playlist B' }),
]);
});
});

describe('POST /playlists', () => {
Expand Down

0 comments on commit de18a81

Please sign in to comment.