@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 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("../createTestVariants-CHE7urh4.js"),x=require("path"),m=require("@flemist/simple-utils"),P=require("@flemist/simple-utils/node"),b=require("fs");function E(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const g=E(x),l=E(b);async function A(e){const t=await l.promises.stat(e).catch(()=>null);if(t==null)return[];if(!t.isDirectory())throw new Error(`[saveErrorVariants] path is not a directory: ${e}`);return(await l.promises.readdir(e)).filter(s=>s.endsWith(".json")).sort((s,a)=>s>a?-1:s<a?1:0).map(s=>g.join(e,s))}async function D(e,t){const n=await l.promises.readFile(e,"utf-8");let r;try{r=JSON.parse(n)}catch{throw new Error(`[saveErrorVariants] invalid JSON in file: ${e}`)}if(t)try{return t(r)}catch{throw new Error(`[saveErrorVariants] jsonToArgs failed for file: ${e}`)}return r}function L(e){const t=Math.random().toString(36).substring(2);return m.formatDateFileName(e.sessionDate,"UTC")+"_"+t+".json"}async function N(e,t,n){let r;if(n){const s=n(e);typeof s=="string"?r=s:r=JSON.stringify(s,null,2)}else r=JSON.stringify(e,null,2);await l.promises.mkdir(g.dirname(t),{recursive:!0}),await l.promises.writeFile(t,r,"utf-8")}function k(e,t){const n=Math.max(e.length,t.length);for(let r=0;r<n;r++){const s=e[r],a=t[r];if(s==null){if(a==null)continue;return 1}if(a==null||s<a)return-1;if(s>a)return 1}return 0}class q{options;filePath;lastSavedArgs=null;constructor(t){this.options=t;const n=new Date;this.filePath=g.resolve(t.dir,t.getFilePath?.({sessionDate:n})??L({sessionDate:n}))}async save(t){m.deepEqualJsonLike(t,this.lastSavedArgs)||(this.lastSavedArgs={...t},await P.fileLock({filePath:this.filePath,func:()=>N(t,this.filePath,this.options.argsToJson)}))}async replay(t){const{testRun:n,variantsIterator:r,testOptions:s,findBestErrorEnabled:a,state:u}=t,V=this.options.useToFindBestError,S=this.options.limitArg??!1,T=this.options.extendTemplates??!1,j=this.options.attemptsPerVariant??1,w=new Map;function d(i){let o=w.get(i);return o===void 0&&(o=r.calcIndexes(i,{includeLimit:!1,limitArg:S}),w.set(i,o)),o}const y=await A(this.options.dir),f=[];for(let i=0,o=y.length;i<o;i++){const p=y[i],c=await D(p,this.options.jsonToArgs);T&&r.extendTemplates(c),d(c)!=null&&f.push(c)}function F(i,o){return k(d(i),d(o))}f.sort(F);for(let i=0,o=f.length;i<o;i++){const p=f[i];for(let c=0;c<j;c++)try{const h=u.tests;u.tests++;const v=n(p,h,s);m.isPromiseLike(v)&&await v}catch(h){if(V&&a){r.addLimit({args:p,error:h});break}throw h}}}}function _(e){return new q(e)}function I(e){const t=O.createTestVariants(e);return function(r){const s=t(r);return function(u){return s({...u,createSaveErrorVariantsStore:_})}}}exports.createTestVariants=I;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("../createTestVariants-B-tIuCMH.js"),k=require("path"),v=require("@flemist/simple-utils"),I=require("@flemist/simple-utils/node"),L=require("fs");function F(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const S=F(k),h=F(L);async function N(e){const t=await h.promises.stat(e).catch(()=>null);if(t==null)return[];if(!t.isDirectory())throw new Error(`[saveErrorVariants] path is not a directory: ${e}`);return(await h.promises.readdir(e)).filter(s=>s.endsWith(".json")).sort((s,o)=>s>o?-1:s<o?1:0).map(s=>S.join(e,s))}async function q(e,t){const n=await h.promises.readFile(e,"utf-8");let r;try{r=JSON.parse(n)}catch{throw new Error(`[saveErrorVariants] invalid JSON in file: ${e}`)}if(t)try{return t(r)}catch{throw new Error(`[saveErrorVariants] jsonToArgs failed for file: ${e}`)}return r}function _(e){const t=Math.random().toString(36).substring(2);return v.formatDateFileName(e.sessionDate,"UTC")+"_"+t+".json"}async function J(e,t,n){let r;if(n){const s=n(e);typeof s=="string"?r=s:r=JSON.stringify(s,null,2)}else r=JSON.stringify(e,null,2);await h.promises.mkdir(S.dirname(t),{recursive:!0}),await h.promises.writeFile(t,r,"utf-8")}function B(e,t){const n=Math.max(e.length,t.length);for(let r=0;r<n;r++){const s=e[r],o=t[r];if(s==null){if(o==null)continue;return 1}if(o==null||s<o)return-1;if(s>o)return 1}return 0}class C{options;filePath;lastSavedArgs=null;constructor(t){this.options=t;const n=new Date;this.filePath=S.resolve(t.dir,t.getFilePath?.({sessionDate:n})??_({sessionDate:n}))}async save(t){v.deepEqualJsonLike(t,this.lastSavedArgs)||(this.lastSavedArgs={...t},await I.fileLock({filePath:this.filePath,func:()=>J(t,this.filePath,this.options.argsToJson)}))}async replay(t){const{testRun:n,variantsIterator:r,testOptions:s,findBestErrorEnabled:o,state:l,logOptions:u,timeControllerInternal:E}=t,P=this.options.useToFindBestError,b=this.options.limitArg??!1,$=this.options.extendTemplates??!1,D=this.options.attemptsPerVariant??1,T=new Map;function w(i){let a=T.get(i);return a===void 0&&(a=r.calcIndexes(i,{includeLimit:!1,limitArg:b}),T.set(i,a)),a}const d=await N(this.options.dir);if(d.length===0)return;const c=[];for(let i=0,a=d.length;i<a;i++){const p=d[i],f=await q(p,this.options.jsonToArgs);$&&r.extendTemplates(f),w(f)!=null?c.push(f):u.replay&&u.func("replay",`[test-variants] replay skipped, args not in template; file: ${p}`)}if(c.length===0)return;const V=u.replay;let A=0,O=0,j=0,m=null;if(V){A=E.now(),O=l.tests,j=l.iterationsAsync,m=y.getMemoryUsage();let i=`[test-variants] replay, files: ${c.length}/${d.length}`;m!=null&&(i+=`, memory: ${y.formatBytes(m)}`),u.func("replay",i)}function M(i,a){return B(w(i),w(a))}c.sort(M);for(let i=0,a=c.length;i<a;i++){const p=c[i];for(let f=0;f<D;f++)try{const g=l.tests;l.tests++;const x=n(p,g,s);v.isPromiseLike(x)&&await x}catch(g){if(P&&o){r.addLimit({args:p,error:g});break}throw g}}if(V){const i=E.now()-A,a=y.formatTestStats(l.tests-O,i,l.maxTestDuration,l.iterationsAsync-j,m);u.func("replay",`[test-variants] replay end, ${a.message}`)}}}function R(e){return new C(e)}function U(e){const t=y.createTestVariants(e);return function(r){const s=t(r);return function(l){return s({...l,createSaveErrorVariantsStore:R})}}}exports.createTestVariants=U;
@@ -1,21 +1,21 @@
1
- import { c as S } from "../createTestVariants-o9_knZSz.mjs";
2
- import * as d from "path";
3
- import { formatDateFileName as A, deepEqualJsonLike as T, isPromiseLike as P } from "@flemist/simple-utils";
4
- import { fileLock as j } from "@flemist/simple-utils/node";
5
- import * as c from "fs";
6
- async function L(e) {
7
- const t = await c.promises.stat(e).catch(() => null);
1
+ import { g as L, f as O, a as k, c as D } from "../createTestVariants-CHmhRbUZ.mjs";
2
+ import * as w from "path";
3
+ import { formatDateFileName as M, deepEqualJsonLike as N, isPromiseLike as J } from "@flemist/simple-utils";
4
+ import { fileLock as B } from "@flemist/simple-utils/node";
5
+ import * as m from "fs";
6
+ async function C(e) {
7
+ const t = await m.promises.stat(e).catch(() => null);
8
8
  if (t == null)
9
9
  return [];
10
10
  if (!t.isDirectory())
11
11
  throw new Error(`[saveErrorVariants] path is not a directory: ${e}`);
12
- return (await c.promises.readdir(e)).filter((s) => s.endsWith(".json")).sort((s, o) => s > o ? -1 : s < o ? 1 : 0).map((s) => d.join(e, s));
12
+ return (await m.promises.readdir(e)).filter((s) => s.endsWith(".json")).sort((s, o) => s > o ? -1 : s < o ? 1 : 0).map((s) => w.join(e, s));
13
13
  }
14
- async function N(e, t) {
15
- const n = await c.promises.readFile(e, "utf-8");
14
+ async function R(e, t) {
15
+ const i = await m.promises.readFile(e, "utf-8");
16
16
  let r;
17
17
  try {
18
- r = JSON.parse(n);
18
+ r = JSON.parse(i);
19
19
  } catch {
20
20
  throw new Error(`[saveErrorVariants] invalid JSON in file: ${e}`);
21
21
  }
@@ -29,22 +29,22 @@ async function N(e, t) {
29
29
  }
30
30
  return r;
31
31
  }
32
- function O(e) {
32
+ function b(e) {
33
33
  const t = Math.random().toString(36).substring(2);
34
- return A(e.sessionDate, "UTC") + "_" + t + ".json";
34
+ return M(e.sessionDate, "UTC") + "_" + t + ".json";
35
35
  }
36
- async function k(e, t, n) {
36
+ async function U(e, t, i) {
37
37
  let r;
38
- if (n) {
39
- const s = n(e);
38
+ if (i) {
39
+ const s = i(e);
40
40
  typeof s == "string" ? r = s : r = JSON.stringify(s, null, 2);
41
41
  } else
42
42
  r = JSON.stringify(e, null, 2);
43
- await c.promises.mkdir(d.dirname(t), { recursive: !0 }), await c.promises.writeFile(t, r, "utf-8");
43
+ await m.promises.mkdir(w.dirname(t), { recursive: !0 }), await m.promises.writeFile(t, r, "utf-8");
44
44
  }
45
- function D(e, t) {
46
- const n = Math.max(e.length, t.length);
47
- for (let r = 0; r < n; r++) {
45
+ function q(e, t) {
46
+ const i = Math.max(e.length, t.length);
47
+ for (let r = 0; r < i; r++) {
48
48
  const s = e[r], o = t[r];
49
49
  if (s == null) {
50
50
  if (o == null)
@@ -58,87 +58,111 @@ function D(e, t) {
58
58
  }
59
59
  return 0;
60
60
  }
61
- class I {
61
+ class W {
62
62
  options;
63
63
  filePath;
64
64
  lastSavedArgs = null;
65
65
  constructor(t) {
66
66
  this.options = t;
67
- const n = /* @__PURE__ */ new Date();
68
- this.filePath = d.resolve(
67
+ const i = /* @__PURE__ */ new Date();
68
+ this.filePath = w.resolve(
69
69
  t.dir,
70
- t.getFilePath?.({ sessionDate: n }) ?? O({ sessionDate: n })
70
+ t.getFilePath?.({ sessionDate: i }) ?? b({ sessionDate: i })
71
71
  );
72
72
  }
73
73
  async save(t) {
74
- T(t, this.lastSavedArgs) || (this.lastSavedArgs = { ...t }, await j({
74
+ N(t, this.lastSavedArgs) || (this.lastSavedArgs = { ...t }, await B({
75
75
  filePath: this.filePath,
76
- func: () => k(t, this.filePath, this.options.argsToJson)
76
+ func: () => U(t, this.filePath, this.options.argsToJson)
77
77
  }));
78
78
  }
79
79
  async replay(t) {
80
80
  const {
81
- testRun: n,
81
+ testRun: i,
82
82
  variantsIterator: r,
83
83
  testOptions: s,
84
84
  findBestErrorEnabled: o,
85
- state: f
86
- } = t, v = this.options.useToFindBestError, y = this.options.limitArg ?? !1, V = this.options.extendTemplates ?? !1, x = this.options.attemptsPerVariant ?? 1, g = /* @__PURE__ */ new Map();
87
- function m(i) {
88
- let a = g.get(i);
89
- return a === void 0 && (a = r.calcIndexes(i, {
85
+ state: l,
86
+ logOptions: u,
87
+ timeControllerInternal: v
88
+ } = t, F = this.options.useToFindBestError, $ = this.options.limitArg ?? !1, P = this.options.extendTemplates ?? !1, j = this.options.attemptsPerVariant ?? 1, E = /* @__PURE__ */ new Map();
89
+ function y(n) {
90
+ let a = E.get(n);
91
+ return a === void 0 && (a = r.calcIndexes(n, {
90
92
  includeLimit: !1,
91
- limitArg: y
92
- }), g.set(i, a)), a;
93
+ limitArg: $
94
+ }), E.set(n, a)), a;
93
95
  }
94
- const w = await L(this.options.dir), u = [];
95
- for (let i = 0, a = w.length; i < a; i++) {
96
- const h = w[i], l = await N(
97
- h,
96
+ const h = await C(this.options.dir);
97
+ if (h.length === 0)
98
+ return;
99
+ const c = [];
100
+ for (let n = 0, a = h.length; n < a; n++) {
101
+ const p = h[n], f = await R(
102
+ p,
98
103
  this.options.jsonToArgs
99
104
  );
100
- V && r.extendTemplates(l), m(l) != null && u.push(l);
105
+ P && r.extendTemplates(f), y(f) != null ? c.push(f) : u.replay && u.func("replay", `[test-variants] replay skipped, args not in template; file: ${p}`);
101
106
  }
102
- function F(i, a) {
103
- return D(m(i), m(a));
107
+ if (c.length === 0)
108
+ return;
109
+ const V = u.replay;
110
+ let S = 0, x = 0, A = 0, d = null;
111
+ if (V) {
112
+ S = v.now(), x = l.tests, A = l.iterationsAsync, d = L();
113
+ let n = `[test-variants] replay, files: ${c.length}/${h.length}`;
114
+ d != null && (n += `, memory: ${O(d)}`), u.func("replay", n);
104
115
  }
105
- u.sort(F);
106
- for (let i = 0, a = u.length; i < a; i++) {
107
- const h = u[i];
108
- for (let l = 0; l < x; l++)
116
+ function I(n, a) {
117
+ return q(y(n), y(a));
118
+ }
119
+ c.sort(I);
120
+ for (let n = 0, a = c.length; n < a; n++) {
121
+ const p = c[n];
122
+ for (let f = 0; f < j; f++)
109
123
  try {
110
- const p = f.tests;
111
- f.tests++;
112
- const E = n(h, p, s);
113
- P(E) && await E;
114
- } catch (p) {
115
- if (v && o) {
124
+ const g = l.tests;
125
+ l.tests++;
126
+ const T = i(p, g, s);
127
+ J(T) && await T;
128
+ } catch (g) {
129
+ if (F && o) {
116
130
  r.addLimit({
117
- args: h,
118
- error: p
131
+ args: p,
132
+ error: g
119
133
  });
120
134
  break;
121
135
  }
122
- throw p;
136
+ throw g;
123
137
  }
124
138
  }
139
+ if (V) {
140
+ const n = v.now() - S, a = k(
141
+ l.tests - x,
142
+ n,
143
+ l.maxTestDuration,
144
+ l.iterationsAsync - A,
145
+ d
146
+ );
147
+ u.func("replay", `[test-variants] replay end, ${a.message}`);
148
+ }
125
149
  }
126
150
  }
127
- function J(e) {
128
- return new I(e);
151
+ function _(e) {
152
+ return new W(e);
129
153
  }
130
- function M(e) {
131
- const t = S(e);
154
+ function K(e) {
155
+ const t = D(e);
132
156
  return function(r) {
133
157
  const s = t(r);
134
- return function(f) {
158
+ return function(l) {
135
159
  return s({
136
- ...f,
137
- createSaveErrorVariantsStore: J
160
+ ...l,
161
+ createSaveErrorVariantsStore: _
138
162
  });
139
163
  };
140
164
  };
141
165
  }
142
166
  export {
143
- M as createTestVariants
167
+ K as createTestVariants
144
168
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flemist/test-variants",
3
- "version": "5.0.16",
3
+ "version": "5.0.17",
4
4
  "description": "Runs a test function with all possible combinations of its parameters.",
5
5
  "sideEffects": false,
6
6
  "types": "build/common/index.d.ts",
@@ -1,4 +0,0 @@
1
- "use strict";const J=require("@flemist/time-controller"),x=require("@flemist/simple-utils"),j=require("@flemist/abort-controller-fast"),Y=require("@flemist/time-limits");class ge extends Error{}function oe(){if(typeof process<"u"&&process.memoryUsage)try{return process.memoryUsage.rss()}catch{}if(typeof performance<"u"){const e=performance.memory;if(e)try{return e.usedJSHeapSize}catch{}}return null}const He=1e3,F=[];function Je(e){return x.formatAny(e,{pretty:!0,maxDepth:5,maxItems:50})}function Xe(...e){const r=e.map(t=>typeof t=="string"?t:Je(t)).join(" ");F.push(r),F.length>He&&F.shift(),console.log(r)}function Ze(){return F.join(`
2
- `)}globalThis.__getStressTestLogLast=Ze;const pe=(e,r)=>{Xe(r)},he=e=>x.formatAny(e,{pretty:!0,maxDepth:20,maxItems:100,maxStringLength:5e3,dontExpandClassInstances:!0,dontExpandFunctions:!0}),I={start:!0,progress:5e3,completed:!0,error:!0,modeChange:!0,debug:!1,func:pe,format:he},Ke={start:!1,progress:!1,completed:!1,error:!1,modeChange:!1,debug:!1,func:pe,format:he};function be(e){return e===!1?Ke:e===!0||!e?I:{start:e.start??I.start,progress:e.progress??I.progress,completed:e.completed??I.completed,error:e.error??I.error,modeChange:e.modeChange??I.modeChange,debug:e.debug??I.debug,func:e.func??I.func,format:e.format??I.format}}function Qe(e,r){const t=e.now();return{startTime:t,startMemory:r,debugMode:!1,tests:0,iterations:0,iterationsAsync:0,maxTestDuration:0,prevLogTime:t,prevLogMemory:r,pendingModeChange:null,prevGcTime:t,prevGcIterations:0,prevGcIterationsAsync:0}}class A extends j.AbortError{}class Ye{_timeController;_subject;_interval;_timer;constructor(r,t){this._interval=r,this._timeController=t??J.timeControllerDefault,this._subject=new x.Subject({startStopNotifier:()=>(this.update(),()=>{this._timeController.clearTimeout(this._timer)})})}update(){this._subject.emit(this._timeController.now()),this._timer=this._timeController.setTimeout(()=>this.update(),this._interval)}get interval(){return this._interval}set interval(r){this._interval=r,this._timer&&(this._timeController.clearTimeout(this._timer),this.update())}subscribe(r){return this._subject.subscribe(r)}}const et=50,tt=5;function rt(e,r){return e==null?null:typeof e=="number"?e:e(r)??null}function ue(e,r){return typeof e=="number"?{iterationsAsync:0,iterationsSync:e}:e!=null&&typeof e=="object"?e:r?{iterationsAsync:1,iterationsSync:0}:{iterationsAsync:0,iterationsSync:1}}function nt(e,r){const t=r.log,o=r.pauseDebuggerOnError??!0,i=r.onStart,s=r.onEnd,n=r.state;let l=null,u=0;function m(y,b,f){l==null&&(l={error:y,args:b,tests:f},t.error&&t.func("error",`[test-variants] error variant: ${t.format(b)}
3
- tests: ${f}
4
- ${t.format(y)}`));const h=Date.now();if(o)debugger;if(Date.now()-h>et&&u<tt){t.func("debug",`[test-variants] debug iteration: ${u}`),u++;return}const w=l;throw l=null,r.onError&&r.onError(w),w.error}const d=new Ye(100);return function(b,f,h){i&&i({args:b,tests:f});const E=rt(r.timeout,b),w=Date.now();let g=h,S=null;E&&(S=new j.AbortControllerFast,g={abortSignal:x.combineAbortSignals(h.abortSignal,S.signal),timeController:h.timeController});function M(){S.abort(new ge(`[test-variants] test timeout ${E}ms exceeded`))}function C(){const v=Date.now()-w;v>n.maxTestDuration&&(n.maxTestDuration=v)}try{let v=e(b,g);if(x.isPromiseLike(v)){let P=null;return S&&(P=d.subscribe(()=>{Date.now()-w>E&&(M(),P())}),v=Promise.race([v,x.abortSignalToPromise(S.signal)])),v.then(O=>{P?.(),C();const D=ue(O,!0);return s&&s({args:b,tests:f,result:D}),D},O=>(P?.(),C(),s&&!(O instanceof A)&&s({args:b,tests:f,error:O}),m(O,b,f)))}C(),S&&Date.now()-w>E&&(M(),S.signal.throwIfAborted());const k=ue(v,!1);return s&&s({args:b,tests:f,result:k}),k}catch(v){return C(),v instanceof A?void 0:(s&&s({args:b,tests:f,error:v}),m(v,b,f))}}}function W(e,r,t){for(let o=0,i=e.length;o<i;o++)if(t?t(e[o],r):e[o]===r)return o;return-1}function ce(e,r,t,o){const i=Object.keys(e.templates),s={},n=[],l=[],u=[],m=i.length;for(let d=0;d<m;d++){const y=i[d];s[y]=void 0,n.push(-1),l.push(void 0),u.push(null)}return{args:s,argsNames:i,indexes:n,argValues:l,argLimits:u,attempts:0,templates:e,limitArgOnError:t,equals:r,includeErrorVariant:o??!1}}function $(e,r){const t=e.templates.templates[r],o=e.templates.extra[r];let i;if(typeof t=="function"?i=t(e.args):i=t,o==null)return i;let s=null;const n=o.length;for(let l=0;l<n;l++){const u=o[l];W(i,u,e.equals)<0&&(s==null?s=[...i,u]:s.push(u))}return s??i}function V(e,r,t){const o=e.argValues[r].length;if(o===0)return-1;const i=e.argLimits[r];if(i==null)return o-1;let s=e.limitArgOnError;if(typeof s=="function"){const n=e.argsNames[r];s=s({name:n,values:e.argValues[r],maxValueIndex:i})}return!t||s?Math.min(i,o-1):o-1}function L(e){const r=e.indexes.length;for(let t=0;t<r;t++){const o=e.argLimits[t];if(o==null)return!1;const i=e.indexes[t];if(i>o)return!0;if(i<o)return!1}return!e.includeErrorVariant}function N(e){const r=e.indexes.length;for(let t=0;t<r;t++)e.indexes[t]=-1,e.argValues[t]=void 0,e.args[e.argsNames[t]]=void 0}function ye(e){let r=!1,t=!0;const o=e.indexes.length;let i=o,s=!1,n=0;for(;n<o;n++){const l=e.argValues[n]==null;(l||s)&&(l&&(r=!0),e.argValues[n]=$(e,e.argsNames[n]));const u=V(e,n,n>i);if(u<0){t=!1,e.indexes[n]=-1;break}l&&(e.indexes[n]=0,e.args[e.argsNames[n]]=e.argValues[n][0]),(s||e.indexes[n]>u)&&(e.indexes[n]=u,e.args[e.argsNames[n]]=e.argValues[n][u],s=!0),i===o&&e.indexes[n]<u&&(i=n)}if(L(e))return N(e),!1;if(r&&t)return!0;for(n--;n>=0;n--){if(e.argValues[n]==null)continue;let l=n>i;const u=V(e,n,l),m=e.indexes[n]+1;if(m<=u){e.indexes[n]=m,e.args[e.argsNames[n]]=e.argValues[n][m],m<u&&(l=!0);for(let d=n+1;d<o;d++)e.args[e.argsNames[d]]=void 0;for(n++;n<o;n++){e.argValues[n]=$(e,e.argsNames[n]);const d=V(e,n,l);if(d<0)break;e.indexes[n]=0,e.args[e.argsNames[n]]=e.argValues[n][0],d>0&&(l=!0)}if(n>=o)return L(e)?(N(e),!1):!0}}return N(e),!1}function ee(e){L(e)&&N(e);let r=!1,t=!0;const o=e.indexes.length;let i=o,s=!1,n=0;for(;n<o;n++){const l=e.argValues[n]==null;(l||s)&&(l&&(r=!0),e.argValues[n]=$(e,e.argsNames[n]));const u=V(e,n,n>i);if(u<0){t=!1,e.indexes[n]=-1;break}l&&(e.indexes[n]=u,e.args[e.argsNames[n]]=e.argValues[n][u]),(s||e.indexes[n]>u)&&(e.indexes[n]=u,e.args[e.argsNames[n]]=e.argValues[n][u],s=!0),i===o&&e.indexes[n]<u&&(i=n)}if((r||s)&&t&&!L(e))return!0;for(n--;n>=0;n--){if(e.argValues[n]==null)continue;let l=n>i;const u=V(e,n,l);let m=e.indexes[n]-1;if(m>u&&(m=u),m>=0){e.indexes[n]=m,e.args[e.argsNames[n]]=e.argValues[n][m],m<u&&(l=!0);for(let d=n+1;d<o;d++)e.args[e.argsNames[d]]=void 0;for(n++;n<o;n++){e.argValues[n]=$(e,e.argsNames[n]);const d=V(e,n,l);if(d<0)break;e.indexes[n]=d,e.args[e.argsNames[n]]=e.argValues[n][d],d>0&&(l=!0)}if(n>=o)return!0}}return N(e),!1}function it(e,r){N(e);const t=e.argsNames,o=t.length;let i=!1;for(let s=0;s<o;s++){const n=t[s],l=r[n];if(l===void 0)return null;e.argValues[s]=$(e,n);const u=V(e,s,i);if(u<0)return null;const m=W(e.argValues[s],l,e.equals);if(m<0||m>u)return null;e.indexes[s]=m,e.args[e.argsNames[s]]=e.argValues[s][m],e.indexes[s]<u&&(i=!0)}return L(e)?null:e.indexes.slice()}function ot(e){const r=e.indexes.length;if(r===0)return!1;let t=!1;for(let o=0;o<r;o++){e.argValues[o]=$(e,e.argsNames[o]);const i=V(e,o,t);if(i<0)return Math.random()<.5?ye(e):ee(e);e.indexes[o]=Math.floor(Math.random()*(i+1)),e.args[e.argsNames[o]]=e.argValues[o][e.indexes[o]],e.indexes[o]<i&&(t=!0)}return L(e)?ee(e):!0}function R(e){return e.mode==="forward"||e.mode==="backward"}function st(e,r,t,o){const i=r[t],s=e.templates[t];if(typeof s!="function"){if(W(s,i,o)>=0)return;s.push(i);return}const n=e.extra[t];if(n==null){e.extra[t]=[i];return}W(n,i,o)>=0||n.push(i)}function at(e,r,t){for(const o in r)if(Object.prototype.hasOwnProperty.call(r,o)){if(o==="seed")continue;st(e,r,o,t)}}function fe(e,r){for(const t in r)if(Object.prototype.hasOwnProperty.call(r,t)){if(t==="seed")continue;if(!e[t])return!1}return!0}const lt=[{mode:"forward"}];function ut(e){const{argsTemplates:r,equals:t,limitArgOnError:o,includeErrorVariant:i,getSeed:s,iterationModes:n,onModeChange:l,limitCompletionCount:u,limitTests:m,limitTime:d}=e,y=e.timeController??J.timeControllerDefault,b={templates:x.deepCloneJsonLike(r),extra:{}},f=n==null||n.length===0?lt:n,h=[];let E=null,w=null,g=0,S=0,M=!1,C=0;function v(){M||(M=!0,C=y.now(),k(),g=0,D(),O())}function k(){for(let a=0,c=f.length;a<c;a++)h.push(P())}function P(){return{navigationState:ce(b,t??null,o??null,i??null),cycleCount:0,completedCount:0,testsInLastTurn:0,tryNextVariantAttempts:0,startTime:null}}function O(){E=ce(b,t??null,!1,!1)}function D(){l?.({mode:f[g],modeIndex:g,tests:S})}function we(a){v(),fe(b.templates,a)&&at(b,a,t)}function ae(a,c){return v(),fe(b.templates,a)?(E.limitArgOnError=c?.limitArg??o??null,E.includeErrorVariant=c?.includeLimit??i??!1,it(E,a)):null}function Te(a){const c=a?.args;if(c==null)return;v();const p=ae(c);if(p!=null){w={args:c,error:a?.error,tests:a?.tests??S},E.argLimits=p;for(let T=0,B=h.length;T<B;T++){const K=h[T].navigationState;K.argLimits=p}}}function Me(){return v(),Ce()}function Ce(){for(;;){if(!Ie())return null;for(;;){const a=Ge();if(a!=null)return h[g].testsInLastTurn++,S++,a;if(_e()){if(!Ne())return null;De();break}}}}function Ie(){return!(Ve()||Ae()||U()&&(Oe()||!Le())||!Pe())}function Ve(){return m!=null&&S>=m}function Ae(){return d!=null&&y.now()-C>=d}function Oe(){if(!U())throw new Error("Unexpected behavior");return u!=null&&u<=0}function Le(){if(!U())throw new Error("Unexpected behavior");for(let a=0,c=f.length;a<c;a++)if(_(f[a])&&X(a))return!0;return!1}function Pe(){for(let a=0,c=f.length;a<c;a++)if(X(a))return!0;return!1}function _e(){g++;const a=g>=f.length;return a&&(g=0),D(),a}function Ne(){if(U()){const a=$e();if(u!=null&&a>=u)return!1}return!0}function U(){for(let a=0,c=f.length;a<c;a++)if(R(f[a]))return!0;return!1}function $e(){let a=!1,c=1/0;for(let p=0,T=h.length;p<T;p++){const B=h[p],K=f[p];R(K)&&(a=!0,X(p)&&B.completedCount<c&&(c=B.completedCount))}if(!a)throw new Error("Unexpected behavior");return c}function X(a){const c=f[a],p=h[a];return c.limitTests!=null&&c.limitTests<=0||_(c)&&(c.cycles!=null&&c.cycles<=0||c.attemptsPerVariant!=null&&c.attemptsPerVariant<=0)?!1:p.tryNextVariantAttempts<2}function De(){g=0;for(let a=0,c=h.length;a<c;a++){const p=h[a];p.testsInLastTurn=0,p.startTime=null}}function Ge(){let a=0;for(;a<2;){if(!qe())return null;const c=Fe();if(c!=null)return c;if(_(f[g])&&Re())return null;a++}return null}function qe(){const a=f[g];return!(ke()||Ue()||_(a)&&!Be(g))}function ke(){const a=f[g],c=h[g];return a.limitTests!=null&&c.testsInLastTurn>=a.limitTests}function Ue(){const a=f[g],c=h[g];return a.limitTime!=null&&c.startTime!=null&&y.now()-c.startTime>=a.limitTime}function _(a){return R(a)}function Be(a){const c=f[a],p=h[a];if(!_(c))throw new Error("Unexpected behavior");return p.cycleCount<(c.cycles??1)}function Re(){const a=f[g],c=h[g];if(!_(a))throw new Error("Unexpected behavior");return c.cycleCount++,c.cycleCount>=(a.cycles??1)?(c.cycleCount=0,c.completedCount++,!0):!1}function Fe(){const a=f[g],c=h[g],p=c.navigationState;if(G(a)){if(Z())return null;const T=je();if(T!=null)return c.startTime==null&&(c.startTime=y.now()),T}return ze()?(c.tryNextVariantAttempts=0,G(a)&&We(),c.startTime==null&&(c.startTime=y.now()),le(p.args)):(c.tryNextVariantAttempts++,null)}function je(){const a=f[g],p=h[g].navigationState;if(!G(a))throw new Error("Unexpected behavior");if(Z())throw new Error("Unexpected behavior");const T=a.attemptsPerVariant??1;return p.attempts>0&&p.attempts<T?L(p)?null:(p.attempts++,le(p.args)):null}function We(){const a=f[g],p=h[g].navigationState;if(!G(a))throw new Error("Unexpected behavior");if(Z())throw new Error("Unexpected behavior");p.attempts=1}function G(a){return R(a)}function Z(){const a=f[g];if(!G(a))throw new Error("Unexpected behavior");return(a.attemptsPerVariant??1)<=0}function ze(){const a=f[g],p=h[g].navigationState;switch(a.mode){case"forward":return ye(p);case"backward":return ee(p);case"random":return ot(p);default:throw new Error(`Unknown mode: ${a.mode}`)}}function le(a){const c={...a};return s!=null&&(c.seed=s({tests:S})),c}return{get limit(){return w},get modeIndex(){return g},get modeConfigs(){return f},get modeStates(){return h},get tests(){return S},calcIndexes:ae,extendTemplates:we,addLimit:Te,next:Me}}function se(e){if(e==null||e<=0)throw new Error(`Iterations = ${e}`);e--;const r=x.waitMicrotasks().then(()=>e);return e<=0?r:r.then(se)}function ct(e,r,t){const o=r.limit?{error:r.limit.error,args:r.limit.args,tests:r.limit.tests}:null;if(o&&!t)throw o.error;return{iterations:e.iterations,bestError:o}}const me=2**31;function ft(e){if(e==null)return{parallel:1,sequentialOnError:!1};if(typeof e=="boolean")return{parallel:e?me:1,sequentialOnError:!1};if(typeof e=="number")return{parallel:e>0?e:1,sequentialOnError:!1};const r=e.count;let t=1;return r===!0?t=me:typeof r=="number"&&r>0&&(t=r),{parallel:t,sequentialOnError:e.sequentialOnError??!1}}function mt(e){const r=e?.saveErrorVariants,t=r&&e.createSaveErrorVariantsStore?e.createSaveErrorVariantsStore(r):null,o=e?.findBestError,{parallel:i,sequentialOnError:s}=ft(e?.parallel);return{store:t,GC_Iterations:e?.GC_Iterations??1e6,GC_IterationsAsync:e?.GC_IterationsAsync??1e4,GC_Interval:e?.GC_Interval??1e3,logOptions:be(e?.log),abortSignalExternal:e?.abortSignal,findBestError:o,dontThrowIfError:o?.dontThrowIfError,timeController:e?.timeController??J.timeControllerDefault,parallel:i,sequentialOnError:s}}function z(e,r,t){const{options:o,variantsIterator:i}=e,s=i.limit?.args??r;if(!o.store)return;const n=o.store.save(s);if(t)return n}function dt(e,r,t,o){const{abortControllerParallel:i,state:s,options:n}=e,{logOptions:l}=n;if(e.options.findBestError)e.variantsIterator.addLimit({args:r,error:t,tests:o}),s.debugMode=!1,z(e,r,!1),n.sequentialOnError&&!i.signal.aborted?(l.debug&&l.func("debug","[test-variants] sequentialOnError: aborting parallel, switching to sequential"),i.abort(new A)):l.debug&&l.func("debug","[test-variants] parallel error in findBestError mode, continuing with new limits");else{if(i.signal.aborted)return;z(e,r,!1),i.abort(t)}}function de(e,r,t,o){const{state:i}=e;if(e.options.findBestError){e.variantsIterator.addLimit({args:r,error:t,tests:o});const n=z(e,r,!0);if(n)return n.then(()=>{i.debugMode=!1});i.debugMode=!1;return}const s=z(e,r,!0);if(s)return s.then(()=>{throw t});throw t}function gt(e,r){const{GC_Iterations:t,GC_IterationsAsync:o,GC_Interval:i}=e.options;return t>0&&e.state.iterations-e.state.prevGcIterations>=t||o>0&&e.state.iterationsAsync-e.state.prevGcIterationsAsync>=o||i>0&&r-e.state.prevGcTime>=i}async function pt(e,r){e.prevGcIterations=e.iterations,e.prevGcIterationsAsync=e.iterationsAsync,e.prevGcTime=r,await se(1)}function q(e){const r=e/1e3;if(r<60)return`${r.toFixed(1)}s`;const t=r/60;return t<60?`${t.toFixed(1)}m`:`${(t/60).toFixed(1)}h`}function te(e){const r=e/1073741824;if(r>=1)return r>=10?`${Math.round(r)}GB`:`${r.toFixed(1)}GB`;const t=e/(1024*1024);return t>=10?`${Math.round(t)}MB`:`${t.toFixed(1)}MB`}function ht(e,r){if(!e)return`mode[${r}]: null`;let t=`mode[${r}]: ${e.mode}`;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}function ve(e,r){const t=e-r,o=t>=0?"+":"";return`${te(e)} (${o}${te(t)})`}function bt(e,r){if(!e.start)return;let t="[test-variants] start";r!=null&&(t+=`, memory: ${te(r)}`),e.func("start",t)}function yt(e){const{options:r,state:t}=e,{logOptions:o,timeController:i}=r;if(!o.completed)return;const s=i.now()-t.startTime;let n=`[test-variants] end, tests: ${t.tests} (${q(s)}), maxTime: ${q(t.maxTestDuration)}, async: ${t.iterationsAsync}`;if(t.startMemory!=null){const l=oe();l!=null&&(n+=`, memory: ${ve(l,t.startMemory)}`)}o.func("completed",n)}function Se(e){const{options:r,state:t}=e,{logOptions:o}=r,i=t.pendingModeChange;!o.modeChange||i==null||(o.func("modeChange",`[test-variants] ${ht(i.mode,i.modeIndex)}`),t.pendingModeChange=null)}function vt(e){const{options:r,state:t}=e,{logOptions:o,timeController:i}=r,s=i.now();if(!o.progress||s-t.prevLogTime<o.progress)return!1;Se(e);const n=s-t.startTime;let l=`[test-variants] tests: ${t.tests} (${q(n)}), maxTime: ${q(t.maxTestDuration)}, async: ${t.iterationsAsync}`;if(t.prevLogMemory!=null){const u=oe();u!=null&&(l+=`, memory: ${ve(u,t.prevLogMemory)}`,t.prevLogMemory=u)}return o.func("progress",l),t.prevLogTime=s,!0}function re(e,r){e.debugMode=!1,r&&(e.iterationsAsync+=r.iterationsAsync,e.iterations+=r.iterationsSync+r.iterationsAsync)}function ne(e){e.state.debugMode=!0,e.abortControllerParallel.abort(new A)}function xe(e,r){const{testRun:t,testOptions:o,state:i}=e,s=i.tests;i.tests++;try{const n=t(r,s,o);if(x.isPromiseLike(n))return n.then(l=>{if(!l){ne(e);return}re(i,l)},l=>de(e,r,l,s));if(!n){ne(e);return}re(i,n)}catch(n){return n instanceof A?void 0:de(e,r,n,s)}}function St(e,r){const{pool:t,abortSignal:o,testRun:i,testOptionsParallel:s,state:n}=e;if(!t)return;const l=n.tests;n.tests++,(async()=>{try{if(o.aborted)return;let u=i(r,l,s);if(x.isPromiseLike(u)&&(u=await u),!u){ne(e);return}re(n,u)}catch(u){if(u instanceof A)return;dt(e,r,u,l)}finally{t.release(1)}})()}function Ee(e){const{options:r,state:t}=e,{logOptions:o,timeController:i,GC_Interval:s}=r;if(!o.progress&&!s)return;vt(e);const n=i.now();if(gt(e,n))return pt(t,n)}function H(e){return e.options.abortSignalExternal?.aborted??!1}function ie(e){return e.abortSignal.aborted}async function Q(e,r){const{pool:t,state:o,options:i}=e,{parallel:s,logOptions:n}=i;let l=null;for(;!H(e);){const u=t&&!ie(e);let m=!1;u&&(t.hold(1)||await Y.poolWait({pool:t,count:1,hold:!0}),m=!0);try{if(r!=null?(l=r,r=null):e.state.debugMode||(l=e.variantsIterator.next()),l==null)break;const d=Ee(e);if(x.isPromiseLike(d)&&await d,H(e))continue;if(u)St(e,l),m=!1;else{n.debug&&t&&ie(e)&&n.func("debug",`[test-variants] parallel aborted, running sequential: tests=${o.tests}`);const y=xe(e,l);x.isPromiseLike(y)&&await y}}finally{m&&t.release(1)}}t&&(await Y.poolWait({pool:t,count:s,hold:!0}),t.release(s))}function xt(e){const{pool:r,state:t,options:o}=e,{logOptions:i}=o;if(r)return Q(e);let s=null;for(;!H(e)&&(e.state.debugMode||(s=e.variantsIterator.next()),s!=null);){const n=Ee(e);if(x.isPromiseLike(n))return n.then(()=>Q(e,s));if(H(e))continue;i.debug&&ie(e)&&i.func("debug",`[test-variants] parallel aborted, running sequential: tests=${t.tests}`);const l=xe(e,s);if(x.isPromiseLike(l))return l.then(()=>Q(e))}}async function Et(e,r,t,o){const i=mt(o),{store:s,logOptions:n,abortSignalExternal:l,findBestError:u,dontThrowIfError:m,timeController:d,parallel:y}=i,b=new j.AbortControllerFast,f=new j.AbortControllerFast,h=x.combineAbortSignals(l,b.signal),E=x.combineAbortSignals(h,f.signal),w={abortSignal:h,timeController:d},g={abortSignal:E,timeController:d};s&&await s.replay({testRun:e,variantsIterator:r,testOptions:w,findBestErrorEnabled:!!u,state:t});const S=y<=1?null:new Y.Pool(y);bt(n,t.startMemory);const M={options:i,testRun:e,variantsIterator:r,testOptions:w,testOptionsParallel:g,abortControllerGlobal:b,abortControllerParallel:f,abortSignal:E,pool:S,state:t};Se(M);try{await xt(M),E.throwIfAborted()}catch(C){throw b.abort(new A),C}return h.throwIfAborted(),b.abort(new A),yt(M),await se(1),ct(t,r,m)}function wt(e){return function(t){return async function(i){const s=be(i?.log),n=i?.timeController??J.timeControllerDefault,l=oe(),u=Qe(n,l),m=nt(e,{onStart:i?.onStart,onEnd:i?.onEnd,onError:i?.onError,log:s,pauseDebuggerOnError:i?.pauseDebuggerOnError,timeout:i?.timeout,state:u}),d=i?.onModeChange;function y(f){u.pendingModeChange=f,d?.(f)}const b=ut({argsTemplates:t,getSeed:i?.getSeed,iterationModes:i?.iterationModes,equals:i?.findBestError?.equals,limitArgOnError:i?.findBestError?.limitArgOnError,includeErrorVariant:i?.findBestError?.includeErrorVariant,timeController:n,onModeChange:y,limitCompletionCount:i?.cycles??1,limitTests:i?.limitTests,limitTime:i?.limitTime});return Et(m,b,u,i)}}}exports.TimeoutError=ge;exports.createTestVariants=wt;