test(api): add tests for API functions in api folder

This commit is contained in:
xtrullor73
2024-07-04 11:31:51 -07:00
parent 441d89cca4
commit f8b7cd7b3e
7 changed files with 738 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
import { expect } from 'chai';
import sinon from 'sinon';
import esmock from 'esmock';
describe('getAlbumArt', () => {
let axiosRetryStub;
let handleErrorStub;
let getAlbumArt;
beforeEach(async () => {
axiosRetryStub = {
get: sinon.stub()
};
handleErrorStub = sinon.stub();
// Mocking the coverArtArchiveApi and its dependencies
const module = await esmock('../../../src/api/metadata/coverArtArchiveApi.js', {
'../../../src/utils/retryAxios.js': {
default: axiosRetryStub
},
'../../../src/errors/generalApiErrorHandler.js': {
default: handleErrorStub
}
});
getAlbumArt = module.default;
});
afterEach(() => {
sinon.restore(); // Restore the original state of sinon stubs
esmock.purge(); // Clean up any esmocked modules
});
it('should retrieve album art URL for a given album ID (success case)', async () => {
const albumId = 'mockAlbumId';
const responseUrl = 'http://coverartarchive.org/mockAlbumId/front.jpg';
axiosRetryStub.get.resolves({
status: 200,
request: { responseURL: responseUrl }
});
const result = await getAlbumArt(albumId);
expect(result).to.equal(responseUrl);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`http://coverartarchive.org/release/${albumId}/front`);
});
it('should retrieve album art URL from redirect', async () => {
const albumId = 'mockAlbumId';
const redirectUrl = 'http://someotherurl.com/front.jpg';
axiosRetryStub.get.resolves({
status: 307,
headers: { location: redirectUrl }
});
const result = await getAlbumArt(albumId);
expect(result).to.equal(redirectUrl);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`http://coverartarchive.org/release/${albumId}/front`);
});
it('should handle 307 redirect error', async () => {
const albumId = 'mockAlbumId';
const redirectUrl = 'http://redirecturl.com/front.jpg';
const error = new Error('Request failed');
error.response = {
status: 307,
headers: { location: redirectUrl }
};
axiosRetryStub.get.rejects(error);
const result = await getAlbumArt(albumId);
expect(result).to.equal(redirectUrl);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`http://coverartarchive.org/release/${albumId}/front`);
});
it('should return null for 404 error', async () => {
const albumId = 'mockAlbumId';
const error = new Error('Not Found');
error.response = {
status: 404
};
axiosRetryStub.get.rejects(error);
const result = await getAlbumArt(albumId);
expect(result).to.be.null;
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`http://coverartarchive.org/release/${albumId}/front`);
});
it('should return null and handle general errors', async () => {
const albumId = 'mockAlbumId';
const errorMessage = 'API responded with an error';
const error = new Error('Request failed');
error.response = {}; // Ensure error.response is an object
axiosRetryStub.get.rejects(error);
handleErrorStub.returns(errorMessage);
const result = await getAlbumArt(albumId);
expect(result).to.be.null;
expect(handleErrorStub.callCount).to.equal(1);
expect(handleErrorStub.firstCall.args[0]).to.equal(error);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`http://coverartarchive.org/release/${albumId}/front`);
});
});

View File

@@ -0,0 +1,82 @@
import { expect } from 'chai';
import sinon from 'sinon';
import esmock from 'esmock';
describe('getLyrics', () => {
let axiosRetryStub;
let handleErrorStub;
let getLyrics;
beforeEach(async () => {
axiosRetryStub = {
get: sinon.stub(),
};
handleErrorStub = sinon.stub();
// Mocking the lyricOvhApi and its dependencies
const module = await esmock('../../../src/api/metadata/lyricOvhApi.js', {
'../../../src/utils/retryAxios.js': {
default: axiosRetryStub,
},
'../../../src/errors/generalApiErrorHandler.js': {
default: handleErrorStub,
},
});
getLyrics = module.default;
});
afterEach(() => {
sinon.restore(); // Restore the original state of sinon stubs
esmock.purge(); // Clean up any esmocked modules
});
it('should fetch lyrics for a specific song successfully', async () => {
const artist = 'mockArtist';
const title = 'mockTitle';
const lyrics = 'These are the mock lyrics';
axiosRetryStub.get.resolves({
status: 200,
data: { lyrics }
});
const result = await getLyrics(artist, title);
expect(result).to.equal(lyrics);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://api.lyrics.ovh/v1/${encodeURIComponent(artist)}/${encodeURIComponent(title)}`);
});
it('should return null if lyrics are not found (404 error)', async () => {
const artist = 'mockArtist';
const title = 'mockTitle';
const error = new Error('Not Found');
error.response = { status: 404 };
axiosRetryStub.get.rejects(error);
const result = await getLyrics(artist, title);
expect(result).to.be.null;
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://api.lyrics.ovh/v1/${encodeURIComponent(artist)}/${encodeURIComponent(title)}`);
});
it('should handle general errors and return null', async () => {
const artist = 'mockArtist';
const title = 'mockTitle';
const errorMessage = 'API responded with an error';
const error = new Error('Request failed');
error.response = {}; // Ensure error.response is an object
axiosRetryStub.get.rejects(error);
handleErrorStub.returns(errorMessage);
const result = await getLyrics(artist, title);
expect(result).to.be.null;
expect(handleErrorStub.callCount).to.equal(1);
expect(handleErrorStub.firstCall.args[0]).to.equal(error);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://api.lyrics.ovh/v1/${encodeURIComponent(artist)}/${encodeURIComponent(title)}`);
});
});

