|
| 1 | +/** |
| 2 | + * Copyright 2016, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +require(`../../../system-test/_setup`); |
| 19 | + |
| 20 | +const fs = require(`fs`); |
| 21 | +const path = require(`path`); |
| 22 | +const proxyquire = require(`proxyquire`).noCallThru(); |
| 23 | +const sqlite3 = require(`sqlite3`).verbose(); |
| 24 | + |
| 25 | +const DB_PATH = path.join(__dirname, `../slackDB.db`); |
| 26 | +const SLACK_TOKEN_PATH = path.join(__dirname, `../.token`); |
| 27 | +const text = `President Obama is speaking at the White House.`; |
| 28 | + |
| 29 | +let db, controllerMock, botkitMock, botMock, program; |
| 30 | + |
| 31 | +test.before.cb((t) => { |
| 32 | + fs.unlink(DB_PATH, (err) => { |
| 33 | + if (err && err.code !== `ENOENT`) { |
| 34 | + t.end(err); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + db = new sqlite3.cached.Database(DB_PATH); |
| 39 | + controllerMock = { |
| 40 | + spawn: sinon.stub().returnsThis(), |
| 41 | + startRTM: sinon.stub().returnsThis(), |
| 42 | + hears: sinon.stub().returnsThis(), |
| 43 | + on: sinon.stub().returnsThis() |
| 44 | + }; |
| 45 | + |
| 46 | + botkitMock = { |
| 47 | + slackbot: sinon.stub().returns(controllerMock) |
| 48 | + }; |
| 49 | + |
| 50 | + botMock = { |
| 51 | + reply: sinon.stub() |
| 52 | + }; |
| 53 | + |
| 54 | + program = proxyquire(`../demo_bot`, { |
| 55 | + botkit: botkitMock |
| 56 | + }); |
| 57 | + |
| 58 | + db.run(program.TABLE_SQL, t.end); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +test.after.cb((t) => { |
| 63 | + fs.unlink(DB_PATH, (err) => { |
| 64 | + if (err) { |
| 65 | + t.end(err); |
| 66 | + return; |
| 67 | + } |
| 68 | + try { |
| 69 | + fs.unlinkSync(SLACK_TOKEN_PATH); |
| 70 | + } catch (err) { |
| 71 | + // Ignore error |
| 72 | + } |
| 73 | + t.end(); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test.serial(`should analyze sentiment in text`, async (t) => { |
| 78 | + const sentiment = await program.analyzeSentiment(text); |
| 79 | + t.is(sentiment > 0, true); |
| 80 | +}); |
| 81 | + |
| 82 | +test.serial(`should analyze entities in text`, async (t) => { |
| 83 | + const entities = await program.analyzeEntities(text, Date.now()); |
| 84 | + t.is(entities.some((entity) => entity.name === `Obama`), true); |
| 85 | + t.is(entities.some((entity) => entity.name === `White House`), true); |
| 86 | + |
| 87 | + await new Promise((resolve, reject) => { |
| 88 | + setTimeout(() => { |
| 89 | + db.all(`select * from entities`, (err, entities) => { |
| 90 | + if (err) { |
| 91 | + reject(err); |
| 92 | + return; |
| 93 | + } |
| 94 | + t.is(entities.some((entity) => entity.name === `Obama`), true); |
| 95 | + t.is(entities.some((entity) => entity.name === `White House`), true); |
| 96 | + resolve(); |
| 97 | + }); |
| 98 | + }, 1000); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +test.serial(`should reply to simple hello message`, (t) => { |
| 103 | + const message = {}; |
| 104 | + |
| 105 | + program.handleSimpleReply(botMock, message); |
| 106 | + |
| 107 | + t.is(botMock.reply.callCount, 1); |
| 108 | + t.deepEqual(botMock.reply.getCall(0).args, [message, `Hello.`]); |
| 109 | +}); |
| 110 | + |
| 111 | +test.cb.serial(`should reply to entities message`, (t) => { |
| 112 | + const message = {}; |
| 113 | + |
| 114 | + program.handleEntitiesReply(botMock, message); |
| 115 | + |
| 116 | + setTimeout(() => { |
| 117 | + try { |
| 118 | + t.is(botMock.reply.callCount, 3); |
| 119 | + t.deepEqual(botMock.reply.getCall(1).args, [message, `Top entities: `]); |
| 120 | + t.deepEqual(botMock.reply.getCall(2).args, [message, `entity: *Obama*, type: PERSON, count: 1\nentity: *White House*, type: LOCATION, count: 1\n`]); |
| 121 | + t.end(); |
| 122 | + } catch (err) { |
| 123 | + t.end(err); |
| 124 | + } |
| 125 | + }, 1000); |
| 126 | +}); |
0 commit comments