@loadmill/executer 0.1.52 → 0.1.55

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 (44) hide show
  1. package/dist/parameter-pools.d.ts +3 -0
  2. package/dist/parameter-pools.js +69 -0
  3. package/dist/parameter-pools.js.map +1 -0
  4. package/dist/post-script/post-script-executor.d.ts +12 -2
  5. package/dist/post-script/post-script-executor.js +28 -6
  6. package/dist/post-script/post-script-executor.js.map +1 -1
  7. package/dist/sequence.d.ts +6 -1
  8. package/dist/sequence.js +51 -18
  9. package/dist/sequence.js.map +1 -1
  10. package/dist/single-runner.d.ts +1 -0
  11. package/dist/single-runner.js +4 -1
  12. package/dist/single-runner.js.map +1 -1
  13. package/package.json +3 -3
  14. package/src/asserter.ts +0 -137
  15. package/src/errors.ts +0 -10
  16. package/src/extraction-combiner.ts +0 -110
  17. package/src/failures.ts +0 -79
  18. package/src/message-creators.ts +0 -44
  19. package/src/mill-info.ts +0 -81
  20. package/src/mill-version.ts +0 -7
  21. package/src/post-script/ast-walker/index.ts +0 -160
  22. package/src/post-script/ast-walker/type-guard.ts +0 -73
  23. package/src/post-script/ast-walker/types.ts +0 -35
  24. package/src/post-script/console-log.ts +0 -24
  25. package/src/post-script/parser/acorn-js-parser.ts +0 -8
  26. package/src/post-script/parser/js-parser.ts +0 -22
  27. package/src/post-script/parser/parser.ts +0 -5
  28. package/src/post-script/post-script-executor.ts +0 -93
  29. package/src/post-script/virtual-machine/virtual-machine.ts +0 -15
  30. package/src/post-script/virtual-machine/vm2-virtual-machine.ts +0 -45
  31. package/src/report-types.ts +0 -127
  32. package/src/request-sequence-result.ts +0 -63
  33. package/src/request-stats.ts +0 -20
  34. package/src/res-keeper.ts +0 -53
  35. package/src/sampler.ts +0 -133
  36. package/src/sequence.ts +0 -1115
  37. package/src/single-runner.ts +0 -68
  38. package/src/test-run-event-emitter.ts +0 -25
  39. package/src/utils.ts +0 -8
  40. package/src/work.ts +0 -17
  41. package/src/ws.ts +0 -286
  42. package/test/post-script-console-log.spec.ts +0 -73
  43. package/test/post-script-executor.spec.ts +0 -685
  44. package/tsconfig.json +0 -9
