script-engine-lib 0.4.3 → 1.0.0-rc0

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 (62) hide show
  1. package/dist/index.d.mts +191 -0
  2. package/dist/index.d.ts +191 -5
  3. package/dist/index.js +21 -29
  4. package/dist/index.mjs +21 -0
  5. package/package.json +56 -52
  6. package/src/index.ts +10 -0
  7. package/dist/decorators/action-before-testing.d.ts +0 -2
  8. package/dist/decorators/action-before-testing.js +0 -23
  9. package/dist/decorators/action-before-testing.test.d.ts +0 -1
  10. package/dist/decorators/action-before-testing.test.js +0 -82
  11. package/dist/engine/script-engine.d.ts +0 -35
  12. package/dist/engine/script-engine.js +0 -175
  13. package/dist/engine/script-engine.test.d.ts +0 -1
  14. package/dist/engine/script-engine.test.js +0 -1277
  15. package/dist/engine/story-engine.d.ts +0 -35
  16. package/dist/engine/story-engine.js +0 -175
  17. package/dist/engine/story-engine.test.d.ts +0 -1
  18. package/dist/engine/story-engine.test.js +0 -1277
  19. package/dist/interfaces.d.ts +0 -51
  20. package/dist/interfaces.js +0 -27
  21. package/dist/simulator/decorators.d.ts +0 -2
  22. package/dist/simulator/decorators.js +0 -23
  23. package/dist/simulator/decorators.test.d.ts +0 -1
  24. package/dist/simulator/decorators.test.js +0 -81
  25. package/dist/simulator/event-branches-map.d.ts +0 -16
  26. package/dist/simulator/event-branches-map.js +0 -114
  27. package/dist/simulator/event-branches-map.test.d.ts +0 -1
  28. package/dist/simulator/event-branches-map.test.js +0 -120
  29. package/dist/simulator/event-test-simulator.d.ts +0 -42
  30. package/dist/simulator/event-test-simulator.js +0 -373
  31. package/dist/simulator/event-test-simulator.test.d.ts +0 -1
  32. package/dist/simulator/event-test-simulator.test.js +0 -1960
  33. package/dist/simulator/execution-history.d.ts +0 -6
  34. package/dist/simulator/execution-history.js +0 -22
  35. package/dist/simulator/helpers/event-branches-map.d.ts +0 -16
  36. package/dist/simulator/helpers/event-branches-map.js +0 -114
  37. package/dist/simulator/helpers/event-branches-map.test.d.ts +0 -1
  38. package/dist/simulator/helpers/event-branches-map.test.js +0 -120
  39. package/dist/simulator/helpers/execution-history.d.ts +0 -6
  40. package/dist/simulator/helpers/execution-history.js +0 -22
  41. package/dist/simulator/helpers/script-branches-map.d.ts +0 -16
  42. package/dist/simulator/helpers/script-branches-map.js +0 -114
  43. package/dist/simulator/helpers/script-branches-map.test.d.ts +0 -1
  44. package/dist/simulator/helpers/script-branches-map.test.js +0 -120
  45. package/dist/simulator/script-test-simulator.d.ts +0 -42
  46. package/dist/simulator/script-test-simulator.js +0 -373
  47. package/dist/simulator/script-test-simulator.test.d.ts +0 -1
  48. package/dist/simulator/script-test-simulator.test.js +0 -2013
  49. package/dist/structured-text/index.d.ts +0 -9
  50. package/dist/structured-text/index.js +0 -8
  51. package/dist/structured-text/rich-text-separator.d.ts +0 -48
  52. package/dist/structured-text/rich-text-separator.js +0 -146
  53. package/dist/structured-text/rich-text-separator.test.d.ts +0 -1
  54. package/dist/structured-text/rich-text-separator.test.js +0 -166
  55. package/dist/structured-text/rich-text-validator.d.ts +0 -19
  56. package/dist/structured-text/rich-text-validator.js +0 -73
  57. package/dist/structured-text/rich-text-validator.test.d.ts +0 -1
  58. package/dist/structured-text/rich-text-validator.test.js +0 -59
  59. package/dist/test.spec.d.ts +0 -0
  60. package/dist/test.spec.js +0 -7
  61. package/dist/test.test.d.ts +0 -1
  62. package/dist/test.test.js +0 -9
