@feasibleone/blong-chain 1.0.0 → 1.0.1

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.
package/dist/index.js ADDED
@@ -0,0 +1,656 @@
1
+ /**
2
+ * Parallel Test Executor
3
+ *
4
+ * Implements the new parallel test execution framework with:
5
+ * - Thenable proxies for automatic dependency detection
6
+ * - Parallel execution with configurable concurrency
7
+ * - Dependency graph tracking
8
+ * - Live progress tracking
9
+ * - Enhanced error reporting
10
+ * - Latency metrics
11
+ */
12
+ import assert from 'node:assert';
13
+ import { EventEmitter } from 'node:events';
14
+ import PQueue from 'p-queue';
15
+ /**
16
+ * Creates a thenable proxy for a given context path.
17
+ * The proxy acts as a Promise and supports nested property access.
18
+ *
19
+ * @param path - The context path (e.g., 'setupData' or 'setupData.user.name')
20
+ * @param promiseManager - The promise manager to get/create promises
21
+ * @returns A thenable proxy that can be awaited or have properties accessed
22
+ */
23
+ function createThenableProxy(path, promiseManager) {
24
+ // Get or create the promise for this path
25
+ const promiseEntry = promiseManager.getOrCreate(path);
26
+ // Create a proxy that intercepts property access
27
+ const proxy = new Proxy(promiseEntry.promise, {
28
+ get(target, prop) {
29
+ // Promise methods: delegate to the real promise
30
+ if (prop === 'then' || prop === 'catch' || prop === 'finally') {
31
+ return target[prop].bind(target);
32
+ }
33
+ // Symbol properties (like Symbol.toStringTag)
34
+ if (typeof prop === 'symbol') {
35
+ return target[prop];
36
+ }
37
+ // Property access: return nested thenable proxy
38
+ return createThenableProxy(`${path}.${prop}`, promiseManager);
39
+ },
40
+ });
41
+ return proxy;
42
+ }
43
+ /**
44
+ * Manages promises for all context paths.
45
+ * Provides lazy creation and caching of promises.
46
+ */
47
+ class PromiseManager {
48
+ promises = new Map();
49
+ realContext;
50
+ constructor(realContext) {
51
+ this.realContext = realContext;
52
+ }
53
+ /**
54
+ * Gets an existing promise or creates a new one for the given path
55
+ */
56
+ getOrCreate(path) {
57
+ if (this.promises.has(path)) {
58
+ return this.promises.get(path);
59
+ }
60
+ let resolve;
61
+ let reject;
62
+ const promise = new Promise((res, rej) => {
63
+ resolve = res;
64
+ reject = rej;
65
+ });
66
+ const entry = {
67
+ promise,
68
+ resolve: resolve,
69
+ reject: reject,
70
+ };
71
+ this.promises.set(path, entry);
72
+ // Check if this is a top-level step that has already completed
73
+ const parts = path.split('.');
74
+ const stepName = parts[0];
75
+ if (parts.length === 1 && stepName in this.realContext) {
76
+ // Step already completed, resolve immediately
77
+ entry.resolve(this.realContext[stepName]);
78
+ }
79
+ else if (parts.length > 1 && stepName in this.realContext) {
80
+ // Nested property of a completed step
81
+ const value = this._getNestedValue(this.realContext[stepName], parts.slice(1).join('.'));
82
+ entry.resolve(value);
83
+ }
84
+ else {
85
+ // Step hasn't completed yet, check if parent is already resolved
86
+ this._autoResolveIfParentResolved(path, entry);
87
+ }
88
+ return entry;
89
+ }
90
+ /**
91
+ * If parent path is already resolved, resolve this child path immediately
92
+ */
93
+ _autoResolveIfParentResolved(path, entry) {
94
+ const parts = path.split('.');
95
+ if (parts.length <= 1)
96
+ return; // No parent
97
+ // Check each parent level from most specific to least
98
+ for (let i = parts.length - 1; i > 0; i--) {
99
+ const parentPath = parts.slice(0, i).join('.');
100
+ const parentEntry = this.promises.get(parentPath);
101
+ if (parentEntry) {
102
+ // Wait for parent to resolve, then resolve child
103
+ parentEntry.promise
104
+ .then(parentValue => {
105
+ // Navigate to the child value
106
+ const childPath = parts.slice(i).join('.');
107
+ const childValue = this._getNestedValue(parentValue, childPath);
108
+ // Resolve the child promise
109
+ entry.resolve(childValue);
110
+ })
111
+ .catch(error => {
112
+ // Parent rejected, reject child too
113
+ entry.reject(error);
114
+ });
115
+ return;
116
+ }
117
+ }
118
+ }
119
+ /**
120
+ * Gets nested value from an object by path
121
+ */
122
+ _getNestedValue(obj, path) {
123
+ const parts = path.split('.');
124
+ let current = obj;
125
+ for (const part of parts) {
126
+ if (current && typeof current === 'object') {
127
+ current = current[part];
128
+ }
129
+ else {
130
+ return undefined;
131
+ }
132
+ }
133
+ return current;
134
+ }
135
+ /**
136
+ * Checks if a promise exists for the given path
137
+ */
138
+ has(path) {
139
+ return this.promises.has(path);
140
+ }
141
+ /**
142
+ * Resolves all promises related to a step's output
143
+ */
144
+ resolveStep(stepName, output) {
145
+ // Resolve the main step promise
146
+ if (this.promises.has(stepName)) {
147
+ this.promises.get(stepName).resolve(output);
148
+ }
149
+ // Resolve nested property promises
150
+ if (typeof output === 'object' && output !== null) {
151
+ this._resolveNestedProperties(stepName, output);
152
+ }
153
+ }
154
+ /**
155
+ * Recursively resolves promises for nested properties
156
+ */
157
+ _resolveNestedProperties(basePath, obj, depth = 0) {
158
+ // Limit recursion depth to avoid infinite loops
159
+ if (depth > 10)
160
+ return;
161
+ for (const [key, value] of Object.entries(obj)) {
162
+ const nestedPath = `${basePath}.${key}`;
163
+ if (this.promises.has(nestedPath)) {
164
+ this.promises.get(nestedPath).resolve(value);
165
+ }
166
+ // Recursively resolve deeper properties
167
+ if (typeof value === 'object' && value !== null) {
168
+ this._resolveNestedProperties(nestedPath, value, depth + 1);
169
+ }
170
+ }
171
+ }
172
+ /**
173
+ * Rejects a promise for a given path
174
+ */
175
+ reject(path, error) {
176
+ if (this.promises.has(path)) {
177
+ this.promises.get(path).reject(error);
178
+ }
179
+ }
180
+ }
181
+ /**
182
+ * Creates a context proxy that returns thenable proxies for all properties
183
+ * except $meta, which is always available directly.
184
+ *
185
+ * Also tracks which properties are accessed for dependency detection.
186
+ */
187
+ function createContextProxy(realContext, promiseManager, currentStep, dependencyTracker) {
188
+ return new Proxy(realContext, {
189
+ get(target, prop) {
190
+ // Special case: $meta is always available directly
191
+ if (prop === '$meta') {
192
+ return target.$meta;
193
+ }
194
+ // Track dependency if we're inside a step execution
195
+ if (currentStep && typeof prop === 'string') {
196
+ dependencyTracker.trackAccess(currentStep, prop);
197
+ }
198
+ // Return thenable proxy for step outputs
199
+ if (typeof prop === 'string') {
200
+ return createThenableProxy(prop, promiseManager);
201
+ }
202
+ return target[prop];
203
+ },
204
+ });
205
+ }
206
+ /**
207
+ * Tracks dependency relationships between steps
208
+ */
209
+ class DependencyTracker {
210
+ dependencies = new Map();
211
+ /**
212
+ * Records that a step accessed a property
213
+ */
214
+ trackAccess(fromStep, property) {
215
+ if (!this.dependencies.has(fromStep)) {
216
+ this.dependencies.set(fromStep, new Set());
217
+ }
218
+ this.dependencies.get(fromStep).add(property);
219
+ }
220
+ /**
221
+ * Gets all dependencies for a step
222
+ */
223
+ getDependencies(stepName) {
224
+ return Array.from(this.dependencies.get(stepName) || []);
225
+ }
226
+ /**
227
+ * Gets all dependency edges as graph edges
228
+ */
229
+ getEdges() {
230
+ const edges = [];
231
+ for (const [from, properties] of this.dependencies.entries()) {
232
+ for (const property of properties) {
233
+ // Extract the base step name from the property path
234
+ const to = property.split('.')[0];
235
+ edges.push({ from, to, property });
236
+ }
237
+ }
238
+ return edges;
239
+ }
240
+ }
241
+ /**
242
+ * Captures source location information for error reporting
243
+ */
244
+ function captureSourceLocation(fn) {
245
+ try {
246
+ const stack = new Error().stack || '';
247
+ const lines = stack.split('\n');
248
+ // Find the first line that's not from this file
249
+ for (let i = 2; i < lines.length; i++) {
250
+ const line = lines[i];
251
+ if (!line.includes('executor.ts') && !line.includes('executor.js')) {
252
+ // Try to parse: "at functionName (file:line:column)"
253
+ const match = line.match(/\((.+):(\d+):(\d+)\)/);
254
+ if (match) {
255
+ return {
256
+ file: match[1],
257
+ line: parseInt(match[2], 10),
258
+ column: parseInt(match[3], 10),
259
+ };
260
+ }
261
+ // Try alternative format: "at file:line:column"
262
+ const altMatch = line.match(/at (.+):(\d+):(\d+)/);
263
+ if (altMatch) {
264
+ return {
265
+ file: altMatch[1],
266
+ line: parseInt(altMatch[2], 10),
267
+ column: parseInt(altMatch[3], 10),
268
+ };
269
+ }
270
+ }
271
+ }
272
+ }
273
+ catch (error) {
274
+ // If parsing fails, return unknown location
275
+ }
276
+ return {
277
+ file: 'unknown',
278
+ line: 0,
279
+ column: 0,
280
+ };
281
+ }
282
+ /**
283
+ * Main test executor class
284
+ */
285
+ export class TestExecutor extends EventEmitter {
286
+ config;
287
+ queue;
288
+ dependencyTracker = new DependencyTracker();
289
+ // Progress tracking
290
+ progress = {
291
+ testName: 'test',
292
+ startTime: 0,
293
+ status: 'pending',
294
+ totalSteps: 0,
295
+ completedSteps: 0,
296
+ failedSteps: 0,
297
+ steps: new Map(),
298
+ groups: [],
299
+ };
300
+ // Dependency graph
301
+ graph = {
302
+ nodes: new Map(),
303
+ edges: [],
304
+ };
305
+ // Latency tracking
306
+ latencyMetrics = new Map();
307
+ // Real context (actual values)
308
+ realContext = {};
309
+ // Promise manager (needs realContext, initialized in constructor)
310
+ promiseManager;
311
+ // Test framework context for nested test output
312
+ testContext;
313
+ constructor(config = {}) {
314
+ super();
315
+ this.config = {
316
+ concurrency: config.concurrency ?? 10,
317
+ captureStackTraces: config.captureStackTraces ?? false,
318
+ framework: config.framework,
319
+ };
320
+ this.queue = new PQueue({ concurrency: this.config.concurrency });
321
+ // Initialize promise manager with reference to realContext
322
+ this.promiseManager = new PromiseManager(this.realContext);
323
+ }
324
+ /**
325
+ * Executes an array of test steps
326
+ */
327
+ async execute(steps, $meta, testContext) {
328
+ // Store test context for nested execution
329
+ this.testContext = testContext;
330
+ // Clear and initialize context with $meta (preserve reference for PromiseManager)
331
+ Object.keys(this.realContext).forEach(key => delete this.realContext[key]);
332
+ this.realContext.$meta = $meta;
333
+ // Initialize progress
334
+ this.progress.testName = steps.name || 'test';
335
+ this.progress.startTime = Date.now();
336
+ this.progress.status = 'running';
337
+ // Count total steps
338
+ this.progress.totalSteps = this._countSteps(steps);
339
+ // Emit test start event
340
+ this.emit('test:start', this.progress);
341
+ try {
342
+ // Execute all steps
343
+ await this._executeSteps(steps, [], this.testContext);
344
+ // Mark as completed
345
+ this.progress.status = 'completed';
346
+ this.progress.endTime = Date.now();
347
+ // Build final dependency graph
348
+ this.graph.edges = this.dependencyTracker.getEdges();
349
+ }
350
+ catch (error) {
351
+ this.progress.status = 'failed';
352
+ this.progress.endTime = Date.now();
353
+ throw error;
354
+ }
355
+ finally {
356
+ this.emit('test:end', this.progress);
357
+ }
358
+ }
359
+ /**
360
+ * Recursively executes steps, handling both functions and nested arrays
361
+ */
362
+ async _executeSteps(steps, groupPath, parentTestContext) {
363
+ const stepPromises = [];
364
+ for (const step of steps) {
365
+ if (Array.isArray(step)) {
366
+ // Nested array - wait for current level to complete first
367
+ await Promise.all(stepPromises);
368
+ stepPromises.length = 0;
369
+ const nestedGroupPath = [...groupPath, step.name || `group-${groupPath.length}`];
370
+ // If we have a test context, use it to create nested test scope
371
+ if (this.testContext && parentTestContext) {
372
+ const nestedName = step.name || `group-${groupPath.length}`;
373
+ await this.testContext.test.call(parentTestContext, nestedName, async (nestedContext) => {
374
+ await this._executeSteps(step, nestedGroupPath, nestedContext);
375
+ });
376
+ }
377
+ else if (this.testContext && groupPath.length === 0) {
378
+ // Top-level nested array
379
+ const nestedName = step.name || `group-${groupPath.length}`;
380
+ await this.testContext.test(nestedName, async (nestedContext) => {
381
+ await this._executeSteps(step, nestedGroupPath, nestedContext);
382
+ });
383
+ }
384
+ else {
385
+ // No test context, execute directly
386
+ await this._executeSteps(step, nestedGroupPath, parentTestContext);
387
+ }
388
+ }
389
+ else if (typeof step === 'function') {
390
+ // Execute function step in parallel
391
+ const promise = this._executeStep(step, groupPath, parentTestContext);
392
+ stepPromises.push(promise);
393
+ }
394
+ }
395
+ // Wait for remaining steps at this level
396
+ await Promise.all(stepPromises);
397
+ }
398
+ /**
399
+ * Executes a single step function
400
+ */
401
+ async _executeStep(fn, groupPath, parentTestContext) {
402
+ const stepName = fn.name || 'anonymous';
403
+ // Capture source location if enabled
404
+ const sourceLocation = this.config.captureStackTraces
405
+ ? captureSourceLocation(fn)
406
+ : undefined;
407
+ // Initialize step progress
408
+ const stepProgress = {
409
+ stepName,
410
+ displayName: stepName,
411
+ groupPath,
412
+ status: 'pending',
413
+ dependencies: [],
414
+ dependents: [],
415
+ sourceLocation,
416
+ };
417
+ this.progress.steps.set(stepName, stepProgress);
418
+ // Initialize dependency graph node
419
+ this.graph.nodes.set(stepName, {
420
+ stepName,
421
+ groupPath,
422
+ status: 'pending',
423
+ });
424
+ // Initialize latency tracking
425
+ const latency = {
426
+ stepName,
427
+ queuedAt: Date.now(),
428
+ queueTime: 0,
429
+ waitTime: 0,
430
+ executionTime: 0,
431
+ totalTime: 0,
432
+ };
433
+ this.latencyMetrics.set(stepName, latency);
434
+ // Wrap execution function for potential test context wrapping
435
+ const executeStepFn = async () => {
436
+ latency.startedAt = Date.now();
437
+ latency.queueTime = latency.startedAt - latency.queuedAt;
438
+ stepProgress.status = 'running';
439
+ stepProgress.startTime = latency.startedAt;
440
+ this.graph.nodes.get(stepName).status = 'running';
441
+ this.graph.nodes.get(stepName).startTime = latency.startedAt;
442
+ this.emit('step:start', stepName, stepProgress);
443
+ try {
444
+ // Create tracking context
445
+ const context = createContextProxy(this.realContext, this.promiseManager, stepName, this.dependencyTracker);
446
+ // Execute the step
447
+ const result = await fn(assert, context);
448
+ // Store result in real context
449
+ this.realContext[stepName] = result;
450
+ // Resolve all promises for this step
451
+ this.promiseManager.resolveStep(stepName, result);
452
+ // Update progress - calculate latency metrics
453
+ latency.completedAt = Date.now();
454
+ latency.totalTime = latency.completedAt - latency.queuedAt;
455
+ latency.executionTime = latency.completedAt - latency.startedAt;
456
+ latency.queueTime = latency.startedAt - latency.queuedAt;
457
+ latency.waitTime = 0; // TODO: More sophisticated wait time tracking
458
+ stepProgress.status = 'completed';
459
+ stepProgress.endTime = latency.completedAt;
460
+ stepProgress.duration = latency.totalTime;
461
+ stepProgress.queueTime = latency.queueTime;
462
+ stepProgress.executionTime = latency.executionTime;
463
+ stepProgress.waitTime = latency.waitTime;
464
+ stepProgress.result = result;
465
+ stepProgress.dependencies = this.dependencyTracker.getDependencies(stepName);
466
+ this.graph.nodes.get(stepName).status = 'completed';
467
+ this.graph.nodes.get(stepName).endTime = latency.completedAt;
468
+ this.progress.completedSteps++;
469
+ this.emit('step:end', stepName, stepProgress);
470
+ }
471
+ catch (error) {
472
+ // Handle error
473
+ latency.completedAt = Date.now();
474
+ latency.totalTime = latency.completedAt - latency.queuedAt;
475
+ latency.executionTime = latency.completedAt - latency.startedAt;
476
+ latency.queueTime = latency.startedAt - latency.queuedAt;
477
+ latency.waitTime = 0;
478
+ stepProgress.status = 'failed';
479
+ stepProgress.endTime = latency.completedAt;
480
+ stepProgress.duration = latency.totalTime;
481
+ stepProgress.dependencies = this.dependencyTracker.getDependencies(stepName);
482
+ const stepError = {
483
+ message: error.message,
484
+ stack: error.stack || '',
485
+ context: { ...this.realContext },
486
+ };
487
+ stepProgress.error = stepError;
488
+ this.graph.nodes.get(stepName).status = 'failed';
489
+ this.graph.nodes.get(stepName).endTime = latency.completedAt;
490
+ this.graph.nodes.get(stepName).error = error;
491
+ this.progress.failedSteps++;
492
+ this.emit('step:error', stepName, error, stepProgress);
493
+ // Reject promises for this step
494
+ this.promiseManager.reject(stepName, error);
495
+ throw error;
496
+ }
497
+ };
498
+ // If we have test context, wrap in nested test
499
+ if (this.testContext && parentTestContext) {
500
+ await this.queue.add(async () => {
501
+ try {
502
+ await this.testContext.test.call(parentTestContext, stepName, async () => {
503
+ await executeStepFn();
504
+ });
505
+ }
506
+ catch (error) {
507
+ // Error already handled in executeStepFn, don't rethrow to break the queue
508
+ // The test framework will report it
509
+ }
510
+ });
511
+ }
512
+ else if (this.testContext && groupPath.length === 0) {
513
+ // Top-level step with test context
514
+ await this.queue.add(async () => {
515
+ try {
516
+ await this.testContext.test(stepName, async () => {
517
+ await executeStepFn();
518
+ });
519
+ }
520
+ catch (error) {
521
+ // Error already handled in executeStepFn, don't rethrow to break the queue
522
+ }
523
+ });
524
+ }
525
+ else {
526
+ // No test context or not at top level
527
+ await this.queue.add(executeStepFn);
528
+ }
529
+ }
530
+ /**
531
+ * Counts total number of steps (including nested)
532
+ */
533
+ _countSteps(steps) {
534
+ let count = 0;
535
+ for (const step of steps) {
536
+ if (Array.isArray(step)) {
537
+ count += this._countSteps(step);
538
+ }
539
+ else if (typeof step === 'function') {
540
+ count++;
541
+ }
542
+ }
543
+ return count;
544
+ }
545
+ /**
546
+ * Gets the current progress snapshot
547
+ */
548
+ getProgress() {
549
+ return this.progress;
550
+ }
551
+ /**
552
+ * Gets the dependency graph
553
+ */
554
+ getDependencyGraph() {
555
+ return this.graph;
556
+ }
557
+ /**
558
+ * Gets latency metrics
559
+ */
560
+ getLatencyReport() {
561
+ const totalDuration = this.progress.endTime
562
+ ? this.progress.endTime - this.progress.startTime
563
+ : 0;
564
+ // Calculate critical path
565
+ const criticalPath = this._calculateCriticalPath();
566
+ // Calculate parallel efficiency
567
+ const totalStepTime = Array.from(this.latencyMetrics.values()).reduce((sum, l) => sum + l.executionTime, 0);
568
+ const parallelEfficiency = totalDuration > 0 ? totalStepTime / totalDuration : 0;
569
+ // Identify bottlenecks
570
+ const bottlenecks = this._identifyBottlenecks();
571
+ return {
572
+ testName: this.progress.testName,
573
+ totalDuration,
574
+ steps: this.latencyMetrics,
575
+ criticalPath,
576
+ parallelEfficiency,
577
+ bottlenecks,
578
+ };
579
+ }
580
+ /**
581
+ * Calculates the critical path (longest dependency chain)
582
+ */
583
+ _calculateCriticalPath() {
584
+ // Build adjacency list
585
+ const adjacency = new Map();
586
+ for (const edge of this.graph.edges) {
587
+ if (!adjacency.has(edge.to)) {
588
+ adjacency.set(edge.to, []);
589
+ }
590
+ adjacency.get(edge.to).push(edge.from);
591
+ }
592
+ // Find longest path using DFS
593
+ const visited = new Set();
594
+ let longestPath = [];
595
+ const dfs = (node, path) => {
596
+ if (visited.has(node))
597
+ return;
598
+ visited.add(node);
599
+ const newPath = [...path, node];
600
+ const children = adjacency.get(node) || [];
601
+ if (children.length === 0) {
602
+ // Leaf node - check if this is the longest path
603
+ if (newPath.length > longestPath.length) {
604
+ longestPath = newPath;
605
+ }
606
+ }
607
+ else {
608
+ for (const child of children) {
609
+ dfs(child, newPath);
610
+ }
611
+ }
612
+ visited.delete(node);
613
+ };
614
+ // Start DFS from all roots (nodes with no dependencies)
615
+ const allNodes = new Set(this.graph.nodes.keys());
616
+ const dependentNodes = new Set(this.graph.edges.map(e => e.from));
617
+ const roots = Array.from(allNodes).filter(n => !dependentNodes.has(n));
618
+ for (const root of roots) {
619
+ dfs(root, []);
620
+ }
621
+ return longestPath.reverse(); // Reverse to get correct order
622
+ }
623
+ /**
624
+ * Identifies bottleneck steps that blocked many other steps
625
+ */
626
+ _identifyBottlenecks() {
627
+ const bottlenecks = new Map();
628
+ // Count how many steps each step blocks
629
+ for (const edge of this.graph.edges) {
630
+ if (!bottlenecks.has(edge.to)) {
631
+ bottlenecks.set(edge.to, new Set());
632
+ }
633
+ bottlenecks.get(edge.to).add(edge.from);
634
+ }
635
+ // Sort by number of blocked steps
636
+ const result = Array.from(bottlenecks.entries())
637
+ .map(([stepName, blockedSteps]) => ({
638
+ stepName,
639
+ executionTime: this.latencyMetrics.get(stepName)?.executionTime || 0,
640
+ blockedSteps: Array.from(blockedSteps),
641
+ }))
642
+ .sort((a, b) => b.blockedSteps.length - a.blockedSteps.length)
643
+ .slice(0, 5); // Top 5 bottlenecks
644
+ return result;
645
+ }
646
+ /**
647
+ * Type-safe event emitter
648
+ */
649
+ on(event, handler) {
650
+ return super.on(event, handler);
651
+ }
652
+ emit(event, ...args) {
653
+ return super.emit(event, ...args);
654
+ }
655
+ }
656
+ //# sourceMappingURL=index.js.map