botium-core 1.13.19 → 1.14.1

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.
Files changed (41) hide show
  1. package/dist/botium-cjs.js +328 -395
  2. package/dist/botium-cjs.js.map +1 -1
  3. package/dist/botium-es.js +328 -393
  4. package/dist/botium-es.js.map +1 -1
  5. package/package.json +24 -29
  6. package/samples/extensions/asserterHooks/botium.json +1 -0
  7. package/samples/extensions/logichooks/botium.json +1 -0
  8. package/samples/extensions/logichooks/custom/MyAsserter.js +3 -3
  9. package/src/Capabilities.js +2 -1
  10. package/src/containers/PluginConnectorContainer.js +8 -0
  11. package/src/containers/plugins/SimpleRestContainer.js +17 -9
  12. package/src/containers/plugins/index.js +29 -41
  13. package/src/helpers/HookUtils.js +32 -68
  14. package/src/scripting/Convo.js +26 -9
  15. package/src/scripting/ScriptingMemory.js +0 -24
  16. package/src/scripting/logichook/LogicHookUtils.js +27 -47
  17. package/src/scripting/logichook/logichooks/ConditionalBusinessHoursLogicHook.js +1 -1
  18. package/src/scripting/logichook/logichooks/ConditionalCapabilityValueBasedLogicHook.js +1 -1
  19. package/src/scripting/logichook/logichooks/ConditionalJsonPathBasedLogicHook.js +1 -1
  20. package/src/scripting/logichook/logichooks/ConditionalTimeBasedLogicHook.js +1 -1
  21. package/test/compiler/precompilerscript.spec.js +24 -26
  22. package/test/connectors/pluginconnectorcontainer.spec.js +60 -0
  23. package/test/connectors/simplerest.spec.js +24 -27
  24. package/test/convo/fillAndApplyScriptingMemory.spec.js +1 -47
  25. package/test/hooks/customhooks.spec.js +3 -25
  26. package/test/logichooks/hookfromsrc.spec.js +13 -3
  27. package/test/plugins/plugins.spec.js +29 -2
  28. package/test/scripting/logichooks/convos/conditional_steps_multiple_condition_groups_no_assertion.convo.txt +6 -6
  29. package/test/scripting/logichooks/convos/conditional_steps_multiple_mandatory_condition_groups.convo.txt +20 -0
  30. package/test/scripting/logichooks/convos/conditional_steps_multiple_optional_condition_groups.convo.txt +20 -0
  31. package/test/scripting/logichooks/customConditionalStepLogicHook.spec.js +16 -0
  32. package/test/security/allowUnsafe.spec.js +20 -129
  33. package/LICENSES-3RDPARTY.txt +0 -6450
  34. package/test/scripting/asserters/convos/customembeddedasserterwithhugo.convo.txt +0 -7
  35. package/test/scripting/asserters/convos/customembeddedasserterwithouthugo.convo.txt +0 -7
  36. package/test/scripting/asserters/customEmbeddedAsserter.json +0 -14
  37. package/test/scripting/asserters/customEmbeddedAsserter.spec.js +0 -55
  38. package/test/scripting/logichooks/convos/custom_embedded.convo.txt +0 -8
  39. package/test/scripting/logichooks/customEmbedded.json +0 -14
  40. package/test/scripting/logichooks/customEmbedded.spec.js +0 -44
  41. package/test/security/convos/withscriptingmemoryfunction.convo.txt +0 -5
@@ -1,11 +1,9 @@
1
- const { NodeVM } = require('vm2')
1
+ const util = require('util')
2
2
  const path = require('path')
3
3
  const fs = require('fs')
4
4
  const isClass = require('is-class')
5
5
  const debug = require('debug')('botium-core-asserterUtils')
6
6
 
7
- const { BotiumError } = require('../BotiumError')
8
-
9
7
  const { DEFAULT_ASSERTERS, DEFAULT_LOGIC_HOOKS, DEFAULT_USER_INPUTS } = require('./LogicHookConsts')
10
8
 
