@flemist/test-variants 2.0.4 → 3.0.0

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,353 @@
1
+ /** Find last index of value in array; uses custom equals or strict equality */
2
+ function findLastIndex(values, value, equals) {
3
+ if (equals) {
4
+ for (let i = values.length - 1; i >= 0; i--) {
5
+ if (equals(values[i], value)) {
6
+ return i;
7
+ }
8
+ }
9
+ return -1;
10
+ }
11
+ for (let i = values.length - 1; i >= 0; i--) {
12
+ if (values[i] === value) {
13
+ return i;
14
+ }
15
+ }
16
+ return -1;
17
+ }
18
+ /** Calculate template values for given key index */
19
+ function calcTemplateValues(templates, args, keyIndex) {
20
+ const template = templates[keyIndex];
21
+ if (typeof template === 'function') {
22
+ return template(args);
23
+ }
24
+ return template;
25
+ }
26
+ /** Reset iterator state for new cycle */
27
+ function resetIteratorState(state, templates, keysCount) {
28
+ state.index = -1;
29
+ state.repeatIndex = 0;
30
+ for (let i = 0; i < keysCount; i++) {
31
+ state.indexes[i] = -1;
32
+ state.variants[i] = [];
33
+ }
34
+ if (keysCount > 0) {
35
+ state.variants[0] = calcTemplateValues(templates, state.args, 0);
36
+ }
37
+ }
38
+ /** Get effective max index for an arg (considering argLimit) */
39
+ function getMaxIndex(state, keyIndex) {
40
+ const variantsLen = state.variants[keyIndex].length;
41
+ const argLimit = state.argLimits[keyIndex];
42
+ if (argLimit == null) {
43
+ return variantsLen;
44
+ }
45
+ return argLimit < variantsLen ? argLimit : variantsLen;
46
+ }
47
+ /** Advance to next variant in cartesian product; returns true if successful */
48
+ function advanceVariant(state, templates, keys, keysCount) {
49
+ var _a;
50
+ for (let keyIndex = keysCount - 1; keyIndex >= 0; keyIndex--) {
51
+ const valueIndex = state.indexes[keyIndex] + 1;
52
+ const maxIndex = getMaxIndex(state, keyIndex);
53
+ if (valueIndex < maxIndex) {
54
+ const key = keys[keyIndex];
55
+ const value = state.variants[keyIndex][valueIndex];
56
+ state.indexes[keyIndex] = valueIndex;
57
+ state.args[key] = value;
58
+ for (keyIndex++; keyIndex < keysCount; keyIndex++) {
59
+ const keyVariants = calcTemplateValues(templates, state.args, keyIndex);
60
+ const keyMaxIndex = (_a = state.argLimits[keyIndex]) !== null && _a !== void 0 ? _a : keyVariants.length;
61
+ if (keyVariants.length === 0 || keyMaxIndex <= 0) {
62
+ break;
63
+ }
64
+ state.indexes[keyIndex] = 0;
65
+ state.variants[keyIndex] = keyVariants;
66
+ const key = keys[keyIndex];
67
+ const value = keyVariants[0];
68
+ state.args[key] = value;
69
+ }
70
+ if (keyIndex >= keysCount) {
71
+ return true;
72
+ }
73
+ }
74
+ }
75
+ return false;
76
+ }
77
+ /** Validate saved args keys match iterator's arg names (ignoring "seed" key) */
78
+ function validateArgsKeys(savedArgs, keysSet, keysCount) {
79
+ const savedKeys = Object.keys(savedArgs).filter(k => k !== 'seed');
80
+ if (savedKeys.length !== keysCount) {
81
+ return false;
82
+ }
83
+ for (const key of savedKeys) {
84
+ if (!keysSet.has(key)) {
85
+ return false;
86
+ }
87
+ }
88
+ return true;
89
+ }
90
+ /** For static templates, verify arg value exists in template values */
91
+ function validateStaticArgsValues(savedArgs, templates, keys, keysCount, equals) {
92
+ for (let i = 0; i < keysCount; i++) {
93
+ const template = templates[i];
94
+ if (typeof template !== 'function') {
95
+ const value = savedArgs[keys[i]];
96
+ if (findLastIndex(template, value, equals) < 0) {
97
+ return false;
98
+ }
99
+ }
100
+ }
101
+ return true;
102
+ }
103
+ /** Check if current position >= pending args position; returns false if current < pending or all args skipped */
104
+ function isPositionReached(state, pendingArgs, keys, keysCount, equals) {
105
+ let anyCompared = false;
106
+ for (let i = 0; i < keysCount; i++) {
107
+ const currentValueIndex = state.indexes[i];
108
+ const pendingValue = pendingArgs[keys[i]];
109
+ const pendingValueIndex = findLastIndex(state.variants[i], pendingValue, equals);
110
+ // Dynamic template value not found - skip this arg from comparison
111
+ if (pendingValueIndex < 0) {
112
+ continue;
113
+ }
114
+ anyCompared = true;
115
+ if (currentValueIndex < pendingValueIndex) {
116
+ return false;
117
+ }
118
+ if (currentValueIndex > pendingValueIndex) {
119
+ return true;
120
+ }
121
+ }
122
+ // All compared args are equal - position reached; or all args skipped - keep pending
123
+ return anyCompared;
124
+ }
125
+ /** Update per-arg limits from args values */
126
+ function updateArgLimits(state, limitArgs, templates, keys, keysCount, equals, limitArgOnError) {
127
+ if (!limitArgOnError) {
128
+ return;
129
+ }
130
+ for (let i = 0; i < keysCount; i++) {
131
+ const key = keys[i];
132
+ const value = limitArgs[key];
133
+ const values = state.variants[i].length > 0
134
+ ? state.variants[i]
135
+ : calcTemplateValues(templates, state.args, i);
136
+ const valueIndex = findLastIndex(values, value, equals);
137
+ // Skip if value not found or already at index 0
138
+ if (valueIndex <= 0) {
139
+ continue;
140
+ }
141
+ // Check callback if provided
142
+ if (typeof limitArgOnError === 'function') {
143
+ const shouldLimit = limitArgOnError({
144
+ name: key,
145
+ valueIndex,
146
+ values,
147
+ maxValueIndex: state.argLimits[i],
148
+ });
149
+ if (!shouldLimit) {
150
+ continue;
151
+ }
152
+ }
153
+ // Update limit: argLimit = min(current argLimit, valueIndex)
154
+ const currentLimit = state.argLimits[i];
155
+ if (currentLimit == null || valueIndex < currentLimit) {
156
+ state.argLimits[i] = valueIndex;
157
+ }
158
+ }
159
+ }
160
+ /** Process pending limits; returns true if any limit was applied */
161
+ function processPendingLimits(state, templates, keys, keysCount, equals, limitArgOnError) {
162
+ let applied = false;
163
+ for (let i = state.pendingLimits.length - 1; i >= 0; i--) {
164
+ const pending = state.pendingLimits[i];
165
+ if (isPositionReached(state, pending.args, keys, keysCount, equals)) {
166
+ // Current position >= pending position: apply limit
167
+ if (state.count == null || state.index < state.count) {
168
+ state.count = state.index;
169
+ state.limit = typeof pending.error !== 'undefined'
170
+ ? { args: pending.args, error: pending.error }
171
+ : { args: pending.args };
172
+ updateArgLimits(state, pending.args, templates, keys, keysCount, equals, limitArgOnError);
173
+ applied = true;
174
+ }
175
+ // Remove from pending
176
+ state.pendingLimits.splice(i, 1);
177
+ }
178
+ }
179
+ return applied;
180
+ }
181
+ /** Creates test variants iterator with limiting capabilities */
182
+ function testVariantsIterator(options) {
183
+ const { argsTemplates, getSeed, repeatsPerVariant: _repeatsPerVariant, equals, limitArgOnError } = options;
184
+ const repeatsPerVariant = _repeatsPerVariant !== null && _repeatsPerVariant !== void 0 ? _repeatsPerVariant : 1;
185
+ const keys = Object.keys(argsTemplates);
186
+ const templates = Object.values(argsTemplates);
187
+ const keysCount = keys.length;
188
+ const keysSet = new Set(keys);
189
+ // Initialize state
190
+ const indexes = [];
191
+ const variants = [];
192
+ const argLimits = [];
193
+ for (let i = 0; i < keysCount; i++) {
194
+ indexes[i] = -1;
195
+ variants[i] = [];
196
+ argLimits[i] = null;
197
+ }
198
+ const state = {
199
+ args: {},
200
+ indexes,
201
+ variants,
202
+ argLimits,
203
+ index: -1,
204
+ cycleIndex: -1,
205
+ repeatIndex: 0,
206
+ count: null,
207
+ limit: null,
208
+ started: false,
209
+ currentArgs: null,
210
+ pendingLimits: [],
211
+ };
212
+ const iterator = {
213
+ get index() {
214
+ return state.index;
215
+ },
216
+ get cycleIndex() {
217
+ return state.cycleIndex;
218
+ },
219
+ get count() {
220
+ return state.count;
221
+ },
222
+ get limit() {
223
+ return state.limit;
224
+ },
225
+ addLimit(_options) {
226
+ const hasArgs = typeof (_options === null || _options === void 0 ? void 0 : _options.args) !== 'undefined' && _options.args !== null;
227
+ const hasIndex = (_options === null || _options === void 0 ? void 0 : _options.index) != null;
228
+ // addLimit() or addLimit({error}) - uses current args and index
229
+ if (!hasArgs && !hasIndex) {
230
+ if (state.index < 0) {
231
+ throw new Error('[testVariantsIterator] addLimit() requires at least one next() call');
232
+ }
233
+ if (state.count == null || state.index < state.count) {
234
+ state.count = state.index;
235
+ state.limit = typeof (_options === null || _options === void 0 ? void 0 : _options.error) !== 'undefined'
236
+ ? { args: state.currentArgs, error: _options.error }
237
+ : { args: state.currentArgs };
238
+ updateArgLimits(state, state.args, templates, keys, keysCount, equals, limitArgOnError);
239
+ }
240
+ return;
241
+ }
242
+ // addLimit({index}) - only index limiting, no args
243
+ if (hasIndex && !hasArgs) {
244
+ if (state.count == null || _options.index < state.count) {
245
+ state.count = _options.index;
246
+ }
247
+ return;
248
+ }
249
+ // addLimit({args}) or addLimit({args, error}) - pending limit
250
+ if (hasArgs && !hasIndex) {
251
+ // Validate args keys match iterator's arg names
252
+ if (!validateArgsKeys(_options.args, keysSet, keysCount)) {
253
+ return; // Discard - unreproducible (templates changed)
254
+ }
255
+ // For static templates, verify arg value exists
256
+ if (!validateStaticArgsValues(_options.args, templates, keys, keysCount, equals)) {
257
+ return; // Discard - unreproducible (value not in template)
258
+ }
259
+ // Store as pending limit
260
+ const pending = typeof _options.error !== 'undefined'
261
+ ? { args: _options.args, error: _options.error }
262
+ : { args: _options.args };
263
+ state.pendingLimits.push(pending);
264
+ return;
265
+ }
266
+ // addLimit({args, index}) or addLimit({args, index, error}) - immediate index + pending args
267
+ if (hasArgs && hasIndex) {
268
+ // Check if this is earliest (before potentially updating count)
269
+ const isEarliest = state.count == null || _options.index < state.count;
270
+ // Always apply index limit
271
+ if (isEarliest) {
272
+ state.count = _options.index;
273
+ }
274
+ // Validate args for limit property update
275
+ if (!validateArgsKeys(_options.args, keysSet, keysCount)) {
276
+ return; // Skip per-arg limits and limit property update
277
+ }
278
+ if (!validateStaticArgsValues(_options.args, templates, keys, keysCount, equals)) {
279
+ return; // Skip per-arg limits and limit property update
280
+ }
281
+ // Update limit if this is earliest
282
+ if (isEarliest) {
283
+ state.limit = typeof _options.error !== 'undefined'
284
+ ? { args: _options.args, error: _options.error }
285
+ : { args: _options.args };
286
+ updateArgLimits(state, _options.args, templates, keys, keysCount, equals, limitArgOnError);
287
+ }
288
+ }
289
+ },
290
+ start() {
291
+ state.cycleIndex++;
292
+ resetIteratorState(state, templates, keysCount);
293
+ state.started = true;
294
+ },
295
+ next() {
296
+ if (!state.started) {
297
+ throw new Error('[testVariantsIterator] start() must be called before next()');
298
+ }
299
+ // Try next repeat for current variant
300
+ if (state.index >= 0 && state.repeatIndex + 1 < repeatsPerVariant) {
301
+ // Check if current variant is still within limit
302
+ if (state.count == null || state.index < state.count) {
303
+ state.repeatIndex++;
304
+ if (getSeed) {
305
+ const seed = getSeed({
306
+ variantIndex: state.index,
307
+ cycleIndex: state.cycleIndex,
308
+ repeatIndex: state.repeatIndex,
309
+ });
310
+ state.currentArgs = Object.assign(Object.assign({}, state.args), { seed });
311
+ }
312
+ else {
313
+ state.currentArgs = Object.assign({}, state.args);
314
+ }
315
+ return state.currentArgs;
316
+ }
317
+ }
318
+ // Move to next variant
319
+ state.repeatIndex = 0;
320
+ if (!advanceVariant(state, templates, keys, keysCount)) {
321
+ // First complete cycle sets count to total variant count
322
+ if (state.count == null) {
323
+ state.count = state.index + 1;
324
+ }
325
+ return null;
326
+ }
327
+ state.index++;
328
+ // Process pending limits at new position
329
+ if (state.pendingLimits.length > 0) {
330
+ processPendingLimits(state, templates, keys, keysCount, equals, limitArgOnError);
331
+ }
332
+ // Check count limit (may have been updated by pending limit)
333
+ if (state.count != null && state.index >= state.count) {
334
+ return null;
335
+ }
336
+ if (getSeed) {
337
+ const seed = getSeed({
338
+ variantIndex: state.index,
339
+ cycleIndex: state.cycleIndex,
340
+ repeatIndex: state.repeatIndex,
341
+ });
342
+ state.currentArgs = Object.assign(Object.assign({}, state.args), { seed });
343
+ }
344
+ else {
345
+ state.currentArgs = Object.assign({}, state.args);
346
+ }
347
+ return state.currentArgs;
348
+ },
349
+ };
350
+ return iterator;
351
+ }
352
+
353
+ export { testVariantsIterator };