@flemist/test-variants 5.0.15 → 5.0.16

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.
@@ -1,1106 +0,0 @@
1
- import { timeControllerDefault as W } from "@flemist/time-controller";
2
- import { formatAny as ge, Subject as Je, combineAbortSignals as Z, isPromiseLike as O, abortSignalToPromise as Xe, deepCloneJsonLike as Ze, waitMicrotasks as Ke } from "@flemist/simple-utils";
3
- import { AbortError as Qe, AbortControllerFast as K } from "@flemist/abort-controller-fast";
4
- import { poolWait as le, Pool as Ye } from "@flemist/time-limits";
5
- class et extends Error {
6
- }
7
- function ne() {
8
- if (typeof process < "u" && process.memoryUsage)
9
- try {
10
- return process.memoryUsage.rss();
11
- } catch {
12
- }
13
- if (typeof performance < "u") {
14
- const e = performance.memory;
15
- if (e)
16
- try {
17
- return e.usedJSHeapSize;
18
- } catch {
19
- }
20
- }
21
- return null;
22
- }
23
- const tt = 1e3, k = [];
24
- function rt(e) {
25
- return ge(e, { pretty: !0, maxDepth: 5, maxItems: 50 });
26
- }
27
- function nt(...e) {
28
- const r = e.map((t) => typeof t == "string" ? t : rt(t)).join(" ");
29
- k.push(r), k.length > tt && k.shift(), console.log(r);
30
- }
31
- function it() {
32
- return k.join(`
33
- `);
34
- }
35
- globalThis.__getStressTestLogLast = it;
36
- const pe = (e, r) => {
37
- nt(r);
38
- }, he = (e) => ge(e, {
39
- pretty: !0,
40
- maxDepth: 20,
41
- maxItems: 100,
42
- maxStringLength: 5e3,
43
- dontExpandClassInstances: !0,
44
- dontExpandFunctions: !0
45
- }), T = {
46
- start: !0,
47
- progress: 5e3,
48
- completed: !0,
49
- error: !0,
50
- modeChange: !0,
51
- debug: !1,
52
- func: pe,
53
- format: he
54
- }, ot = {
55
- start: !1,
56
- progress: !1,
57
- completed: !1,
58
- error: !1,
59
- modeChange: !1,
60
- debug: !1,
61
- func: pe,
62
- format: he
63
- };
64
- function be(e) {
65
- return e === !1 ? ot : e === !0 || !e ? T : {
66
- start: e.start ?? T.start,
67
- progress: e.progress ?? T.progress,
68
- completed: e.completed ?? T.completed,
69
- error: e.error ?? T.error,
70
- modeChange: e.modeChange ?? T.modeChange,
71
- debug: e.debug ?? T.debug,
72
- func: e.func ?? T.func,
73
- format: e.format ?? T.format
74
- };
75
- }
76
- function st(e, r) {
77
- const t = e.now();
78
- return {
79
- startTime: t,
80
- startMemory: r,
81
- debugMode: !1,
82
- tests: 0,
83
- iterations: 0,
84
- iterationsAsync: 0,
85
- maxTestDuration: 0,
86
- prevLogTime: t,
87
- prevLogMemory: r,
88
- pendingModeChange: null,
89
- prevGcTime: t,
90
- prevGcIterations: 0,
91
- prevGcIterationsAsync: 0
92
- };
93
- }
94
- class V extends Qe {
95
- }
96
- class at {
97
- _timeController;
98
- _subject;
99
- _interval;
100
- _timer;
101
- constructor(r, t) {
102
- this._interval = r, this._timeController = t ?? W, this._subject = new Je({
103
- startStopNotifier: () => (this.update(), () => {
104
- this._timeController.clearTimeout(this._timer);
105
- })
106
- });
107
- }
108
- update() {
109
- this._subject.emit(this._timeController.now()), this._timer = this._timeController.setTimeout(
110
- () => this.update(),
111
- this._interval
112
- );
113
- }
114
- get interval() {
115
- return this._interval;
116
- }
117
- set interval(r) {
118
- this._interval = r, this._timer && (this._timeController.clearTimeout(this._timer), this.update());
119
- }
120
- subscribe(r) {
121
- return this._subject.subscribe(r);
122
- }
123
- }
124
- const lt = 50, ut = 5;
125
- function ct(e, r) {
126
- return e == null ? null : typeof e == "number" ? e : e(r) ?? null;
127
- }
128
- function ue(e, r) {
129
- return typeof e == "number" ? { iterationsAsync: 0, iterationsSync: e } : e != null && typeof e == "object" ? e : r ? { iterationsAsync: 1, iterationsSync: 0 } : { iterationsAsync: 0, iterationsSync: 1 };
130
- }
131
- function ft(e, r) {
132
- const t = r.log, o = r.pauseDebuggerOnError ?? !0, i = r.onStart, s = r.onEnd;
133
- r.state;
134
- let n = null, l = 0;
135
- function u(m, h, y) {
136
- n == null && (n = { error: m, args: h, tests: y }, t.error && t.func(
137
- "error",
138
- `[test-variants] error variant: ${t.format(h)}
139
- tests: ${y}
140
- ${t.format(m)}`
141
- ));
142
- const d = Date.now();
143
- if (o)
144
- debugger;
145
- if (Date.now() - d > lt && l < ut) {
146
- t.func(
147
- "debug",
148
- `[test-variants] debug iteration: ${l}`
149
- ), l++;
150
- return;
151
- }
152
- const v = n;
153
- throw n = null, r.onError && r.onError(v), v.error;
154
- }
155
- const g = new at(100);
156
- return function(h, y, d) {
157
- i && i({ args: h, tests: y });
158
- const b = ct(r.timeout, h);
159
- let v = null, M = d, f = null;
160
- b && (v = Date.now(), f = new K(), M = {
161
- abortSignal: Z(
162
- d.abortSignal,
163
- f.signal
164
- ),
165
- timeController: d.timeController
166
- });
167
- function x() {
168
- f.abort(
169
- new et(
170
- `[test-variants] test timeout ${b}ms exceeded`
171
- )
172
- );
173
- }
174
- try {
175
- let S = e(h, M);
176
- if (O(S)) {
177
- let w = null;
178
- return f && (w = g.subscribe(() => {
179
- Date.now() - v > b && (x(), w());
180
- }), S = Promise.race([
181
- S,
182
- Xe(f.signal)
183
- ])), S.then(
184
- (A) => {
185
- w?.();
186
- const D = ue(A, !0);
187
- return s && s({ args: h, tests: y, result: D }), D;
188
- },
189
- (A) => (w?.(), s && !(A instanceof V) && s({ args: h, tests: y, error: A }), u(A, h, y))
190
- );
191
- }
192
- v != null && Date.now() - v > b && (x(), f.signal.throwIfAborted());
193
- const C = ue(S, !1);
194
- return s && s({ args: h, tests: y, result: C }), C;
195
- } catch (S) {
196
- return S instanceof V ? void 0 : (s && s({ args: h, tests: y, error: S }), u(S, h, y));
197
- }
198
- };
199
- }
200
- function R(e, r, t) {
201
- for (let o = 0, i = e.length; o < i; o++)
202
- if (t ? t(e[o], r) : e[o] === r)
203
- return o;
204
- return -1;
205
- }
206
- function ce(e, r, t, o) {
207
- const i = Object.keys(e.templates), s = {}, n = [], l = [], u = [], g = i.length;
208
- for (let m = 0; m < g; m++) {
209
- const h = i[m];
210
- s[h] = void 0, n.push(-1), l.push(void 0), u.push(null);
211
- }
212
- return {
213
- args: s,
214
- argsNames: i,
215
- indexes: n,
216
- argValues: l,
217
- argLimits: u,
218
- attempts: 0,
219
- templates: e,
220
- limitArgOnError: t,
221
- equals: r,
222
- includeErrorVariant: o ?? !1
223
- };
224
- }
225
- function P(e, r) {
226
- const t = e.templates.templates[r], o = e.templates.extra[r];
227
- let i;
228
- if (typeof t == "function" ? i = t(e.args) : i = t, o == null)
229
- return i;
230
- let s = null;
231
- const n = o.length;
232
- for (let l = 0; l < n; l++) {
233
- const u = o[l];
234
- R(i, u, e.equals) < 0 && (s == null ? s = [...i, u] : s.push(u));
235
- }
236
- return s ?? i;
237
- }
238
- function I(e, r, t) {
239
- const o = e.argValues[r].length;
240
- if (o === 0)
241
- return -1;
242
- const i = e.argLimits[r];
243
- if (i == null)
244
- return o - 1;
245
- let s = e.limitArgOnError;
246
- if (typeof s == "function") {
247
- const n = e.argsNames[r];
248
- s = s({
249
- name: n,
250
- values: e.argValues[r],
251
- maxValueIndex: i
252
- });
253
- }
254
- return !t || s ? Math.min(i, o - 1) : o - 1;
255
- }
256
- function L(e) {
257
- const r = e.indexes.length;
258
- for (let t = 0; t < r; t++) {
259
- const o = e.argLimits[t];
260
- if (o == null)
261
- return !1;
262
- const i = e.indexes[t];
263
- if (i > o)
264
- return !0;
265
- if (i < o)
266
- return !1;
267
- }
268
- return !e.includeErrorVariant;
269
- }
270
- function N(e) {
271
- const r = e.indexes.length;
272
- for (let t = 0; t < r; t++)
273
- e.indexes[t] = -1, e.argValues[t] = void 0, e.args[e.argsNames[t]] = void 0;
274
- }
275
- function ye(e) {
276
- let r = !1, t = !0;
277
- const o = e.indexes.length;
278
- let i = o, s = !1, n = 0;
279
- for (; n < o; n++) {
280
- const l = e.argValues[n] == null;
281
- (l || s) && (l && (r = !0), e.argValues[n] = P(
282
- e,
283
- e.argsNames[n]
284
- ));
285
- const u = I(
286
- e,
287
- n,
288
- n > i
289
- );
290
- if (u < 0) {
291
- t = !1, e.indexes[n] = -1;
292
- break;
293
- }
294
- l && (e.indexes[n] = 0, e.args[e.argsNames[n]] = e.argValues[n][0]), (s || e.indexes[n] > u) && (e.indexes[n] = u, e.args[e.argsNames[n]] = e.argValues[n][u], s = !0), i === o && e.indexes[n] < u && (i = n);
295
- }
296
- if (L(e))
297
- return N(e), !1;
298
- if (r && t)
299
- return !0;
300
- for (n--; n >= 0; n--) {
301
- if (e.argValues[n] == null)
302
- continue;
303
- let l = n > i;
304
- const u = I(e, n, l), g = e.indexes[n] + 1;
305
- if (g <= u) {
306
- e.indexes[n] = g, e.args[e.argsNames[n]] = e.argValues[n][g], g < u && (l = !0);
307
- for (let m = n + 1; m < o; m++)
308
- e.args[e.argsNames[m]] = void 0;
309
- for (n++; n < o; n++) {
310
- e.argValues[n] = P(
311
- e,
312
- e.argsNames[n]
313
- );
314
- const m = I(e, n, l);
315
- if (m < 0)
316
- break;
317
- e.indexes[n] = 0, e.args[e.argsNames[n]] = e.argValues[n][0], m > 0 && (l = !0);
318
- }
319
- if (n >= o)
320
- return L(e) ? (N(e), !1) : !0;
321
- }
322
- }
323
- return N(e), !1;
324
- }
325
- function Q(e) {
326
- L(e) && N(e);
327
- let r = !1, t = !0;
328
- const o = e.indexes.length;
329
- let i = o, s = !1, n = 0;
330
- for (; n < o; n++) {
331
- const l = e.argValues[n] == null;
332
- (l || s) && (l && (r = !0), e.argValues[n] = P(
333
- e,
334
- e.argsNames[n]
335
- ));
336
- const u = I(
337
- e,
338
- n,
339
- n > i
340
- );
341
- if (u < 0) {
342
- t = !1, e.indexes[n] = -1;
343
- break;
344
- }
345
- l && (e.indexes[n] = u, e.args[e.argsNames[n]] = e.argValues[n][u]), (s || e.indexes[n] > u) && (e.indexes[n] = u, e.args[e.argsNames[n]] = e.argValues[n][u], s = !0), i === o && e.indexes[n] < u && (i = n);
346
- }
347
- if ((r || s) && t && !L(e))
348
- return !0;
349
- for (n--; n >= 0; n--) {
350
- if (e.argValues[n] == null)
351
- continue;
352
- let l = n > i;
353
- const u = I(e, n, l);
354
- let g = e.indexes[n] - 1;
355
- if (g > u && (g = u), g >= 0) {
356
- e.indexes[n] = g, e.args[e.argsNames[n]] = e.argValues[n][g], g < u && (l = !0);
357
- for (let m = n + 1; m < o; m++)
358
- e.args[e.argsNames[m]] = void 0;
359
- for (n++; n < o; n++) {
360
- e.argValues[n] = P(
361
- e,
362
- e.argsNames[n]
363
- );
364
- const m = I(e, n, l);
365
- if (m < 0)
366
- break;
367
- e.indexes[n] = m, e.args[e.argsNames[n]] = e.argValues[n][m], m > 0 && (l = !0);
368
- }
369
- if (n >= o)
370
- return !0;
371
- }
372
- }
373
- return N(e), !1;
374
- }
375
- function mt(e, r) {
376
- N(e);
377
- const t = e.argsNames, o = t.length;
378
- let i = !1;
379
- for (let s = 0; s < o; s++) {
380
- const n = t[s], l = r[n];
381
- if (l === void 0)
382
- return null;
383
- e.argValues[s] = P(e, n);
384
- const u = I(e, s, i);
385
- if (u < 0)
386
- return null;
387
- const g = R(
388
- e.argValues[s],
389
- l,
390
- e.equals
391
- );
392
- if (g < 0 || g > u)
393
- return null;
394
- e.indexes[s] = g, e.args[e.argsNames[s]] = e.argValues[s][g], e.indexes[s] < u && (i = !0);
395
- }
396
- return L(e) ? null : e.indexes.slice();
397
- }
398
- function dt(e) {
399
- const r = e.indexes.length;
400
- if (r === 0)
401
- return !1;
402
- let t = !1;
403
- for (let o = 0; o < r; o++) {
404
- e.argValues[o] = P(e, e.argsNames[o]);
405
- const i = I(e, o, t);
406
- if (i < 0)
407
- return Math.random() < 0.5 ? ye(e) : Q(e);
408
- e.indexes[o] = Math.floor(Math.random() * (i + 1)), e.args[e.argsNames[o]] = e.argValues[o][e.indexes[o]], e.indexes[o] < i && (t = !0);
409
- }
410
- return L(e) ? Q(e) : !0;
411
- }
412
- function B(e) {
413
- return e.mode === "forward" || e.mode === "backward";
414
- }
415
- function gt(e, r, t, o) {
416
- const i = r[t], s = e.templates[t];
417
- if (typeof s != "function") {
418
- if (R(s, i, o) >= 0)
419
- return;
420
- s.push(i);
421
- return;
422
- }
423
- const n = e.extra[t];
424
- if (n == null) {
425
- e.extra[t] = [i];
426
- return;
427
- }
428
- R(n, i, o) >= 0 || n.push(i);
429
- }
430
- function pt(e, r, t) {
431
- for (const o in r)
432
- if (Object.prototype.hasOwnProperty.call(r, o)) {
433
- if (o === "seed")
434
- continue;
435
- gt(e, r, o, t);
436
- }
437
- }
438
- function fe(e, r) {
439
- for (const t in r)
440
- if (Object.prototype.hasOwnProperty.call(r, t)) {
441
- if (t === "seed")
442
- continue;
443
- if (!e[t])
444
- return !1;
445
- }
446
- return !0;
447
- }
448
- const ht = [{ mode: "forward" }];
449
- function bt(e) {
450
- const {
451
- argsTemplates: r,
452
- equals: t,
453
- limitArgOnError: o,
454
- includeErrorVariant: i,
455
- getSeed: s,
456
- iterationModes: n,
457
- onModeChange: l,
458
- limitCompletionCount: u,
459
- limitTests: g,
460
- limitTime: m
461
- } = e, h = e.timeController ?? W, y = {
462
- templates: Ze(r),
463
- extra: {}
464
- }, d = n == null || n.length === 0 ? ht : n, b = [];
465
- let v = null, M = null, f = 0, x = 0, S = !1, C = 0;
466
- function w() {
467
- S || (S = !0, C = h.now(), A(), f = 0, oe(), we());
468
- }
469
- function A() {
470
- for (let a = 0, c = d.length; a < c; a++)
471
- b.push(D());
472
- }
473
- function D() {
474
- return {
475
- navigationState: ce(
476
- y,
477
- t ?? null,
478
- o ?? null,
479
- i ?? null
480
- ),
481
- cycleCount: 0,
482
- completedCount: 0,
483
- testsInLastTurn: 0,
484
- tryNextVariantAttempts: 0,
485
- startTime: null
486
- };
487
- }
488
- function we() {
489
- v = ce(
490
- y,
491
- t ?? null,
492
- // Initial values; overwritten in calcIndexes before each use
493
- !1,
494
- !1
495
- );
496
- }
497
- function oe() {
498
- l?.({
499
- mode: d[f],
500
- modeIndex: f,
501
- tests: x
502
- });
503
- }
504
- function Te(a) {
505
- w(), fe(y.templates, a) && pt(y, a, t);
506
- }
507
- function se(a, c) {
508
- return w(), fe(y.templates, a) ? (v.limitArgOnError = c?.limitArg ?? o ?? null, v.includeErrorVariant = c?.includeLimit ?? i ?? !1, mt(v, a)) : null;
509
- }
510
- function Me(a) {
511
- const c = a?.args;
512
- if (c == null)
513
- return;
514
- w();
515
- const p = se(c);
516
- if (p != null) {
517
- M = {
518
- args: c,
519
- error: a?.error,
520
- tests: a?.tests ?? x
521
- }, v.argLimits = p;
522
- for (let E = 0, U = b.length; E < U; E++) {
523
- const J = b[E].navigationState;
524
- J.argLimits = p;
525
- }
526
- }
527
- }
528
- function Ie() {
529
- return w(), Ve();
530
- }
531
- function Ve() {
532
- for (; ; ) {
533
- if (!Ce())
534
- return null;
535
- for (; ; ) {
536
- const a = qe();
537
- if (a != null)
538
- return b[f].testsInLastTurn++, x++, a;
539
- if (Pe()) {
540
- if (!$e())
541
- return null;
542
- De();
543
- break;
544
- }
545
- }
546
- }
547
- }
548
- function Ce() {
549
- return !(Ae() || Oe() || q() && (Le() || !_e()) || !Ne());
550
- }
551
- function Ae() {
552
- return g != null && x >= g;
553
- }
554
- function Oe() {
555
- return m != null && h.now() - C >= m;
556
- }
557
- function Le() {
558
- if (!q())
559
- throw new Error("Unexpected behavior");
560
- return u != null && u <= 0;
561
- }
562
- function _e() {
563
- if (!q())
564
- throw new Error("Unexpected behavior");
565
- for (let a = 0, c = d.length; a < c; a++)
566
- if (_(d[a]) && z(a))
567
- return !0;
568
- return !1;
569
- }
570
- function Ne() {
571
- for (let a = 0, c = d.length; a < c; a++)
572
- if (z(a))
573
- return !0;
574
- return !1;
575
- }
576
- function Pe() {
577
- f++;
578
- const a = f >= d.length;
579
- return a && (f = 0), oe(), a;
580
- }
581
- function $e() {
582
- if (q()) {
583
- const a = Ge();
584
- if (u != null && a >= u)
585
- return !1;
586
- }
587
- return !0;
588
- }
589
- function q() {
590
- for (let a = 0, c = d.length; a < c; a++)
591
- if (B(d[a]))
592
- return !0;
593
- return !1;
594
- }
595
- function Ge() {
596
- let a = !1, c = 1 / 0;
597
- for (let p = 0, E = b.length; p < E; p++) {
598
- const U = b[p], J = d[p];
599
- B(J) && (a = !0, z(p) && U.completedCount < c && (c = U.completedCount));
600
- }
601
- if (!a)
602
- throw new Error("Unexpected behavior");
603
- return c;
604
- }
605
- function z(a) {
606
- const c = d[a], p = b[a];
607
- return c.limitTests != null && c.limitTests <= 0 || _(c) && (c.cycles != null && c.cycles <= 0 || c.attemptsPerVariant != null && c.attemptsPerVariant <= 0) ? !1 : p.tryNextVariantAttempts < 2;
608
- }
609
- function De() {
610
- f = 0;
611
- for (let a = 0, c = b.length; a < c; a++) {
612
- const p = b[a];
613
- p.testsInLastTurn = 0, p.startTime = null;
614
- }
615
- }
616
- function qe() {
617
- let a = 0;
618
- for (; a < 2; ) {
619
- if (!Ue())
620
- return null;
621
- const c = Fe();
622
- if (c != null)
623
- return c;
624
- if (_(d[f]) && je())
625
- return null;
626
- a++;
627
- }
628
- return null;
629
- }
630
- function Ue() {
631
- const a = d[f];
632
- return !(Be() || ke() || _(a) && !Re(f));
633
- }
634
- function Be() {
635
- const a = d[f], c = b[f];
636
- return a.limitTests != null && c.testsInLastTurn >= a.limitTests;
637
- }
638
- function ke() {
639
- const a = d[f], c = b[f];
640
- return a.limitTime != null && c.startTime != null && h.now() - c.startTime >= a.limitTime;
641
- }
642
- function _(a) {
643
- return B(a);
644
- }
645
- function Re(a) {
646
- const c = d[a], p = b[a];
647
- if (!_(c))
648
- throw new Error("Unexpected behavior");
649
- return p.cycleCount < (c.cycles ?? 1);
650
- }
651
- function je() {
652
- const a = d[f], c = b[f];
653
- if (!_(a))
654
- throw new Error("Unexpected behavior");
655
- return c.cycleCount++, c.cycleCount >= (a.cycles ?? 1) ? (c.cycleCount = 0, c.completedCount++, !0) : !1;
656
- }
657
- function Fe() {
658
- const a = d[f], c = b[f], p = c.navigationState;
659
- if ($(a)) {
660
- if (H())
661
- return null;
662
- const E = We();
663
- if (E != null)
664
- return c.startTime == null && (c.startTime = h.now()), E;
665
- }
666
- return He() ? (c.tryNextVariantAttempts = 0, $(a) && ze(), c.startTime == null && (c.startTime = h.now()), ae(p.args)) : (c.tryNextVariantAttempts++, null);
667
- }
668
- function We() {
669
- const a = d[f], p = b[f].navigationState;
670
- if (!$(a))
671
- throw new Error("Unexpected behavior");
672
- if (H())
673
- throw new Error("Unexpected behavior");
674
- const E = a.attemptsPerVariant ?? 1;
675
- return p.attempts > 0 && p.attempts < E ? L(p) ? null : (p.attempts++, ae(p.args)) : null;
676
- }
677
- function ze() {
678
- const a = d[f], p = b[f].navigationState;
679
- if (!$(a))
680
- throw new Error("Unexpected behavior");
681
- if (H())
682
- throw new Error("Unexpected behavior");
683
- p.attempts = 1;
684
- }
685
- function $(a) {
686
- return B(a);
687
- }
688
- function H() {
689
- const a = d[f];
690
- if (!$(a))
691
- throw new Error("Unexpected behavior");
692
- return (a.attemptsPerVariant ?? 1) <= 0;
693
- }
694
- function He() {
695
- const a = d[f], p = b[f].navigationState;
696
- switch (a.mode) {
697
- case "forward":
698
- return ye(p);
699
- case "backward":
700
- return Q(p);
701
- case "random":
702
- return dt(p);
703
- default:
704
- throw new Error(`Unknown mode: ${a.mode}`);
705
- }
706
- }
707
- function ae(a) {
708
- const c = { ...a };
709
- return s != null && (c.seed = s({ tests: x })), c;
710
- }
711
- return {
712
- get limit() {
713
- return M;
714
- },
715
- get modeIndex() {
716
- return f;
717
- },
718
- get modeConfigs() {
719
- return d;
720
- },
721
- get modeStates() {
722
- return b;
723
- },
724
- get tests() {
725
- return x;
726
- },
727
- calcIndexes: se,
728
- extendTemplates: Te,
729
- addLimit: Me,
730
- next: Ie
731
- };
732
- }
733
- function ie(e) {
734
- if (e == null || e <= 0)
735
- throw new Error(`Iterations = ${e}`);
736
- e--;
737
- const r = Ke().then(() => e);
738
- return e <= 0 ? r : r.then(ie);
739
- }
740
- function yt(e, r, t) {
741
- const o = r.limit ? {
742
- error: r.limit.error,
743
- args: r.limit.args,
744
- tests: r.limit.tests
745
- } : null;
746
- if (o && !t)
747
- throw o.error;
748
- return {
749
- iterations: e.iterations,
750
- bestError: o
751
- };
752
- }
753
- const me = 2 ** 31;
754
- function vt(e) {
755
- if (e == null)
756
- return { parallel: 1, sequentialOnError: !1 };
757
- if (typeof e == "boolean")
758
- return {
759
- parallel: e ? me : 1,
760
- sequentialOnError: !1
761
- };
762
- if (typeof e == "number")
763
- return { parallel: e > 0 ? e : 1, sequentialOnError: !1 };
764
- const r = e.count;
765
- let t = 1;
766
- return r === !0 ? t = me : typeof r == "number" && r > 0 && (t = r), {
767
- parallel: t,
768
- sequentialOnError: e.sequentialOnError ?? !1
769
- };
770
- }
771
- function St(e) {
772
- const r = e?.saveErrorVariants, t = r && e.createSaveErrorVariantsStore ? e.createSaveErrorVariantsStore(r) : null, o = e?.findBestError, { parallel: i, sequentialOnError: s } = vt(
773
- e?.parallel
774
- );
775
- return {
776
- store: t,
777
- GC_Iterations: e?.GC_Iterations ?? 1e6,
778
- GC_IterationsAsync: e?.GC_IterationsAsync ?? 1e4,
779
- GC_Interval: e?.GC_Interval ?? 1e3,
780
- logOptions: be(e?.log),
781
- abortSignalExternal: e?.abortSignal,
782
- findBestError: o,
783
- dontThrowIfError: o?.dontThrowIfError,
784
- timeController: e?.timeController ?? W,
785
- parallel: i,
786
- sequentialOnError: s
787
- };
788
- }
789
- function j(e, r, t) {
790
- const { options: o, variantsIterator: i } = e, s = i.limit?.args ?? r;
791
- if (!o.store)
792
- return;
793
- const n = o.store.save(s);
794
- if (t)
795
- return n;
796
- }
797
- function xt(e, r, t, o) {
798
- const { abortControllerParallel: i, state: s, options: n } = e, { logOptions: l } = n;
799
- if (e.options.findBestError)
800
- e.variantsIterator.addLimit({ args: r, error: t, tests: o }), s.debugMode = !1, j(e, r, !1), n.sequentialOnError && !i.signal.aborted ? (l.debug && l.func(
801
- "debug",
802
- "[test-variants] sequentialOnError: aborting parallel, switching to sequential"
803
- ), i.abort(new V())) : l.debug && l.func(
804
- "debug",
805
- "[test-variants] parallel error in findBestError mode, continuing with new limits"
806
- );
807
- else {
808
- if (i.signal.aborted)
809
- return;
810
- j(e, r, !1), i.abort(t);
811
- }
812
- }
813
- function de(e, r, t, o) {
814
- const { state: i } = e;
815
- if (e.options.findBestError) {
816
- e.variantsIterator.addLimit({ args: r, error: t, tests: o });
817
- const n = j(e, r, !0);
818
- if (n)
819
- return n.then(() => {
820
- i.debugMode = !1;
821
- });
822
- i.debugMode = !1;
823
- return;
824
- }
825
- const s = j(e, r, !0);
826
- if (s)
827
- return s.then(() => {
828
- throw t;
829
- });
830
- throw t;
831
- }
832
- function Et(e, r) {
833
- const { GC_Iterations: t, GC_IterationsAsync: o, GC_Interval: i } = e.options;
834
- return t > 0 && e.state.iterations - e.state.prevGcIterations >= t || o > 0 && e.state.iterationsAsync - e.state.prevGcIterationsAsync >= o || i > 0 && r - e.state.prevGcTime >= i;
835
- }
836
- async function wt(e, r) {
837
- e.prevGcIterations = e.iterations, e.prevGcIterationsAsync = e.iterationsAsync, e.prevGcTime = r, await ie(1);
838
- }
839
- function G(e) {
840
- const r = e / 1e3;
841
- if (r < 60)
842
- return `${r.toFixed(1)}s`;
843
- const t = r / 60;
844
- return t < 60 ? `${t.toFixed(1)}m` : `${(t / 60).toFixed(1)}h`;
845
- }
846
- function Y(e) {
847
- const r = e / 1073741824;
848
- if (r >= 1)
849
- return r >= 10 ? `${Math.round(r)}GB` : `${r.toFixed(1)}GB`;
850
- const t = e / (1024 * 1024);
851
- return t >= 10 ? `${Math.round(t)}MB` : `${t.toFixed(1)}MB`;
852
- }
853
- function Tt(e, r) {
854
- if (!e)
855
- return `mode[${r}]: null`;
856
- let t = `mode[${r}]: ${e.mode}`;
857
- return (e.mode === "forward" || e.mode === "backward") && (e.cycles != null && (t += `, cycles=${e.cycles}`), e.attemptsPerVariant != null && (t += `, attempts=${e.attemptsPerVariant}`)), e.limitTime != null && (t += `, limitTime=${G(e.limitTime)}`), e.limitTests != null && (t += `, limitTests=${e.limitTests}`), t;
858
- }
859
- function ve(e, r) {
860
- const t = e - r, o = t >= 0 ? "+" : "";
861
- return `${Y(e)} (${o}${Y(t)})`;
862
- }
863
- function Mt(e, r) {
864
- if (!e.start)
865
- return;
866
- let t = "[test-variants] start";
867
- r != null && (t += `, memory: ${Y(r)}`), e.func("start", t);
868
- }
869
- function It(e) {
870
- const { options: r, state: t } = e, { logOptions: o, timeController: i } = r;
871
- if (!o.completed)
872
- return;
873
- const s = i.now() - t.startTime;
874
- let n = `[test-variants] end, tests: ${t.tests} (${G(s)}), maxTime: ${G(t.maxTestDuration)}, async: ${t.iterationsAsync}`;
875
- if (t.startMemory != null) {
876
- const l = ne();
877
- l != null && (n += `, memory: ${ve(l, t.startMemory)}`);
878
- }
879
- o.func("completed", n);
880
- }
881
- function Se(e) {
882
- const { options: r, state: t } = e, { logOptions: o } = r, i = t.pendingModeChange;
883
- !o.modeChange || i == null || (o.func(
884
- "modeChange",
885
- `[test-variants] ${Tt(i.mode, i.modeIndex)}`
886
- ), t.pendingModeChange = null);
887
- }
888
- function Vt(e) {
889
- const { options: r, state: t } = e, { logOptions: o, timeController: i } = r, s = i.now();
890
- if (!o.progress || s - t.prevLogTime < o.progress)
891
- return !1;
892
- Se(e);
893
- const n = s - t.startTime;
894
- let l = `[test-variants] tests: ${t.tests} (${G(n)}), maxTime: ${G(t.maxTestDuration)}, async: ${t.iterationsAsync}`;
895
- if (t.prevLogMemory != null) {
896
- const u = ne();
897
- u != null && (l += `, memory: ${ve(u, t.prevLogMemory)}`, t.prevLogMemory = u);
898
- }
899
- return o.func("progress", l), t.prevLogTime = s, !0;
900
- }
901
- function ee(e, r) {
902
- e.debugMode = !1, r && (e.iterationsAsync += r.iterationsAsync, e.iterations += r.iterationsSync + r.iterationsAsync);
903
- }
904
- function te(e) {
905
- e.state.debugMode = !0, e.abortControllerParallel.abort(new V());
906
- }
907
- function xe(e, r) {
908
- const { testRun: t, testOptions: o, state: i } = e, s = i.tests;
909
- i.tests++;
910
- try {
911
- const n = t(r, s, o);
912
- if (O(n))
913
- return n.then(
914
- (l) => {
915
- if (!l) {
916
- te(e);
917
- return;
918
- }
919
- ee(i, l);
920
- },
921
- (l) => de(e, r, l, s)
922
- );
923
- if (!n) {
924
- te(e);
925
- return;
926
- }
927
- ee(i, n);
928
- } catch (n) {
929
- return n instanceof V ? void 0 : de(e, r, n, s);
930
- }
931
- }
932
- function Ct(e, r) {
933
- const { pool: t, abortSignal: o, testRun: i, testOptionsParallel: s, state: n } = e;
934
- if (!t)
935
- return;
936
- const l = n.tests;
937
- n.tests++, (async () => {
938
- try {
939
- if (o.aborted)
940
- return;
941
- let u = i(r, l, s);
942
- if (O(u) && (u = await u), !u) {
943
- te(e);
944
- return;
945
- }
946
- ee(n, u);
947
- } catch (u) {
948
- if (u instanceof V)
949
- return;
950
- xt(e, r, u, l);
951
- } finally {
952
- t.release(1);
953
- }
954
- })();
955
- }
956
- function Ee(e) {
957
- const { options: r, state: t } = e, { logOptions: o, timeController: i, GC_Interval: s } = r;
958
- if (!o.progress && !s)
959
- return;
960
- Vt(e);
961
- const n = i.now();
962
- if (Et(e, n))
963
- return wt(t, n);
964
- }
965
- function F(e) {
966
- return e.options.abortSignalExternal?.aborted ?? !1;
967
- }
968
- function re(e) {
969
- return e.abortSignal.aborted;
970
- }
971
- async function X(e, r) {
972
- const { pool: t, state: o, options: i } = e, { parallel: s, logOptions: n } = i;
973
- let l = null;
974
- for (; !F(e); ) {
975
- const u = t && !re(e);
976
- let g = !1;
977
- u && (t.hold(1) || await le({ pool: t, count: 1, hold: !0 }), g = !0);
978
- try {
979
- if (r != null ? (l = r, r = null) : e.state.debugMode || (l = e.variantsIterator.next()), l == null)
980
- break;
981
- const m = Ee(e);
982
- if (O(m) && await m, F(e))
983
- continue;
984
- if (u)
985
- Ct(e, l), g = !1;
986
- else {
987
- n.debug && t && re(e) && n.func(
988
- "debug",
989
- `[test-variants] parallel aborted, running sequential: tests=${o.tests}`
990
- );
991
- const h = xe(e, l);
992
- O(h) && await h;
993
- }
994
- } finally {
995
- g && t.release(1);
996
- }
997
- }
998
- t && (await le({ pool: t, count: s, hold: !0 }), t.release(s));
999
- }
1000
- function At(e) {
1001
- const { pool: r, state: t, options: o } = e, { logOptions: i } = o;
1002
- if (r)
1003
- return X(e);
1004
- let s = null;
1005
- for (; !F(e) && (e.state.debugMode || (s = e.variantsIterator.next()), s != null); ) {
1006
- const n = Ee(e);
1007
- if (O(n))
1008
- return n.then(() => X(e, s));
1009
- if (F(e))
1010
- continue;
1011
- i.debug && re(e) && i.func(
1012
- "debug",
1013
- `[test-variants] parallel aborted, running sequential: tests=${t.tests}`
1014
- );
1015
- const l = xe(e, s);
1016
- if (O(l))
1017
- return l.then(() => X(e));
1018
- }
1019
- }
1020
- async function Ot(e, r, t, o) {
1021
- const i = St(o), {
1022
- store: s,
1023
- logOptions: n,
1024
- abortSignalExternal: l,
1025
- findBestError: u,
1026
- dontThrowIfError: g,
1027
- timeController: m,
1028
- parallel: h
1029
- } = i, y = new K(), d = new K(), b = Z(
1030
- l,
1031
- y.signal
1032
- ), v = Z(
1033
- b,
1034
- d.signal
1035
- ), M = {
1036
- abortSignal: b,
1037
- timeController: m
1038
- }, f = {
1039
- abortSignal: v,
1040
- timeController: m
1041
- };
1042
- s && await s.replay({
1043
- testRun: e,
1044
- variantsIterator: r,
1045
- testOptions: M,
1046
- findBestErrorEnabled: !!u,
1047
- state: t
1048
- });
1049
- const x = h <= 1 ? null : new Ye(h);
1050
- Mt(n, t.startMemory);
1051
- const S = {
1052
- options: i,
1053
- testRun: e,
1054
- variantsIterator: r,
1055
- testOptions: M,
1056
- testOptionsParallel: f,
1057
- abortControllerGlobal: y,
1058
- abortControllerParallel: d,
1059
- abortSignal: v,
1060
- pool: x,
1061
- state: t
1062
- };
1063
- Se(S);
1064
- try {
1065
- await At(S), v.throwIfAborted();
1066
- } catch (C) {
1067
- throw y.abort(new V()), C;
1068
- }
1069
- return b.throwIfAborted(), y.abort(new V()), It(S), await ie(1), yt(t, r, g);
1070
- }
1071
- function Gt(e) {
1072
- return function(t) {
1073
- return async function(i) {
1074
- const s = be(i?.log), n = i?.timeController ?? W, l = ne(), u = st(n, l), g = ft(e, {
1075
- onStart: i?.onStart,
1076
- onEnd: i?.onEnd,
1077
- onError: i?.onError,
1078
- log: s,
1079
- pauseDebuggerOnError: i?.pauseDebuggerOnError,
1080
- timeout: i?.timeout,
1081
- state: u
1082
- }), m = i?.onModeChange;
1083
- function h(d) {
1084
- u.pendingModeChange = d, m?.(d);
1085
- }
1086
- const y = bt({
1087
- argsTemplates: t,
1088
- getSeed: i?.getSeed,
1089
- iterationModes: i?.iterationModes,
1090
- equals: i?.findBestError?.equals,
1091
- limitArgOnError: i?.findBestError?.limitArgOnError,
1092
- includeErrorVariant: i?.findBestError?.includeErrorVariant,
1093
- timeController: n,
1094
- onModeChange: h,
1095
- limitCompletionCount: i?.cycles ?? 1,
1096
- limitTests: i?.limitTests,
1097
- limitTime: i?.limitTime
1098
- });
1099
- return Ot(g, y, u, i);
1100
- };
1101
- };
1102
- }
1103
- export {
1104
- et as T,
1105
- Gt as c
1106
- };