@@ -1,685 +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 { result } = runPostScript({ staticContext: { $, __: {} } }, {},
206
- `
207
- resBodyCopy = $;
208
- a = $.a;
209
- b = $.b;
210
- `);
211
- assert.deepStrictEqual(result.a, 1);
212
- assert.deepStrictEqual(result.b, 2);
213
- assert.deepStrictEqual(result.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: { $: $, __: {} } }, {}, 'res = $;'));
309
- });
310
- });
311
-
312
- describe('global object', () => {
313
- it('should return an empty object for a variable referencing the global object', () => {
314
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'c=global');
315
- assert.deepEqual(
316
- result.c,
317
- {}
318
- );
319
- });
320
- });
321
-
322
- describe('destructuring', () => {
323
- describe('object', () => {
324
- it('should detect declaration of root property', () => {
325
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'const { x } = { x: 1 };');
326
- assert.equal(result.x, 1);
327
- });
328
-
329
- it('should detect declaration of 2-level nested property', () => {
330
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
331
- let { x: { y } } = { x: { y: 1 } };
332
- `);
333
- assert.equal(result.y, 1);
334
- });
335
-
336
- it('should detect declaration of 3-level nested property', () => {
337
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
338
- let { x: { y: { z } } } = { x: { y: { z: 1 } } };
339
- `);
340
- assert.equal(result.z, 1);
341
- });
342
-
343
- it('should detect declaration of 4-level nested property', () => {
344
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
345
- let { x: { y: { z: { w } } } } = { x: { y: { z: { w: 1 } } } };
346
- `);
347
- assert.equal(result.w, 1);
348
- });
349
-
350
- it('should detect declaration of two (2) root properties', () => {
351
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
352
- let { x, y } = { x: 1, y: 2 };
353
- `);
354
- assert.equal(result.x, 1);
355
- assert.equal(result.y, 2);
356
- });
357
-
358
- it('should detect declaration of a root property and a nested property', () => {
359
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
360
- let { x, y: { z } } = { x: 1, y: { z: 2 } };
361
- `);
362
- assert.equal(result.x, 1);
363
- assert.equal(result.z, 2);
364
- });
365
-
366
- it('should detect rest element object declaration', () => {
367
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
368
- const {x, ...theRest } = { x: 1, y: 2, z: 3 };
369
- `);
370
- assert.equal(result.x, 1);
371
- assert.equal(result.theRest.y, 2);
372
- assert.equal(result.theRest.z, 3);
373
- });
374
-
375
- it('should detect implicit declaration of root property ', () => {
376
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '({ x } = { x: 1 });');
377
- assert.equal(result.x, 1);
378
- });
379
-
380
- it('should detect implicit declaration of 2-level nested property ', () => {
381
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '({ x: { y } } = { x: { y: 1 } });');
382
- assert.equal(result.y, 1);
383
- });
384
-
385
- it('should detect implicit declaration of 3-level nested property ', () => {
386
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '({ x: { y: { z } } } = { x: { y: { z: 1 } } });');
387
- assert.equal(result.z, 1);
388
- });
389
-
390
- it('should detect implicit declaration of two (2) root properties', () => {
391
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
392
- ({ x, y } = { x: 1, y: 2 });
393
- `);
394
- assert.equal(result.x, 1);
395
- assert.equal(result.y, 2);
396
- });
397
-
398
- it('should detect implicit declaration of a root property and a nested property', () => {
399
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
400
- ({ x, y: { z } } = { x: 1, y: { z: 2 } });
401
- `);
402
- assert.equal(result.x, 1);
403
- assert.equal(result.z, 2);
404
- });
405
- });
406
-
407
- describe('array', () => {
408
- it('should detect destructured array value', () => {
409
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
410
- const [ x ] = [ 1 ];
411
- `);
412
- assert.equal(result.x, 1);
413
- });
414
-
415
- it('should detect 2 destructured array values', () => {
416
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
417
- const [ x, y ] = [ 1, 2 ];
418
- `);
419
- assert.equal(result.x, 1);
420
- assert.equal(result.y, 2);
421
- });
422
-
423
- it('should detect spread array values', () => {
424
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let [...elements] = [1, 2];');
425
- assert.equal(result.elements.length, 2);
426
- });
427
-
428
- it('should detect "regular" & spread array values', () => {
429
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let [a, b, ...rest] = [10, 20, 30, 40, 50];');
430
- assert.equal(result.a,10);
431
- assert.equal(result.b,20);
432
- assert.equal(result.rest.length,3);
433
- });
434
-
435
- it('should detect implicit declaration of array destructuring ', () => {
436
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '([ x ] = [ 1 ]);');
437
- assert.equal(result.x, 1);
438
- });
439
-
440
- it('should detect 2 implicit declarations of array destructuring ', () => {
441
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '([ x, y ] = [ 1, 2 ]);');
442
- assert.equal(result.x, 1);
443
- assert.equal(result.y, 2);
444
- });
445
-
446
- it('should detect implicit spread array values', () => {
447
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, '[...elements] = [1, 2];');
448
- assert.equal(result.elements.length, 2);
449
- });
450
-
451
- it('should detect implicit "regular" & spread array values', () => {
452
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, 'let [a, b, ...rest] = [10, 20, 30, 40, 50];');
453
- assert.equal(result.a,10);
454
- assert.equal(result.b,20);
455
- assert.equal(result.rest.length,3);
456
- });
457
- });
458
-
459
- describe('array of objects', () => {
460
- it('should detect destructured object from array', () => {
461
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
462
- const [ { x } ] = [ { x: 1 } ];
463
- `);
464
- assert.equal(result.x, 1);
465
- });
466
- it('should detect nested destructured object from array', () => {
467
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
468
- const [ { x: { y } } ] = [ { x: { y: 1 } } ];
469
- `);
470
- assert.equal(result.y, 1);
471
- });
472
- it('should detect destructuring of 3-level nested property from array', () => {
473
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
474
- let [{ x: { y: { z } } }] = [{ x: { y: { z: 1 } } }];
475
- `);
476
- assert.equal(result.z, 1);
477
- });
478
- it('should detect destructuring of two (2) root properties from array', () => {
479
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
480
- let [{ x, y }] = [{ x: 1, y: 2 }];
481
- `);
482
- assert.equal(result.x, 1);
483
- assert.equal(result.y, 2);
484
- });
485
- it('should detect destructuring of a root property and a nested property from array', () => {
486
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
487
- let [{ x, y: { z } }] = [{ x: 1, y: { z: 2 } }];
488
- `);
489
- assert.equal(result.x, 1);
490
- assert.equal(result.z, 2);
491
- });
492
- it('should detect destructuring of mixed type elements of array', () => {
493
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
494
- let [{ x }, y, z, w, q, t] = [{ x: 1 }, 2, 'a', false, undefined, null];
495
- `);
496
- assert.equal(result.x, 1);
497
- assert.equal(result.y, 2);
498
- assert.equal(result.z, 'a');
499
- assert.equal(result.w, false);
500
- assert.equal(result.q, undefined);
501
- assert.equal(result.t, null);
502
- });
503
- });
504
-
505
- describe('object with array value', () => {
506
- it('should detect an array value destructured from object', () => {
507
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
508
- const { x } = { x: [1,2] };
509
- `);
510
- assert.ok(Array.isArray(result.x));
511
- assert.equal(result.x[0], 1);
512
- assert.equal(result.x[1], 2);
513
- });
514
- it('should detect an object destructured from array destructured from object', () => {
515
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
516
- const { x: [ { y } ] } = { x: [ { y: 1 } ] };
517
- `);
518
- assert.equal(result.y, 1);
519
- });
520
- it('should detect a deep nested destructuring mix of obj and arr', () => {
521
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
522
- const { a: [ { b: [ { c } ], } ], } = { a: [ { b: [ { c: ['unacceptable'] } ] } ] }
523
- `);
524
- assert.ok(Array.isArray(result.c));
525
- assert.equal(result.c[0], 'unacceptable');
526
- assert.equal(result.c.length, 1);
527
- });
528
- });
529
-
530
- describe('implicit object with array value', () => {
531
- it('should detect an implicit array value destructured from object', () => {
532
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
533
- ({ x } = { x: [1,2] });
534
- `);
535
- assert.ok(Array.isArray(result.x));
536
- assert.equal(result.x[0], 1);
537
- assert.equal(result.x[1], 2);
538
- });
539
- it('should detect an object destructured from array destructured from object', () => {
540
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
541
- ({ x: [ { y } ] } = { x: [ { y: 1 } ] });
542
- `);
543
- assert.equal(result.y, 1);
544
- });
545
- it('should detect a deep nested destructuring mix of obj and arr', () => {
546
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
547
- ({ a: [ { b: [ { c } ], } ], } = { a: [ { b: [ { c: ['unacceptable'] } ] } ] });
548
- `);
549
- assert.ok(Array.isArray(result.c));
550
- assert.equal(result.c[0], 'unacceptable');
551
- assert.equal(result.c.length, 1);
552
- });
553
- it('console.log in post script', () => {
554
- const { stdout } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
555
- const a = { "A":1,"B":2,"C":3 };
556
- const b = 'print me';
557
- const c = [ 1,2,3 ];
558
- console.log(a);
559
- console.log(b,c);
560
- `);
561
- assert.deepEqual(stdout , ['{\n "A": 1,\n "B": 2,\n "C": 3\n} ','print me [\n 1,\n 2,\n 3\n] ']);
562
- });
563
- });
564
-
565
- describe('array of arrays', () => {
566
- it('should find destructured array element of array', () => {
567
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
568
- const [ [ x ] ] = [ [ 1 ] ];
569
- `);
570
- assert.equal(result.x, 1);
571
- });
572
- it('should find destructured array elements of array', () => {
573
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
574
- const [ [ x, y ] ] = [ [ 1, 2 ] ];
575
- `);
576
- assert.equal(result.x, 1);
577
- assert.equal(result.y, 2);
578
- });
579
- it('should find destructured array element of array inside an object', () => {
580
- const { result } = runPostScript({ staticContext: { $: {}, __: {} } }, {}, `
581
- const { x: [ [ y ] ] } = { x: [ [ 1 ] ] };
582
- `);
583
- assert.equal(result.y, 1);
584
- });
585
- });
586
- });
587
-
588
- describe('built-in functions', () => {
589
- const $ = {
590
- num: 1,
591
- str: 'a',
592
- bool: true,
593
- undf: undefined,
594
- nul: null,
595
- arr: [2,'b',false, { c: 3 }],
596
- obj: { nest: { d: 4 } },
597
- };
598
-
599
- const staticContext = { $, __: parameterFunctionOperations };
600
- it('should have access to both built-in function and response body', () => {
601
- const { result } = runPostScript( { staticContext }, {}, `
602
- const x = __.usd();
603
- const res = $;
604
- `);
605
- assert.deepStrictEqual(result.x, '$');
606
- assert.deepStrictEqual(result.res, $);
607
- });
608
-
609
- describe('should have all the built-in function 😈', () => {
610
- it('__random_hex', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_hex();')); });
611
- it('__random_uuid', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_uuid();')); });
612
- it('__random_from', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_from($.str);')); });
613
- it('__random_chars', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_chars();')); });
614
- it('__random_number', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_number();')); });
615
- it('__random_digits', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_digits();')); });
616
- it('__random_uppers', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_uppers();')); });
617
- it('__random_lowers', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_lowers();')); });
618
- it('__random_letters', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_letters();')); });
619
- it('__random_boolean', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_boolean();')); });
620
- it('__random_seeded_number', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.random_seeded_number();')); });
621
- it('__usd', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.usd();')); });
622
- it('__eq', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.eq();')); });
623
- it('__neq', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.neq();')); });
624
- it('__eqi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.eqi($.str, "A");')); });
625
- it('__neqi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.neqi($.str, "A");')); });
626
- it('__matches', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.matches();')); });
627
- it('__array_matches', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_matches(JSON.stringify($.arr), "1");')); });
628
- it('__array_pluck', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_pluck();')); });
629
- it('__array_in_range', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_in_range(JSON.stringify($.arr), 0, 100);')); });
630
- it('__array_includes', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_includes(JSON.stringify($.arr), JSON.stringify($.arr));')); });
631
- it('__array_sum', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_sum(JSON.stringify($.arr));')); });
632
- it('__array_sort', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_sort(JSON.stringify($.arr));')); });
633
- it('__array_sort_numbers', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_sort_numbers("[4,3,2]");')); });
634
- it('__array_is_unique', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_is_unique(JSON.stringify($.arr));')); });
635
- it('__contains', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.contains($.str, "a");')); });
636
- it('__containsi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.containsi($.str, "a");')); });
637
- it('__is_uuid', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.is_uuid($.str);')); });
638
- it('__is_number', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.is_number();')); });
639
- it('__length', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.length($.str);')); });
640
- it('__array_length', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.array_length(JSON.stringify($.arr));')); });
641
- it('__escape_regexp', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.escape_regexp();')); });
642
- it('__escape_quotes', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.escape_quotes($.str);')); });
643
- it('__encode_url', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.encode_url();')); });
644
- it('__decode_url', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.decode_url();')); });
645
- it('__decode_base64', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.decode_base64();')); });
646
- it('__encode_base64', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.encode_base64();')); });
647
- it('__lower', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.lower($.str);')); });
648
- it('__upper', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.upper($.str);')); });
649
- it('__if_then_else', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.if_then_else($.str,$.str,$.str);')); });
650
- it('__switch', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.switch();')); });
651
- it('__switchi', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.switchi($.str);')); });
652
- it('__pick', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.pick("random",$.str);')); });
653
- it('__pick_random', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.pick_random("random",$.str);')); });
654
- it('__split_pick', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.split_pick($.str,$.str,"random");')); });
655
- it('__slice', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.slice($.str,"1");')); });
656
- it('__regexp', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.regexp();')); });
657
- it('__json_keys', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.json_keys(JSON.stringify($));')); });
658
- it('__jsonpath_keys', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath_keys(JSON.stringify($),"$");')); });
659
- it('__jsonpath', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath(JSON.stringify($),"$");')); });
660
- it('__jsonpath_all', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath_all(JSON.stringify($),"$");')); });
661
- it('__jsonpath_apply', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonpath_apply(JSON.stringify($),"$.str","asd");')); });
662
- it('__jsonschema', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jsonschema(JSON.stringify($),"{}");')); });
663
- it('__jquery', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.jquery($.str,$.str);')); });
664
- it('__now', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.now();')); });
665
- it('__now_ms', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.now_ms();')); });
666
- it('__now_iso', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.now_iso();')); });
667
- it('__date_iso', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.date_iso();')); });
668
- it('__and', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.and();')); });
669
- it('__or', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.or();')); });
670
- it('__not', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.not();')); });
671
- it('__true', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.true();')); });
672
- it('__false', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.false();')); });
673
- it('__add', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.add();')); });
674
- it('__sub', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.sub($.num,$.num);')); });
675
- it('__neg', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.neg($.num,$.num);')); });
676
- it('__abs', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.abs($.num);')); });
677
- it('__mult', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.mult();')); });
678
- it('__div', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.div($.num,$.num);')); });
679
- it('__lt', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.lt();')); });
680
- it('__lte', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.lte();')); });
681
- it('__gt', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.gt();')); });
682
- it('__gt', () => { assert.doesNotThrow(() => runPostScript({ staticContext }, {}, '__.gt();')); });
683
- });
684
- });
685
- });