11
9
  DEFAULT_ASSERTERS.forEach((asserter) => {
@@ -129,19 +127,7 @@ module.exports = class LogicHookUtils {
129
127
  }
130
128
  }
131
129
 
132
- const _checkUnsafe = () => {
133
- if (!this.caps[Capabilities.SECURITY_ALLOW_UNSAFE]) {
134
- throw new BotiumError(
135
- 'Security Error. Using unsafe component is not allowed',
136
- {
137
- type: 'security',
138
- subtype: 'allow unsafe',
139
- source: path.basename(__filename),
140
- cause: { src: !!src, ref, args, hookType }
141
- }
142
- )
143
- }
144
- }
130
+ const allowUnsafe = !!this.caps[Capabilities.SECURITY_ALLOW_UNSAFE]
145
131
 
146
132
  if (!src) {
147
133
  const packageName = `botium-${hookType}-${ref}`
@@ -154,10 +140,10 @@ module.exports = class LogicHookUtils {
154
140
  } else if (isClass(CheckClass.PluginClass)) {
155
141
  return new CheckClass.PluginClass({ ref, ...this.buildScriptContext }, this.caps, args)
156
142
  } else {
157
- throw new Error(`${packageName} class or function or PluginClass field expected`)
143
+ throw new Error('Either class or function or PluginClass field expected')
158
144
  }
159
145
  } catch (err) {
160
- throw new Error(`Failed to fetch hook ${ref} ${hookType} from guessed package ${packageName} - ${err.message}`)
146
+ throw new Error(`Logic Hook specification ${ref} ${hookType} (${packageName}) invalid: ${err.message}`)
161
147
  }
162
148
  }
163
149
 
@@ -166,14 +152,14 @@ module.exports = class LogicHookUtils {
166
152
  const CheckClass = src
167
153
  return new CheckClass({ ref, ...this.buildScriptContext }, this.caps, args)
168
154
  } catch (err) {
169
- throw new Error(`Failed to load package ${ref} from provided class - ${err.message}`)
155
+ throw new Error(`Logic Hook specification ${ref} from class invalid: ${err.message}`)
170
156
  }
171
157
  }
172
158
  if (_.isFunction(src)) {
173
159
  try {
174
160
  return src({ ref, ...this.buildScriptContext }, this.caps, args)
175
161
  } catch (err) {
176
- throw new Error(`Failed to load package ${ref} from provided function - ${err.message}`)
162
+ throw new Error(`Logic Hook specification ${ref} from function invalid: ${err.message}`)
177
163
  }
178
164
  }
179
165
  if (_.isObject(src) && !_.isString(src)) {
@@ -183,26 +169,15 @@ module.exports = class LogicHookUtils {
183
169
  const script = src[key]
184
170
  if (_.isFunction(script)) {
185
171
  return script(args)
186
- } else if (_.isString(script)) {
187
- try {
188
- const vm = new NodeVM({
189
- eval: false,
190
- require: false,
191
- sandbox: args
192
- })
193
- return vm.run(script)
194
- } catch (err) {
195
- throw new Error(`Script ${key} is not valid - ${err.message || err}`)
196
- }
197
172
  } else {
198
- throw new Error(`Script "${key}" is not valid - only functions and javascript code accepted`)
173
+ throw new Error(`Script ${key} is not valid - only functions accepted`)
199
174
  }
200
175
  }
201
176
  return result
202
177
  }, {})
203
178
  return hookObject
204
179
  } catch (err) {
205
- throw new Error(`Failed to load package ${ref} ${hookType} from provided src function - ${err.message}`)
180
+ throw new Error(`Logic Hook specification ${ref} ${hookType} from provided src (${util.inspect(src)}) invalid: ${err.message}`)
206
181
  }
207
182
  }
208
183
 
@@ -215,8 +190,8 @@ module.exports = class LogicHookUtils {
215
190
  }]
216
191
  if (src.indexOf('/') >= 0) {
217
192
  tryLoads.push({
218
- tryLoadPackageName: src.substr(0, src.lastIndexOf('/')),
219
- tryLoadAsserterByName: src.substr(src.lastIndexOf('/') + 1)
193
+ tryLoadPackageName: src.substring(0, src.lastIndexOf('/')),
194
+ tryLoadAsserterByName: src.substring(src.lastIndexOf('/') + 1)
220
195
  })
221
196
  }
222
197
 
@@ -243,29 +218,34 @@ module.exports = class LogicHookUtils {
243
218
  } else if (_.isFunction(CheckClass.PluginClass)) {
244
219
  return CheckClass.PluginClass({ ref, ...this.buildScriptContext }, this.caps, args)
245
220
  } else {
246
- throw new Error(`${src} class or function expected`)
221
+ throw new Error('Expected class or function')
247
222
  }
248
223
  }
249
224
 
