-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathresponse.js
163 lines (143 loc) · 6.22 KB
/
response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict';
const striptags = require('striptags');
const assert = require('chai').assert;
const _ = require('underscore');
const fuzzy = require('fuzzyset.js');
function initResponse(test, conversationApi, fixSpaces, fuzzyDefault, isSsml) {
const type = isSsml ? 'ssml' : 'plain text';
const FUZZY_MIN_SCORE = _.isUndefined(fuzzyDefault) ? 0.93 : fuzzyDefault; // minimum distance to match strings
const fixSpacesRegex = /(^[ ]{1,})|( )(?=[.,;!()/])|([ ]{1,}$)|[ ]{1,}(?= )/g;
// Private
function processActual(actual) {
if (type !== 'ssml') actual = striptags(actual);
if (fixSpaces) {
return actual.replace(fixSpacesRegex, '');
}
return actual;
}
// public
const responseApi = {
shouldEqual: (...args) => shouldEqual(true, ...args),
shouldNotEqual: (...args) => shouldEqual(false, ...args),
shouldContain: (...args) => shouldContain(true, ...args),
shouldNotContain: (...args) => shouldContain(false, ...args),
shouldApproximate: (...args) => shouldApproximate(true, ...args),
shouldNotApproximate: (...args) => shouldApproximate(false, ...args),
shouldMatch: (...args) => shouldMatch(true, ...args),
shouldNotMatch: (...args) => shouldMatch(false, ...args)
};
function api() {
return _.extend(conversationApi, responseApi);
}
function not(b) {
return b ? '' : 'not ';
}
function paramPassed(param) {
return !(_.isNull(param) || _.isUndefined(param));
}
function shouldFuzzyMatch(shouldOrNot, actual, expectedSet, minFuzzyScore) {
const match = expectedSet.get(actual);
const res = {fuzzyMatch: false, score: null};
if (match && match[0][0]) {
res.score = match[0][0];
if (match[0][0] >= minFuzzyScore) {
res.fuzzyMatch = true;
}
}
return shouldOrNot ? res : {fuzzyMatch: !res.fuzzyMatch, score: res.score};
}
function getActuals(testCase) {
let speech = '';
let reprompt = '';
if (testCase.actual.response) {
if (testCase.actual.response.outputSpeech.ssml) {
speech = processActual(testCase.actual.response.outputSpeech.ssml);
}
if (testCase.actual.response.reprompt && testCase.actual.response.reprompt.outputSpeech.ssml) {
reprompt = processActual(testCase.actual.response.reprompt.outputSpeech.ssml);
}
}
return {speech, reprompt};
}
// used to build public function
function shouldEqual(shouldOrNot, expected, expectedReprompt) {
test.checks.push((testCase) => {
const actuals = getActuals(testCase);
if (paramPassed(expected)) {
it(`Alexa's ${type} response should ${not(shouldOrNot)}equal: ${expected}`, () =>
shouldOrNot ? assert.equal(actuals.speech, expected) : assert.notEqual(actuals.speech, expected)
);
}
if (paramPassed(expectedReprompt)) {
it(`Alexa's ${type} reprompt should ${not(shouldOrNot)}equal: ${expectedReprompt}`, () =>
shouldOrNot ? assert.equal(actuals.reprompt, expectedReprompt) : assert.notEqual(actuals.reprompt, expectedReprompt)
);
}
});
return api(); // response.shouldEqual(...).shouldContain(...).{something}
}
function shouldContain(shouldOrNot, expected, expectedReprompt) {
test.checks.push((testCase) => {
const actuals = getActuals(testCase);
if (paramPassed(expected)) {
it(`Alexa's ${type} response should ${not(shouldOrNot)}contain: ${expected}`, () =>
shouldOrNot ? assert.include(actuals.speech, expected) : assert.notInclude(actuals.speech, expected)
);
}
if (paramPassed(expectedReprompt)) {
it(`Alexa's ${type} reprompt should ${not(shouldOrNot)}contain: ${expectedReprompt}`, () =>
shouldOrNot ? assert.include(actuals.reprompt, expectedReprompt) : assert.notInclude(actuals.reprompt, expectedReprompt)
);
}
});
return api();
}
function shouldMatch(shouldOrNot, regex, regexReprompt) {
test.checks.push((testCase) => {
const actuals = getActuals(testCase);
if (paramPassed(regex)) {
it(`Alexa's ${type} response should ${not(shouldOrNot)}match: ${regex}`, () =>
shouldOrNot ? assert.match(actuals.speech, regex) : assert.notMatch(actuals.speech, regex)
);
}
if (paramPassed(regexReprompt)) {
it(`Alexa's ${type} reprompt should ${not(shouldOrNot)}match: ${regexReprompt}`, () =>
shouldOrNot ? assert.match(actuals.reprompt, regexReprompt) : assert.notMatch(actuals.reprompt, regexReprompt)
);
}
});
return api();
}
function shouldApproximate(shouldOrNot, expected, expectedReprompt, fuzzyScoreOverride) {
const minFuzzyScore = (typeof fuzzyScoreOverride === 'undefined') ? FUZZY_MIN_SCORE : fuzzyScoreOverride;
test.checks.push((testCase) => {
const actuals = getActuals(testCase);
if (paramPassed(expected)) {
const expectedFuzzy = fuzzy([expected]);
it(`Alexa's ${type} response should ${not(shouldOrNot)}approximate (min. fuzzy match score: ${minFuzzyScore}): ${expected}`, () => {
const {fuzzyMatch, score} = shouldFuzzyMatch(shouldOrNot, actuals.speech, expectedFuzzy, minFuzzyScore);
assert(
fuzzyMatch,
`Actual is similar to expected with a score over ${minFuzzyScore}\n\nActual:\n\n ${actuals.speech}\n\nFuzzy Expected:\n\n ${expected}\n\nActual Fuzzy Score:\n\n ${score}`
);
});
}
if (paramPassed(expectedReprompt)) {
const repromptFuzzy = fuzzy([expectedReprompt]);
it(`Alexa's ${type} reprompt should ${not(shouldOrNot)}approximate (min. fuzzy match score: ${minFuzzyScore}): ${expected}`, () => {
const {fuzzyMatch, score} = shouldFuzzyMatch(shouldOrNot, actuals.reprompt, repromptFuzzy, minFuzzyScore);
assert(
fuzzyMatch,
`Actual is similar to expected with a score over ${minFuzzyScore}\n\nActual:\n\n ${actuals.reprompt}\n\nFuzzy Expected:\n\n ${expectedReprompt}\n\nActual Fuzzy Score:\n\n ${score}`
);
});
}
});
return api(); // response.shouldEqual(...).shouldContain(...).{something}
}
return responseApi; // response.{something}
}
module.exports = {
plain: (...args) => initResponse(...args, false),
ssml: (...args) => initResponse(...args, true)
};