Added Tests

- added tests for the dj class
- moved to rewire for mocking
pull/26/head
Trivernis 6 years ago
parent b8a92134fe
commit 990628244e

@ -15,11 +15,14 @@ let sysData = {};
* @return {String} A string that represents the file-extension. * @return {String} A string that represents the file-extension.
*/ */
exports.getExtension = function (filename) { exports.getExtension = function (filename) {
if (!filename) return null; if (!filename)
return null;
try { try {
let exts = filename.match(/\.[a-z]+/g); // get the extension by using regex let exts = filename.match(/\.\w+/g); // get the extension by using regex
if (exts) return exts[exts.length - 1]; // return the found extension if (exts)
else return null; // return null if no extension could be found return exts.pop(); // return the found extension
else
return null; // return null if no extension could be found
} catch (error) { } catch (error) {
console.error(error); console.error(error);
return null; return null;

@ -23,6 +23,7 @@
"chai": "^4.2.0", "chai": "^4.2.0",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"nyc": "^13.1.0", "nyc": "^13.1.0",
"rewire": "^4.0.1",
"sinon": "^7.2.3" "sinon": "^7.2.3"
} }
} }

@ -1,10 +1,13 @@
const sinon = require('sinon'), const sinon = require('sinon'),
chai = require('chai'); chai = require('chai'),
rewiremock = require('rewiremock').default;
beforeEach(() => { beforeEach(() => {
this.sandbox = sinon.createSandbox(); this.sandbox = sinon.createSandbox();
rewiremock.enable();
}); });
afterEach(() => { afterEach(() => {
this.sandbox.restore(); this.sandbox.restore();
rewiremock.disable();
}); });