@@ -1,1277 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const helpers_lib_1 = require("helpers-lib");
13
- const vitest_1 = require("vitest");
14
- const script_engine_1 = require("./script-engine");
15
- class DefaultFunctions extends script_engine_1.ScriptEngineFunctions {
16
- onPlayerChoice(choices) { }
17
- onDialog(text, speaker) { }
18
- }
19
- (0, vitest_1.describe)('ScriptEngine', () => {
20
- (0, vitest_1.describe)('Start Script', () => {
21
- (0, vitest_1.test)('start a valid script', () => {
22
- let scriptDefinitions = {
23
- test: {
24
- id: 'test',
25
- actions: [
26
- {
27
- type: 'command',
28
- value: 'a = 2'
29
- }
30
- ]
31
- }
32
- };
33
- let variables = { a: 1 };
34
- let functions = new DefaultFunctions();
35
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
36
- scriptEngine.start('test');
37
- (0, vitest_1.expect)(scriptEngine['stack'].isEmpty).toEqual(false);
38
- });
39
- (0, vitest_1.test)('start an script while the action stack is not empty', () => {
40
- let scriptDefinitions = {
41
- test: {
42
- id: 'test',
43
- actions: [
44
- {
45
- type: 'command',
46
- value: 'a = 2'
47
- }
48
- ]
49
- }
50
- };
51
- let variables = { a: 1 };
52
- let functions = new DefaultFunctions();
53
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
54
- scriptEngine.start('test');
55
- (0, vitest_1.expect)(() => scriptEngine.start('test')).toThrowError(`ScriptEngine: Existing script haven't been concluded yet.`);
56
- });
57
- (0, vitest_1.test)('start an invalid script', () => {
58
- let scriptDefinitions = {
59
- test: {
60
- id: 'test',
61
- actions: [
62
- {
63
- type: 'command',
64
- value: 'a = 2'
65
- }
66
- ]
67
- }
68
- };
69
- let variables = { a: 1 };
70
- let functions = new DefaultFunctions();
71
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
72
- (0, vitest_1.expect)(() => scriptEngine.start('invalid')).toThrowError(`ScriptEngine: Script definition not found: invalid`);
73
- });
74
- });
75
- (0, vitest_1.describe)('Commands', () => {
76
- (0, vitest_1.test)('setting variable directly', () => {
77
- let scriptDefinitions = {
78
- test: {
79
- id: 'test',
80
- actions: [
81
- {
82
- type: 'command',
83
- value: 'a = 2'
84
- }
85
- ]
86
- }
87
- };
88
- let variables = { a: 1 };
89
- let functions = new DefaultFunctions();
90
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
91
- scriptEngine.start('test');
92
- scriptEngine.next();
93
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
94
- });
95
- (0, vitest_1.test)('function call empty', () => {
96
- let scriptDefinitions = {
97
- test: {
98
- id: 'test',
99
- actions: [
100
- {
101
- type: 'command',
102
- value: 'foo()'
103
- }
104
- ]
105
- }
106
- };
107
- let variables = { a: 1 };
108
- class Functions {
109
- onPlayerChoice(choices) { }
110
- onDialog(text, speaker) { }
111
- foo() {
112
- variables.a = 2;
113
- }
114
- }
115
- __decorate([
116
- (0, helpers_lib_1.JSEngineFunction)(),
117
- __metadata("design:type", Function),
118
- __metadata("design:paramtypes", []),
119
- __metadata("design:returntype", void 0)
120
- ], Functions.prototype, "foo", null);
121
- let functions = new Functions();
122
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
123
- scriptEngine.start('test');
124
- scriptEngine.next();
125
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
126
- });
127
- (0, vitest_1.test)('function call sending parameter', () => {
128
- let scriptDefinitions = {
129
- test: {
130
- id: 'test',
131
- actions: [
132
- {
133
- type: 'command',
134
- value: 'foo(2)'
135
- }
136
- ]
137
- }
138
- };
139
- let variables = { a: 1 };
140
- class Functions {
141
- onPlayerChoice(choices) { }
142
- onDialog(text, speaker) { }
143
- foo(value) {
144
- variables.a = value;
145
- }
146
- }
147
- __decorate([
148
- (0, helpers_lib_1.JSEngineFunction)(),
149
- __metadata("design:type", Function),
150
- __metadata("design:paramtypes", [Number]),
151
- __metadata("design:returntype", void 0)
152
- ], Functions.prototype, "foo", null);
153
- let functions = new Functions();
154
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
155
- scriptEngine.start('test');
156
- scriptEngine.next();
157
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
158
- });
159
- (0, vitest_1.test)('setting variable with function call', () => {
160
- let scriptDefinitions = {
161
- test: {
162
- id: 'test',
163
- actions: [
164
- {
165
- type: 'command',
166
- value: 'a = foo(2)'
167
- }
168
- ]
169
- }
170
- };
171
- let variables = { a: 1 };
172
- class Functions {
173
- onPlayerChoice(choices) { }
174
- onDialog(text, speaker) { }
175
- foo(value) {
176
- return value + 1;
177
- }
178
- }
179
- __decorate([
180
- (0, helpers_lib_1.JSEngineFunction)(),
181
- __metadata("design:type", Function),
182
- __metadata("design:paramtypes", [Number]),
183
- __metadata("design:returntype", Number)
184
- ], Functions.prototype, "foo", null);
185
- let functions = new Functions();
186
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
187
- scriptEngine.start('test');
188
- scriptEngine.next();
189
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
190
- });
191
- });
192
- (0, vitest_1.describe)('Dialog', () => {
193
- class DialogTestFunctions extends script_engine_1.ScriptEngineFunctions {
194
- constructor() {
195
- super(...arguments);
196
- this.lastDialogText = '';
197
- this.lastDialogSpeaker = '';
198
- }
199
- onPlayerChoice(choices) { }
200
- onDialog(text, speaker) {
201
- this.lastDialogText = text;
202
- this.lastDialogSpeaker = speaker;
203
- }
204
- foo(value) {
205
- return 'Hello, ' + value;
206
- }
207
- }
208
- __decorate([
209
- (0, helpers_lib_1.JSEngineFunction)(),
210
- __metadata("design:type", Function),
211
- __metadata("design:paramtypes", [String]),
212
- __metadata("design:returntype", String)
213
- ], DialogTestFunctions.prototype, "foo", null);
214
- (0, vitest_1.test)('display dialog with text and speaker', () => {
215
- let scriptDefinitions = {
216
- test: {
217
- id: 'test',
218
- actions: [
219
- {
220
- type: 'dialog',
221
- value: {
222
- text: 'Hello, world!',
223
- speaker: 'Narrator'
224
- }
225
- }
226
- ]
227
- }
228
- };
229
- let variables = {};
230
- let functions = new DialogTestFunctions();
231
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
232
- scriptEngine.start('test');
233
- scriptEngine.next();
234
- (0, vitest_1.expect)(functions.lastDialogText).toEqual('Hello, world!');
235
- (0, vitest_1.expect)(functions.lastDialogSpeaker).toEqual('Narrator');
236
- });
237
- (0, vitest_1.test)('display dialog text with function call', () => {
238
- let scriptDefinitions = {
239
- test: {
240
- id: 'test',
241
- actions: [
242
- {
243
- type: 'dialog',
244
- value: {
245
- text: '${foo("world")}!',
246
- speaker: 'Narrator'
247
- }
248
- }
249
- ]
250
- }
251
- };
252
- let variables = {};
253
- let functions = new DialogTestFunctions();
254
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
255
- scriptEngine.start('test');
256
- scriptEngine.next();
257
- (0, vitest_1.expect)(functions.lastDialogText).toEqual('Hello, world!');
258
- (0, vitest_1.expect)(functions.lastDialogSpeaker).toEqual('Narrator');
259
- });
260
- (0, vitest_1.test)('display dialog text with variable', () => {
261
- let scriptDefinitions = {
262
- test: {
263
- id: 'test',
264
- actions: [
265
- {
266
- type: 'dialog',
267
- value: {
268
- text: '${a}',
269
- speaker: 'Narrator'
270
- }
271
- }
272
- ]
273
- }
274
- };
275
- let variables = { a: 'Hello, world!' };
276
- let functions = new DialogTestFunctions();
277
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
278
- scriptEngine.start('test');
279
- scriptEngine.next();
280
- (0, vitest_1.expect)(functions.lastDialogText).toEqual('Hello, world!');
281
- (0, vitest_1.expect)(functions.lastDialogSpeaker).toEqual('Narrator');
282
- });
283
- (0, vitest_1.test)('multiple dialogs in sequence', () => {
284
- let scriptDefinitions = {
285
- test: {
286
- id: 'test',
287
- actions: [
288
- {
289
- type: 'dialog',
290
- value: {
291
- text: 'First message',
292
- speaker: 'Character1'
293
- }
294
- },
295
- {
296
- type: 'dialog',
297
- value: {
298
- text: 'Second message',
299
- speaker: 'Character2'
300
- }
301
- }
302
- ]
303
- }
304
- };
305
- let variables = {};
306
- let functions = new DialogTestFunctions();
307
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
308
- scriptEngine.start('test');
309
- scriptEngine.next();
310
- (0, vitest_1.expect)(functions.lastDialogText).toEqual('First message');
311
- (0, vitest_1.expect)(functions.lastDialogSpeaker).toEqual('Character1');
312
- scriptEngine.next();
313
- (0, vitest_1.expect)(functions.lastDialogText).toEqual('Second message');
314
- (0, vitest_1.expect)(functions.lastDialogSpeaker).toEqual('Character2');
315
- });
316
- (0, vitest_1.test)('dialog mixed with other actions', () => {
317
- let scriptDefinitions = {
318
- test: {
319
- id: 'test',
320
- actions: [
321
- {
322
- type: 'command',
323
- value: 'a = 1'
324
- },
325
- {
326
- type: 'dialog',
327
- value: {
328
- text: 'Setting variable',
329
- speaker: 'System'
330
- }
331
- },
332
- {
333
- type: 'command',
334
- value: 'a = 2'
335
- }
336
- ]
337
- }
338
- };
339
- let variables = { a: 0 };
340
- let functions = new DialogTestFunctions();
341
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
342
- scriptEngine.start('test');
343
- scriptEngine.next();
344
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(1);
345
- scriptEngine.next();
346
- (0, vitest_1.expect)(functions.lastDialogText).toEqual('Setting variable');
347
- (0, vitest_1.expect)(functions.lastDialogSpeaker).toEqual('System');
348
- scriptEngine.next();
349
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
350
- });
351
- });
352
- (0, vitest_1.describe)('Jump To Script', () => {
353
- (0, vitest_1.test)('jump to a valid script', () => {
354
- let scriptDefinitions = {
355
- test: {
356
- id: 'test',
357
- actions: [
358
- {
359
- type: 'jumpTo',
360
- value: 'jumpedScript'
361
- }
362
- ]
363
- },
364
- jumpedScript: {
365
- id: 'jumpedScript',
366
- actions: [
367
- {
368
- type: 'command',
369
- value: 'a = 2'
370
- }
371
- ]
372
- }
373
- };
374
- let variables = { a: 1 };
375
- let functions = new DefaultFunctions();
376
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
377
- scriptEngine.start('test');
378
- scriptEngine.next();
379
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
380
- });
381
- (0, vitest_1.test)('jumping to an script and returning back', () => {
382
- let scriptDefinitions = {
383
- test: {
384
- id: 'test',
385
- actions: [
386
- {
387
- type: 'jumpTo',
388
- value: 'jumpedScript'
389
- },
390
- {
391
- type: 'command',
392
- value: 'b = 2'
393
- }
394
- ]
395
- },
396
- jumpedScript: {
397
- id: 'jumpedScript',
398
- actions: [
399
- {
400
- type: 'command',
401
- value: 'a = 2'
402
- }
403
- ]
404
- }
405
- };
406
- let variables = { a: 1, b: 1 };
407
- let functions = new DefaultFunctions();
408
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
409
- scriptEngine.start('test');
410
- scriptEngine.next();
411
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
412
- scriptEngine.next();
413
- (0, vitest_1.expect)(scriptEngine.variables.b).toEqual(2);
414
- });
415
- (0, vitest_1.test)('edge case - jumping to an script and returning back with combination of branching', () => {
416
- let scriptDefinitions = {
417
- test: {
418
- id: 'test',
419
- actions: [
420
- {
421
- type: 'branchByCondition',
422
- value: [
423
- {
424
- condition: 'a === 1',
425
- actions: [
426
- {
427
- type: 'command',
428
- value: 'a = 2'
429
- },
430
- {
431
- type: 'jumpTo',
432
- value: 'jumpedScript'
433
- }
434
- ]
435
- },
436
- {
437
- actions: [
438
- {
439
- type: 'command',
440
- value: 'b = 2'
441
- }
442
- ]
443
- }
444
- ]
445
- }
446
- ]
447
- },
448
- jumpedScript: {
449
- id: 'jumpedScript',
450
- actions: [
451
- {
452
- type: 'jumpTo',
453
- value: 'test'
454
- }
455
- ]
456
- }
457
- };
458
- let variables = { a: 1, b: 1 };
459
- let functions = new DefaultFunctions();
460
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
461
- scriptEngine.start('test');
462
- scriptEngine.next();
463
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
464
- (0, vitest_1.expect)(scriptEngine.variables.b).toEqual(1);
465
- scriptEngine.next();
466
- (0, vitest_1.expect)(scriptEngine.variables.b).toEqual(2);
467
- });
468
- });
469
- (0, vitest_1.describe)('Branch By Condition', () => {
470
- (0, vitest_1.test)('branch to condition 1', () => {
471
- let scriptDefinitions = {
472
- test: {
473
- id: 'test',
474
- actions: [
475
- {
476
- type: 'branchByCondition',
477
- value: [
478
- {
479
- condition: 'a === 1',
480
- actions: [
481
- {
482
- type: 'command',
483
- value: 'a = 2'
484
- }
485
- ]
486
- },
487
- {
488
- actions: [
489
- {
490
- type: 'command',
491
- value: 'a = 3'
492
- }
493
- ]
494
- }
495
- ]
496
- }
497
- ]
498
- }
499
- };
500
- let variables = { a: 1 };
501
- let functions = new DefaultFunctions();
502
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
503
- scriptEngine.start('test');
504
- scriptEngine.next();
505
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
506
- });
507
- (0, vitest_1.test)('branch to condition default', () => {
508
- let scriptDefinitions = {
509
- test: {
510
- id: 'test',
511
- actions: [
512
- {
513
- type: 'branchByCondition',
514
- value: [
515
- {
516
- condition: 'a === 2',
517
- actions: [
518
- {
519
- type: 'command',
520
- value: 'a = 2'
521
- }
522
- ]
523
- },
524
- {
525
- actions: [
526
- {
527
- type: 'command',
528
- value: 'a = 3'
529
- }
530
- ]
531
- }
532
- ]
533
- }
534
- ]
535
- }
536
- };
537
- let variables = { a: 1 };
538
- let functions = new DefaultFunctions();
539
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
540
- scriptEngine.start('test');
541
- scriptEngine.next();
542
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
543
- });
544
- (0, vitest_1.test)('no condition is met', () => {
545
- let scriptDefinitions = {
546
- test: {
547
- id: 'test',
548
- actions: [
549
- {
550
- type: 'branchByCondition',
551
- value: [
552
- {
553
- condition: 'a === 2',
554
- actions: [
555
- {
556
- type: 'command',
557
- value: 'a = 2'
558
- }
559
- ]
560
- },
561
- {
562
- condition: 'a === 3',
563
- actions: [
564
- {
565
- type: 'command',
566
- value: 'a = 3'
567
- }
568
- ]
569
- }
570
- ]
571
- }
572
- ]
573
- }
574
- };
575
- let variables = { a: 1 };
576
- let functions = new DefaultFunctions();
577
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
578
- scriptEngine.start('test');
579
- (0, vitest_1.expect)(() => scriptEngine.next()).toThrow('ScriptEngine: No branch is fulfilled the condition in branchByCondition action.');
580
- });
581
- (0, vitest_1.test)('trying to jump while action stack is not empty', () => {
582
- let scriptDefinitions = {
583
- test: {
584
- id: 'test',
585
- actions: [
586
- {
587
- type: 'branchByCondition',
588
- value: [
589
- {
590
- condition: 'a === 1',
591
- actions: [
592
- {
593
- type: 'command',
594
- value: 'a = 2'
595
- }
596
- ]
597
- },
598
- {
599
- actions: [
600
- {
601
- type: 'command',
602
- value: 'a = 3'
603
- }
604
- ]
605
- }
606
- ]
607
- },
608
- {
609
- type: 'command',
610
- value: 'a = 3'
611
- }
612
- ]
613
- }
614
- };
615
- let variables = { a: 1 };
616
- let functions = new DefaultFunctions();
617
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
618
- scriptEngine.start('test');
619
- (0, vitest_1.expect)(variables.a).toEqual(1);
620
- scriptEngine.next();
621
- (0, vitest_1.expect)(variables.a).toEqual(2);
622
- scriptEngine.next();
623
- (0, vitest_1.expect)(variables.a).toEqual(3);
624
- });
625
- });
626
- (0, vitest_1.describe)('Branch By Chance', () => {
627
- (0, vitest_1.test)('branch by valid chance', () => {
628
- let scriptDefinitions = {
629
- test: {
630
- id: 'test',
631
- actions: [
632
- {
633
- type: 'branchByChance',
634
- value: [
635
- {
636
- label: 'a',
637
- weight: 0,
638
- actions: [
639
- {
640
- type: 'command',
641
- value: 'a = 2'
642
- }
643
- ]
644
- },
645
- {
646
- label: 'b',
647
- weight: 1,
648
- actions: [
649
- {
650
- type: 'command',
651
- value: 'a = 3'
652
- }
653
- ]
654
- }
655
- ]
656
- }
657
- ]
658
- }
659
- };
660
- let variables = { a: 1 };
661
- let functions = new DefaultFunctions();
662
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
663
- scriptEngine.start('test');
664
- scriptEngine.next();
665
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
666
- });
667
- (0, vitest_1.test)('branch by dynamic weight', () => {
668
- let scriptDefinitions = {
669
- test: {
670
- id: 'test',
671
- actions: [
672
- {
673
- type: 'branchByChance',
674
- value: [
675
- {
676
- label: 'a',
677
- weight: 'a - 1',
678
- actions: [
679
- {
680
- type: 'command',
681
- value: 'a = 2'
682
- }
683
- ]
684
- },
685
- {
686
- label: 'b',
687
- weight: 'a',
688
- actions: [
689
- {
690
- type: 'command',
691
- value: 'a = 3'
692
- }
693
- ]
694
- }
695
- ]
696
- }
697
- ]
698
- }
699
- };
700
- let variables = { a: 1 };
701
- let functions = new DefaultFunctions();
702
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
703
- scriptEngine.start('test');
704
- scriptEngine.next();
705
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
706
- });
707
- (0, vitest_1.test)('branch by valid conditioned chance', () => {
708
- let scriptDefinitions = {
709
- test: {
710
- id: 'test',
711
- actions: [
712
- {
713
- type: 'branchByChance',
714
- value: [
715
- {
716
- label: 'a',
717
- weight: 0,
718
- actions: [
719
- {
720
- type: 'command',
721
- value: 'a = 2'
722
- }
723
- ]
724
- },
725
- {
726
- label: 'b',
727
- weight: 1,
728
- condition: 'a === 1',
729
- actions: [
730
- {
731
- type: 'command',
732
- value: 'a = 3'
733
- }
734
- ]
735
- }
736
- ]
737
- }
738
- ]
739
- }
740
- };
741
- let variables = { a: 1 };
742
- let functions = new DefaultFunctions();
743
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
744
- scriptEngine.start('test');
745
- scriptEngine.next();
746
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
747
- });
748
- (0, vitest_1.test)('branch by no condition is met', () => {
749
- let scriptDefinitions = {
750
- test: {
751
- id: 'test',
752
- actions: [
753
- {
754
- type: 'branchByChance',
755
- value: [
756
- {
757
- label: 'a',
758
- weight: 1,
759
- condition: 'a === "invalid"',
760
- actions: [
761
- {
762
- type: 'command',
763
- value: 'a = 2'
764
- }
765
- ]
766
- },
767
- {
768
- label: 'b',
769
- weight: 1,
770
- condition: 'a === "invalid"',
771
- actions: [
772
- {
773
- type: 'command',
774
- value: 'a = 3'
775
- }
776
- ]
777
- }
778
- ]
779
- }
780
- ]
781
- }
782
- };
783
- let variables = { a: 1 };
784
- let functions = new DefaultFunctions();
785
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
786
- scriptEngine.start('test');
787
- (0, vitest_1.expect)(() => scriptEngine.next()).toThrowError(`ScriptEngine: No branch is fulfilled the condition in branchByChance action.`);
788
- });
789
- (0, vitest_1.test)('branch by valid false conditioned chance', () => {
790
- let scriptDefinitions = {
791
- test: {
792
- id: 'test',
793
- actions: [
794
- {
795
- type: 'branchByChance',
796
- value: [
797
- {
798
- label: 'a',
799
- weight: 0,
800
- actions: [
801
- {
802
- type: 'command',
803
- value: 'a = 2'
804
- }
805
- ]
806
- },
807
- {
808
- label: 'b',
809
- weight: 1,
810
- condition: 'a !== 1',
811
- actions: [
812
- {
813
- type: 'command',
814
- value: 'a = 3'
815
- }
816
- ]
817
- }
818
- ]
819
- }
820
- ]
821
- }
822
- };
823
- let variables = { a: 1 };
824
- let functions = new DefaultFunctions();
825
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
826
- scriptEngine.start('test');
827
- scriptEngine.next();
828
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(2);
829
- });
830
- (0, vitest_1.test)('branching while action stack is not empty', () => {
831
- let scriptDefinitions = {
832
- test: {
833
- id: 'test',
834
- actions: [
835
- {
836
- type: 'branchByChance',
837
- value: [
838
- {
839
- label: 'a',
840
- weight: 0,
841
- actions: [
842
- {
843
- type: 'command',
844
- value: 'a = 3'
845
- }
846
- ]
847
- },
848
- {
849
- label: 'b',
850
- weight: 1,
851
- actions: [
852
- {
853
- type: 'command',
854
- value: 'a = 2'
855
- }
856
- ]
857
- }
858
- ]
859
- },
860
- {
861
- type: 'command',
862
- value: 'a = 3'
863
- }
864
- ]
865
- }
866
- };
867
- let variables = { a: 1 };
868
- let functions = new DefaultFunctions();
869
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
870
- scriptEngine.start('test');
871
- (0, vitest_1.expect)(variables.a).toEqual(1);
872
- scriptEngine.next();
873
- (0, vitest_1.expect)(variables.a).toEqual(2);
874
- scriptEngine.next();
875
- (0, vitest_1.expect)(variables.a).toEqual(3);
876
- });
877
- });
878
- (0, vitest_1.describe)('Branch By Player Choice', () => {
879
- (0, vitest_1.test)('branch by valid choice', () => {
880
- let scriptDefinitions = {
881
- test: {
882
- id: 'test',
883
- actions: [
884
- {
885
- type: 'branchByPlayerChoice',
886
- value: [
887
- {
888
- text: 'a',
889
- actions: [
890
- {
891
- type: 'command',
892
- value: 'a = 2'
893
- }
894
- ]
895
- },
896
- {
897
- text: 'b',
898
- actions: [
899
- {
900
- type: 'command',
901
- value: 'a = 3'
902
- }
903
- ]
904
- }
905
- ]
906
- }
907
- ]
908
- }
909
- };
910
- let variables = { a: 1 };
911
- let playerChoicesCalledWith;
912
- class TestFunctions extends script_engine_1.ScriptEngineFunctions {
913
- onPlayerChoice(choices) {
914
- playerChoicesCalledWith = choices;
915
- }
916
- onDialog(text, speaker) { }
917
- }
918
- let functions = new TestFunctions();
919
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
920
- scriptEngine.start('test');
921
- scriptEngine.next();
922
- (0, vitest_1.expect)(playerChoicesCalledWith).toEqual(['a', 'b']);
923
- (0, vitest_1.expect)(scriptEngine.state).toEqual(script_engine_1.ScriptEngineState.waitingForPlayerChoice);
924
- scriptEngine.playerChoice(1);
925
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
926
- });
927
- (0, vitest_1.test)('branch by invalid choice', () => {
928
- let scriptDefinitions = {
929
- test: {
930
- id: 'test',
931
- actions: [
932
- {
933
- type: 'branchByPlayerChoice',
934
- value: [
935
- {
936
- text: 'a',
937
- actions: [
938
- {
939
- type: 'command',
940
- value: 'a = 2'
941
- }
942
- ]
943
- },
944
- {
945
- text: 'b',
946
- actions: [
947
- {
948
- type: 'command',
949
- value: 'a = 3'
950
- }
951
- ]
952
- }
953
- ]
954
- }
955
- ]
956
- }
957
- };
958
- let variables = { a: 1 };
959
- let playerChoicesCalledWith;
960
- class TestFunctions extends script_engine_1.ScriptEngineFunctions {
961
- onPlayerChoice(choices) {
962
- playerChoicesCalledWith = choices;
963
- }
964
- onDialog(text, speaker) { }
965
- }
966
- let functions = new TestFunctions();
967
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
968
- scriptEngine.start('test');
969
- scriptEngine.next();
970
- (0, vitest_1.expect)(playerChoicesCalledWith).toEqual(['a', 'b']);
971
- (0, vitest_1.expect)(() => scriptEngine.playerChoice(3)).toThrowError(`ScriptEngine: Invalid player choice.`);
972
- });
973
- (0, vitest_1.test)('branch by no condition is met', () => {
974
- let scriptDefinitions = {
975
- test: {
976
- id: 'test',
977
- actions: [
978
- {
979
- type: 'branchByPlayerChoice',
980
- value: [
981
- {
982
- text: 'a',
983
- condition: 'a === "invalid"',
984
- actions: [
985
- {
986
- type: 'command',
987
- value: 'a = 2'
988
- }
989
- ]
990
- },
991
- {
992
- text: 'b',
993
- condition: 'a === "invalid"',
994
- actions: [
995
- {
996
- type: 'command',
997
- value: 'a = 3'
998
- }
999
- ]
1000
- }
1001
- ]
1002
- }
1003
- ]
1004
- }
1005
- };
1006
- let variables = { a: 1 };
1007
- let playerChoicesCalledWith;
1008
- class TestFunctions extends script_engine_1.ScriptEngineFunctions {
1009
- onPlayerChoice(choices) {
1010
- playerChoicesCalledWith = choices;
1011
- }
1012
- onDialog(text, speaker) { }
1013
- }
1014
- let functions = new TestFunctions();
1015
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
1016
- scriptEngine.start('test');
1017
- (0, vitest_1.expect)(() => scriptEngine.next()).toThrowError(`ScriptEngine: No branch is fulfilled the condition in branchByPlayerChoice action.`);
1018
- (0, vitest_1.expect)(playerChoicesCalledWith).toEqual(undefined);
1019
- });
1020
- (0, vitest_1.test)('choose player option while no player choice branch is active should throw error', () => {
1021
- let scriptDefinitions = {
1022
- test: {
1023
- id: 'test',
1024
- actions: [
1025
- {
1026
- type: 'command',
1027
- value: 'a = 2'
1028
- }
1029
- ]
1030
- }
1031
- };
1032
- let variables = { a: 1 };
1033
- let functions = new DefaultFunctions();
1034
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
1035
- scriptEngine.start('test');
1036
- (0, vitest_1.expect)(() => scriptEngine.playerChoice(0)).toThrowError(`ScriptEngine: No player branching choices available.`);
1037
- });
1038
- (0, vitest_1.test)('calling next while waiting for player option should throw error', () => {
1039
- let scriptDefinitions = {
1040
- test: {
1041
- id: 'test',
1042
- actions: [
1043
- {
1044
- type: 'branchByPlayerChoice',
1045
- value: [
1046
- {
1047
- text: 'a',
1048
- actions: [
1049
- {
1050
- type: 'command',
1051
- value: 'a = 2'
1052
- }
1053
- ]
1054
- },
1055
- {
1056
- text: 'b',
1057
- actions: [
1058
- {
1059
- type: 'command',
1060
- value: 'a = 3'
1061
- }
1062
- ]
1063
- }
1064
- ]
1065
- },
1066
- {
1067
- type: 'command',
1068
- value: 'a = 3'
1069
- }
1070
- ]
1071
- }
1072
- };
1073
- let variables = { a: 1 };
1074
- let playerChoicesCalledWith;
1075
- class TestFunctions extends script_engine_1.ScriptEngineFunctions {
1076
- onPlayerChoice(choices) {
1077
- playerChoicesCalledWith = choices;
1078
- }
1079
- onDialog(text, speaker) { }
1080
- }
1081
- let functions = new TestFunctions();
1082
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
1083
- scriptEngine.start('test');
1084
- scriptEngine.next();
1085
- (0, vitest_1.expect)(playerChoicesCalledWith).toEqual(['a', 'b']);
1086
- (0, vitest_1.expect)(scriptEngine.state).toEqual(script_engine_1.ScriptEngineState.waitingForPlayerChoice);
1087
- (0, vitest_1.expect)(() => scriptEngine.next()).toThrow('ScriptEngine: The engine is not in running state. Next action cannot be executed. State: waitingForPlayerChoice');
1088
- });
1089
- (0, vitest_1.test)('branching while action stack is not empty', () => {
1090
- let scriptDefinitions = {
1091
- test: {
1092
- id: 'test',
1093
- actions: [
1094
- {
1095
- type: 'branchByPlayerChoice',
1096
- value: [
1097
- {
1098
- text: 'a',
1099
- actions: [
1100
- {
1101
- type: 'command',
1102
- value: 'a = 2'
1103
- }
1104
- ]
1105
- },
1106
- {
1107
- text: 'b',
1108
- actions: [
1109
- {
1110
- type: 'command',
1111
- value: 'a = 3'
1112
- }
1113
- ]
1114
- }
1115
- ]
1116
- },
1117
- {
1118
- type: 'command',
1119
- value: 'a = 3'
1120
- }
1121
- ]
1122
- }
1123
- };
1124
- let variables = { a: 1 };
1125
- let functions = new DefaultFunctions();
1126
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
1127
- scriptEngine.start('test');
1128
- (0, vitest_1.expect)(variables.a).toEqual(1);
1129
- scriptEngine.next();
1130
- (0, vitest_1.expect)(scriptEngine.state).toEqual(script_engine_1.ScriptEngineState.waitingForPlayerChoice);
1131
- scriptEngine.playerChoice(0);
1132
- (0, vitest_1.expect)(variables.a).toEqual(2);
1133
- scriptEngine.next();
1134
- (0, vitest_1.expect)(variables.a).toEqual(3);
1135
- });
1136
- (0, vitest_1.test)('branch by player choice with string evaluation', () => {
1137
- let playerChoicesCalledWith = [];
1138
- class TestFunctions extends script_engine_1.ScriptEngineFunctions {
1139
- onPlayerChoice(choices) {
1140
- playerChoicesCalledWith = [...choices];
1141
- }
1142
- onDialog(text, speaker) { }
1143
- }
1144
- let scriptDefinitions = {
1145
- test: {
1146
- id: 'test',
1147
- actions: [
1148
- {
1149
- type: 'branchByPlayerChoice',
1150
- value: [
1151
- {
1152
- text: '${greet("Player")}',
1153
- actions: [
1154
- {
1155
- type: 'command',
1156
- value: 'a = 2'
1157
- }
1158
- ]
1159
- },
1160
- {
1161
- text: '${greet("Friend")}',
1162
- actions: [
1163
- {
1164
- type: 'command',
1165
- value: 'a = 3'
1166
- }
1167
- ]
1168
- }
1169
- ]
1170
- }
1171
- ]
1172
- }
1173
- };
1174
- let variables = { a: 1 };
1175
- let functions = new TestFunctions();
1176
- class Functions extends TestFunctions {
1177
- greet(name) {
1178
- return `Hello, ${name}!`;
1179
- }
1180
- }
1181
- __decorate([
1182
- (0, helpers_lib_1.JSEngineFunction)(),
1183
- __metadata("design:type", Function),
1184
- __metadata("design:paramtypes", [String]),
1185
- __metadata("design:returntype", String)
1186
- ], Functions.prototype, "greet", null);
1187
- functions = new Functions();
1188
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables);
1189
- scriptEngine.start('test');
1190
- scriptEngine.next();
1191
- (0, vitest_1.expect)(playerChoicesCalledWith).toEqual(['Hello, Player!', 'Hello, Friend!']);
1192
- (0, vitest_1.expect)(scriptEngine.state).toEqual(script_engine_1.ScriptEngineState.waitingForPlayerChoice);
1193
- scriptEngine.playerChoice(0);
1194
- (0, vitest_1.expect)(variables.a).toEqual(2);
1195
- });
1196
- });
1197
- (0, vitest_1.describe)('Manual Testing Mode', () => {
1198
- (0, vitest_1.test)('branch by valid chance', () => {
1199
- let scriptDefinitions = {
1200
- test: {
1201
- id: 'test',
1202
- actions: [
1203
- {
1204
- type: 'branchByChance',
1205
- value: [
1206
- {
1207
- label: 'a',
1208
- weight: 1,
1209
- actions: [
1210
- {
1211
- type: 'command',
1212
- value: 'a = 2'
1213
- }
1214
- ]
1215
- },
1216
- {
1217
- label: 'b',
1218
- weight: 1,
1219
- actions: [
1220
- {
1221
- type: 'command',
1222
- value: 'a = 3'
1223
- }
1224
- ]
1225
- }
1226
- ]
1227
- }
1228
- ]
1229
- }
1230
- };
1231
- let variables = { a: 1 };
1232
- let playerChoicesCalledWith;
1233
- class TestFunctions extends script_engine_1.ScriptEngineFunctions {
1234
- onPlayerChoice(choices) {
1235
- playerChoicesCalledWith = choices;
1236
- }
1237
- onDialog(text, speaker) { }
1238
- }
1239
- let functions = new TestFunctions();
1240
- let scriptEngine = new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables, { manualTestingMode: true });
1241
- scriptEngine.start('test');
1242
- scriptEngine.next();
1243
- (0, vitest_1.expect)(playerChoicesCalledWith).toEqual(['a', 'b']);
1244
- scriptEngine.playerChoice(1);
1245
- (0, vitest_1.expect)(scriptEngine.variables.a).toEqual(3);
1246
- });
1247
- });
1248
- (0, vitest_1.describe)('Edge Cases', () => {
1249
- (0, vitest_1.test)('onPlayerChoice shall not be decorated as JSEngineFunction', () => {
1250
- let scriptDefinitions = {
1251
- test: {
1252
- id: 'test',
1253
- actions: [
1254
- {
1255
- type: 'command',
1256
- value: 'a = foo(2)'
1257
- }
1258
- ]
1259
- }
1260
- };
1261
- let variables = {};
1262
- class Functions {
1263
- onPlayerChoice(choices) { }
1264
- onDialog(text, speaker) { }
1265
- }
1266
- __decorate([
1267
- (0, helpers_lib_1.JSEngineFunction)(),
1268
- __metadata("design:type", Function),
1269
- __metadata("design:paramtypes", [Array]),
1270
- __metadata("design:returntype", void 0)
1271
- ], Functions.prototype, "onPlayerChoice", null);
1272
- let functions = new Functions();
1273
- (0, vitest_1.expect)(() => new script_engine_1.ScriptEngine(scriptDefinitions, functions, variables)).toThrow('ScriptEngine: onPlayerChoice function shall not be decorated as a JSEngineFunction.');
1274
- });
1275
- });
1276
- });
1277
- //# sourceMappingURL=script-engine.test.js.map