Skip to content

Commit

Permalink
test(wit.ai): add more tests for unsupported and postback messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Pintus committed Aug 21, 2019
1 parent 6f0c636 commit f0bb7d9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
100 changes: 98 additions & 2 deletions test/ts/witai-datacollection.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as chai from 'chai';
import { BotAgentManager, BotRequest, BotResponse } from '../../dist';
import * as http from 'request-promise-native';
import { BotAgentManager, BotRequest, BotResponse, PostbackMessage } from '../../dist';
import { DataCollectorTestWitBot } from './bot';
require('dotenv').config();

Expand Down Expand Up @@ -36,7 +36,7 @@ const getHTTPOptions = function getOptions(body) {
describe('Testing Wit.ai based bot for a simple data collection ', function() {
let env = process.env;

describe('Starting the bot', function() {
describe('Sending plain text messages', function() {
const getSettings = function(): any {
return {
engine: {
Expand Down Expand Up @@ -124,6 +124,102 @@ describe('Testing Wit.ai based bot for a simple data collection ', function() {
return;
});

after('shutdown bot manager', function() {
server.close();
});
});
describe('Sending plain text messages and one postback message for support type', function() {
const getSettings = function(): any {
return {
engine: {
type: engineType,
settings: {
token: env.WIT_TOKEN
}
}
};
};
const manager = new BotAgentManager();
let server;
before('starting bot manager', async function() {
manager.registerAgent(
engineType,
async (req: BotRequest): Promise<BotResponse> => {
const bot = new DataCollectorTestWitBot(req.settings.engine.settings.token);
return bot.sendMessage(req);
}
);
// Run the BotManager:
const port = (process.env.PORT as any) || 8080;
server = await manager.listen(port);
return;
});
it('should perform data collection and then end with data filled', async function() {
const request1: BotRequest = {
language: 'en',
event: 'start',
settings: getSettings()
};
//console.dir(request1, { colors: true, depth: 20 });
const result1 = await http(getHTTPOptions(request1));
//console.dir(result1, { colors: true, depth: 20 });
result1.context.contexts.should.include('ask_for_name');

const request2: BotRequest = {
language: 'en',
event: 'continue',
message: getTextMessage('Antonio Obama'),
settings: getSettings(),
context: result1.context,
data: result1.data
};
//console.log('Sending msg2');
//console.dir(request2, { colors: true, depth: 20 });
const result2 = await http(getHTTPOptions(request2));
//console.dir(result2, { colors: true, depth: 20 });
result2.context.contexts.should.include('ask_for_address');
result2.event.should.equal('continue');
result2.messages[0].body.should.contain('Send me your full address, please');

const request3: BotRequest = {
language: 'en',
event: 'continue',
message: getTextMessage('Palm Beach Street, 29 - Los Angeles'),
settings: getSettings(),
context: result2.context,
data: result2.data
};
//console.log('Sending msg2');
//console.dir(request2, { colors: true, depth: 20 });
const result3 = await http(getHTTPOptions(request3));
//console.dir(result2, { colors: true, depth: 20 });
result3.context.contexts.should.include('ask_for_support_type');
result3.event.should.equal('continue');
result3.messages[0].body.should.contain('Please, briefly describe why you need our support');

const request4: BotRequest = {
language: 'en',
event: 'continue',
message: {
code: 'message',
type: 'postback',
body: "I'm getting an error"
} as PostbackMessage,
settings: getSettings(),
context: result3.context,
data: result3.data
};
const result4 = await http(getHTTPOptions(request4));
//console.dir(result4, { colors: true, depth: 20 });
result4.context.contexts.should.include('end');
result4.event.should.equal('end');
result4.messages[0].body.should.contain('Done. Thank you.');
result4.data.name.should.contain('Antonio Obama');
result4.data.address.should.contain('Palm Beach Street, 29 - Los Angeles');
result4.data.supportType.should.contain('technical');
return;
});

after('shutdown bot manager', function() {
server.close();
});
Expand Down
19 changes: 17 additions & 2 deletions test/ts/witai-general.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as chai from 'chai';
import { BotAgentManager, BotRequest, BotResponse } from '../../dist';
import * as http from 'request-promise-native';
import { DataCollectorTestWitBot } from './witai-nointents-bot';
import { BotAgentManager, BotRequest, BotResponse, IsWritingMessage } from '../../dist';
import { DataCollectorTestWitBot as OkBot } from './bot';
import { DataCollectorTestWitBot } from './witai-nointents-bot';
require('dotenv').config();

chai.should();
Expand Down Expand Up @@ -118,6 +118,21 @@ describe('Testing Wit.ai based bot for a simple data collection ', function() {
result1.statusCode.should.equal(500);
return;
});
it('for an unsupported message type, it should raise an error', async function() {
const request1: BotRequest = {
language: 'en',
event: 'continue',
message: {
code: 'message',
type: 'iswriting'
} as IsWritingMessage,
settings: getSettings()
};
//console.dir(request1, { colors: true, depth: 20 });
const result1 = await http(getHTTPOptions(request1));
result1.statusCode.should.equal(500);
return;
});
it('for a message classified with unknown intent, contexts ok, it should use the inContext() function and return the entered text ', async function() {
const request1: BotRequest = {
language: 'en',
Expand Down

0 comments on commit f0bb7d9

Please sign in to comment.