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