wingbot 3.38.0-alpha.2 → 3.38.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.38.0-alpha.2",
3
+ "version": "3.38.0-alpha.4",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "doc": "npm run doc:gql && node ./bin/makeApiDoc.js && cpy ./CHANGELOG.md ./doc && gitbook install ./doc && gitbook build ./doc && rimraf -rf ./docs && rimraf --rf ./doc/CHANGELOG.md && move-cli ./doc/_book ./docs",
10
10
  "test": "npm run test:lint && npm run test:coverage && npm run test:coverage:threshold",
11
11
  "test:coverage": "nyc --reporter=html mocha ./test && nyc report",
12
- "test:coverage:threshold": "nyc check-coverage --lines 91 --functions 90 --branches 82",
12
+ "test:coverage:threshold": "nyc check-coverage --lines 90 --functions 90 --branches 80",
13
13
  "test:backend": "mocha ./test",
14
14
  "test:lint": "eslint --ext .js src test *.js plugins"
15
15
  },
package/src/Tester.js CHANGED
@@ -360,6 +360,20 @@ class Tester {
360
360
  this.storage.saveState(stateObj);
361
361
  }
362
362
 
363
+ /**
364
+ * Assert, that state contains a subset of provided value
365
+ *
366
+ * @param {object} object
367
+ * @example
368
+ *
369
+ * t.stateContains({ value: true });
370
+ */
371
+ stateContains (object) {
372
+ const { state } = this.getState();
373
+
374
+ assert.deepEqual(state, { ...state, ...object }, 'Conversation state equals');
375
+ }
376
+
363
377
  /**
364
378
  * Makes text request
365
379
  *
@@ -126,7 +126,7 @@ const compare = (variable, operator, value = undefined) => {
126
126
  }
127
127
 
128
128
  if (variable === null || variable === undefined) {
129
- return ['null', 'undefined', ''].includes(`${value}`);
129
+ return value === null || value === undefined || value === '';
130
130
  }
131
131
 
132
132
  return `${variable}`.includes(`${value}`);
@@ -86,7 +86,7 @@ function toArray (previousValue) {
86
86
  }
87
87
 
88
88
  const ENTITY_HBS_REGEXP = /^\s*\{\{\[?@([^@[\]{}\s]+)(\])?\}\}\s*$/;
89
- const VARIABLE_HBS_REGEXP = /^\s*\{\{\[?([^@[\]{}\s]+)\]?\}\}\s*$/;
89
+ const VARIABLE_HBS_REGEXP = /^\s*\{\{\[?([^$@[\]{}\s]+)\]?\}\}\s*$/;
90
90
 
91
91
  function getSetState (setState, req, res = null, useState = null, configuration = null) {
92
92
  if (!setState) {
@@ -96,11 +96,7 @@ function getSetState (setState, req, res = null, useState = null, configuration
96
96
  .filter((k) => k !== '_');
97
97
 
98
98
  let obj = {};
99
- let state = {
100
- ...req.state,
101
- ...(res ? res.newState : {}),
102
- ...(useState || {})
103
- };
99
+ let state = stateData(req, res, configuration, useState);
104
100
 
105
101
  keys.forEach((k) => {
106
102
  const val = setState[k];
@@ -161,8 +157,6 @@ function getSetState (setState, req, res = null, useState = null, configuration
161
157
  valAsArray.shift();
162
158
  } else if (val._$pop) {
163
159
  valAsArray.pop();
164
- } else if (val._$pushT) {
165
- valAsArray.push(req.text());
166
160
  } else {
167
161
  const value = val._$add || val._$rem || val._$push || val._$set;
168
162
  const [, entity, rear] = `${value}`.match(ENTITY_HBS_REGEXP) || [];
@@ -181,7 +175,7 @@ function getSetState (setState, req, res = null, useState = null, configuration
181
175
  values = [];
182
176
  } else {
183
177
  const useValue = typeof value === 'string'
184
- ? handlebars.compile(value)(stateData(req, res, configuration))
178
+ ? handlebars.compile(value)(state)
185
179
  .split(/(?<!\\),/g)
186
180
  .map((v) => v.replace(/\\,/g, ',').trim())
187
181
  : value;
@@ -207,7 +201,7 @@ function getSetState (setState, req, res = null, useState = null, configuration
207
201
  set = val;
208
202
  }
209
203
  } else if (typeof val === 'string') {
210
- set = handlebars.compile(val)(stateData(req, res, configuration));
204
+ set = handlebars.compile(val)(state);
211
205
  } else if (val === null
212
206
  || SCALAR_TYPES.includes(typeof val)) {
213
207
  set = val;
@@ -69,6 +69,16 @@ function makeExpectedKeyword (
69
69
 
70
70
  /** @typedef {import('../Responder').QuickReply} QuickReply */
71
71
 
72
+ const THIS_REGEX = /\{\{\$this\}\}/g;
73
+
74
+ function hasThis (val) {
75
+ return typeof val === 'string' && val.match(THIS_REGEX);
76
+ }
77
+
78
+ function replaceThis (val, title) {
79
+ return val.replace(THIS_REGEX, title);
80
+ }
81
+
72
82
  /**
73
83
  *
74
84
  * @ignore
@@ -208,6 +218,25 @@ function makeQuickReplies (replies, path = '', translate = (w) => w, quickReplyC
208
218
 
209
219
  const hasData = Object.keys(data).length !== 0;
210
220
  const hasSetState = setState && Object.keys(setState).length !== 0;
221
+ const translatedTitle = translate(title);
222
+
223
+ if (hasSetState) {
224
+ // replace {{this}}
225
+ Object.entries(setState)
226
+ .forEach(([key, val]) => {
227
+ if (typeof val === 'object' && val) {
228
+ Object.entries(val)
229
+ .forEach(([k, v]) => {
230
+ if (k.match(/^_\$/) && hasThis(v)) {
231
+ // eslint-disable-next-line no-param-reassign
232
+ val[k] = replaceThis(v, translatedTitle);
233
+ }
234
+ });
235
+ } else if (hasThis(val)) {
236
+ setState[key] = replaceThis(val, translatedTitle);
237
+ }
238
+ });
239
+ }
211
240
 
212
241
  if (data._senderMeta
213
242
  && data._senderMeta.flag === FLAG_DISAMBIGUATION_SELECTED) {
@@ -229,7 +258,6 @@ function makeQuickReplies (replies, path = '', translate = (w) => w, quickReplyC
229
258
  payload = JSON.stringify(payload);
230
259
  }
231
260
 
232
- const translatedTitle = translate(title);
233
261
  const translatedAiTitle = typeof aiTitle === 'string' ? translate(aiTitle) : aiTitle;
234
262
  const expect = makeExpectedKeyword(
235
263
  absoluteAction,
@@ -11,18 +11,23 @@
11
11
  * @param {Request} req
12
12
  * @param {Responder} res
13
13
  * @param {object} configuration
14
+ * @param {object} [stateOverride]
14
15
  * @returns {object}
15
16
  */
16
- module.exports = function stateData (req, res = null, configuration = null) {
17
+ module.exports = function stateData (req, res = null, configuration = null, stateOverride = {}) {
17
18
  const c = configuration || req.configuration;
18
19
 
20
+ const $this = req.text();
21
+
19
22
  return {
20
23
  c,
21
24
  configuration: c,
22
25
  ...req.state,
23
26
  ...(res ? res.newState : {}),
27
+ ...stateOverride,
28
+ $this,
24
29
  ...req.actionData(),
25
30
  ...(res ? res.data : {}),
26
- $input: req.text()
31
+ $input: $this
27
32
  };
28
33
  };