script-engine-lib 0.4.2 → 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 (94) 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 -47
  6. package/src/index.ts +10 -0
  7. package/.eslintrc.js +0 -212
  8. package/.vscode/launch.json +0 -15
  9. package/.vscode/settings.json +0 -15
  10. package/dist/decorators/action-before-testing.d.ts +0 -2
  11. package/dist/decorators/action-before-testing.js +0 -23
  12. package/dist/decorators/action-before-testing.js.map +0 -1
  13. package/dist/decorators/action-before-testing.test.d.ts +0 -1
  14. package/dist/decorators/action-before-testing.test.js +0 -82
  15. package/dist/decorators/action-before-testing.test.js.map +0 -1
  16. package/dist/engine/script-engine.d.ts +0 -35
  17. package/dist/engine/script-engine.js +0 -175
  18. package/dist/engine/script-engine.js.map +0 -1
  19. package/dist/engine/script-engine.test.d.ts +0 -1
  20. package/dist/engine/script-engine.test.js +0 -1277
  21. package/dist/engine/script-engine.test.js.map +0 -1
  22. package/dist/engine/story-engine.d.ts +0 -35
  23. package/dist/engine/story-engine.js +0 -175
  24. package/dist/engine/story-engine.js.map +0 -1
  25. package/dist/engine/story-engine.test.d.ts +0 -1
  26. package/dist/engine/story-engine.test.js +0 -1277
  27. package/dist/engine/story-engine.test.js.map +0 -1
  28. package/dist/index.js.map +0 -1
  29. package/dist/interfaces.d.ts +0 -51
  30. package/dist/interfaces.js +0 -27
  31. package/dist/interfaces.js.map +0 -1
  32. package/dist/simulator/decorators.d.ts +0 -2
  33. package/dist/simulator/decorators.js +0 -23
  34. package/dist/simulator/decorators.js.map +0 -1
  35. package/dist/simulator/decorators.test.d.ts +0 -1
  36. package/dist/simulator/decorators.test.js +0 -81
  37. package/dist/simulator/decorators.test.js.map +0 -1
  38. package/dist/simulator/event-branches-map.d.ts +0 -16
  39. package/dist/simulator/event-branches-map.js +0 -114
  40. package/dist/simulator/event-branches-map.js.map +0 -1
  41. package/dist/simulator/event-branches-map.test.d.ts +0 -1
  42. package/dist/simulator/event-branches-map.test.js +0 -120
  43. package/dist/simulator/event-branches-map.test.js.map +0 -1
  44. package/dist/simulator/event-test-simulator.d.ts +0 -42
  45. package/dist/simulator/event-test-simulator.js +0 -373
  46. package/dist/simulator/event-test-simulator.js.map +0 -1
  47. package/dist/simulator/event-test-simulator.test.d.ts +0 -1
  48. package/dist/simulator/event-test-simulator.test.js +0 -1960
  49. package/dist/simulator/event-test-simulator.test.js.map +0 -1
  50. package/dist/simulator/execution-history.d.ts +0 -6
  51. package/dist/simulator/execution-history.js +0 -22
  52. package/dist/simulator/execution-history.js.map +0 -1
  53. package/dist/simulator/helpers/event-branches-map.d.ts +0 -16
  54. package/dist/simulator/helpers/event-branches-map.js +0 -114
  55. package/dist/simulator/helpers/event-branches-map.js.map +0 -1
  56. package/dist/simulator/helpers/event-branches-map.test.d.ts +0 -1
  57. package/dist/simulator/helpers/event-branches-map.test.js +0 -120
  58. package/dist/simulator/helpers/event-branches-map.test.js.map +0 -1
  59. package/dist/simulator/helpers/execution-history.d.ts +0 -6
  60. package/dist/simulator/helpers/execution-history.js +0 -22
  61. package/dist/simulator/helpers/execution-history.js.map +0 -1
  62. package/dist/simulator/helpers/script-branches-map.d.ts +0 -16
  63. package/dist/simulator/helpers/script-branches-map.js +0 -114
  64. package/dist/simulator/helpers/script-branches-map.js.map +0 -1
  65. package/dist/simulator/helpers/script-branches-map.test.d.ts +0 -1
  66. package/dist/simulator/helpers/script-branches-map.test.js +0 -120
  67. package/dist/simulator/helpers/script-branches-map.test.js.map +0 -1
  68. package/dist/simulator/script-test-simulator.d.ts +0 -42
  69. package/dist/simulator/script-test-simulator.js +0 -373
  70. package/dist/simulator/script-test-simulator.js.map +0 -1
  71. package/dist/simulator/script-test-simulator.test.d.ts +0 -1
  72. package/dist/simulator/script-test-simulator.test.js +0 -2013
  73. package/dist/simulator/script-test-simulator.test.js.map +0 -1
  74. package/dist/structured-text/index.d.ts +0 -9
  75. package/dist/structured-text/index.js +0 -8
  76. package/dist/structured-text/index.js.map +0 -1
  77. package/dist/structured-text/rich-text-separator.d.ts +0 -48
  78. package/dist/structured-text/rich-text-separator.js +0 -146
  79. package/dist/structured-text/rich-text-separator.js.map +0 -1
  80. package/dist/structured-text/rich-text-separator.test.d.ts +0 -1
  81. package/dist/structured-text/rich-text-separator.test.js +0 -166
  82. package/dist/structured-text/rich-text-separator.test.js.map +0 -1
  83. package/dist/structured-text/rich-text-validator.d.ts +0 -19
  84. package/dist/structured-text/rich-text-validator.js +0 -73
  85. package/dist/structured-text/rich-text-validator.js.map +0 -1
  86. package/dist/structured-text/rich-text-validator.test.d.ts +0 -1
  87. package/dist/structured-text/rich-text-validator.test.js +0 -59
  88. package/dist/structured-text/rich-text-validator.test.js.map +0 -1
  89. package/dist/test.spec.js.map +0 -1
  90. package/dist/test.test.d.ts +0 -1
  91. package/dist/test.test.js +0 -9
  92. package/dist/test.test.js.map +0 -1
  93. package/tsconfig.json +0 -24
  94. package/vitest.config.ts +0 -1
