Skip to content

Commit

Permalink
Merge pull request #3 from ExpediaInc/jgamell/architecturalImprovements
Browse files Browse the repository at this point in the history
Architectural improvements
  • Loading branch information
vchanExpedia authored Jan 10, 2017
2 parents 753d08c + 4951709 commit 3e75888
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 84 deletions.
13 changes: 12 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
"code": 100,
"ignoreStrings": true,
"ignoreTemplateLiterals": true
}]
}],
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}],
"quotes": ["error", "single", {
"allowTemplateLiterals": true,
"avoidEscape": true
}
],
"prefer-arrow-callback": "error",
"camelcase": "warn"
}
}
70 changes: 0 additions & 70 deletions events.js

This file was deleted.

19 changes: 7 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
const colors = require('colors/safe');
const StackTrace = require('stacktrace-js');
const _ = require('underscore');
const ev = require('./events');
const response = require('./response.js');
const RequestBuilder = require('./request-builder');
const response = require('./response');

function sendEvent(event, app) {
function sendRequest(event, app) {
return new Promise((resolve, reject) => {
app.handler(event, {
succeed: resolve,
Expand All @@ -29,13 +29,12 @@ module.exports = function conversation({name, app, appId,
fixSpaces = false,
fuzzyDistance = 0.93
}) {
ev.init({appId, sessionId, userId, accessToken, requestId, locale});
const requestBuilder = RequestBuilder.init({appId, sessionId, userId, accessToken, requestId, locale});
// chain of promises to handle the different conversation steps
const conversationName = name;
const tests = [];
let dialog = Promise.resolve(); // start of chain of promises
let step = -1;
let isNew = true;

const api = { // public API
userSays,
Expand Down Expand Up @@ -75,23 +74,19 @@ module.exports = function conversation({name, app, appId,

// Public

function userSays(intentName, slots) {
function userSays(intentName, slotsArg) {
step++;
initStep(step);
slots = slots || {};
if (step > 0) isNew = false;
const slots = slotsArg || {};
const index = step;
dialog = dialog.then(prevEvent =>
sendEvent(ev.buildRequest(intentName, slots, isNew, prevEvent), app).then(res => {
sendRequest(requestBuilder.build(intentName, slots, prevEvent), app).then(res => {
tests[index] = _.extend(tests[index], {intentName, slots, actual: res});
return res;
})
); // return promise already

const testCase = tests[step];

console.log('Instantiating responses fixSpaces = '+fixSpaces);

api.plainResponse = response.plain(testCase, api, fixSpaces, fuzzyDistance);
api.ssmlResponse = response.ssml(testCase, api, fixSpaces, fuzzyDistance);

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
"name": "alexa-conversation",
"version": "0.0.1",
"description": "Functional test framework for Amazon Alexa skills",
"repository": "https://github.com/ExpediaInc/alexa-conversation",
"license": "ISC",
"engines": {
"node": ">=5.0.0"
},
"main": "index.js",
"scripts": {
"test": "mocha --recursive ./test"
"lint": "eslint .",
"test": "mocha --recursive ./test",
"build": "npm lint && npm test"
},
"keywords": [],
"author": "",
Expand Down
64 changes: 64 additions & 0 deletions request-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

const _ = require('underscore');

function buildSlots(slots) {
const res = {};
_.each(slots, (value, key) => {
res[key] = {
name: key,
value: value
};
});
return res;
}

function buildSession(e) {
return e ? e.sessionAttributes : {};
}

function init(options) {
let isNew = true;

// public API

const api = {
init,
build
};

function build(intentName, slots, prevEvent) {
if (!options.appId) throw String('AppId not specified. Please run events.init(appId) before building a Request');
const res = { // override more stuff later as we need
session: {
sessionId: options.sessionId,
application: {
applicationId: options.appId
},
attributes: buildSession(prevEvent),
user: {
userId: options.userId,
accessToken: options.accessToken
},
new: isNew
},
request: {
type: 'IntentRequest',
requestId: options.requestId,
locale: options.locale,
timestamp: (new Date()).toISOString(),
intent: {
name: intentName,
slots: buildSlots(slots)
}
},
version: '1.0'
};
isNew = false;
return res;
}

return api;
}

module.exports = {init};

0 comments on commit 3e75888

Please sign in to comment.