@loadmill/executer 0.1.50 → 0.1.53

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 (46) hide show
  1. package/dist/extraction-combiner.js +6 -6
  2. package/dist/extraction-combiner.js.map +1 -1
  3. package/dist/mill-info.d.ts +4 -0
  4. package/dist/mill-version.js +1 -1
  5. package/dist/post-script/console-log.d.ts +7 -0
  6. package/dist/post-script/console-log.js +31 -0
  7. package/dist/post-script/console-log.js.map +1 -0
  8. package/dist/post-script/post-script-executor.js +7 -4
  9. package/dist/post-script/post-script-executor.js.map +1 -1
  10. package/dist/request-sequence-result.d.ts +1 -0
  11. package/dist/sequence.d.ts +1 -1
  12. package/dist/sequence.js +18 -8
  13. package/dist/sequence.js.map +1 -1
  14. package/dist/single-runner.d.ts +1 -0
  15. package/dist/single-runner.js +1 -1
  16. package/dist/single-runner.js.map +1 -1
  17. package/package.json +3 -3
  18. package/src/asserter.ts +0 -137
  19. package/src/errors.ts +0 -10
  20. package/src/extraction-combiner.ts +0 -110
  21. package/src/failures.ts +0 -79
  22. package/src/message-creators.ts +0 -44
  23. package/src/mill-info.ts +0 -76
  24. package/src/mill-version.ts +0 -7
  25. package/src/post-script/ast-walker/index.ts +0 -160
  26. package/src/post-script/ast-walker/type-guard.ts +0 -73
  27. package/src/post-script/ast-walker/types.ts +0 -35
  28. package/src/post-script/parser/acorn-js-parser.ts +0 -8
  29. package/src/post-script/parser/js-parser.ts +0 -22
  30. package/src/post-script/parser/parser.ts +0 -5
  31. package/src/post-script/post-script-executor.ts +0 -89
  32. package/src/post-script/virtual-machine/virtual-machine.ts +0 -15
  33. package/src/post-script/virtual-machine/vm2-virtual-machine.ts +0 -45
  34. package/src/report-types.ts +0 -127
  35. package/src/request-sequence-result.ts +0 -63
  36. package/src/request-stats.ts +0 -20
  37. package/src/res-keeper.ts +0 -53
  38. package/src/sampler.ts +0 -133
  39. package/src/sequence.ts +0 -1107
  40. package/src/single-runner.ts +0 -66
  41. package/src/test-run-event-emitter.ts +0 -25
  42. package/src/utils.ts +0 -8
  43. package/src/work.ts +0 -17
  44. package/src/ws.ts +0 -286
  45. package/test/post-script-executor.spec.ts +0 -677
  46. package/tsconfig.json +0 -9
