Skip to content

Commit

Permalink
Update: test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
benchambule committed Jul 27, 2024
1 parent 6416b28 commit b075ec0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 57 deletions.
29 changes: 16 additions & 13 deletions test/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function createBot(){

bot.addMenus(menus);

bot.intercept('*', () => {
bot.intercept('*', async () => {
return {
menu: {
name: "*-intercepted",
Expand All @@ -59,7 +59,7 @@ function createBot(){
}
});

bot.intercept('welcome', () => {
bot.intercept('welcome', async () => {
return {
menu: {
name: "welcome-intercepted",
Expand All @@ -84,11 +84,10 @@ describe('interceptors', function() {
name: "divider",
entrypoint: 'divider',
keyword: "@divider",
inline: true,
description: 'Display random divider'
});

bot.intercept('divider', function(req){
bot.intercept('divider', async function(req){
const reqs = req.prompt.trim().split(" ");
if(reqs.length < 3){
return {
Expand All @@ -107,7 +106,7 @@ describe('interceptors', function() {
}
});

bot.at('divider', function(req){
bot.at('divider', async function(req){
const reqs = req.prompt.trim().split(" ");

const a = reqs[1];
Expand All @@ -120,23 +119,27 @@ describe('interceptors', function() {
}
});

const result = bot.process({msisdn: '123', prompt:"@divider 10 5"});
assert.equal(result.menu.message, "10 / 5 = 2");
(async () => {
const result = await bot.process({msisdn: '123', prompt:"@divider 10 5"});
assert.equal(result.menu.message, "10 / 5 = 2");

assert.equal(bot.process({msisdn: '123', prompt:"@divider 10 0"}).menu.message, "Division by zero is not allowed");
assert.equal(bot.process({msisdn: '123', prompt:"@divider 10"}).menu.message, "Please provide the dividend and the divisor");
assert.equal(await (bot.process({msisdn: '123', prompt:"@divider 10 0"})).menu.message, "Division by zero is not allowed");
assert.equal(await (bot.process({msisdn: '123', prompt:"@divider 10"})).menu.message, "Please provide the dividend and the divisor");
})();
});
});

describe('non-inline bots', function() {
it('non-inline bot must return the correct menu', function(){
const bot = createBot();

let session = bot.process({msisdn:123, prompt:"@bot"});
assert.equal(session.menu.name, "welcome-intercepted");
(async () => {
let session = await bot.process({msisdn:123, prompt:"@bot"});
assert.equal(session.menu.name, "welcome-intercepted");

session = bot.process({msisdn:123, prompt:"1"}, session);
assert.equal(session.menu.name, "*-intercepted");
session = await bot.process({msisdn:123, prompt:"1"}, session);
assert.equal(session.menu.name, "*-intercepted");
})();
});
});
});
49 changes: 26 additions & 23 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const createOptionsBot = () => {
}
);

info.at('main', () => {
info.at('main', async () => {
const menu = {
name: 'main',
title: "Welcome to Ben's bot",
Expand All @@ -30,7 +30,7 @@ const createOptionsBot = () => {
};
});

info.at('profession', () => {
info.at('profession', async () => {
const menu = {
name: 'profession',
title: "Ben's profession",
Expand All @@ -44,7 +44,7 @@ const createOptionsBot = () => {
};
});

info.at('name', () => {
info.at('name', async () => {
const menu = {
name: "name",
title: "Ben's full name",
Expand All @@ -58,7 +58,7 @@ const createOptionsBot = () => {
};
});

info.at('age', () => {
info.at('age', async () => {
const menu = {
name: "age",
title: "Ben's age",
Expand All @@ -72,7 +72,7 @@ const createOptionsBot = () => {
};
});

info.at('country', () => {
info.at('country', async () => {
const menu = {
name: "country",
title: "Ben's country",
Expand All @@ -86,7 +86,7 @@ const createOptionsBot = () => {
};
});

info.intercept('*', function(request, tags, ctxt){
info.intercept('*', async function(request, tags, ctxt){
if(request.prompt.trim() === '@exit'){
return {
menu: {
Expand All @@ -103,29 +103,32 @@ const createOptionsBot = () => {

describe('options', function() {
it('bot must return the correct selected menu', function() {
const bot = createOptionsBot();
let session = bot.process({msisdn: "1234", prompt: '@info'});
assert.equal(session.menu.name, 'main');
(async () => {
const bot = createOptionsBot();

let session = await bot.process({msisdn: "1234", prompt: '@info'});
assert.equal(session.menu.name, 'main');

session = bot.process({msisdn: "1234", prompt: '1'}, session);
assert.equal(session.menu.name, 'name');
session = await bot.process({msisdn: "1234", prompt: '1'}, session);
assert.equal(session.menu.name, 'name');

session = bot.process({msisdn: "1234", prompt: '@info'}, session);
assert.equal(session.menu.name, 'main');
session = await bot.process({msisdn: "1234", prompt: '@info'}, session);
assert.equal(session.menu.name, 'main');

session = bot.process({msisdn: "1234", prompt: '2'}, session);
assert.equal(session.menu.name, 'profession');
session = await bot.process({msisdn: "1234", prompt: '2'}, session);
assert.equal(session.menu.name, 'profession');

session = bot.process({msisdn: "1234", prompt: '0'}, session);
assert.equal(session.menu.name, 'main');
session = await bot.process({msisdn: "1234", prompt: '0'}, session);
assert.equal(session.menu.name, 'main');

session = bot.process({msisdn: "1234", prompt: '3'}, session);
assert.equal(session.menu.name, 'age');
session = await bot.process({msisdn: "1234", prompt: '3'}, session);
assert.equal(session.menu.name, 'age');

session = bot.process({msisdn: "1234", prompt: '0'}, session);
assert.equal(session.menu.name, 'main');
session = await bot.process({msisdn: "1234", prompt: '0'}, session);
assert.equal(session.menu.name, 'main');

session = bot.process({msisdn: "1234", prompt: '4'}, session);
assert.equal(session.menu.name, 'country');
session = await bot.process({msisdn: "1234", prompt: '4'}, session);
assert.equal(session.menu.name, 'country');
})();
});
});
26 changes: 14 additions & 12 deletions test/required.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const createRequireBot = () => {
}
);

enq.at('name', () => {
enq.at('name', async () => {
const menu = {
name: 'name',
title: "Welcome to the enquiry bot",
Expand All @@ -31,7 +31,7 @@ const createRequireBot = () => {
};
});

enq.at('birthday', () => {
enq.at('birthday', async () => {
const menu = {
name: 'birthday',
title: "Welcome to the enquiry bot",
Expand All @@ -49,7 +49,7 @@ const createRequireBot = () => {
};
});

enq.at('sport', (request, tags) => {
enq.at('sport', async (request, tags) => {
tags['birthday'] = tags['birthday'].replace(/\//g, '-');

const menu = {
Expand All @@ -69,7 +69,7 @@ const createRequireBot = () => {
};
});

enq.at('show_info', () => {
enq.at('show_info', async () => {
const txt = "Name: {{name}}\nBirthday: {{birthday}}\nFavourite sport: {{sport}}";
const menu = {
name: 'show_info',
Expand All @@ -86,15 +86,17 @@ const createRequireBot = () => {
}

describe('Required', () => {
it('It should store and retrieve required info as tags', async () => {
const enq = createRequireBot();
it('It should store and retrieve required info as tags', () => {
(async () => {
const enq = createRequireBot();

let session = enq.process({'msisdn': '123', "prompt": "@enq"});
session = enq.process({'msisdn': '123', "prompt": "Ben Chambule"}, session);
session = enq.process({'msisdn': '123', "prompt": "23/04/1994"}, session);
session = enq.process({'msisdn': '123', "prompt": "no-sport"}, session);
const menu = enq.process({'msisdn': '123', "prompt": "Football"}, session).menu;
let session = await enq.process({'msisdn': '123', "prompt": "@enq"});
session = await enq.process({'msisdn': '123', "prompt": "Ben Chambule"}, session);
session = await enq.process({'msisdn': '123', "prompt": "23/04/1994"}, session);
session = await enq.process({'msisdn': '123', "prompt": "no-sport"}, session);
const menu = await enq.process({'msisdn': '123', "prompt": "Football"}, session).menu;

assert.equal(menu.message, "Name: Ben Chambule\nBirthday: 23-04-1994\nFavourite sport: Football");
assert.equal(menu.message, "Name: Ben Chambule\nBirthday: 23-04-1994\nFavourite sport: Football");
})();
});
})
20 changes: 11 additions & 9 deletions test/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const createInquiryBot = () => {
}
);

bot.at('main', function(req, tags){
bot.at('main', async function(req, tags){
const menu = {
name: 'main',
title: "Welcome to Ben's bot",
Expand All @@ -25,7 +25,7 @@ const createInquiryBot = () => {
}
});

bot.at('age', function(req, tags){
bot.at('age', async function(req, tags){
const menu = {
name: 'age',
title: "Welcome to Ben's bot",
Expand All @@ -41,7 +41,7 @@ const createInquiryBot = () => {
}
});

bot.at('show_info', function(req, tags){
bot.at('show_info', async function(req, tags){
tags['age'] = req.prompt;
const menu = {
name: 'show_info',
Expand All @@ -61,12 +61,14 @@ const createInquiryBot = () => {

describe('Tags', function(){
it('should be able to store and retrieve tags', function(){
const bot = createInquiryBot();
(async () => {
const bot = createInquiryBot();

let session = await bot.process({'msisdn': '123', "prompt": "@enq"});
session = await bot.process({'msisdn': '123', "prompt": "Ben Chambule"}, session);
const menu = await bot.process({'msisdn': '123', "prompt": "30"}, session).menu;

let session = bot.process({'msisdn': '123', "prompt": "@enq"});
session = bot.process({'msisdn': '123', "prompt": "Ben Chambule"}, session);
const menu = bot.process({'msisdn': '123', "prompt": "30"}, session).menu;

assert.equal(menu.message, "Provided information\nName: Ben Chambule\nAge: 30");
assert.equal(menu.message, "Provided information\nName: Ben Chambule\nAge: 30");
})();
});
});

0 comments on commit b075ec0

Please sign in to comment.