@ -1,12 +1,30 @@
const mockobjects = require('./mockobjects.js'), const mockobjects = require('./mockobjects.js'),
sinon = require('sinon'); sinon = require('sinon'),
let Discord = require("discord.js"),
assert = require('assert'), assert = require('assert'),
config = require('../config.json'); rewire = require('rewire');
let Discord = require("discord.js");
describe('lib/utils', function() { describe('lib/utils', function() {
const utils = require('../lib/utils.js'); const utils = require('../lib/utils.js');
describe('#getExtension', function() {
it('returns the correct extension for a filename', function(done) {
assert(utils.getExtension('test.txt') === '.txt');
assert(utils.getExtension('test.tar.gz') === '.gz');
assert(utils.getExtension('../lib/utils.js') === '.js');
assert(utils.getExtension('.gitignore') === '.gitignore');
done();
});
it('returns null if the file has no extension or is no file', function(done) {
assert(utils.getExtension('filenameisstrange') === null);
assert(utils.getExtension('...') === null);
assert(utils.getExtension(Object.create({})) === null);
assert(utils.getExtension(null) === null);
done();
});
});
describe('#YouTube', function() { describe('#YouTube', function() {
it('returns if an url is valid', function(done) { it('returns if an url is valid', function(done) {
@ -134,35 +152,39 @@ describe('lib/utils', function() {
let modifiedMockLogger = mockobjects.mockLogger; let modifiedMockLogger = mockobjects.mockLogger;
modifiedMockLogger.error = (msg) => {}; modifiedMockLogger.error = (msg) => {};
let confVer = new utils.ConfigVerifyer(testObj, ['key1', 'key1.key3']); let confVer = new utils.ConfigVerifyer(testObj, ['key1', 'key1.key3']);
assert(!confVer.verifyConfig(mockobjects.mockLogger)); assert(!confVer.verifyConfig(modifiedMockLogger));
confVer = new utils.ConfigVerifyer(testObj, ['key1', 'key1.key2', 'key7.key8.0.key9']); confVer = new utils.ConfigVerifyer(testObj, ['key1', 'key1.key2', 'key7.key8.0.key9']);
assert(!confVer.verifyConfig(mockobjects.mockLogger)); assert(!confVer.verifyConfig(modifiedMockLogger));
done(); done();
}) })
}); });
}); });
// TODO: Repair and activate later describe('The dj class', function () {
describe('The dj class', function *() { const music = rewire('../lib/music');
const music = require('../lib/music'); const Readable = require('stream').Readable;
let ytdl = require("ytdl-core");
let yttl = require('get-youtube-title');
let ypi = require('youtube-playlist-info');
let ytdlMock = sinon.mock(ytdl); music.__set__("logger", mockobjects.mockLogger);
let yttlMock = sinon.mock(yttl); music.__set__("yttl", (id, cb) => {
let ypiMock = sinon.mock(ypi); cb(null, 'test');
});
music.__set__('ytdl', () => {
let s = new Readable();
s._read = () => {};
s.push('chunkofdataabc');
s.push(null);
return s;
});
it('connects to a VoiceChannel', function () { it('connects to a VoiceChannel', function (done) {
let dj = new music.DJ(mockobjects.mockVoicechannel); let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect(); dj.connect().then(()=> {
console.log(dj.connected);
assert(dj.connected); assert(dj.connected);
done();
});
}); });
it('listens on Repeat', function() { it('listens on Repeat', function () {
let dj = new music.DJ(mockobjects.mockVoicechannel); let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.current = {'url': '', 'title': ''}; dj.current = {'url': '', 'title': ''};
dj.listenOnRepeat = true; dj.listenOnRepeat = true;
@ -171,20 +193,112 @@ describe('The dj class', function *() {
assert(dj.queue.length > 0); assert(dj.queue.length > 0);
}); });
it('plays Files', function () { it('plays Files', function (done) {
let dj = new music.DJ(mockobjects.mockVoicechannel); let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect(); dj.connect().then(() => {
dj.playFile(); dj.playFile();
assert(dj.playing);
done();
});
});
it('plays YouTube urls', function (done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.playYouTube('http://www.youtube.com/watch?v=ABCDEFGHIJK');
});
setTimeout(() => {
assert(dj.playing); assert(dj.playing);
done();
}, 211);
});
it('gets the video name', function (done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.getVideoName('http://www.youtube.com/watch?v=ABCDEFGHIJK').then((name) => {
assert(name === 'test');
done();
})
});
it('sets the volume', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.playFile();
dj.setVolume(100);
assert(dj.volume === 100);
done();
})
}); });
it('plays YouTube urls', function () { it('pauses playback', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.playFile();
dj.pause();
done();
})
});
it('resumes playback', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel); let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.playYouTube('http://www.youtube.com/watch?v=abc'); dj.connect().then(() => {
dj.playFile();
dj.resume();
done();
})
});
it('stops playback', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.playFile();
assert(dj.playing); assert(dj.playing);
dj.stop();
assert(!dj.conn && !dj.disp);
done();
});
}); });
it('skips songs', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.playYouTube('http://www.youtube.com/watch?v=ABCDEFGHIJK');
dj.playYouTube('http://www.youtube.com/watch?v=ABCDEFGHIJK');
dj.playYouTube('http://www.youtube.com/watch?v=ABCDEFGHIJK');
dj.playYouTube('http://www.youtube.com/watch?v=ABCDEFGHIJK');
dj.skip();
dj.skip();
done();
});
});
it('returns a playlist', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.queue = [{
'title': 'title',
'url': 'http://www.youtube.com/watch?v=ABCDEFGHIJK'}, {
'title': 'title',
'url': 'http://www.youtube.com/watch?v=ABCDEFGHIJK'}];
assert(dj.playlist.length > 0);
done();
});
});
it('clears the queue', function(done) {
let dj = new music.DJ(mockobjects.mockVoicechannel);
dj.connect().then(() => {
dj.queue = [{
'title': 'title',
'url': 'http://www.youtube.com/watch?v=ABCDEFGHIJK'}, {
'title': 'title',
'url': 'http://www.youtube.com/watch?v=ABCDEFGHIJK'}];
dj.clear();
assert(dj.queue.length === 0);
done();
});
})
}); });
Loading…
Cancel
Save