@@ -1,2013 +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
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- var __importDefault = (this && this.__importDefault) || function (mod) {
15
- return (mod && mod.__esModule) ? mod : { "default": mod };
16
- };
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- const helpers_lib_1 = require("helpers-lib");
19
- const joi_1 = __importDefault(require("joi"));
20
- const vitest_1 = require("vitest");
21
- const interfaces_1 = require("../interfaces");
22
- const script_test_simulator_1 = require("./script-test-simulator");
23
- let SampleFunctions = class SampleFunctions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
24
- onPlayerChoice(choices) { }
25
- onDialog(text, speaker) { }
26
- onEnd() { }
27
- onScriptBranchingEnd() { }
28
- gameOver() { }
29
- foo(a) { }
30
- };
31
- __decorate([
32
- (0, helpers_lib_1.JSEngineFunction)(),
33
- __metadata("design:type", Function),
34
- __metadata("design:paramtypes", []),
35
- __metadata("design:returntype", void 0)
36
- ], SampleFunctions.prototype, "gameOver", null);
37
- __decorate([
38
- (0, helpers_lib_1.JSEngineFunction)(),
39
- __param(0, (0, helpers_lib_1.Schema)(joi_1.default.string().required())),
40
- __metadata("design:type", Function),
41
- __metadata("design:paramtypes", [String]),
42
- __metadata("design:returntype", void 0)
43
- ], SampleFunctions.prototype, "foo", null);
44
- SampleFunctions = __decorate([
45
- (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
46
- ], SampleFunctions);
47
- (0, vitest_1.describe)('ScriptTestSimulator', () => {
48
- let functions;
49
- (0, vitest_1.beforeEach)(() => {
50
- functions = new SampleFunctions();
51
- });
52
- (0, vitest_1.describe)('Visiting Branches', () => {
53
- (0, vitest_1.test)('sample 1 - visiting base script', () => {
54
- let scripts = [{ id: 'scriptTest', actions: [{ type: 'command', value: 'gameOver()' }] }];
55
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
56
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
57
- simulator.simulateScript(scripts[0], jsEngine, []);
58
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
59
- });
60
- (0, vitest_1.test)('sample 2 - visiting branch by condition', () => {
61
- let scripts = [
62
- {
63
- id: 'scriptTest',
64
- actions: [
65
- {
66
- type: 'branchByCondition',
67
- value: [
68
- {
69
- actions: [{ type: 'command', value: 'gameOver()' }]
70
- }
71
- ]
72
- }
73
- ]
74
- }
75
- ];
76
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
77
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
78
- simulator.simulateScript(scripts[0], jsEngine, []);
79
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
80
- });
81
- (0, vitest_1.test)('sample 3 - visiting branch by chance', () => {
82
- let scripts = [
83
- {
84
- id: 'scriptTest',
85
- actions: [
86
- {
87
- type: 'branchByChance',
88
- value: [
89
- {
90
- label: 'a',
91
- weight: 1,
92
- actions: [{ type: 'command', value: 'gameOver()' }]
93
- }
94
- ]
95
- }
96
- ]
97
- }
98
- ];
99
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
100
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
101
- simulator.simulateScript(scripts[0], jsEngine, []);
102
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
103
- });
104
- (0, vitest_1.test)('sample 4 - visiting branch by player choice', () => {
105
- let scripts = [
106
- {
107
- id: 'scriptTest',
108
- actions: [
109
- {
110
- type: 'branchByPlayerChoice',
111
- value: [
112
- {
113
- text: 'sample text',
114
- actions: [{ type: 'command', value: 'gameOver()' }]
115
- }
116
- ]
117
- }
118
- ]
119
- }
120
- ];
121
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
122
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
123
- simulator.simulateScript(scripts[0], jsEngine, []);
124
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
125
- });
126
- (0, vitest_1.test)('sample 5 - jump to script', () => {
127
- let scripts = [
128
- {
129
- id: 'scriptTest',
130
- actions: [
131
- {
132
- type: 'jumpTo',
133
- value: 'targetScript'
134
- },
135
- {
136
- type: 'command',
137
- value: 'a = 1'
138
- }
139
- ]
140
- },
141
- {
142
- id: 'targetScript',
143
- actions: [{ type: 'command', value: 'gameOver()' }]
144
- }
145
- ];
146
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
147
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
148
- simulator.simulateScript(scripts[0], jsEngine, []);
149
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
150
- });
151
- });
152
- (0, vitest_1.describe)('Actions Before Testing', () => {
153
- (0, vitest_1.test)('sample 1 - executing commands', () => {
154
- let actionsBeforeTesting = [{ type: 'command', value: 'a = true' }];
155
- let scripts = [
156
- {
157
- id: 'scriptTest',
158
- actions: [
159
- {
160
- type: 'branchByCondition',
161
- value: [
162
- {
163
- condition: 'a === true',
164
- actions: [{ type: 'command', value: 'gameOver()' }]
165
- }
166
- ]
167
- }
168
- ]
169
- }
170
- ];
171
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
172
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
173
- simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting);
174
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
175
- });
176
- (0, vitest_1.test)('sample 2 - executing commands in all branches', () => {
177
- let actionsBeforeTesting = [
178
- { type: 'runScript', value: 'startingScript' },
179
- { type: 'command', value: 'a *= a' }
180
- ];
181
- let scripts = [
182
- {
183
- id: 'startingScript',
184
- actions: [
185
- {
186
- type: 'branchByChance',
187
- value: [
188
- {
189
- label: 'a',
190
- weight: 1,
191
- actions: [{ type: 'command', value: 'a = 1' }]
192
- },
193
- {
194
- label: 'b',
195
- weight: 1,
196
- actions: [{ type: 'command', value: 'a = 2' }]
197
- }
198
- ]
199
- }
200
- ]
201
- },
202
- {
203
- id: 'scriptTest',
204
- actions: [
205
- {
206
- type: 'branchByCondition',
207
- value: [
208
- {
209
- condition: 'a === 1',
210
- actions: [{ type: 'command', value: 'gameOver()' }]
211
- },
212
- {
213
- condition: 'a === 4',
214
- actions: [{ type: 'command', value: 'gameOver()' }]
215
- }
216
- ]
217
- }
218
- ]
219
- }
220
- ];
221
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
222
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
223
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
224
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[0]);
225
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[1]);
226
- simulator.simulateScript(scripts[1], jsEngine, actionsBeforeTesting);
227
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
228
- });
229
- (0, vitest_1.test)('sample 3 - running another script before', () => {
230
- let actionsBeforeTesting = [{ type: 'runScript', value: 'startingScript' }];
231
- let scripts = [
232
- { id: 'startingScript', actions: [{ type: 'command', value: 'a = true' }] },
233
- {
234
- id: 'scriptTest',
235
- actions: [
236
- {
237
- type: 'branchByCondition',
238
- value: [
239
- {
240
- condition: 'a === true',
241
- actions: [{ type: 'command', value: 'gameOver()' }]
242
- }
243
- ]
244
- }
245
- ]
246
- }
247
- ];
248
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
249
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
250
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
251
- simulator.simulateScript(scripts[1], jsEngine, actionsBeforeTesting);
252
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
253
- });
254
- (0, vitest_1.test)('sample 4 - running another script before that has multiple branching', () => {
255
- let actionsBeforeTesting = [{ type: 'runScript', value: 'startingScript' }];
256
- let scripts = [
257
- {
258
- id: 'startingScript',
259
- actions: [
260
- {
261
- type: 'branchByChance',
262
- value: [
263
- {
264
- label: 'a',
265
- weight: 1,
266
- actions: [{ type: 'command', value: 'a = true' }]
267
- },
268
- {
269
- label: 'b',
270
- weight: 1,
271
- actions: [{ type: 'command', value: 'b = true' }]
272
- }
273
- ]
274
- }
275
- ]
276
- },
277
- {
278
- id: 'scriptTest',
279
- actions: [
280
- {
281
- type: 'branchByCondition',
282
- value: [
283
- {
284
- condition: 'a === true',
285
- actions: [{ type: 'command', value: 'gameOver()' }]
286
- },
287
- {
288
- condition: 'b === true',
289
- actions: [{ type: 'command', value: 'gameOver()' }]
290
- }
291
- ]
292
- }
293
- ]
294
- }
295
- ];
296
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
297
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
298
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
299
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[0]);
300
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[1]);
301
- simulator.simulateScript(scripts[1], jsEngine, actionsBeforeTesting);
302
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
303
- });
304
- });
305
- (0, vitest_1.describe)('Returning Back From Branching Cases', () => {
306
- (0, vitest_1.test)('branchByCondition', () => {
307
- let scripts = [
308
- {
309
- id: 'scriptTest',
310
- actions: [
311
- {
312
- type: 'branchByCondition',
313
- value: [
314
- {
315
- condition: 'a !== true',
316
- actions: [{ type: 'command', value: 'gameOver()' }]
317
- }
318
- ]
319
- },
320
- { type: 'command', value: 'gameOver()' }
321
- ]
322
- }
323
- ];
324
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
325
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
326
- simulator.simulateScript(scripts[0], jsEngine, []);
327
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
328
- });
329
- (0, vitest_1.test)('branchByChance', () => {
330
- let scripts = [
331
- {
332
- id: 'scriptTest',
333
- actions: [
334
- {
335
- type: 'branchByChance',
336
- value: [
337
- {
338
- label: 'a',
339
- weight: 1,
340
- actions: [{ type: 'command', value: 'gameOver()' }]
341
- }
342
- ]
343
- },
344
- { type: 'command', value: 'gameOver()' }
345
- ]
346
- }
347
- ];
348
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
349
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
350
- simulator.simulateScript(scripts[0], jsEngine, []);
351
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
352
- });
353
- (0, vitest_1.test)('branchByPlayerChoice', () => {
354
- let scripts = [
355
- {
356
- id: 'scriptTest',
357
- actions: [
358
- {
359
- type: 'branchByPlayerChoice',
360
- value: [
361
- {
362
- text: 'sample text',
363
- actions: [{ type: 'command', value: 'gameOver()' }]
364
- }
365
- ]
366
- },
367
- { type: 'command', value: 'gameOver()' }
368
- ]
369
- }
370
- ];
371
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
372
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
373
- simulator.simulateScript(scripts[0], jsEngine, []);
374
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
375
- });
376
- });
377
- (0, vitest_1.describe)('Looping Scripts', () => {
378
- (0, vitest_1.test)('sample 1 - script that loops to itself without errors', () => {
379
- let scripts = [
380
- {
381
- id: 'startingScript',
382
- actions: [
383
- {
384
- type: 'branchByPlayerChoice',
385
- value: [
386
- { text: 'option 1', actions: [{ type: 'jumpTo', value: 'startingScript' }] },
387
- { text: 'option 2', actions: [{ type: 'command', value: 'gameOver()' }] }
388
- ]
389
- }
390
- ]
391
- }
392
- ];
393
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
394
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
395
- simulator.simulateScript(scripts[0], jsEngine, []);
396
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
397
- });
398
- (0, vitest_1.test)('sample 2 - scripts that loops to each other without errors', () => {
399
- let scripts = [
400
- {
401
- id: 'script1',
402
- actions: [
403
- {
404
- type: 'branchByPlayerChoice',
405
- value: [
406
- { text: 'option 1', actions: [{ type: 'jumpTo', value: 'script2' }] },
407
- { text: 'option 2', actions: [{ type: 'command', value: 'gameOver()' }] }
408
- ]
409
- }
410
- ]
411
- },
412
- {
413
- id: 'script2',
414
- actions: [
415
- {
416
- type: 'jumpTo',
417
- value: 'script1'
418
- }
419
- ]
420
- }
421
- ];
422
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
423
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
424
- simulator.simulateScript(scripts[0], jsEngine, []);
425
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
426
- });
427
- (0, vitest_1.test)('sample 3 - script that loops on actions before testing without errors', () => {
428
- let actionsBeforeTesting = [{ type: 'runScript', value: 'startingScript' }];
429
- let scripts = [
430
- {
431
- id: 'startingScript',
432
- actions: [
433
- {
434
- type: 'branchByPlayerChoice',
435
- value: [
436
- { text: 'option 1', actions: [{ type: 'jumpTo', value: 'startingScript' }] },
437
- { text: 'option 2', actions: [{ type: 'command', value: 'gameOver()' }] }
438
- ]
439
- }
440
- ]
441
- },
442
- { id: 'scriptTest', actions: [{ type: 'command', value: 'gameOver()' }] }
443
- ];
444
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
445
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
446
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
447
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[0]);
448
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[1]);
449
- simulator.simulateScript(scripts[1], jsEngine, actionsBeforeTesting);
450
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
451
- });
452
- (0, vitest_1.test)('sample 4 - script that loops to itself and getting stuck without exiting option', () => {
453
- let scripts = [
454
- {
455
- id: 'startingScript',
456
- actions: [
457
- {
458
- type: 'branchByPlayerChoice',
459
- value: [{ text: 'option 1', actions: [{ type: 'jumpTo', value: 'startingScript' }] }]
460
- }
461
- ]
462
- }
463
- ];
464
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
465
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
466
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrow('All possible paths are creating infinite loops, no exit found while executing: startingScript');
467
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
468
- });
469
- (0, vitest_1.test)('sample 5 - script that loops to each other and getting stuck without exiting option', () => {
470
- let scripts = [
471
- {
472
- id: 'script1',
473
- actions: [
474
- {
475
- type: 'branchByPlayerChoice',
476
- value: [{ text: 'option 1', actions: [{ type: 'jumpTo', value: 'script2' }] }]
477
- }
478
- ]
479
- },
480
- {
481
- id: 'script2',
482
- actions: [
483
- {
484
- type: 'jumpTo',
485
- value: 'script1'
486
- }
487
- ]
488
- }
489
- ];
490
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
491
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
492
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrow('All possible paths are creating infinite loops, no exit found while executing: script1');
493
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
494
- });
495
- (0, vitest_1.test)('sample 6 - script that loops before testing and getting stuck without exiting option', () => {
496
- let actionsBeforeTesting = [{ type: 'runScript', value: 'startingScript' }];
497
- let scripts = [
498
- {
499
- id: 'startingScript',
500
- actions: [
501
- {
502
- type: 'branchByPlayerChoice',
503
- value: [{ text: 'option 1', actions: [{ type: 'jumpTo', value: 'startingScript' }] }]
504
- }
505
- ]
506
- },
507
- { id: 'scriptTest', actions: [{ type: 'command', value: 'gameOver()' }] }
508
- ];
509
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
510
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
511
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
512
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[0]);
513
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[1]);
514
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[1], jsEngine, actionsBeforeTesting)).toThrow('All possible paths are creating infinite loops, no exit found while executing: startingScript');
515
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
516
- {
517
- scriptId: 'scriptTest',
518
- path: []
519
- }
520
- ]);
521
- });
522
- (0, vitest_1.test)('sample 7 - script that loops to itself with trivial state change', () => {
523
- let actionsBeforeTesting = [{ type: 'command', value: 'a = 0' }];
524
- let scripts = [
525
- {
526
- id: 'startingScript',
527
- actions: [
528
- {
529
- type: 'branchByPlayerChoice',
530
- value: [
531
- { text: 'option 1', actions: [{ type: 'command', value: 'gameOver()' }] },
532
- {
533
- text: 'option 2',
534
- actions: [
535
- { type: 'command', value: 'a++' },
536
- { type: 'jumpTo', value: 'startingScript' }
537
- ]
538
- }
539
- ]
540
- }
541
- ]
542
- }
543
- ];
544
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
545
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
546
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting)).toThrow('Maximum depth "100" is reached. Try to reduce the script branching tree or check infinite loops with trivial state changes.');
547
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
548
- });
549
- (0, vitest_1.test)('sample 8 - scripts that loops to each other with trivial state change', () => {
550
- let actionsBeforeTesting = [{ type: 'command', value: 'a = 0' }];
551
- let scripts = [
552
- {
553
- id: 'script1',
554
- actions: [
555
- {
556
- type: 'branchByPlayerChoice',
557
- value: [
558
- { text: 'option 1', actions: [{ type: 'command', value: 'gameOver()' }] },
559
- {
560
- text: 'option 2',
561
- actions: [
562
- { type: 'command', value: 'a++' },
563
- { type: 'jumpTo', value: 'script2' }
564
- ]
565
- }
566
- ]
567
- }
568
- ]
569
- },
570
- {
571
- id: 'script2',
572
- actions: [
573
- {
574
- type: 'jumpTo',
575
- value: 'script1'
576
- }
577
- ]
578
- }
579
- ];
580
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
581
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
582
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting)).toThrow('Maximum depth "100" is reached. Try to reduce the script branching tree or check infinite loops with trivial state changes.');
583
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
584
- });
585
- (0, vitest_1.test)('sample 9 - script that loops to itself with trivial state change', () => {
586
- let actionsBeforeTesting = [
587
- { type: 'command', value: 'a = 0' },
588
- { type: 'runScript', value: 'startingScript' }
589
- ];
590
- let scripts = [
591
- {
592
- id: 'startingScript',
593
- actions: [
594
- {
595
- type: 'branchByPlayerChoice',
596
- value: [
597
- {
598
- text: 'option 1',
599
- actions: [
600
- { type: 'command', value: 'a++' },
601
- { type: 'jumpTo', value: 'startingScript' }
602
- ]
603
- },
604
- { text: 'option 2', actions: [{ type: 'command', value: 'gameOver()' }] }
605
- ]
606
- }
607
- ]
608
- },
609
- { id: 'scriptTest', actions: [{ type: 'command', value: 'gameOver()' }] }
610
- ];
611
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
612
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
613
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
614
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[0]);
615
- simulator['scriptBranchesMap'].setVisited(scripts[0].actions[0].value[1]);
616
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[1], jsEngine, actionsBeforeTesting)).toThrow('Maximum depth "100" is reached. Try to reduce the script branching tree or check infinite loops with trivial state changes.');
617
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
618
- {
619
- scriptId: 'scriptTest',
620
- path: []
621
- }
622
- ]);
623
- });
624
- });
625
- (0, vitest_1.describe)('On Player Choice', () => {
626
- (0, vitest_1.test)('sample 1 - callback call', () => {
627
- let Functions = class Functions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
628
- constructor() {
629
- super(...arguments);
630
- this.onPlayerChoiceCalledWith = [];
631
- }
632
- onPlayerChoice(choices) {
633
- this.onPlayerChoiceCalledWith = choices;
634
- }
635
- onDialog(text, speaker) { }
636
- onEnd() { }
637
- onScriptBranchingEnd() { }
638
- gameOver() { }
639
- foo(a) { }
640
- };
641
- __decorate([
642
- (0, helpers_lib_1.JSEngineFunction)(),
643
- __metadata("design:type", Function),
644
- __metadata("design:paramtypes", []),
645
- __metadata("design:returntype", void 0)
646
- ], Functions.prototype, "gameOver", null);
647
- __decorate([
648
- (0, helpers_lib_1.JSEngineFunction)(),
649
- __param(0, (0, helpers_lib_1.Schema)(joi_1.default.string().required())),
650
- __metadata("design:type", Function),
651
- __metadata("design:paramtypes", [String]),
652
- __metadata("design:returntype", void 0)
653
- ], Functions.prototype, "foo", null);
654
- Functions = __decorate([
655
- (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
656
- ], Functions);
657
- let scripts = [
658
- {
659
- id: 'scriptTest',
660
- actions: [
661
- {
662
- type: 'branchByPlayerChoice',
663
- value: [
664
- {
665
- text: 'sample text',
666
- actions: [{ type: 'command', value: 'gameOver()' }]
667
- }
668
- ]
669
- }
670
- ]
671
- }
672
- ];
673
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
674
- let jsEngine = new helpers_lib_1.JSEngine(new Functions(), {});
675
- simulator.simulateScript(scripts[0], jsEngine, []);
676
- (0, vitest_1.expect)(jsEngine.functions.onPlayerChoiceCalledWith).toEqual(['sample text']);
677
- });
678
- (0, vitest_1.test)('sample 2 - branch text with variable', () => {
679
- let Functions = class Functions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
680
- constructor() {
681
- super(...arguments);
682
- this.onPlayerChoiceCalledWith = [];
683
- }
684
- onPlayerChoice(choices) {
685
- this.onPlayerChoiceCalledWith = choices;
686
- }
687
- onDialog(text, speaker) { }
688
- onEnd() { }
689
- onScriptBranchingEnd() { }
690
- gameOver() { }
691
- };
692
- __decorate([
693
- (0, helpers_lib_1.JSEngineFunction)(),
694
- __metadata("design:type", Function),
695
- __metadata("design:paramtypes", []),
696
- __metadata("design:returntype", void 0)
697
- ], Functions.prototype, "gameOver", null);
698
- Functions = __decorate([
699
- (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
700
- ], Functions);
701
- let scripts = [
702
- {
703
- id: 'scriptTest',
704
- actions: [
705
- {
706
- type: 'branchByPlayerChoice',
707
- value: [
708
- {
709
- text: '${message}',
710
- actions: [{ type: 'command', value: 'gameOver()' }]
711
- }
712
- ]
713
- }
714
- ]
715
- }
716
- ];
717
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
718
- let jsEngine = new helpers_lib_1.JSEngine(new Functions(), { message: 'Hello from variable' });
719
- simulator.simulateScript(scripts[0], jsEngine, []);
720
- (0, vitest_1.expect)(jsEngine.functions.onPlayerChoiceCalledWith).toEqual(['Hello from variable']);
721
- });
722
- (0, vitest_1.test)('sample 3 - branch text with function call', () => {
723
- let Functions = class Functions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
724
- constructor() {
725
- super(...arguments);
726
- this.onPlayerChoiceCalledWith = [];
727
- }
728
- onPlayerChoice(choices) {
729
- this.onPlayerChoiceCalledWith = choices;
730
- }
731
- onDialog(text, speaker) { }
732
- onEnd() { }
733
- onScriptBranchingEnd() { }
734
- gameOver() { }
735
- getMessage(name) {
736
- return `Hello ${name}`;
737
- }
738
- };
739
- __decorate([
740
- (0, helpers_lib_1.JSEngineFunction)(),
741
- __metadata("design:type", Function),
742
- __metadata("design:paramtypes", []),
743
- __metadata("design:returntype", void 0)
744
- ], Functions.prototype, "gameOver", null);
745
- __decorate([
746
- (0, helpers_lib_1.JSEngineFunction)(),
747
- __metadata("design:type", Function),
748
- __metadata("design:paramtypes", [String]),
749
- __metadata("design:returntype", String)
750
- ], Functions.prototype, "getMessage", null);
751
- Functions = __decorate([
752
- (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
753
- ], Functions);
754
- let scripts = [
755
- {
756
- id: 'scriptTest',
757
- actions: [
758
- {
759
- type: 'branchByPlayerChoice',
760
- value: [
761
- {
762
- text: '${getMessage("world")}',
763
- actions: [{ type: 'command', value: 'gameOver()' }]
764
- }
765
- ]
766
- }
767
- ]
768
- }
769
- ];
770
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
771
- let jsEngine = new helpers_lib_1.JSEngine(new Functions(), {});
772
- simulator.simulateScript(scripts[0], jsEngine, []);
773
- (0, vitest_1.expect)(jsEngine.functions.onPlayerChoiceCalledWith).toEqual(['Hello world']);
774
- });
775
- (0, vitest_1.test)('sample 4 - multiple branches with string conversion', () => {
776
- let Functions = class Functions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
777
- constructor() {
778
- super(...arguments);
779
- this.onPlayerChoiceCalledWith = [];
780
- }
781
- onPlayerChoice(choices) {
782
- this.onPlayerChoiceCalledWith = choices;
783
- }
784
- onDialog(text, speaker) { }
785
- onEnd() { }
786
- onScriptBranchingEnd() { }
787
- gameOver() { }
788
- getMessage(name) {
789
- return `Hello ${name}`;
790
- }
791
- };
792
- __decorate([
793
- (0, helpers_lib_1.JSEngineFunction)(),
794
- __metadata("design:type", Function),
795
- __metadata("design:paramtypes", []),
796
- __metadata("design:returntype", void 0)
797
- ], Functions.prototype, "gameOver", null);
798
- __decorate([
799
- (0, helpers_lib_1.JSEngineFunction)(),
800
- __metadata("design:type", Function),
801
- __metadata("design:paramtypes", [String]),
802
- __metadata("design:returntype", String)
803
- ], Functions.prototype, "getMessage", null);
804
- Functions = __decorate([
805
- (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
806
- ], Functions);
807
- let scripts = [
808
- {
809
- id: 'scriptTest',
810
- actions: [
811
- {
812
- type: 'branchByPlayerChoice',
813
- value: [
814
- {
815
- text: '${getMessage("world")}',
816
- actions: [{ type: 'command', value: 'gameOver()' }]
817
- },
818
- {
819
- text: '${message}',
820
- actions: [{ type: 'command', value: 'gameOver()' }]
821
- }
822
- ]
823
- }
824
- ]
825
- }
826
- ];
827
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
828
- let jsEngine = new helpers_lib_1.JSEngine(new Functions(), { message: 'Hello from variable' });
829
- simulator.simulateScript(scripts[0], jsEngine, []);
830
- (0, vitest_1.expect)(jsEngine.functions.onPlayerChoiceCalledWith).toEqual(['Hello world', 'Hello from variable']);
831
- });
832
- (0, vitest_1.test)('sample 5 - callback thrown error', () => {
833
- let Functions = class Functions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
834
- onPlayerChoice(choices) {
835
- throw new Error('onPlayerChoice error');
836
- }
837
- onDialog(text, speaker) { }
838
- onEnd() { }
839
- onScriptBranchingEnd() { }
840
- gameOver() { }
841
- foo(a) { }
842
- };
843
- __decorate([
844
- (0, helpers_lib_1.JSEngineFunction)(),
845
- __metadata("design:type", Function),
846
- __metadata("design:paramtypes", []),
847
- __metadata("design:returntype", void 0)
848
- ], Functions.prototype, "gameOver", null);
849
- __decorate([
850
- (0, helpers_lib_1.JSEngineFunction)(),
851
- __param(0, (0, helpers_lib_1.Schema)(joi_1.default.string().required())),
852
- __metadata("design:type", Function),
853
- __metadata("design:paramtypes", [String]),
854
- __metadata("design:returntype", void 0)
855
- ], Functions.prototype, "foo", null);
856
- Functions = __decorate([
857
- (0, helpers_lib_1.AutoValidate)({ allowReadingNonExistantProperties: true })
858
- ], Functions);
859
- let scripts = [
860
- {
861
- id: 'scriptTest',
862
- actions: [
863
- {
864
- type: 'branchByPlayerChoice',
865
- value: [
866
- {
867
- text: 'sample text',
868
- actions: [{ type: 'command', value: 'gameOver()' }]
869
- }
870
- ]
871
- }
872
- ]
873
- }
874
- ];
875
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
876
- let jsEngine = new helpers_lib_1.JSEngine(new Functions(), {});
877
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
878
- try {
879
- simulator.simulateScript(scripts[0], jsEngine, []);
880
- }
881
- catch (e) {
882
- let error = e;
883
- (0, vitest_1.expect)(error.message).include('Error: onPlayerChoice error');
884
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
885
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
886
- }
887
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
888
- {
889
- scriptId: 'scriptTest',
890
- path: ['actions', '0', 'branchByPlayerChoice', '0']
891
- }
892
- ]);
893
- });
894
- });
895
- (0, vitest_1.describe)('On End', () => {
896
- (0, vitest_1.describe)('Basic', () => {
897
- let onEndCallCount = 0;
898
- class OnEndFunctions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
899
- onPlayerChoice(choices) { }
900
- onDialog(text, speaker) { }
901
- onEnd() {
902
- onEndCallCount++;
903
- }
904
- onScriptBranchingEnd() { }
905
- }
906
- let onEndFunctions;
907
- (0, vitest_1.beforeEach)(() => {
908
- onEndFunctions = new OnEndFunctions();
909
- onEndCallCount = 0;
910
- });
911
- (0, vitest_1.test)('sample 1', () => {
912
- let scripts = [
913
- {
914
- id: 'test',
915
- actions: [
916
- {
917
- type: 'command',
918
- value: 'a = 2'
919
- }
920
- ]
921
- }
922
- ];
923
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
924
- let jsEngine = new helpers_lib_1.JSEngine(onEndFunctions, {});
925
- (0, vitest_1.expect)(onEndCallCount).toEqual(0);
926
- simulator.simulateScript(scripts[0], jsEngine, []);
927
- (0, vitest_1.expect)(onEndCallCount).toEqual(1);
928
- });
929
- (0, vitest_1.test)('sample 2 - callback thrown error', () => {
930
- let scripts = [
931
- {
932
- id: 'test',
933
- actions: [
934
- {
935
- type: 'command',
936
- value: 'a = 2'
937
- }
938
- ]
939
- }
940
- ];
941
- class Functions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
942
- onPlayerChoice(choices) { }
943
- onDialog(text, speaker) { }
944
- onEnd() {
945
- throw new Error('onEnd error');
946
- }
947
- onScriptBranchingEnd() { }
948
- }
949
- let throwErrorFunctions = new Functions();
950
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
951
- let jsEngine = new helpers_lib_1.JSEngine(throwErrorFunctions, {});
952
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
953
- try {
954
- simulator.simulateScript(scripts[0], jsEngine, []);
955
- }
956
- catch (e) {
957
- let error = e;
958
- (0, vitest_1.expect)(error.message).include('Error: onEnd error');
959
- (0, vitest_1.expect)(error.location.scriptId).toEqual('test');
960
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
961
- }
962
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
963
- });
964
- (0, vitest_1.test)('sample 3 - script that loops to itself', () => {
965
- let scripts = [
966
- {
967
- id: 'startingScript',
968
- actions: [
969
- {
970
- type: 'branchByPlayerChoice',
971
- value: [
972
- { text: 'option 1', actions: [{ type: 'jumpTo', value: 'startingScript' }] },
973
- { text: 'option 2', actions: [{ type: 'command', value: 'a = 1' }] }
974
- ]
975
- }
976
- ]
977
- }
978
- ];
979
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
980
- let jsEngine = new helpers_lib_1.JSEngine(onEndFunctions, {});
981
- simulator.simulateScript(scripts[0], jsEngine, []);
982
- (0, vitest_1.expect)(onEndCallCount).toEqual(1);
983
- });
984
- (0, vitest_1.test)('sample 4 - jump to script', () => {
985
- let scripts = [
986
- {
987
- id: 'scriptTest',
988
- actions: [
989
- {
990
- type: 'jumpTo',
991
- value: 'targetScript'
992
- }
993
- ]
994
- },
995
- {
996
- id: 'targetScript',
997
- actions: [{ type: 'command', value: 'a = 1' }]
998
- }
999
- ];
1000
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1001
- let jsEngine = new helpers_lib_1.JSEngine(onEndFunctions, {});
1002
- simulator.simulateScript(scripts[0], jsEngine, []);
1003
- (0, vitest_1.expect)(onEndCallCount).toEqual(1);
1004
- });
1005
- (0, vitest_1.test)('sample 5 - on end should not be called twice', () => {
1006
- let scripts = [
1007
- {
1008
- id: 'scriptTest',
1009
- actions: [
1010
- { type: 'command', value: 'a = 1' },
1011
- {
1012
- type: 'branchByChance',
1013
- value: [
1014
- {
1015
- label: 'success',
1016
- weight: 1,
1017
- actions: [
1018
- {
1019
- type: 'jumpTo',
1020
- value: 'targetScript'
1021
- }
1022
- ]
1023
- }
1024
- ]
1025
- }
1026
- ]
1027
- },
1028
- {
1029
- id: 'targetScript',
1030
- actions: [{ type: 'command', value: 'a = 2' }]
1031
- }
1032
- ];
1033
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1034
- let jsEngine = new helpers_lib_1.JSEngine(onEndFunctions, {});
1035
- simulator.simulateScript(scripts[0], jsEngine, []);
1036
- (0, vitest_1.expect)(onEndCallCount).toEqual(1);
1037
- });
1038
- });
1039
- (0, vitest_1.describe)('Dialog Test', () => {
1040
- class DialogTestFunctions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
1041
- onPlayerChoice(choices) { }
1042
- onDialog(text, speaker) {
1043
- this.dialogCalledWith = text;
1044
- }
1045
- onEnd() { }
1046
- onScriptBranchingEnd() { }
1047
- foo(a) {
1048
- return 'hello ' + a;
1049
- }
1050
- }
1051
- __decorate([
1052
- (0, helpers_lib_1.JSEngineFunction)(),
1053
- __param(0, (0, helpers_lib_1.Schema)(joi_1.default.string().required())),
1054
- __metadata("design:type", Function),
1055
- __metadata("design:paramtypes", [String]),
1056
- __metadata("design:returntype", String)
1057
- ], DialogTestFunctions.prototype, "foo", null);
1058
- let dialogTestFunctions;
1059
- (0, vitest_1.beforeEach)(() => {
1060
- dialogTestFunctions = new DialogTestFunctions();
1061
- });
1062
- (0, vitest_1.test)('sample 1', () => {
1063
- let scripts = [
1064
- {
1065
- id: 'scriptTest',
1066
- actions: [{ type: 'dialog', value: { text: 'hello world', speaker: 'narrator' } }]
1067
- }
1068
- ];
1069
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1070
- let jsEngine = new helpers_lib_1.JSEngine(dialogTestFunctions, {});
1071
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).not.toThrowError();
1072
- (0, vitest_1.expect)(dialogTestFunctions.dialogCalledWith).toEqual('hello world');
1073
- });
1074
- (0, vitest_1.test)('sample 2 - dialog text with variable', () => {
1075
- let scripts = [
1076
- {
1077
- id: 'scriptTest',
1078
- actions: [{ type: 'dialog', value: { text: '${a}', speaker: 'narrator' } }]
1079
- }
1080
- ];
1081
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1082
- let jsEngine = new helpers_lib_1.JSEngine(dialogTestFunctions, { a: 'hello world' });
1083
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).not.toThrowError();
1084
- (0, vitest_1.expect)(dialogTestFunctions.dialogCalledWith).toEqual('hello world');
1085
- });
1086
- (0, vitest_1.test)('sample 3 - dialog text with function call', () => {
1087
- let scripts = [
1088
- {
1089
- id: 'scriptTest',
1090
- actions: [{ type: 'dialog', value: { text: '${foo("world")}', speaker: 'narrator' } }]
1091
- }
1092
- ];
1093
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1094
- let jsEngine = new helpers_lib_1.JSEngine(dialogTestFunctions, { foo: () => 'hello world' });
1095
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).not.toThrowError();
1096
- (0, vitest_1.expect)(dialogTestFunctions.dialogCalledWith).toEqual('hello world');
1097
- });
1098
- });
1099
- (0, vitest_1.describe)('Actions Before Testing End', () => {
1100
- let onBranchingEndCallCount = 0;
1101
- let onEndCallCount = 0;
1102
- class OnRunScriptEndFunctions extends script_test_simulator_1.ScriptEngineSimulatorFunctions {
1103
- onPlayerChoice(choices) { }
1104
- onDialog(text, speaker) { }
1105
- onEnd() {
1106
- onEndCallCount++;
1107
- }
1108
- onScriptBranchingEnd() {
1109
- onBranchingEndCallCount++;
1110
- }
1111
- }
1112
- let onRunScriptEndFunctions;
1113
- (0, vitest_1.beforeEach)(() => {
1114
- onRunScriptEndFunctions = new OnRunScriptEndFunctions();
1115
- onEndCallCount = 0;
1116
- onBranchingEndCallCount = 0;
1117
- });
1118
- (0, vitest_1.test)('sample 1', () => {
1119
- let actionsBeforeTesting = [{ type: 'runScript', value: 'startingScript' }];
1120
- let scripts = [
1121
- {
1122
- id: 'startingScript',
1123
- actions: [
1124
- {
1125
- type: 'branchByChance',
1126
- value: [
1127
- {
1128
- label: 'a',
1129
- weight: 1,
1130
- actions: [{ type: 'command', value: 'a = 1' }]
1131
- },
1132
- {
1133
- label: 'b',
1134
- weight: 1,
1135
- actions: [{ type: 'command', value: 'a = 2' }]
1136
- }
1137
- ]
1138
- }
1139
- ]
1140
- },
1141
- {
1142
- id: 'scriptTest',
1143
- actions: [
1144
- {
1145
- type: 'branchByCondition',
1146
- value: [
1147
- {
1148
- condition: 'a === 1',
1149
- actions: [{ type: 'command', value: 'gameOver()' }]
1150
- },
1151
- {
1152
- condition: 'a === 4',
1153
- actions: [{ type: 'command', value: 'gameOver()' }]
1154
- }
1155
- ]
1156
- }
1157
- ]
1158
- }
1159
- ];
1160
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1161
- let jsEngine = new helpers_lib_1.JSEngine(onRunScriptEndFunctions, {});
1162
- (0, vitest_1.expect)(onBranchingEndCallCount).toEqual(0);
1163
- simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting);
1164
- (0, vitest_1.expect)(onEndCallCount).toEqual(4);
1165
- (0, vitest_1.expect)(onBranchingEndCallCount).toEqual(2);
1166
- });
1167
- });
1168
- });
1169
- (0, vitest_1.describe)('Error Cases', () => {
1170
- (0, vitest_1.describe)('Unique Cases', () => {
1171
- (0, vitest_1.test)('sample 1 - unreached branch error', () => {
1172
- let scripts = [
1173
- {
1174
- id: 'scriptTest',
1175
- actions: [
1176
- {
1177
- type: 'branchByCondition',
1178
- value: [
1179
- {
1180
- actions: [{ type: 'command', value: 'gameOver()' }]
1181
- },
1182
- {
1183
- condition: 'a === true',
1184
- actions: [{ type: 'command', value: 'gameOver()' }]
1185
- }
1186
- ]
1187
- }
1188
- ]
1189
- }
1190
- ];
1191
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1192
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1193
- simulator.simulateScript(scripts[0], jsEngine, []);
1194
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1195
- {
1196
- scriptId: 'scriptTest',
1197
- path: ['actions', '0', 'branchByCondition', '1']
1198
- }
1199
- ]);
1200
- });
1201
- (0, vitest_1.test)('sample 2 - runtime line execution errors', () => {
1202
- let scripts = [
1203
- {
1204
- id: 'scriptTest',
1205
- actions: [
1206
- { type: 'command', value: 'a = 0' },
1207
- { type: 'command', value: 'foo(a)' }
1208
- ]
1209
- }
1210
- ];
1211
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1212
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1213
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1214
- try {
1215
- simulator.simulateScript(scripts[0], jsEngine, []);
1216
- }
1217
- catch (e) {
1218
- let error = e;
1219
- (0, vitest_1.expect)(error.message).include('Error: Validation failed for argument at position 1 in foo: "value" must be a string');
1220
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1221
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '1']);
1222
- }
1223
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1224
- });
1225
- (0, vitest_1.test)('sample 3 - runtime line execution errors on actions before testing', () => {
1226
- let actionsBeforeTesting = [
1227
- { type: 'command', value: 'a = 0' },
1228
- { type: 'command', value: 'foo(a)' },
1229
- { type: 'runScript', value: 'startingScript' }
1230
- ];
1231
- let scripts = [
1232
- {
1233
- id: 'scriptTest',
1234
- actions: [{ type: 'command', value: 'gameOver()' }]
1235
- }
1236
- ];
1237
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1238
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1239
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting)).toThrowError(interfaces_1.ActionsBeforeTestingError);
1240
- try {
1241
- simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting);
1242
- }
1243
- catch (e) {
1244
- let error = e;
1245
- (0, vitest_1.expect)(error.message).include('Error: Validation failed for argument at position 1 in foo: "value" must be a string');
1246
- (0, vitest_1.expect)(error.actionIndex).toEqual(1);
1247
- }
1248
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1249
- {
1250
- scriptId: 'scriptTest',
1251
- path: []
1252
- }
1253
- ]);
1254
- });
1255
- (0, vitest_1.test)('sample 4 - script execution error on actions before testing', () => {
1256
- let actionsBeforeTesting = [
1257
- { type: 'runScript', value: 'script1' },
1258
- { type: 'runScript', value: 'script2' }
1259
- ];
1260
- let scripts = [
1261
- {
1262
- id: 'script1',
1263
- actions: [{ type: 'command', value: 'a = 1' }]
1264
- },
1265
- {
1266
- id: 'script2',
1267
- actions: [{ type: 'command', value: 'foo(a)' }]
1268
- },
1269
- {
1270
- id: 'script3',
1271
- actions: [{ type: 'command', value: 'a = "test"' }]
1272
- }
1273
- ];
1274
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1275
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1276
- simulator['scriptBranchesMap'].setVisited(scripts[0]);
1277
- simulator['scriptBranchesMap'].setVisited(scripts[1]);
1278
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[2], jsEngine, actionsBeforeTesting)).toThrowError(interfaces_1.BranchingBeforeTestingError);
1279
- try {
1280
- simulator.simulateScript(scripts[0], jsEngine, actionsBeforeTesting);
1281
- }
1282
- catch (e) {
1283
- let error = e;
1284
- (0, vitest_1.expect)(error.message).include('Error: Validation failed for argument at position 1 in foo: "value" must be a string');
1285
- (0, vitest_1.expect)(error.actionIndex).toEqual(1);
1286
- (0, vitest_1.expect)(error.location).toEqual({
1287
- scriptId: 'script2',
1288
- path: ['actions', '0']
1289
- });
1290
- }
1291
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1292
- {
1293
- scriptId: 'script3',
1294
- path: []
1295
- }
1296
- ]);
1297
- });
1298
- (0, vitest_1.test)('sample 5 - script execution error on actions before testing', () => {
1299
- let scripts = [
1300
- {
1301
- id: 'script1',
1302
- actions: [{ type: 'command', value: 'a = 1' }]
1303
- },
1304
- {
1305
- id: 'script2',
1306
- actions: [{ type: 'command', value: 'a = "str"' }]
1307
- }
1308
- ];
1309
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1310
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1311
- simulator.simulateScript(scripts[0], jsEngine, []);
1312
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[1], jsEngine, [])).toThrow('JSEngine: Type mismatch during variable set. The type of "a" is "string", and it is tried to set to "number".');
1313
- });
1314
- (0, vitest_1.test)('sample 6 - runtime dialog string execution errors', () => {
1315
- let scripts = [
1316
- {
1317
- id: 'scriptTest',
1318
- actions: [{ type: 'dialog', value: { text: '${foo(a}', speaker: 'narrator' } }]
1319
- }
1320
- ];
1321
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1322
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1323
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1324
- try {
1325
- simulator.simulateScript(scripts[0], jsEngine, []);
1326
- }
1327
- catch (e) {
1328
- let error = e;
1329
- (0, vitest_1.expect)(error.message).include('SyntaxError: missing ) after argument list\n\n----Execution history----\nRunning script: scriptTest');
1330
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1331
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
1332
- }
1333
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1334
- });
1335
- (0, vitest_1.test)('sample 7 - runtime weight execution errors', () => {
1336
- let scripts = [
1337
- {
1338
- id: 'scriptTest',
1339
- actions: [
1340
- {
1341
- type: 'branchByChance',
1342
- value: [{ weight: 'a', label: 'a', actions: [{ type: 'command', value: 'gameOver()' }] }]
1343
- }
1344
- ]
1345
- }
1346
- ];
1347
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1348
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1349
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1350
- try {
1351
- simulator.simulateScript(scripts[0], jsEngine, []);
1352
- }
1353
- catch (e) {
1354
- let error = e;
1355
- (0, vitest_1.expect)(error.message).include('Error: Weight of branch 0 is not a valid number: "a" -> NaN.');
1356
- }
1357
- });
1358
- (0, vitest_1.test)('sample 8 - AutoValidate', () => {
1359
- let scripts = [
1360
- {
1361
- id: 'scriptTest',
1362
- actions: [{ type: 'command', value: 'foo(5)' }]
1363
- }
1364
- ];
1365
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1366
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1367
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1368
- try {
1369
- simulator.simulateScript(scripts[0], jsEngine, []);
1370
- }
1371
- catch (e) {
1372
- let error = e;
1373
- (0, vitest_1.expect)(error.message).includes('Error: Validation failed for argument at position 1 in foo: "value" must be a string');
1374
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1375
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
1376
- }
1377
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1378
- });
1379
- (0, vitest_1.test)('sample 9 - AutoValidate after duplicating jsEntine in branching', () => {
1380
- let scripts = [
1381
- {
1382
- id: 'scriptTest',
1383
- actions: [
1384
- {
1385
- type: 'branchByChance',
1386
- value: [
1387
- {
1388
- label: 'a',
1389
- weight: 1,
1390
- actions: [{ type: 'command', value: 'foo(5)' }]
1391
- }
1392
- ]
1393
- }
1394
- ]
1395
- }
1396
- ];
1397
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1398
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1399
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1400
- try {
1401
- simulator.simulateScript(scripts[0], jsEngine, []);
1402
- }
1403
- catch (e) {
1404
- let error = e;
1405
- (0, vitest_1.expect)(error.message).includes('Error: Validation failed for argument at position 1 in foo: "value" must be a string');
1406
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1407
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0', 'branchByChance', '0', 'actions', '0']);
1408
- }
1409
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1410
- });
1411
- });
1412
- (0, vitest_1.describe)('Condition Check Error', () => {
1413
- (0, vitest_1.test)('branchByCondition', () => {
1414
- let scripts = [
1415
- {
1416
- id: 'scriptTest',
1417
- actions: [
1418
- {
1419
- type: 'branchByCondition',
1420
- value: [
1421
- {
1422
- condition: 'foo()',
1423
- actions: [{ type: 'command', value: 'gameOver()' }]
1424
- }
1425
- ]
1426
- }
1427
- ]
1428
- }
1429
- ];
1430
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1431
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1432
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1433
- try {
1434
- simulator.simulateScript(scripts[0], jsEngine, []);
1435
- }
1436
- catch (e) {
1437
- let error = e;
1438
- (0, vitest_1.expect)(error.message).includes('Error: Missing arguments in function "foo". Expected: 1, received: 0');
1439
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1440
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0', 'branchByCondition', '0']);
1441
- }
1442
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1443
- {
1444
- scriptId: 'scriptTest',
1445
- path: ['actions', '0', 'branchByCondition', '0']
1446
- }
1447
- ]);
1448
- });
1449
- (0, vitest_1.test)('branchByChance', () => {
1450
- let scripts = [
1451
- {
1452
- id: 'scriptTest',
1453
- actions: [
1454
- {
1455
- type: 'branchByChance',
1456
- value: [
1457
- {
1458
- label: 'a',
1459
- weight: 1,
1460
- condition: 'foo()',
1461
- actions: [{ type: 'command', value: 'gameOver()' }]
1462
- }
1463
- ]
1464
- }
1465
- ]
1466
- }
1467
- ];
1468
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1469
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1470
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1471
- try {
1472
- simulator.simulateScript(scripts[0], jsEngine, []);
1473
- }
1474
- catch (e) {
1475
- let error = e;
1476
- (0, vitest_1.expect)(error.message).includes('Error: Missing arguments in function "foo". Expected: 1, received: 0');
1477
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1478
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0', 'branchByChance', '0']);
1479
- }
1480
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1481
- {
1482
- scriptId: 'scriptTest',
1483
- path: ['actions', '0', 'branchByChance', '0']
1484
- }
1485
- ]);
1486
- });
1487
- (0, vitest_1.test)('branchByPlayerChoice', () => {
1488
- let scripts = [
1489
- {
1490
- id: 'scriptTest',
1491
- actions: [
1492
- {
1493
- type: 'branchByPlayerChoice',
1494
- value: [
1495
- {
1496
- text: 'sample text',
1497
- condition: 'foo()',
1498
- actions: [{ type: 'command', value: 'gameOver()' }]
1499
- }
1500
- ]
1501
- }
1502
- ]
1503
- }
1504
- ];
1505
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1506
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1507
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1508
- try {
1509
- simulator.simulateScript(scripts[0], jsEngine, []);
1510
- }
1511
- catch (e) {
1512
- let error = e;
1513
- (0, vitest_1.expect)(error.message).includes('Error: Missing arguments in function "foo". Expected: 1, received: 0');
1514
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1515
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0', 'branchByPlayerChoice', '0']);
1516
- }
1517
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1518
- {
1519
- scriptId: 'scriptTest',
1520
- path: ['actions', '0', 'branchByPlayerChoice', '0']
1521
- }
1522
- ]);
1523
- });
1524
- });
1525
- (0, vitest_1.describe)('Having No Options In Branching Actions', () => {
1526
- (0, vitest_1.test)('branchByCondition', () => {
1527
- let scripts = [
1528
- {
1529
- id: 'scriptTest',
1530
- actions: [
1531
- {
1532
- type: 'branchByCondition',
1533
- value: [
1534
- {
1535
- condition: 'a === true',
1536
- actions: [{ type: 'command', value: 'gameOver()' }]
1537
- }
1538
- ]
1539
- }
1540
- ]
1541
- }
1542
- ];
1543
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1544
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1545
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1546
- try {
1547
- simulator.simulateScript(scripts[0], jsEngine, []);
1548
- }
1549
- catch (e) {
1550
- let error = e;
1551
- (0, vitest_1.expect)(error.message).include('No option is fulfilled the condition during branching.');
1552
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1553
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
1554
- }
1555
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1556
- {
1557
- scriptId: 'scriptTest',
1558
- path: ['actions', '0', 'branchByCondition', '0']
1559
- }
1560
- ]);
1561
- });
1562
- (0, vitest_1.test)('branchByChance', () => {
1563
- let scripts = [
1564
- {
1565
- id: 'scriptTest',
1566
- actions: [
1567
- {
1568
- type: 'branchByChance',
1569
- value: [
1570
- {
1571
- label: 'a',
1572
- weight: 1,
1573
- condition: 'a === true',
1574
- actions: [{ type: 'command', value: 'gameOver()' }]
1575
- }
1576
- ]
1577
- }
1578
- ]
1579
- }
1580
- ];
1581
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1582
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1583
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1584
- try {
1585
- simulator.simulateScript(scripts[0], jsEngine, []);
1586
- }
1587
- catch (e) {
1588
- let error = e;
1589
- (0, vitest_1.expect)(error.message).include('No option is fulfilled the condition during branching.');
1590
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1591
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
1592
- }
1593
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1594
- {
1595
- scriptId: 'scriptTest',
1596
- path: ['actions', '0', 'branchByChance', '0']
1597
- }
1598
- ]);
1599
- });
1600
- (0, vitest_1.test)('branchByPlayerChoice', () => {
1601
- let scripts = [
1602
- {
1603
- id: 'scriptTest',
1604
- actions: [
1605
- {
1606
- type: 'branchByPlayerChoice',
1607
- value: [
1608
- {
1609
- text: 'sample text',
1610
- condition: 'a === true',
1611
- actions: [{ type: 'command', value: 'gameOver()' }]
1612
- }
1613
- ]
1614
- }
1615
- ]
1616
- }
1617
- ];
1618
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1619
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1620
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrowError(interfaces_1.SimulationError);
1621
- try {
1622
- simulator.simulateScript(scripts[0], jsEngine, []);
1623
- }
1624
- catch (e) {
1625
- let error = e;
1626
- (0, vitest_1.expect)(error.message).include('No option is fulfilled the condition during branching.');
1627
- (0, vitest_1.expect)(error.location.scriptId).toEqual('scriptTest');
1628
- (0, vitest_1.expect)(error.location.path).toEqual(['actions', '0']);
1629
- }
1630
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([
1631
- {
1632
- scriptId: 'scriptTest',
1633
- path: ['actions', '0', 'branchByPlayerChoice', '0']
1634
- }
1635
- ]);
1636
- });
1637
- });
1638
- });
1639
- (0, vitest_1.describe)('Rich Text', () => {
1640
- (0, vitest_1.test)('basic rich text execution', () => {
1641
- let scripts = [
1642
- {
1643
- id: 'scriptTest',
1644
- actions: [{ type: 'dialog', value: { text: '<b>Hello</b> world', speaker: 'NPC' } }]
1645
- }
1646
- ];
1647
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1648
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1649
- simulator.simulateScript(scripts[0], jsEngine, []);
1650
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1651
- });
1652
- (0, vitest_1.test)('unknown tag', () => {
1653
- let scripts = [
1654
- {
1655
- id: 'scriptTest',
1656
- actions: [{ type: 'dialog', value: { text: '<b>Hello</b> world', speaker: 'NPC' } }]
1657
- }
1658
- ];
1659
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts, { richTextTags: new Set(['valid']) });
1660
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1661
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrow(`Unknown tag 'b' at position 0. Only these tags are allowed: valid`);
1662
- });
1663
- (0, vitest_1.test)('invalid rich text', () => {
1664
- let scripts = [
1665
- {
1666
- id: 'scriptTest',
1667
- actions: [{ type: 'dialog', value: { text: '<a>Hello<b></a> world</b>', speaker: 'NPC' } }]
1668
- }
1669
- ];
1670
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts, { richTextTags: new Set(['a', 'b']) });
1671
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1672
- (0, vitest_1.expect)(() => simulator.simulateScript(scripts[0], jsEngine, [])).toThrow(`Mismatched tags: expected closing tag for 'b', but found 'a' at position 11`);
1673
- });
1674
- });
1675
- (0, vitest_1.describe)('Dialog Action Type', () => {
1676
- (0, vitest_1.test)('basic dialog execution', () => {
1677
- let scripts = [
1678
- {
1679
- id: 'scriptTest',
1680
- actions: [
1681
- {
1682
- type: 'dialog',
1683
- value: {
1684
- text: 'Hello world',
1685
- speaker: 'NPC'
1686
- }
1687
- }
1688
- ]
1689
- }
1690
- ];
1691
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1692
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1693
- simulator.simulateScript(scripts[0], jsEngine, []);
1694
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1695
- });
1696
- (0, vitest_1.test)('dialog with multiple actions', () => {
1697
- let scripts = [
1698
- {
1699
- id: 'scriptTest',
1700
- actions: [
1701
- {
1702
- type: 'dialog',
1703
- value: {
1704
- text: 'First message',
1705
- speaker: 'NPC1'
1706
- }
1707
- },
1708
- {
1709
- type: 'dialog',
1710
- value: {
1711
- text: 'Second message',
1712
- speaker: 'NPC2'
1713
- }
1714
- },
1715
- {
1716
- type: 'command',
1717
- value: 'gameOver()'
1718
- }
1719
- ]
1720
- }
1721
- ];
1722
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1723
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1724
- simulator.simulateScript(scripts[0], jsEngine, []);
1725
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1726
- });
1727
- (0, vitest_1.test)('dialog in branch by condition', () => {
1728
- let scripts = [
1729
- {
1730
- id: 'scriptTest',
1731
- actions: [
1732
- {
1733
- type: 'branchByCondition',
1734
- value: [
1735
- {
1736
- condition: 'a === true',
1737
- actions: [
1738
- {
1739
- type: 'dialog',
1740
- value: {
1741
- text: 'Condition met',
1742
- speaker: 'NPC'
1743
- }
1744
- }
1745
- ]
1746
- },
1747
- {
1748
- actions: [
1749
- {
1750
- type: 'dialog',
1751
- value: {
1752
- text: 'Default case',
1753
- speaker: 'NPC'
1754
- }
1755
- }
1756
- ]
1757
- }
1758
- ]
1759
- }
1760
- ]
1761
- }
1762
- ];
1763
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1764
- let jsEngine = new helpers_lib_1.JSEngine(functions, { a: true });
1765
- simulator.simulateScript(scripts[0], jsEngine, []);
1766
- simulator.simulateScript(scripts[0], jsEngine, [{ type: 'command', value: 'a = false' }]);
1767
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1768
- });
1769
- (0, vitest_1.test)('dialog in branch by chance', () => {
1770
- let scripts = [
1771
- {
1772
- id: 'scriptTest',
1773
- actions: [
1774
- {
1775
- type: 'branchByChance',
1776
- value: [
1777
- {
1778
- label: 'chance1',
1779
- weight: 1,
1780
- actions: [
1781
- {
1782
- type: 'dialog',
1783
- value: {
1784
- text: 'First chance',
1785
- speaker: 'NPC1'
1786
- }
1787
- }
1788
- ]
1789
- },
1790
- {
1791
- label: 'chance2',
1792
- weight: 1,
1793
- actions: [
1794
- {
1795
- type: 'dialog',
1796
- value: {
1797
- text: 'Second chance',
1798
- speaker: 'NPC2'
1799
- }
1800
- }
1801
- ]
1802
- }
1803
- ]
1804
- }
1805
- ]
1806
- }
1807
- ];
1808
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1809
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1810
- simulator.simulateScript(scripts[0], jsEngine, []);
1811
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1812
- });
1813
- (0, vitest_1.test)('dialog in branch by player choice', () => {
1814
- let scripts = [
1815
- {
1816
- id: 'scriptTest',
1817
- actions: [
1818
- {
1819
- type: 'branchByPlayerChoice',
1820
- value: [
1821
- {
1822
- text: 'Choice 1',
1823
- actions: [
1824
- {
1825
- type: 'dialog',
1826
- value: {
1827
- text: 'You chose option 1',
1828
- speaker: 'NPC'
1829
- }
1830
- }
1831
- ]
1832
- },
1833
- {
1834
- text: 'Choice 2',
1835
- actions: [
1836
- {
1837
- type: 'dialog',
1838
- value: {
1839
- text: 'You chose option 2',
1840
- speaker: 'NPC'
1841
- }
1842
- }
1843
- ]
1844
- }
1845
- ]
1846
- }
1847
- ]
1848
- }
1849
- ];
1850
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1851
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1852
- simulator.simulateScript(scripts[0], jsEngine, []);
1853
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1854
- });
1855
- (0, vitest_1.test)('dialog with special characters', () => {
1856
- let scripts = [
1857
- {
1858
- id: 'scriptTest',
1859
- actions: [
1860
- {
1861
- type: 'dialog',
1862
- value: {
1863
- text: 'Hello "world" with \'quotes\' and new\nline',
1864
- speaker: 'NPC with spaces and !@#$%'
1865
- }
1866
- }
1867
- ]
1868
- }
1869
- ];
1870
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1871
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1872
- simulator.simulateScript(scripts[0], jsEngine, []);
1873
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1874
- });
1875
- (0, vitest_1.test)('dialog with empty text and speaker', () => {
1876
- let scripts = [
1877
- {
1878
- id: 'scriptTest',
1879
- actions: [
1880
- {
1881
- type: 'dialog',
1882
- value: {
1883
- text: '',
1884
- speaker: ''
1885
- }
1886
- }
1887
- ]
1888
- }
1889
- ];
1890
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1891
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1892
- simulator.simulateScript(scripts[0], jsEngine, []);
1893
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1894
- });
1895
- (0, vitest_1.test)('dialog mixed with other action types', () => {
1896
- let scripts = [
1897
- {
1898
- id: 'scriptTest',
1899
- actions: [
1900
- {
1901
- type: 'dialog',
1902
- value: {
1903
- text: 'Initial dialog',
1904
- speaker: 'NPC'
1905
- }
1906
- },
1907
- {
1908
- type: 'command',
1909
- value: 'a = 1'
1910
- },
1911
- {
1912
- type: 'branchByCondition',
1913
- value: [
1914
- {
1915
- condition: 'a === 1',
1916
- actions: [
1917
- {
1918
- type: 'dialog',
1919
- value: {
1920
- text: 'Condition met',
1921
- speaker: 'NPC'
1922
- }
1923
- }
1924
- ]
1925
- }
1926
- ]
1927
- },
1928
- {
1929
- type: 'command',
1930
- value: 'gameOver()'
1931
- }
1932
- ]
1933
- }
1934
- ];
1935
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
1936
- let jsEngine = new helpers_lib_1.JSEngine(functions, {});
1937
- simulator.simulateScript(scripts[0], jsEngine, []);
1938
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
1939
- });
1940
- (0, vitest_1.test)('dialog in nested branching scenarios', () => {
1941
- let scripts = [
1942
- {
1943
- id: 'scriptTest',
1944
- actions: [
1945
- {
1946
- type: 'branchByCondition',
1947
- value: [
1948
- {
1949
- condition: 'a === true',
1950
- actions: [
1951
- {
1952
- type: 'dialog',
1953
- value: {
1954
- text: 'First condition',
1955
- speaker: 'NPC1'
1956
- }
1957
- },
1958
- {
1959
- type: 'branchByChance',
1960
- value: [
1961
- {
1962
- label: 'chance1',
1963
- weight: 1,
1964
- actions: [
1965
- {
1966
- type: 'dialog',
1967
- value: {
1968
- text: 'Nested chance 1',
1969
- speaker: 'NPC2'
1970
- }
1971
- }
1972
- ]
1973
- },
1974
- {
1975
- label: 'chance2',
1976
- weight: 1,
1977
- actions: [
1978
- {
1979
- type: 'branchByPlayerChoice',
1980
- value: [
1981
- {
1982
- text: 'Nested choice',
1983
- actions: [
1984
- {
1985
- type: 'dialog',
1986
- value: {
1987
- text: 'Nested player choice',
1988
- speaker: 'NPC3'
1989
- }
1990
- }
1991
- ]
1992
- }
1993
- ]
1994
- }
1995
- ]
1996
- }
1997
- ]
1998
- }
1999
- ]
2000
- }
2001
- ]
2002
- }
2003
- ]
2004
- }
2005
- ];
2006
- let simulator = new script_test_simulator_1.ScriptTestSimulator(scripts);
2007
- let jsEngine = new helpers_lib_1.JSEngine(functions, { a: true });
2008
- simulator.simulateScript(scripts[0], jsEngine, []);
2009
- (0, vitest_1.expect)(simulator.getUnvisitedBranchLocations()).toEqual([]);
2010
- });
2011
- });
2012
- });
2013
- //# sourceMappingURL=script-test-simulator.test.js.map