View File

@@ -0,0 +1,84 @@
import { expect } from 'chai';
import sinon from 'sinon';
import esmock from 'esmock';
describe('getMetadata', () => {
let axiosRetryStub;
let handleErrorStub;
let getMetadata;
beforeEach(async () => {
axiosRetryStub = {
get: sinon.stub()
};
handleErrorStub = sinon.stub();
// Mocking the musicBrainzApi and its dependencies
const module = await esmock('../../../src/api/metadata/musicBrainzApi.js', {
'../../../src/utils/retryAxios.js': {
default: axiosRetryStub,
},
'../../../src/errors/generalApiErrorHandler.js': {
default: handleErrorStub,
},
});
getMetadata = module.default;
});
afterEach(() => {
sinon.restore(); // Restore the original state of sinon stubs
esmock.purge(); // Clean up any esmocked modules
});
it('should fetch metadata for a given recording ID successfully', async () => {
const recordingId = 'mockRecordingId';
const metadata = { title: 'Mock Title', artists: [], releases: [] };
axiosRetryStub.get.resolves({
status: 200,
data: metadata
});
const result = await getMetadata(recordingId);
expect(result).to.deep.equal(metadata);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://musicbrainz.org/ws/2/recording/${recordingId}?fmt=json&inc=artists+releases`);
});
it('should return null if metadata is not found (404 error)', async () => {
const recordingId = 'mockRecordingId';
const error = new Error('Not Found');
error.response = { status: 404 };
axiosRetryStub.get.rejects(error);
const result = await getMetadata(recordingId);
expect(result).to.be.null;
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://musicbrainz.org/ws/2/recording/${recordingId}?fmt=json&inc=artists+releases`);
});
it('should handle general errors and throw an error', async () => {
const recordingId = 'mockRecordingId';
const errorMessage = 'API responded with an error';
const error = new Error('Request failed');
error.response = {}; // Ensure error.response is an object
axiosRetryStub.get.rejects(error);
handleErrorStub.returns(errorMessage);
try {
await getMetadata(recordingId);
// We should not reach here
expect.fail('Expected getMetadata to throw');
} catch (err) {
expect(err.message).to.equal(errorMessage);
expect(handleErrorStub.callCount).to.equal(1);
expect(handleErrorStub.firstCall.args[0]).to.equal(error);
}
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://musicbrainz.org/ws/2/recording/${recordingId}?fmt=json&inc=artists+releases`);
});
});

View File

@@ -0,0 +1,84 @@
import { expect } from 'chai';
import sinon from 'sinon';
import esmock from 'esmock';
describe('getMetadata', () => {
let axiosRetryStub;
let handleErrorStub;
let getMetadata;
beforeEach(async () => {
axiosRetryStub = {
get: sinon.stub()
};
handleErrorStub = sinon.stub();
// Mocking the spotifyApi and its dependencies
const module = await esmock('../../../src/api/metadata/spotifyApi.js', {
'../../../src/utils/retryAxios.js': {
default: axiosRetryStub,
},
'../../../src/errors/generalApiErrorHandler.js': {
default: handleErrorStub,
},
});
getMetadata = module.default;
});
afterEach(() => {
sinon.restore(); // Restore the original state of sinon stubs
esmock.purge(); // Clean up any esmocked modules
});
it('should fetch metadata for a given track ID successfully', async () => {
const trackId = 'mockTrackId';
const accessToken = 'mockAccessToken';
const metadata = { name: 'Mock Song', artists: [], album: {} };
axiosRetryStub.get.resolves({
status: 200,
data: metadata
});
const result = await getMetadata(trackId, accessToken);
expect(result).to.deep.equal(metadata);
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://api.spotify.com/v1/tracks/${trackId}`);
expect(axiosRetryStub.get.firstCall.args[1]).to.deep.include({
headers: {
Authorization: `Bearer ${accessToken}`
}
});
});
it('should handle errors and throw an error message', async () => {
const trackId = 'mockTrackId';
const accessToken = 'mockAccessToken';
const errorMessage = 'API responded with an error';
const error = new Error('Request failed');
error.response = {}; // Ensure error.response is an object
axiosRetryStub.get.rejects(error);
handleErrorStub.returns(errorMessage);
try {
await getMetadata(trackId, accessToken);
// We should not reach here
expect.fail('Expected getMetadata to throw');
} catch (err) {
expect(err.message).to.equal(errorMessage);
expect(handleErrorStub.callCount).to.equal(1);
expect(handleErrorStub.firstCall.args[0]).to.equal(error);
expect(handleErrorStub.firstCall.args[1]).to.equal(trackId);
}
expect(axiosRetryStub.get.callCount).to.equal(1);
expect(axiosRetryStub.get.firstCall.args[0]).to.equal(`https://api.spotify.com/v1/tracks/${trackId}`);
expect(axiosRetryStub.get.firstCall.args[1]).to.deep.include({
headers: {
Authorization: `Bearer ${accessToken}`
}
});
});
});