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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 | 2x
2x
288x
288x
288x
288x
288x
288x
278x
2x
2x
2x
278x
278x
270x
260x
254x
246x
244x
36x
36x
36x
2x
254x
30x
224x
10x
10x
2x
2x
8x
2x
214x
214x
214x
4x
4x
210x
2x
244x
238x
6x
4x
4x
2x
2x
2x
246x
222x
24x
22x
22x
2x
2x
2x
6x
6x
2x
2x
4x
4x
260x
248x
12x
6x
6x
2x
4x
2x
6x
4x
2x
2x
2x
270x
10x
10x
2x
10x
10x
10x
68x
12x
12x
2x
10x
2x
56x
16x
2x
14x
16x
40x
24x
2x
22x
36x
36x
16x
2x
270x
254x
16x
8x
2x
6x
2x
278x
268x
10x
10x
2x
2x
8x
8x
2x
2x
6x
24x
24x
24x
24x
24x
24x
24x
4x
4x
2x
4x
2x
4x
4x
4x
2x
2x
288x
288x
288x
2x | const BaseScriptStep = require('./BaseScriptStep');
const inspect = require('util').inspect;
function BotMessage(step, config, bot, logReporter, prevStepPromise) {
BaseScriptStep.call(this, step, config, bot, logReporter, prevStepPromise);
this.receivedMessage = null;
this.replyResolver = null;
this.replyPromise = new Promise((resolve, reject) => {
this.replyResolver = resolve;
})
this.stepFinishedPromise = Promise.all([prevStepPromise, this.replyPromise]).then(() => {
return this.validate();
});
}
BotMessage.prototype = Object.create(BaseScriptStep.prototype);
BotMessage.prototype.constructor = BotMessage;
BotMessage.prototype.validate = function () {
return new Promise((resolve, reject) => {
this.validateSuggestedActions()
.then(() => {
return this.validateAttachments();
})
.then(() => {
return this.validateAttachmentLayout();
})
.then(() => {
return this.validateBotMessage();
})
.then(() => {
return this.validateEndConversation();
})
.then(() => {
return this.validateTyping();
})
.then(resolve)
.catch((error) => {
this.logReporter.info(`Expected message at step ${this.step}`, this.config);
this.logReporter.expectationError(this.step, this.receivedMessage, error);
reject(error);
})
})
}
BotMessage.prototype.validateBotMessage = function () {
if (!this.config.bot) {
return Promise.resolve();
}
if ("function" == typeof this.config.bot) {
let promise = this.config.bot.call(null, this.receivedMessage);
if (!(promise instanceof Promise )) {
let error = `Message validation by callback failed. Callback MUST return a promise `;
return Promise.reject(error);
}
return promise.catch((error) => {
return Promise.reject(error);
})
} else {
let isRegExp = this.config.bot.test ? true : false;
let result = isRegExp ? this.config.bot.test(this.receivedMessage.text) : this.receivedMessage.text === this.config.bot;
if (!result) {
let error = `Received text <${this.receivedMessage.text}> does not match <${this.config.bot}>`;
return Promise.reject(error);
}
return Promise.resolve();
}
}
BotMessage.prototype.validateTyping = function () {
if (!this.config.typing) {
return Promise.resolve();
}
if ("typing" == this.receivedMessage.type) {
this.logReporter.typing(this.step);
return Promise.resolve();
} else {
let msg = 'Typing indicator expected';
return Promise.reject(msg);
}
}
BotMessage.prototype.validateEndConversation = function () {
if (!this.config.endConversation) {
return Promise.resolve();
}
if ("endOfConversation" == this.receivedMessage.type) {
this.logReporter.endConversation(this.step);
return Promise.resolve();
} else {
let msg = 'endConversation indicator expected'
return Promise.reject(msg);
}
}
BotMessage.prototype.validateAttachmentLayout = function () {
function returnValidationError(errorMessage, rejectionError) {
let msg = 'Validation of Attachment Layout Failed:' + errorMessage;
if (rejectionError) {
msg += "\n Rejection Error Captured:\n" + rejectionError.toString ? rejectionError.toString() : inspect(rejectionError);
throw rejectionError;
}
Iif (rejectionError) {
}
return Promise.reject((msg))
}
if (!this.config.attachmentLayout) {
return Promise.resolve();
}
if ("function" == typeof this.config.attachmentLayout) {
let promise = this.config.attachmentLayout.call(null, this.receivedMessage.attachmentLayout);
if (!(promise instanceof Promise )) {
return returnValidationError('Callback MUST return a promise');
}
return promise.catch((error) => {
return returnValidationError('Promise returned by Callback rejected.', error);
})
} else if (this.config.attachmentLayout == this.receivedMessage.attachmentLayout) {
return Promise.resolve();
} else {
let msg = 'Expected value != Received value\n'
+ '\nExpected value: ' + inspect(this.config.attachmentLayout)
+ '\nReceived value: ' + inspect(this.receivedMessage.attachmentLayout)
return returnValidationError(msg);
}
}
BotMessage.prototype.validateAttachments = function () {
let iterateAndValidate = function (validatorConfig, receivedValue, path, error) {
function throwValidationError(title, keyPath, optionalPromiseError) {
let msg = '[Attachment validation failed] ' + title
+ '\nExpected value: ' + inspect(validatorConfig)
+ '\nReceived value: ' + inspect(receivedValue)
+ '\nKey Path to Failure in attachment: ' + keyPath + "\n";
if (optionalPromiseError) {
msg += `[Rejection Error Received]:\n${inspect(optionalPromiseError)}`
}
let result = new Error(msg);
result.promiseError = error;
throw result;
}
if ("function" == typeof validatorConfig) {
let promise = validatorConfig.call(null, receivedValue, path);
if (!(promise instanceof Promise )) {
throwValidationError('Callback MUST return a Promise', path);
}
return promise.catch((optionalPromiseError) => {
throwValidationError('The Promise instance returned by callback rejected. ', path, optionalPromiseError);
})
} else if (Array.isArray(validatorConfig)) {
if (!Array.isArray(receivedValue)) {
throwValidationError('Array instance expected. ', path);
}
validatorConfig.forEach((item, i) => {
iterateAndValidate(item, receivedValue[i], path + `[${i}]`)
});
} else if ("object" == typeof validatorConfig) {
if ("object" != typeof receivedValue) {
throwValidationError('Object instance expected. ', path);
}
for (let key in validatorConfig) {
Eif (validatorConfig.hasOwnProperty(key)) {
iterateAndValidate(validatorConfig[key], receivedValue[key], path + `[${key}]`);
}
}
} else if (validatorConfig != receivedValue) {
throwValidationError('Scalar value expected. ', path);
}
}
if (!this.config.attachments) {
return Promise.resolve();
}
let response = iterateAndValidate(this.config.attachments, this.receivedMessage.attachments, 'attachments')
if (response instanceof Promise) {
return response;
} else {
return Promise.resolve();
}
}
BotMessage.prototype.validateSuggestedActions = function () {
if (!this.config.suggestedActions) {
return Promise.resolve();
}
let isSuggestedActionsPresent = this.receivedMessage.suggestedActions
&& this.receivedMessage.suggestedActions.actions
&& this.receivedMessage.suggestedActions.actions.length;
if (!isSuggestedActionsPresent) {
let msg = `Step #${this.step}, Message misses Suggested Actions`;
return Promise.reject(msg);
}
let isRangeError = this.config.suggestedActions.length != this.receivedMessage.suggestedActions.actions.length;
if (isRangeError) {
let msg = (`Step #${this.step}, amount of received suggested actions (${this.receivedMessage.suggestedActions.actions.length}) differs from expected (${this.config.suggestedActions.length})`);
return Promise.reject(msg);
}
for (let i = 0; i < this.config.suggestedActions.length; i++) {
let expectedAction = this.config.suggestedActions[i];
let messageAction = this.receivedMessage.suggestedActions.actions[i];
//
let isSameType = messageAction['type'] == expectedAction.data['type'];
let isSameValue = messageAction['value'] == expectedAction.data['value'];
let isSameTitle = messageAction['title'] == expectedAction.data['title'];
let isOk = isSameTitle && isSameType && isSameValue;
if (!isOk) {
let msg = `Step #${this.step}, Failed to compare actions with index ${i}. Reasons:`;
if (!isSameTitle) {
msg += `\n- Expected title: ${expectedAction.data.title}\n- Received title: ${messageAction["title"]}`;
}
if (!isSameType) {
msg += "\n- Wrong type of the card";
}
Eif (!isSameValue) {
msg += `\n- Messages are different:\n- Expected value: ${expectedAction.data.value}\n- Received value: ${messageAction['value']}`;
}
return Promise.reject(msg);
}
}
return Promise.resolve();
}
BotMessage.prototype.receiveBotReply = function (receivedMessage) {
this.receivedMessage = receivedMessage;
this.logReporter.messageReceived(this.step, receivedMessage);
this.replyResolver();
}
module.exports = BotMessage; |