@@ -1,677 +0,0 @@
1
- import { parameterFunctionOperations } from '@loadmill/core/dist/parameters';
2
- import { strict as assert } from 'assert';
3
- import { suite, describe, it } from 'mocha';
4
-
5
- import { PostScriptRunner } from '../src/post-script/post-script-executor';
6
-
7
- const runPostScript = new PostScriptRunner().run;
8
-
9
- suite('Post Script Executor', () => {
10
- describe('Empty user code', () => {
11
- it('should return empty object', () => {
12
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { }, '');
13
- assert.deepStrictEqual(result, { });
14
- });
15
-
16
- it('should return empty object when static not empty', () => {
17
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { }, '');
18
- assert.deepStrictEqual(result, { });
19
- });
20
-
21
- it('should return identical dynamic object', () => {
22
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { key: 'value' }, '');
23
- assert.deepStrictEqual(result, { key: 'value' });
24
- });
25
-
26
- it('should return identical dynamic function', () => {
27
- const func = (a, b) => a + b;
28
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { func }, '');
29
- assert.deepStrictEqual(result, { func });
30
- assert.equal(result.func(1, 2), 3);
31
- });
32
-
33
- it('should return identical dynamic array', () => {
34
- const arr = [1, '2', null, {}, [], undefined];
35
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { arr }, '');
36
- assert.deepStrictEqual(result, { arr });
37
- assert.equal(result.arr, arr);
38
- assert.deepStrictEqual(result.arr, [1, '2', null, {}, [], undefined]);
39
- });
40
- });
41
-
42
- describe('Override dynamic data', () => {
43
- it('should override dynamic var with user code', () => {
44
- const dynamicVar = 'arnon';
45
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { dynamicVar }, `
46
- dynamicVar = 'yochai';
47
- `);
48
- assert.notDeepStrictEqual(result, { dynamicVar });
49
- assert.deepStrictEqual(result, { dynamicVar: 'yochai' });
50
- });
51
-
52
- it('should override dynamic function with user code', () => {
53
- const dynamicFunc = (a, b) => a + b;
54
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, { dynamicFunc }, `
55
- dynamicFunc = (a, b) => a - b;
56
- `);
57
- assert.notDeepStrictEqual(result, { dynamicFunc });
58
- assert.deepStrictEqual(result.dynamicFunc(7,3), 4);
59
- });
60
- });
61
-
62
- describe('simple variable declaration', () => {
63
- it('should detect var declaration', () => {
64
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'var myVar = 123;');
65
- assert.equal(result.myVar, 123);
66
- });
67
-
68
- it('should detect let declaration', () => {
69
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let myVar = 123;');
70
- assert.equal(result.myVar, 123);
71
- });
72
-
73
- it('should detect const declaration', () => {
74
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'const myVar = 123;');
75
- assert.equal(result.myVar, 123);
76
- });
77
- });
78
-
79
- describe('multiple declarations in one line', () => {
80
- it('should detect 2 declarations in one line', () => {
81
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {},
82
- 'let username = \'ido\', password = \'solarwinds123\';'
83
- );
84
-
85
- assert.equal(result.username, 'ido');
86
- assert.equal(result.password, 'solarwinds123');
87
- });
88
- });
89
-
90
- describe('implicit declaration', () => {
91
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {},
92
- 'implicitDeclaration = \'with not let, const or var\';'
93
- );
94
-
95
- it('should detect declaration with not let, const or var', () => {
96
- assert.equal(result.implicitDeclaration, 'with not let, const or var');
97
- });
98
- });
99
-
100
- describe('implicit & multiple declarations in one line', () => {
101
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {},
102
- 'p1 = 1, p2 = 2;'
103
- );
104
-
105
- it('should detect 2 implicit & multiple declarations', () => {
106
- assert.equal(result.p1, 1);
107
- assert.equal(result.p2, 2);
108
- });
109
- });
110
-
111
- describe('simple function declarations', () => {
112
- it('should detect function declaration', () => {
113
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {},
114
- `function add(x,y) {
115
- return x + y;
116
- }`
117
- );
118
- assert.equal(result.add(3,6), 9);
119
- });
120
-
121
- it('should detect pointer to function', () => {
122
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {},
123
- `const add = function(x,y) {
124
- return x + y;
125
- }`
126
- );
127
- assert.equal(result.add(3, 6), 9);
128
- });
129
- });
130
-
131
- describe('arrow function declarations', () => {
132
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {},
133
- `const add = (x,y) => {
134
- return x + y;
135
- }`
136
- );
137
-
138
- it('should detect function declaration', () => {
139
- assert.equal(result.add(3,6), 9);
140
- });
141
- });
142
-
143
- describe('Error Handling', () => {
144
- it('should throw Uncaught ReferenceError', () => {
145
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'func()'), {
146
- name: 'ReferenceError',
147
- message: 'func is not defined',
148
- });
149
- });
150
-
151
- it('should throw ReferenceError: process is not defined', () => {
152
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'process.exit()'), {
153
- name: 'ReferenceError',
154
- message: 'process is not defined',
155
- });
156
- });
157
-
158
- it('should throw EvalError', () => {
159
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'eval("process.exit()")'), {
160
- name: 'EvalError',
161
- message: 'Code generation from strings disallowed for this context',
162
- });
163
- });
164
-
165
- it('should throw Uncaught RangeError', () => {
166
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {},
167
- `
168
- function f() {
169
- f();
170
- }
171
- f();
172
- `
173
- ), {
174
- name: 'RangeError',
175
- message: 'Maximum call stack size exceeded',
176
- });
177
- });
178
- });
179
-
180
- describe('Using the assert module', () => {
181
- it('should have the assert module', () => {
182
- assert.ok(runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'a = assert'));
183
- });
184
-
185
- it('should run assert.equal module', () => {
186
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
187
- const a = 1;
188
- const ass = assert.equal(a, 2);
189
- `
190
- ), {
191
- name: 'AssertionError',
192
- message: /Expected values to be strictly equal/,
193
- });
194
- });
195
- });
196
-
197
-
198
- describe('static data read & write', () => {
199
- const $ = {
200
- a: 1,
201
- b: 2
202
- };
203
-
204
- it('should read values from static data', () => {
205
- const res = runPostScript({ staticContext: { $, __: {} } }, {},
206
- `
207
- resBodyCopy = $;
208
- a = $.a;
209
- b = $.b;
210
- `);
211
- assert.deepStrictEqual(res.a, 1);
212
- assert.deepStrictEqual(res.b, 2);
213
- assert.deepStrictEqual(res.resBodyCopy, { a: 1, b: 2 });
214
- });
215
- });
216
-
217
- describe('endless loops are run time bounded', () => {
218
- it('should halt the endless FOR loop', () => {
219
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'for(;;);'), {
220
- name: 'Error',
221
- message: /Script execution timed out/,
222
- });
223
- });
224
-
225
- it('should halt the endless WHILE loop', () => {
226
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'while(true);'), {
227
- name: 'Error',
228
- message: /Script execution timed out/,
229
- });
230
- });
231
-
232
- it('should halt the endless DO WHILE loop', () => {
233
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'do {} while(true);'), {
234
- name: 'Error',
235
- message: /Script execution timed out/,
236
- });
237
- });
238
-
239
- it('should halt the endless FOR...OF loop', () => {
240
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
241
- const arr = [1];
242
- for (const x of arr) {
243
- arr.push(x);
244
- }
245
- `), {
246
- name: 'Error',
247
- message: /Script execution timed out/,
248
- });
249
- });
250
- });
251
-
252
- describe('code longer than 1 second throws error', () => {
253
- it('should throw exceeded allowed execution time error', () => {
254
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {},
255
- `
256
- const wait = (ms) => {
257
- const start = Date.now();
258
- while (Date.now() - start < ms);
259
- }
260
- wait(2000)
261
- `
262
- ), {
263
- name: 'Error',
264
- message: 'Script execution timed out after 1000ms',
265
- });
266
- });
267
-
268
- });
269
-
270
- // possible solution here is using isolated vm (seperated process --max-old-space-size)
271
- describe('Memory abuse', () => {
272
- // it('should not allow memory bomb', () => {
273
- // const res = runPostScript({ staticContext: { $: {}, __: {} } }, {},
274
- // `
275
- // (new Array(Math.pow(2, 32) - 1)).fill(1);
276
- // `);
277
- // });
278
- it('should not allow access to Buffer', () => {
279
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {},
280
- `
281
- JSON.stringify(Buffer.alloc(1000000000))
282
- `
283
- ), {
284
- name: 'TypeError',
285
- message: /is not a function/,
286
- });
287
- });
288
- });
289
-
290
- describe('Cyclic object', () => {
291
- it('should throw if circular object', () => {
292
- assert.throws(() => runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
293
- a = {}
294
- b = {}
295
- a.b = b
296
- b.a = a
297
- `), {
298
- name: 'TypeError',
299
- message: 'Cyclic object value. Circular reference in property a',
300
- });
301
- });
302
-
303
- it('should not throw if nested object exists more than once but not cyclic ', () => {
304
- const obj = { };
305
- const containsObj = { obj: obj };
306
- const $ = { containsObj, obj };
307
- assert.doesNotThrow(() => JSON.stringify($));
308
- assert.doesNotThrow(() => runPostScript({ staticContext: { $: $, __: {} } }, {}, `
309
- res = $;
310
- `));
311
- });
312
- });
313
-
314
- describe('global object', () => {
315
- it('should return an empty object for a variable referencing the global object', () => {
316
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'c=global');
317
- assert.deepEqual(
318
- result.c,
319
- {}
320
- );
321
- });
322
- });
323
-
324
- describe('destructuring', () => {
325
- describe('object', () => {
326
- it('should detect declaration of root property', () => {
327
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'const { x } = { x: 1 };');
328
- assert.equal(result.x, 1);
329
- });
330
-
331
- it('should detect declaration of 2-level nested property', () => {
332
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
333
- let { x: { y } } = { x: { y: 1 } };
334
- `);
335
- assert.equal(result.y, 1);
336
- });
337
-
338
- it('should detect declaration of 3-level nested property', () => {
339
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
340
- let { x: { y: { z } } } = { x: { y: { z: 1 } } };
341
- `);
342
- assert.equal(result.z, 1);
343
- });
344
-
345
- it('should detect declaration of 4-level nested property', () => {
346
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
347
- let { x: { y: { z: { w } } } } = { x: { y: { z: { w: 1 } } } };
348
- `);
349
- assert.equal(result.w, 1);
350
- });
351
-
352
- it('should detect declaration of two (2) root properties', () => {
353
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
354
- let { x, y } = { x: 1, y: 2 };
355
- `);
356
- assert.equal(result.x, 1);
357
- assert.equal(result.y, 2);
358
- });
359
-
360
- it('should detect declaration of a root property and a nested property', () => {
361
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
362
- let { x, y: { z } } = { x: 1, y: { z: 2 } };
363
- `);
364
- assert.equal(result.x, 1);
365
- assert.equal(result.z, 2);
366
- });
367
-
368
- it('should detect rest element object declaration', () => {
369
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
370
- const {x, ...theRest } = { x: 1, y: 2, z: 3 };
371
- `);
372
- assert.equal(result.x, 1);
373
- assert.equal(result.theRest.y, 2);
374
- assert.equal(result.theRest.z, 3);
375
- });
376
-
377
- it('should detect implicit declaration of root property ', () => {
378
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '({ x } = { x: 1 });');
379
- assert.equal(result.x, 1);
380
- });
381
-
382
- it('should detect implicit declaration of 2-level nested property ', () => {
383
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '({ x: { y } } = { x: { y: 1 } });');
384
- assert.equal(result.y, 1);
385
- });
386
-
387
- it('should detect implicit declaration of 3-level nested property ', () => {
388
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '({ x: { y: { z } } } = { x: { y: { z: 1 } } });');
389
- assert.equal(result.z, 1);
390
- });
391
-
392
- it('should detect implicit declaration of two (2) root properties', () => {
393
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
394
- ({ x, y } = { x: 1, y: 2 });
395
- `);
396
- assert.equal(result.x, 1);
397
- assert.equal(result.y, 2);
398
- });
399
-
400
- it('should detect implicit declaration of a root property and a nested property', () => {
401
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
402
- ({ x, y: { z } } = { x: 1, y: { z: 2 } });
403
- `);
404
- assert.equal(result.x, 1);
405
- assert.equal(result.z, 2);
406
- });
407
- });
408
-
409
- describe('array', () => {
410
- it('should detect destructured array value', () => {
411
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
412
- const [ x ] = [ 1 ];
413
- `);
414
- assert.equal(result.x, 1);
415
- });
416
-
417
- it('should detect 2 destructured array values', () => {
418
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
419
- const [ x, y ] = [ 1, 2 ];
420
- `);
421
- assert.equal(result.x, 1);
422
- assert.equal(result.y, 2);
423
- });
424
-
425
- it('should detect spread array values', () => {
426
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let [...elements] = [1, 2];');
427
- assert.equal(result.elements.length, 2);
428
- });
429
-
430
- it('should detect "regular" & spread array values', () => {
431
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let [a, b, ...rest] = [10, 20, 30, 40, 50];');
432
- assert.equal(result.a,10);
433
- assert.equal(result.b,20);
434
- assert.equal(result.rest.length,3);
435
- });
436
-
437
- it('should detect implicit declaration of array destructuring ', () => {
438
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '([ x ] = [ 1 ]);');
439
- assert.equal(result.x, 1);
440
- });
441
-
442
- it('should detect 2 implicit declarations of array destructuring ', () => {
443
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '([ x, y ] = [ 1, 2 ]);');
444
- assert.equal(result.x, 1);
445
- assert.equal(result.y, 2);
446
- });
447
-
448
- it('should detect implicit spread array values', () => {
449
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '[...elements] = [1, 2];');
450
- assert.equal(result.elements.length, 2);
451
- });
452
-
453
- it('should detect implicit "regular" & spread array values', () => {
454
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let [a, b, ...rest] = [10, 20, 30, 40, 50];');
455
- assert.equal(result.a,10);
456
- assert.equal(result.b,20);
457
- assert.equal(result.rest.length,3);
458
- });
459
- });
460
-
461
- describe('array of objects', () => {
462
- it('should detect destructured object from array', () => {
463
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
464
- const [ { x } ] = [ { x: 1 } ];
465
- `);
466
- assert.equal(result.x, 1);
467
- });
468
- it('should detect nested destructured object from array', () => {
469
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
470
- const [ { x: { y } } ] = [ { x: { y: 1 } } ];
471
- `);
472
- assert.equal(result.y, 1);
473
- });
474
- it('should detect destructuring of 3-level nested property from array', () => {
475
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
476
- let [{ x: { y: { z } } }] = [{ x: { y: { z: 1 } } }];
477
- `);
478
- assert.equal(result.z, 1);
479
- });
480
- it('should detect destructuring of two (2) root properties from array', () => {
481
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
482
- let [{ x, y }] = [{ x: 1, y: 2 }];
483
- `);
484
- assert.equal(result.x, 1);
485
- assert.equal(result.y, 2);
486
- });
487
- it('should detect destructuring of a root property and a nested property from array', () => {
488
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
489
- let [{ x, y: { z } }] = [{ x: 1, y: { z: 2 } }];
490
- `);
491
- assert.equal(result.x, 1);
492
- assert.equal(result.z, 2);
493
- });
494
- it('should detect destructuring of mixed type elements of array', () => {
495
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
496
- let [{ x }, y, z, w, q, t] = [{ x: 1 }, 2, 'a', false, undefined, null];
497
- `);
498
- assert.equal(result.x, 1);
499
- assert.equal(result.y, 2);
500
- assert.equal(result.z, 'a');
501
- assert.equal(result.w, false);
502
- assert.equal(result.q, undefined);
503
- assert.equal(result.t, null);
504
- });
505
- });
506
-
507
- describe('object with array value', () => {
508
- it('should detect an array value destructured from object', () => {
509
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
510
- const { x } = { x: [1,2] };
511
- `);
512
- assert.ok(Array.isArray(result.x));
513
- assert.equal(result.x[0], 1);
514
- assert.equal(result.x[1], 2);
515
- });
516
- it('should detect an object destructured from array destructured from object', () => {
517
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
518
- const { x: [ { y } ] } = { x: [ { y: 1 } ] };
519
- `);
520
- assert.equal(result.y, 1);
521
- });
522
- it('should detect a deep nested destructuring mix of obj and arr', () => {
523
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
524
- const { a: [ { b: [ { c } ], } ], } = { a: [ { b: [ { c: ['unacceptable'] } ] } ] }
525
- `);
526
- assert.ok(Array.isArray(result.c));
527
- assert.equal(result.c[0], 'unacceptable');
528
- assert.equal(result.c.length, 1);
529
- });
530
- });
531
-
532
- describe('implicit object with array value', () => {
533
- it('should detect an implicit array value destructured from object', () => {
534
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
535
- ({ x } = { x: [1,2] });
536
- `);
537
- assert.ok(Array.isArray(result.x));
538
- assert.equal(result.x[0], 1);
539
- assert.equal(result.x[1], 2);
540
- });
541
- it('should detect an object destructured from array destructured from object', () => {
542
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
543
- ({ x: [ { y } ] } = { x: [ { y: 1 } ] });
544
- `);
545
- assert.equal(result.y, 1);
546
- });
547
- it('should detect a deep nested destructuring mix of obj and arr', () => {
548
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
549
- ({ a: [ { b: [ { c } ], } ], } = { a: [ { b: [ { c: ['unacceptable'] } ] } ] });
550
- `);
551
- assert.ok(Array.isArray(result.c));
552
- assert.equal(result.c[0], 'unacceptable');
553
- assert.equal(result.c.length, 1);
554
- });
555
- });
556
-
557
- describe('array of arrays', () => {
558
- it('should find destructured array element of array', () => {
559
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
560
- const [ [ x ] ] = [ [ 1 ] ];
561
- `);
562
- assert.equal(result.x, 1);
563
- });
564
- it('should find destructured array elements of array', () => {
565
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
566
- const [ [ x, y ] ] = [ [ 1, 2 ] ];
567
- `);
568
- assert.equal(result.x, 1);
569
- assert.equal(result.y, 2);
570
- });
571
- it('should find destructured array element of array inside an object', () => {
572
- const result = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
573
- const { x: [ [ y ] ] } = { x: [ [ 1 ] ] };
574
- `);
575
- assert.equal(result.y, 1);
576
- });
577
- });
578
- });
579
-
580
- describe('built-in functions', () => {
581
- const $ = {
582
- num: 1,
583
- str: 'a',
584
- bool: true,
585
- undf: undefined,
586
- nul: null,
587
- arr: [2,'b',false, { c: 3 }],
588
- obj: { nest: { d: 4 } },
589
- };
590
-
591
- const staticContext = { $, __: parameterFunctionOperations };
592
- it('should have access to both built-in function and response body', () => {
593
- const result = runPostScript( { staticContext }, {}, `
594
- const x = __.usd();
595
- const res = $;
596
- `);
597
- assert.deepStrictEqual(result.x, '$');
598
- assert.deepStrictEqual(result.res, $);
599
- });
600
-
601
- describe('should have all the built-in function 😈', () => {
602
- it('__random_hex', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_hex();')); });
603
- it('__random_uuid', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_uuid();')); });
604
- it('__random_from', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_from($.str);')); });
605
- it('__random_chars', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_chars();')); });
606
- it('__random_number', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_number();')); });
607
- it('__random_digits', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_digits();')); });
608
- it('__random_uppers', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_uppers();')); });
609
- it('__random_lowers', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_lowers();')); });
610
- it('__random_letters', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_letters();')); });
611
- it('__random_boolean', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_boolean();')); });
612
- it('__random_seeded_number', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_seeded_number();')); });
613
- it('__usd', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.usd();')); });
614
- it('__eq', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.eq();')); });
615
- it('__neq', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.neq();')); });
616
- it('__eqi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.eqi($.str, "A");')); });
617
- it('__neqi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.neqi($.str, "A");')); });
618
- it('__matches', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.matches();')); });
619
- it('__array_matches', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_matches(JSON.stringify($.arr), "1");')); });
620
- it('__array_pluck', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_pluck();')); });
621
- it('__array_in_range', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_in_range(JSON.stringify($.arr), 0, 100);')); });
622
- it('__array_includes', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_includes(JSON.stringify($.arr), JSON.stringify($.arr));')); });
623
- it('__array_sum', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_sum(JSON.stringify($.arr));')); });
624
- it('__array_sort', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_sort(JSON.stringify($.arr));')); });
625
- it('__array_sort_numbers', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_sort_numbers("[4,3,2]");')); });
626
- it('__array_is_unique', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_is_unique(JSON.stringify($.arr));')); });
627
- it('__contains', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.contains($.str, "a");')); });
628
- it('__containsi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.containsi($.str, "a");')); });
629
- it('__is_uuid', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.is_uuid($.str);')); });
630
- it('__is_number', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.is_number();')); });
631
- it('__length', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.length($.str);')); });
632
- it('__array_length', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_length(JSON.stringify($.arr));')); });
633
- it('__escape_regexp', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.escape_regexp();')); });
634
- it('__escape_quotes', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.escape_quotes($.str);')); });
635
- it('__encode_url', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.encode_url();')); });
636
- it('__decode_url', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.decode_url();')); });
637
- it('__decode_base64', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.decode_base64();')); });
638
- it('__encode_base64', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.encode_base64();')); });
639
- it('__lower', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.lower($.str);')); });
640
- it('__upper', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.upper($.str);')); });
641
- it('__if_then_else', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.if_then_else($.str,$.str,$.str);')); });
642
- it('__switch', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.switch();')); });
643
- it('__switchi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.switchi($.str);')); });
644
- it('__pick', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.pick("random",$.str);')); });
645
- it('__pick_random', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.pick_random("random",$.str);')); });
646
- it('__split_pick', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.split_pick($.str,$.str,"random");')); });
647
- it('__slice', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.slice($.str,"1");')); });
648
- it('__regexp', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.regexp();')); });
649
- it('__json_keys', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.json_keys(JSON.stringify($));')); });
650
- it('__jsonpath_keys', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath_keys(JSON.stringify($),"$");')); });
651
- it('__jsonpath', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath(JSON.stringify($),"$");')); });
652
- it('__jsonpath_all', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath_all(JSON.stringify($),"$");')); });
653
- it('__jsonpath_apply', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath_apply(JSON.stringify($),"$.str","asd");')); });
654
- it('__jsonschema', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonschema(JSON.stringify($),"{}");')); });
655
- it('__jquery', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jquery($.str,$.str);')); });
656
- it('__now', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.now();')); });
657
- it('__now_ms', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.now_ms();')); });
658
- it('__now_iso', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.now_iso();')); });
659
- it('__date_iso', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.date_iso();')); });
660
- it('__and', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.and();')); });
661
- it('__or', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.or();')); });
662
- it('__not', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.not();')); });
663
- it('__true', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.true();')); });
664
- it('__false', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.false();')); });
665
- it('__add', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.add();')); });
666
- it('__sub', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.sub($.num,$.num);')); });
667
- it('__neg', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.neg($.num,$.num);')); });
668
- it('__abs', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.abs($.num);')); });
669
- it('__mult', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.mult();')); });
670
- it('__div', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.div($.num,$.num);')); });
671
- it('__lt', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.lt();')); });
672
- it('__lte', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.lte();')); });
673
- it('__gt', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.gt();')); });
674
- it('__gt', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.gt();')); });
675
- });
676
- });
677
- });