250
225
  for (const tryLoad of tryLoads) {
251
- const tryLoadFile = path.resolve(process.cwd(), tryLoad.tryLoadPackageName)
252
- if (fs.existsSync(tryLoadFile)) {
253
- _checkUnsafe()
226
+ if (this.caps.SAFEDIR) {
227
+ const tryLoadFile = path.resolve(this.caps.SAFEDIR, tryLoad.tryLoadPackageName)
228
+ if (tryLoadFile.startsWith(path.resolve(this.caps.SAFEDIR))) {
229
+ if (fs.existsSync(tryLoadFile)) {
230
+ try {
231
+ return tryLoadFromSource(tryLoadFile, tryLoad.tryLoadAsserterByName)
232
+ } catch (err) {
233
+ loadErr.push(`Logic Hook specification ${ref} ${hookType} from "${src}" invalid: ${err.message} `)
234
+ }
235
+ }
236
+ }
237
+ }
238
+ if (allowUnsafe || tryLoad.tryLoadPackageName.startsWith('botium-')) {
254
239
  try {
255
- return tryLoadFromSource(tryLoadFile, tryLoad.tryLoadAsserterByName)
240
+ return tryLoadFromSource(tryLoad.tryLoadPackageName, tryLoad.tryLoadAsserterByName)
256
241
  } catch (err) {
257
- loadErr.push(`Failed to fetch ${ref} ${hookType} from ${src} - ${err.message} `)
242
+ loadErr.push(`Logic Hook specification ${ref} ${hookType} from "${src}" invalid: ${err.message} `)
258
243
  }
259
244
  }
260
- try {
261
- return tryLoadFromSource(tryLoad.tryLoadPackageName, tryLoad.tryLoadAsserterByName)
262
- } catch (err) {
263
- loadErr.push(`Failed to fetch ${ref} ${hookType} from ${src} - ${err.message} `)
264
- }
265
245
  }
266
246
 
267
247
  loadErr.forEach(debug)
268
248
  }
269
- throw new Error(`Failed to fetch ${ref} ${hookType}, no idea how to load ...`)
249
+ throw new Error(`Logic Hook specification ${ref} ${hookType} from "${util.inspect(src)}" invalid : no loader available`)
270
250
  }
271
251
  }
@@ -51,6 +51,6 @@ module.exports = class ConditionalBusinessHoursLogicHook {
51
51
  }
52
52
  params.now = moment()
53
53
  convoStep.conditional.skip = !this._isBetween(params)
54
- debug(`ConditionalBusinessHoursLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${convoStep.conditional}`)
54
+ debug(`ConditionalBusinessHoursLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${util.inspect(convoStep.conditional)}`)
55
55
  }
56
56
  }
@@ -32,6 +32,6 @@ module.exports = class ConditionalCapabilityValueBasedLogicHook {
32
32
  conditionGroupId
33
33
  }
34
34
  convoStep.conditional.skip = !this._isCapabilityValueEqual(params)
35
- debug(`ConditionalCapabilityValueBasedLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${convoStep.conditional}`)
35
+ debug(`ConditionalCapabilityValueBasedLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${util.inspect(convoStep.conditional)}`)
36
36
  }
37
37
  }
@@ -26,6 +26,6 @@ module.exports = class ConditionalJsonPathBasedLogicHook {
26
26
  skip = !(values && values.length > 0 && values.includes(params.value))
27
27
  }
28
28
  convoStep.conditional.skip = skip
29
- debug(`ConditionalJsonPathBasedLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${convoStep.conditional}`)
29
+ debug(`ConditionalJsonPathBasedLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${util.inspect(convoStep.conditional)}`)
30
30
  }
31
31
  }
@@ -41,6 +41,6 @@ module.exports = class ConditionalTimeBasedLogicHook {
41
41
  }
42
42
  params.now = moment()
43
43
  convoStep.conditional.skip = !this._isBetween(params)
44
- debug(`ConditionalTimeBasedLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${convoStep.conditional}`)
44
+ debug(`ConditionalTimeBasedLogicHook onBotPrepare ${convo.header.name}/${convoStep.stepTag}, args: ${util.inspect(args)}, convoStep.conditional: ${util.inspect(convoStep.conditional)}`)
45
45
  }
46
46
  }
