Skip to content

Commit

Permalink
Merge branch 'feature/unit_tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtek committed Nov 2, 2018
2 parents 20d2095 + 73d7ff4 commit aa97fd1
Show file tree
Hide file tree
Showing 16 changed files with 2,595 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.idea
yarn.lock
/.vscode
/test/test-storage-write.json
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ test.js
mock.js
fulfillment_template.js
.DS_Store
/test/test-storage-write.json
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ const { Convo } = require('./src/convo');
const { ConvoApp } = require('./src/convo-app');
const { Say } = require('./src/say');
const { ConvoStorage } = require('./src/convo-storage');
const { ConvoTest } = require('./src/convo-test');

module.exports = { Convo, ConvoApp, Say, ConvoStorage };
module.exports = { Convo, ConvoApp, Say, ConvoStorage, ConvoTest };
91 changes: 90 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@tmtek/convo",
"version": "0.2.19",
"version": "0.2.21",
"description": "A uility for building conversational responses in DialogFlow fufillments",
"main": "index.js",
"scripts": {
"test": "eslint *.js src"
"test": "mocha"
},
"author": "tmtek",
"license": "MIT",
Expand All @@ -30,6 +30,7 @@
"homepage": "https://github.com/tmtek/convo#readme",
"devDependencies": {
"eslint": "^5.7.0",
"eslint-config-synacor": "^3.0.3"
"eslint-config-synacor": "^3.0.3",
"mocha": "^5.2.0"
}
}
44 changes: 38 additions & 6 deletions src/convo-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ class ConvoApp {
if (!value) {
return value;
}
return (typeof value === 'string' || value instanceof String) ? parseInt(value, 10) : value;
return (typeof value === 'string' || value instanceof String) ? parseInt(value, 10) : isNaN(value) ? null : value;
}

static correctForZeroIndex(value) {
if (value === undefined || value === null || typeof value !== 'number') {
throw new Error('can\'t correct for a value that is not a number.');
}
return value >= 1 ? value -1 : value;
}

constructor() {
this.registeredIntents = [];
//this.registeredIntents = [];
this.registeredIntents = {};
this.onRegisterIntents();
this._help = this.onPrepareHelp();
}
Expand All @@ -28,19 +32,42 @@ class ConvoApp {
onRegisterIntents() {}

registerIntent(intent, intentHandler) {
this.registeredIntents.push({ intent, intentHandler });
if (!intent || !intentHandler) {
throw new Error('You must submit an intent and intent handler.');
}
//this.registeredIntents.push({ intent, intentHandler });
this.registeredIntents[intent] = intentHandler;
return this;
}

bind(dialogFlowApp) {
this.registeredIntents.forEach(({ intent, intentHandler }) => {
Object.keys(this.registeredIntents).map(key => ({
intent: key,
intentHandler: this.registeredIntents[key]
})).forEach(({ intent, intentHandler }) => {
dialogFlowApp.intent(intent, (conv, params, extra) => intentHandler(new Convo(conv), params, extra));
});
return dialogFlowApp;
}

intent(convo, intent, params = {}, option = {}, debugOptions = {}){
let registeredIntent = this.registeredIntents.filter(registeredIntent => registeredIntent.intent === intent)[0];
return registeredIntent.intentHandler(convo, params, option, debugOptions).then(convo => ({ app: this, convo }));
//let registeredIntent = this.registeredIntents.filter(registeredIntent => registeredIntent.intent === intent)[0];
//return registeredIntent.intentHandler(convo, params, option, debugOptions).then(convo => ({ app: this, convo }));
if (!convo || !intent) {
throw new Error('You must submit a Convo and an intent.');
}
if (!this.registeredIntents[intent]) {
return Promise.resolve({ app: this, convo });
}
return Promise.resolve(
this.registeredIntents[intent](convo, params, option, debugOptions)
)
.then(resp => {
((debugOptions && debugOptions.log) && (!resp ||!resp.requests || resp.requests.length === 0)) && console.warn(
`intent:'${intent}' will cause Dialogflow to throw an error because you are not sending any repsonse to the user.`
);
return { app: this, convo, requests: resp && resp.requests };
});
}

registerListIntents() {
Expand Down Expand Up @@ -107,6 +134,11 @@ class ConvoApp {
);
}

presentList(convo, type, list, paging) {
return convo.setList(type, list, paging)
.forListPage(data => this._onRespondForList(data));
}

onListSelectUI(convo, type, itemName) {
return convo.selectFromListPage(ConvoApp.ensureNumber(itemName.split('_')[1]));
}
Expand Down
5 changes: 4 additions & 1 deletion src/convo-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ const { Convo } = require('./convo');

class ConvoStorage {
constructor(filename) {
if (!filename) {
throw new Error('ConvoStorage requires a filename to write to.');
}
this._filename = filename;
}

load(callback) {
new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
if (!this._filename) {
resolve({});
return;
Expand Down
34 changes: 34 additions & 0 deletions src/convo-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

class ConvoTest {
static containsResponseType(requests, types) {
if (!requests) {
throw new Error('Requests were not supplied.');
}
if (!types || types.length === 0) {
return false;
}
return types
.map(type => requests.filter(request => request.payload.type === type).length > 0)
.filter(typeVal => !typeVal)
.length === 0;
}
static isConversationClose(requests) {
return requests[0] && requests[0].action === 'close';
}
static testConversation(conversation, done) {
if (!conversation || !conversation.then) {
throw new Error('You must pass a conversational promise.');
}
if (!done) {
throw new Error('You must pass a done function.');
}
let isDone = false;
conversation.catch(err => {
isDone = true;
done(err);
})
.then(() => !isDone && done());
}
}

module.exports = { ConvoTest };
Loading

0 comments on commit aa97fd1

Please sign in to comment.