2021-04-16 04:34:29 +00:00
|
|
|
import test from 'ava';
|
|
|
|
|
import MusicBrainz from '../../src/api/index.js';
|
|
|
|
|
import client from '../helpers/client/musicbrainz.js';
|
2016-12-07 08:23:02 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('getLookupURL() generates a lookup URL', (t) => {
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
client.getLookupURL('artist', 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8', {
|
2021-04-16 04:34:29 +00:00
|
|
|
inc: ['recordings', 'release-groups'],
|
2017-11-07 05:54:56 +00:00
|
|
|
}),
|
|
|
|
|
'artist/c8da2e40-bd28-4d4e-813a-bd2f51958ba8?inc=recordings%2Brelease-groups'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
|
|
|
|
});
|
2016-12-07 08:23:02 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('getBrowseURL() generates a browse URL', (t) => {
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
client.getBrowseURL('recording', {
|
|
|
|
|
artist: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8',
|
|
|
|
|
limit: null,
|
2021-04-16 04:34:29 +00:00
|
|
|
offset: 0,
|
2017-11-07 05:54:56 +00:00
|
|
|
}),
|
|
|
|
|
'recording?artist=c8da2e40-bd28-4d4e-813a-bd2f51958ba8&offset=0'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
|
|
|
|
});
|
2016-12-07 08:23:02 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('getSearchURL() generates a search URL', (t) => {
|
2017-11-07 05:54:56 +00:00
|
|
|
t.is(
|
|
|
|
|
client.getSearchURL('artist', 'Lures', { inc: null }),
|
|
|
|
|
'artist?query=Lures'
|
2021-04-16 04:34:29 +00:00
|
|
|
);
|
|
|
|
|
});
|
2016-12-07 08:23:02 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('lookup() sends a lookup query', async (t) => {
|
|
|
|
|
const response = await client.lookup(
|
|
|
|
|
'artist',
|
|
|
|
|
'c8da2e40-bd28-4d4e-813a-bd2f51958ba8'
|
|
|
|
|
);
|
2016-12-09 00:08:11 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
t.is(response.id, 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8');
|
|
|
|
|
t.is(response.type, 'Group');
|
|
|
|
|
});
|
2016-12-15 07:18:53 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('rejects the promise when the API returns an error', (t) => {
|
|
|
|
|
const req = client.lookup('artist', '5b11f4ce-a62d-471e-81fc-a69a8278c7da', {
|
|
|
|
|
inc: ['foobar'],
|
|
|
|
|
});
|
|
|
|
|
return t.throwsAsync(req, {
|
|
|
|
|
name: 'MusicBrainzError',
|
|
|
|
|
message: 'foobar is not a valid inc parameter for the artist resource.',
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-12-15 07:18:53 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('rejects non-MusicBrainz errors', (t) => {
|
|
|
|
|
const client = new MusicBrainz({ baseURL: '$!@#$' });
|
|
|
|
|
return t.throwsAsync(
|
2017-11-07 05:54:56 +00:00
|
|
|
client.get('artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da'),
|
2021-04-16 04:34:29 +00:00
|
|
|
{
|
|
|
|
|
name: 'TypeError',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
2016-12-20 05:21:07 +00:00
|
|
|
|
2021-04-16 04:34:29 +00:00
|
|
|
test('uses the default error impementation if there is no JSON error', (t) => {
|
|
|
|
|
let error = {
|
|
|
|
|
name: 'HTTPError',
|
|
|
|
|
response: {
|
|
|
|
|
statusCode: 501,
|
|
|
|
|
body: 'yikes',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
t.is(client.parseErrorMessage(error), error);
|
|
|
|
|
error = {
|
|
|
|
|
name: 'HTTPError',
|
|
|
|
|
response: {
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
body: {},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
t.is(client.parseErrorMessage(error), error);
|
|
|
|
|
error = {
|
|
|
|
|
name: 'HTTPError',
|
|
|
|
|
response: {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
body: null,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
t.is(client.parseErrorMessage(error), error);
|
|
|
|
|
});
|