From b7e66f8fd5241b5e126fad1c4b8c86f35acb761d Mon Sep 17 00:00:00 2001 From: Trivernis Date: Sat, 26 Jan 2019 13:34:01 +0100 Subject: [PATCH] Added Tests - added tests for lib/guilding --- lib/guilding.js | 1 + test/cmdTest.js | 39 ------------------- test/guildingTest.js | 44 --------------------- test/mockobjects.js | 24 ++++++++++++ test/test.js | 91 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 83 deletions(-) delete mode 100644 test/cmdTest.js delete mode 100644 test/guildingTest.js diff --git a/lib/guilding.js b/lib/guilding.js index 555fb09..048400b 100644 --- a/lib/guilding.js +++ b/lib/guilding.js @@ -12,6 +12,7 @@ let logger = require('winston'); exports.setLogger = function (newLogger) { logger = newLogger; music.setLogger(logger); + cmd.setLogger(logger); }; /** diff --git a/test/cmdTest.js b/test/cmdTest.js deleted file mode 100644 index eae8b1a..0000000 --- a/test/cmdTest.js +++ /dev/null @@ -1,39 +0,0 @@ -const cmd = require("../lib/cmd.js"), - mockobjects = require("./mockobjects.js"), - servercmd = require('../commands/servercommands'); - -function main() { - cmd.setLogger(mockobjects.mockLogger); - console.log('Creating new servant instance'); - let servant = new cmd.Servant('#'); - console.log('registering all music commands...'); - - for (let [key, value] of Object.entries(servercmd.music)) { - servant.createCommand(value, () => { - console.log(` - invoked ${value.name} callback`); - }); - } - - console.log('parsing and deleting all music commands...'); - for (let [key, value] of Object.entries(servercmd.music)) { - servant.parseCommand({ - content: '#' + value.name, - author: { - tag: undefined - } - }); - servant.removeCommand(value.name); - } - - process.exit(0); -} - -if (typeof require !== "undefined" && require.main === module) { - process.on("unhandledRejection", (reason, p) => { - console.error("Unhandled Rejection at: Promise", p, "reason:", reason); - throw Error("Promise rejection"); - }); - - setTimeout(() => process.exit(1), 60000); - main(); -} \ No newline at end of file diff --git a/test/guildingTest.js b/test/guildingTest.js deleted file mode 100644 index fcad9c1..0000000 --- a/test/guildingTest.js +++ /dev/null @@ -1,44 +0,0 @@ -const guilding = require("../lib/guilding.js") - music = require("../lib/music.js"), - mockobjects = require("./mockobjects.js"), - servercmd = require("../commands/servercommands"); - -function main() { - guilding.setLogger(mockobjects.mockLogger); - music.setLogger(mockobjects.mockLogger); - console.log('Creating guildHandler instance'); - let guildHandler = new guilding.GuildHandler('TEST', '#'); - guildHandler.dj = new music.DJ(mockobjects.mockVoicechannel); - - setTimeout(() => { - for (let [key, value] of Object.entries(servercmd.music)) { - guildHandler.handleMessage({ - content: '#' + value.name + ' arg1 arg2 arg3 arg4', - author: { - tag: undefined, - id: 0, - createdTimestamp: new Date(), - username: 'TEST' - }, - member: { - voiceChannel: mockobjects.mockVoicechannel - }, - channel: mockobjects.mockChannel, - reply: mockobjects.mockChannel.send - }); - } - - guildHandler.destroy(); - process.exit(0); - }, 1000); -} - -if (typeof require !== "undefined" && require.main === module) { - process.on("unhandledRejection", (reason, p) => { - console.error("Unhandled Rejection at: Promise", p, "reason:", reason); - throw Error("Promise rejection"); - }); - - setTimeout(() => process.exit(1), 60000); - main(); -} \ No newline at end of file diff --git a/test/mockobjects.js b/test/mockobjects.js index d34eb15..62da037 100644 --- a/test/mockobjects.js +++ b/test/mockobjects.js @@ -72,4 +72,28 @@ exports.mockCommand = { title: 'rich embed' }}; } +}; + +exports.MockDatabase = class { + constructor(file, callback) { + callback(); + } + + run(sql, values, callback) { + if(callback) { + callback(); + } + } + + get() { + return null; + } + + all() { + return null + } + + close() { + return true; + } }; \ No newline at end of file diff --git a/test/test.js b/test/test.js index a380f04..9df1dc0 100644 --- a/test/test.js +++ b/test/test.js @@ -346,4 +346,95 @@ describe('lib/cmd', function() { assert(spy.called); }); }); +}); + +describe('lib/guilding', function() { + const guilding = rewire('../lib/guilding'); + guilding.__set__("sqlite3", null); + guilding.__set__("utils", { + dirExistence: (file, callback) => { + } + }); + guilding.setLogger(mockobjects.mockLogger); + + describe('#GuildHandler', function() { + + it('initializes', function() { + let gh = new guilding.GuildHandler('test', ''); + gh.db = new mockobjects.MockDatabase('', ()=>{}); + gh.createTables(); + gh.registerMusicCommands(); + gh.ready = true; + assert(gh.ready); + }); + + it('destroyes itself', function() { + let gh = new guilding.GuildHandler('test', ''); + gh.db = new mockobjects.MockDatabase('', ()=>{}); + gh.createTables(); + gh.registerMusicCommands(); + gh.ready = true; + gh.destroy(); + assert(!gh.dj.conn); + }); + + it('answers messages', function() { + let gh = new guilding.GuildHandler('test', ''); + gh.db = new mockobjects.MockDatabase('', ()=>{}); + gh.createTables(); + gh.registerMusicCommands(); + gh.ready = true; + let msgSpy = sinon.spy(); + gh.answerMessage({ + content: 'test', + author: { + tag: undefined + }, + reply: msgSpy, + channel: { + send: msgSpy + } + }, 'Answer'); + assert(msgSpy.called); + }); + + it('handles messages', function() { + let gh = new guilding.GuildHandler('test', '~'); + gh.db = new mockobjects.MockDatabase('', ()=>{}); + gh.ready = true; + let cbSpy = sinon.spy(); + gh.servant.createCommand(mockobjects.mockCommand, cbSpy); + assert(gh.servant.commands['~test']); + gh.handleMessage({ + content: '~test', + author: { + tag: undefined + }}); + assert(cbSpy.called); + }); + + it('connects and plays', function(done) { + const music = rewire('../lib/music'); + const Readable = require('stream').Readable; + + music.__set__("logger", mockobjects.mockLogger); + music.__set__("yttl", (id, cb) => { + cb(null, 'test'); + }); + music.__set__('ytdl', () => { + let s = new Readable(); + s._read = () => {}; + s.push('chunkofdataabc'); + s.push(null); + return s; + }); + let gh = new guilding.GuildHandler('test', '~'); + gh.db = new mockobjects.MockDatabase('', ()=>{}); + gh.ready = true; + gh.dj = new music.DJ(mockobjects.mockVoicechannel); + gh.connectAndPlay(mockobjects.mockVoicechannel, 'test', false).then(() => { + done(); + }) + }); + }); }); \ No newline at end of file