@flemist/test-variants 5.0.15 → 5.0.17

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