@flemist/test-variants 5.0.12 → 5.0.13

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