@@ -33,13 +33,13 @@ const afterCustom = async (thisParam) => {
33
33
 
34
34
  describe('compiler.precompiler.script', function () {
35
35
  it('should execute non-standard json', async function () {
36
- await beforeCustom(this, `
36
+ await beforeCustom(this, ({ scriptData }) => {
37
37
  const utterances = {}
38
38
  for (const entry of scriptData) {
39
39
  utterances[entry.intent] = entry.sentences
40
40
  }
41
- module.exports = {utterances}
42
- `)
41
+ return { utterances }
42
+ })
43
43
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json')
44
44
  this.compiler.ExpandUtterancesToConvos()
45
45
  this.compiler.ExpandConvos()
@@ -54,15 +54,15 @@ describe('compiler.precompiler.script', function () {
54
54
  })
55
55
 
56
56
  it('should filter by extension, accepted', async function () {
57
- await beforeCustom(this, `
58
- if (filename.endsWith('.json')) {
57
+ await beforeCustom(this, ({ filename, scriptData }) => {
58
+ if (filename.endsWith('.json')) {
59
59
  const utterances = {}
60
60
  for (const entry of scriptData) {
61
61
  utterances[entry.intent] = entry.sentences
62
62
  }
63
- module.exports = {utterances}
63
+ return { utterances }
64
64
  }
65
- `)
65
+ })
66
66
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json')
67
67
  this.compiler.ExpandUtterancesToConvos()
68
68
  this.compiler.ExpandConvos()
@@ -71,15 +71,15 @@ describe('compiler.precompiler.script', function () {
71
71
  })
72
72
 
73
73
  it('should filter by extension, rejected', async function () {
74
- await beforeCustom(this, `
75
- if (filename.endsWith('.xxx')) {
74
+ await beforeCustom(this, ({ filename, scriptData }) => {
75
+ if (filename.endsWith('.xxx')) {
76
76
  const utterances = {}
77
77
  for (const entry of scriptData) {
78
78
  utterances[entry.intent] = entry.sentences
79
79
  }
80
- module.exports = {utterances}
80
+ return { utterances }
81
81
  }
82
- `)
82
+ })
83
83
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json')
84
84
  this.compiler.ExpandUtterancesToConvos()
85
85
  this.compiler.ExpandConvos()
@@ -88,15 +88,15 @@ describe('compiler.precompiler.script', function () {
88
88
  })
89
89
 
90
90
  it('should filter by content, accepted', async function () {
91
- await beforeCustom(this, `
91
+ await beforeCustom(this, ({ scriptData }) => {
92
92
  if (scriptData) {
93
93
  const utterances = {}
94
94
  for (const entry of scriptData) {
95
95
  utterances[entry.intent] = entry.sentences
96
96
  }
97
- module.exports = {utterances}
97
+ return { utterances }
98
98
  }
99
- `)
99
+ })
100
100
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json')
101
101
  this.compiler.ExpandUtterancesToConvos()
102
102
  this.compiler.ExpandConvos()
@@ -105,15 +105,15 @@ describe('compiler.precompiler.script', function () {
105
105
  })
106
106
 
107
107
  it('should filter by content, rejected', async function () {
108
- await beforeCustom(this, `
108
+ await beforeCustom(this, ({ scriptData }) => {
109
109
  if (scriptData.utterances) {
110
110
  const utterances = {}
111
111
  for (const entry of scriptData.utterances) {
112
112
  utterances[entry.intent] = entry.sentences
113
113
  }
114
- module.exports = {utterances}
114
+ return { utterances }
115
115
  }
116
- `)
116
+ })
117
117
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json')
118
118
  this.compiler.ExpandUtterancesToConvos()
119
119
  this.compiler.ExpandConvos()
@@ -122,13 +122,13 @@ describe('compiler.precompiler.script', function () {
122
122
  })
123
123
 
124
124
  it('should change extension', async function () {
125
- await beforeCustom(this, `
125
+ await beforeCustom(this, ({ filename, scriptData }) => {
126
126
  const utterances = {}
127
127
  for (const entry of scriptData) {
128
128
  utterances[entry.intent] = entry.sentences
129
129
  }
130
- module.exports = { scriptBuffer:{utterances}, filename: filename + ".json" }
131
- `)
130
+ return { scriptBuffer: { utterances }, filename: filename + '.json' }
131
+ })
132
132
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json.txt')
133
133
  this.compiler.ExpandUtterancesToConvos()
134
134
  this.compiler.ExpandConvos()
@@ -137,13 +137,13 @@ describe('compiler.precompiler.script', function () {
137
137
  })
138
138
 
139
139
  it('should not read anything without extension change', async function () {
140
- await beforeCustom(this, `
140
+ await beforeCustom(this, ({ scriptData }) => {
141
141
  const utterances = {}
142
142
  for (const entry of scriptData) {
143
143
  utterances[entry.intent] = entry.sentences
144
144
  }
145
- module.exports = { scriptBuffer:{utterances} }
146
- `)
145
+ return { scriptBuffer: { utterances } }
146
+ })
147
147
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script.json.txt')
148
148
  this.compiler.ExpandUtterancesToConvos()
149
149
  this.compiler.ExpandConvos()
@@ -152,9 +152,7 @@ describe('compiler.precompiler.script', function () {
152
152
  })
153
153
 
154
154
  it('should be able to precompile text to text', async function () {
155
- await beforeCustom(this, `
156
- module.exports = scriptData.replace("Hi!", "Hi Bot!")
157
- `)
155
+ await beforeCustom(this, ({ scriptData }) => scriptData.replace('Hi!', 'Hi Bot!'))
158
156
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'convos_precompiler_script_text_to_text.convo.txt')
159
157
  this.compiler.ExpandUtterancesToConvos()
160
158
  this.compiler.ExpandConvos()
@@ -0,0 +1,60 @@
1
+ const assert = require('chai').assert
2
+ const BotDriver = require('../../').BotDriver
3
+ const Capabilities = require('../../').Capabilities
4
+
5
+ const echoConnectorWithMetadata = ({ queueBotSays }) => {
6
+ return {
7
+ UserSays (msg) {
8
+ const botMsg = {
9
+ sender: 'bot',
10
+ sourceData: msg.sourceData,
11
+ messageText: `Response of ${msg.messageText}`
12
+ }
13
+ queueBotSays(botMsg)
14
+ },
15
+ GetMetaData () {
16
+ return Promise.resolve('Sample response from GetMetaData')
17
+ }
18
+ }
19
+ }
20
+ const echoConnectorWithoutMetadata = ({ queueBotSays }) => {
21
+ return {
22
+ UserSays (msg) {
23
+ const botMsg = {
24
+ sender: 'bot',
25
+ sourceData: msg.sourceData,
26
+ messageText: `Response of ${msg.messageText}`
27
+ }
28
+ queueBotSays(botMsg)
29
+ }
30
+ }
31
+ }
32
+
33
+ describe('compiler.precompiler.json', function () {
34
+ beforeEach(async function () {
35
+ this.init = async (withGetMetaData) => {
36
+ const myCaps = {
37
+ [Capabilities.PROJECTNAME]: 'connector.basecontainer',
38
+ [Capabilities.CONTAINERMODE]: withGetMetaData ? echoConnectorWithMetadata : echoConnectorWithoutMetadata
39
+ }
40
+ const driver = new BotDriver(myCaps)
41
+ this.compiler = driver.BuildCompiler()
42
+ this.container = await driver.Build()
43
+ }
44
+ })
45
+ afterEach(async function () {
46
+ this.container && await this.container.Clean()
47
+ })
48
+
49
+ it('should get metadata if its available', async function () {
50
+ await this.init(true)
51
+ const result = await this.container.GetMetaData()
52
+ assert.equal(result, 'Sample response from GetMetaData')
53
+ })
54
+
55
+ it('should get undefined if metadata is not available', async function () {
56
+ await this.init(false)
57
+ const result = await this.container.GetMetaData()
58
+ assert.equal(result, undefined)
59
+ })
60
+ })
@@ -50,7 +50,6 @@ const myCapsScriptingMemory = {
50
50
  FUNCTION_WITHOUT_PARAM: '{{fnc.year}}',
51
51
  FUNCTION_WITH_PARAM: '{{#fnc.random}}5{{/fnc.random}}',
52
52
  FUNCTION_WITH_PARAM_FROM_SCRIPTING_MEMORY: '{{#fnc.random}}{{msg.scriptingMemory.functionArgument}}{{/fnc.random}}',
53
- USING_CODE: '{{#fnc.func}}1 + 2{{/fnc.func}}',
54
53
  SAMPLE_ENV: '{{#fnc.env}}SAMPLE_ENV{{/fnc.env}}',
55
54
  VARIABLE: '{{msg.scriptingMemory.variable}}',
56
55
  PROJECTNAME: '{{fnc.projectname}}',
@@ -91,11 +90,11 @@ const myCapsHookBase = {
91
90
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$']
92
91
  }
93
92
  const myCapsRequestHookFromString = Object.assign({
94
- [Capabilities.SIMPLEREST_REQUEST_HOOK]: `
93
+ [Capabilities.SIMPLEREST_REQUEST_HOOK]: ({ requestOptions, context }) => {
95
94
  let counter = 1
96
- requestOptions.body = {bodyFieldRequestHook: counter++}
95
+ requestOptions.body = { bodyFieldRequestHook: counter++ }
97
96
  context.contextFieldRequestHook = counter
98
- `
97
+ }
99
98
  }, myCapsHookBase)
100
99
  const myCapsRequestHookFromStringInvalid = Object.assign({
101
100
  [Capabilities.SIMPLEREST_REQUEST_HOOK]: '!'
@@ -108,12 +107,13 @@ const myCapsRequestHookFromFunction = Object.assign({
108
107
  }
109
108
  }, myCapsHookBase)
110
109
  const myCapsRequestHookFromModule = Object.assign({
111
- [Capabilities.SIMPLEREST_REQUEST_HOOK]: 'test/connectors/logicHook.js'
110
+ [Capabilities.SAFEDIR]: './test/connectors/',
111
+ [Capabilities.SIMPLEREST_REQUEST_HOOK]: 'logicHook.js'
112
112
  }, myCapsHookBase)
113
113
  const myCapsResponseHook = Object.assign({
114
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
115
- botMsg.messageText = 'message text from hook'
116
- `
114
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: ({ botMsg }) => {
115
+ botMsg.messageText = 'message text from hook'
116
+ }
117
117
  }, myCapsHookBase)
118
118
 
119
119
  const msg = {
@@ -460,9 +460,6 @@ describe('connectors.simplerest', function () {
460
460
  assert.exists(request.body.SAMPLE_ENV)
461
461
  assert.equal(request.body.SAMPLE_ENV, 'SAMPLE_ENV')
462
462
 
463
- assert.exists(request.body.USING_CODE)
464
- assert.equal(request.body.USING_CODE, 3)
465
-
466
463
  assert.exists(request.body.VARIABLE)
467
464
  assert.equal(request.body.VARIABLE, 'varvalue')
468
465
 
@@ -519,13 +516,13 @@ describe('connectors.simplerest', function () {
519
516
  it('should use request hook, from module', async function () {
520
517
  await _assertHook(Object.assign({}, myCapsRequestHookFromModule))
521
518
  })
522
- it('should use request hook, from invalid string', async function () {
519
+ it('should reject request hook, from invalid string', async function () {
523
520
  const driver = new BotDriver(myCapsRequestHookFromStringInvalid)
524
521
  try {
525
522
  await driver.Build()
526
523
  assert.fail('it should have failed')
527
524
  } catch (err) {
528
- assert.isTrue(err.message.includes('Cant load hook, syntax is not valid'))
525
+ assert.isTrue(err.message.includes('Hook specification "\'!\'" invalid'))
529
526
  }
530
527
  })
531
528
  it('should add query params from UPDATE_CUSTOM (without "?")', async function () {
@@ -741,9 +738,9 @@ describe('connectors.simplerest', function () {
741
738
  const myCaps = Object.assign({}, myCapsGet, {
742
739
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
743
740
  [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
744
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
745
- botMsg.nlp = { intent: { name: 'hugo' } }
746
- `,
741
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: ({ botMsg }) => {
742
+ botMsg.nlp = { intent: { name: 'hugo' } }
743
+ },
747
744
  [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
748
745
  })
749
746
  const driver = new BotDriver(myCaps)
@@ -764,9 +761,9 @@ describe('connectors.simplerest', function () {
764
761
  const myCaps = Object.assign({}, myCapsGet, {
765
762
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
766
763
  [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
767
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
768
- botMsg.someextradata = 'message text from hook'
769
- `,
764
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: ({ botMsg }) => {
765
+ botMsg.someextradata = 'message text from hook'
766
+ },
770
767
  [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
771
768
  })
772
769
  const driver = new BotDriver(myCaps)
@@ -787,9 +784,9 @@ describe('connectors.simplerest', function () {
787
784
  const myCaps = Object.assign({}, myCapsGet, {
788
785
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
789
786
  [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
790
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
791
- botMsg.messageText = 'message text from hook'
792
- `,
787
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: ({ botMsg }) => {
788
+ botMsg.messageText = 'message text from hook'
789
+ },
793
790
  [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
794
791
  })
795
792
  const driver = new BotDriver(myCaps)
@@ -1061,7 +1058,7 @@ describe('connectors.simplerest', function () {
1061
1058
  await this.init({
1062
1059
  [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingencoded',
1063
1060
  [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true,
1064
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'body.text = JSON.parse(body.text).prop'
1061
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: ({ body }) => { body.text = JSON.parse(body.text).prop }
1065
1062
  })
1066
1063
 
1067
1064
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
@@ -1074,7 +1071,7 @@ describe('connectors.simplerest', function () {
1074
1071
  await this.init({
1075
1072
  [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingstring',
1076
1073
  [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true,
1077
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'changeBody({ text: body })'
1074
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: ({ changeBody, body }) => changeBody({ text: body })
1078
1075
  })
1079
1076
 
1080
1077
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
@@ -1114,7 +1111,7 @@ describe('connectors.simplerest', function () {
1114
1111
  await this.init({
1115
1112
  [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/startencoded',
1116
1113
  [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true,
1117
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'body.text = JSON.parse(body.text).prop'
1114
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: ({ body }) => { body.text = JSON.parse(body.text).prop }
1118
1115
  })
1119
1116
 
1120
1117
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
@@ -1127,7 +1124,7 @@ describe('connectors.simplerest', function () {
1127
1124
  await this.init({
1128
1125
  [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/startstring',
1129
1126
  [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true,
1130
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'changeBody({ text: body })'
1127
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: ({ changeBody, body }) => changeBody({ text: body })
1131
1128
  })
1132
1129
 
1133
1130
  this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
@@ -1283,7 +1280,7 @@ describe('connectors.simplerest', function () {
1283
1280
  [Capabilities.SIMPLEREST_URL]: () => 'https://mock.com/endpoint',
1284
1281
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => ['$.text'],
1285
1282
  [Capabilities.SIMPLEREST_POLL_URL]: () => 'https://mock.com/poll',
1286
- [Capabilities.SIMPLEREST_POLL_REQUEST_HOOK]: 'requestOptions.uri = "https://mock.com/_from_hook"'
1283
+ [Capabilities.SIMPLEREST_POLL_REQUEST_HOOK]: ({ requestOptions }) => { requestOptions.uri = 'https://mock.com/_from_hook' }
1287
1284
  }
1288
1285
  const scope = nock('https://mock.com')
1289
1286
  .get('/endpoint')
@@ -168,7 +168,7 @@ describe('convo.fillAndApplyScriptingMemory', function () {
168
168
  [Capabilities.PROJECTNAME]: 'convo.scriptingmemory',
169
169
  [Capabilities.CONTAINERMODE]: echoConnector,
170
170
  [Capabilities.SCRIPTING_ENABLE_MEMORY]: true,
171
- [Capabilities.CUSTOMHOOK_ONBUILD]: 'module.exports = ({ container }) => { console.log("customhooks called"); container.caps.MYTOKEN = "test1234" }'
171
+ [Capabilities.CUSTOMHOOK_ONBUILD]: ({ container }) => { console.log('customhooks called'); container.caps.MYTOKEN = 'test1234' }
172
172
  }
173
173
  const driver = new BotDriver(myCaps)
174
174
  const compiler = driver.BuildCompiler()
@@ -847,52 +847,6 @@ describe('convo.fillAndApplyScriptingMemory', function () {
847
847
  assert(result.length === 36, '$uniqid invalid')
848
848
  })
849
849
 
850
- it('func', async function () {
851
- const result = ScriptingMemory.apply(
852
- { caps: CAPS_ENABLE_SCRIPTING_MEMORY },
853
- {},
854
- '$func(3*5)'
855
- )
856
-
857
- assert(result === '15', 'func invalid')
858
- })
859
- it('func with caps', async function () {
860
- const result = ScriptingMemory.apply(
861
- { caps: Object.assign({}, CAPS_ENABLE_SCRIPTING_MEMORY, { mycap: 'botium' }) },
862
- {},
863
- '$func(caps.mycap)'
864
- )
865
- assert.equal(result, 'botium')
866
- })
867
- it('func invalid code', async function () {
868
- try {
869
- ScriptingMemory.apply(
870
- { caps: CAPS_ENABLE_SCRIPTING_MEMORY },
871
- {},
872
- '$func(hugo123)'
873
- )
874
- assert.fail('should have failed')
875
- } catch (err) {
876
- assert.isTrue(err.message.indexOf('func function execution failed') >= 0)
877
- }
878
- })
879
- it('func environment variable', async function () {
880
- process.env.MY_VAR_VALUE = 'botium'
881
- const result = ScriptingMemory.apply(
882
- { caps: CAPS_ENABLE_SCRIPTING_MEMORY },
883
- {},
884
- '$func(process.env.MY_VAR_VALUE)'
885
- )
886
- assert.equal(result, 'botium')
887
- })
888
- it('func with moment', async function () {
889
- const result = ScriptingMemory.apply(
890
- { caps: CAPS_ENABLE_SCRIPTING_MEMORY },
891
- {},
892
- '$func(moment(\\).subtract(1, "month"\\).startOf("month"\\).format("DD.MM.YYYY"\\))'
893
- )
894
- assert.equal(result, moment().subtract(1, 'month').startOf('month').format('DD.MM.YYYY'))
895
- })
896
850
  it('environment variable', async function () {
897
851
  process.env.MY_VAR_VALUE = 'botium'
898
852
  const result = ScriptingMemory.apply(
@@ -65,28 +65,6 @@ describe('customhooks.hookfromsrc', function () {
65
65
  assert.isTrue(onStopCalled)
66
66
  assert.isTrue(onCleanCalled)
67
67
  })
68
- it('should call hooks from string function', async function () {
69
- const { container } = await buildDriver({
70
- [Capabilities.CUSTOMHOOK_ONBUILD]: 'module.exports = ({ container }) => { container.onBuildCalled = true }',
71
- [Capabilities.CUSTOMHOOK_ONSTART]: 'module.exports = ({ container }) => { container.onStartCalled = true }',
72
- [Capabilities.CUSTOMHOOK_ONUSERSAYS]: 'module.exports = ({ container }) => { container.onUserSaysCalled = true }',
73
- [Capabilities.CUSTOMHOOK_ONBOTRESPONSE]: 'module.exports = ({ container }) => { container.onBotResponseCalled = true }',
74
- [Capabilities.CUSTOMHOOK_ONSTOP]: 'module.exports = ({ container }) => { container.onStopCalled = true }',
75
- [Capabilities.CUSTOMHOOK_ONCLEAN]: 'module.exports = ({ container }) => { container.onCleanCalled = true }'
76
- })
77
- await container.Start()
78
- await container.UserSaysText('hallo')
79
- await container.WaitBotSays()
80
- await container.Stop()
81
- await container.Clean()
82
-
83
- assert.isTrue(container.onBuildCalled)
84
- assert.isTrue(container.onStartCalled)
85
- assert.isTrue(container.onUserSaysCalled)
86
- assert.isTrue(container.onBotResponseCalled)
87
- assert.isTrue(container.onStopCalled)
88
- assert.isTrue(container.onCleanCalled)
89
- })
90
68
  it('should change meMsg from hook', async function () {
91
69
  const { container } = await buildDriver({
92
70
  [Capabilities.CUSTOMHOOK_ONUSERSAYS]: ({ meMsg }) => {
@@ -115,7 +93,7 @@ describe('customhooks.hookfromsrc', function () {
115
93
 
116
94
  assert.equal(botMsg.fromHook, 1)
117
95
  })
118
- it('should call http api from string function', async function () {
96
+ it('should call http api from function', async function () {
119
97
  const scope = nock('https://gettoken.com')
120
98
  .get('/get')
121
99
  .reply(200, {
@@ -124,7 +102,7 @@ describe('customhooks.hookfromsrc', function () {
124
102
  .persist()
125
103
 
126
104
  const { container } = await buildDriver({
127
- [Capabilities.CUSTOMHOOK_ONSTART]: `module.exports = async ({ container, request }) => {
105
+ [Capabilities.CUSTOMHOOK_ONSTART]: async ({ container, request }) => {
128
106
  return new Promise((resolve, reject) => {
129
107
  request({ method: 'get', uri: 'https://gettoken.com/get', json: true }, (err, response, body) => {
130
108
  if (err) return reject(err)
@@ -132,7 +110,7 @@ describe('customhooks.hookfromsrc', function () {
132
110
  resolve()
133
111
  })
134
112
  })
135
- }`
113
+ }
136
114
  })
137
115
  await container.Start()
138
116
  assert.equal(container.caps.MYTOKEN, 'thisisausertoken')