@omnitron-dev/testing 0.4.1 → 0.4.3

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.
Files changed (154) hide show
  1. package/dist/async/deferred.d.ts +15 -0
  2. package/dist/async/deferred.js +35 -0
  3. package/dist/async/deferred.js.map +1 -0
  4. package/dist/async/events.d.ts +32 -0
  5. package/dist/async/events.js +176 -0
  6. package/dist/async/events.js.map +1 -0
  7. package/dist/async/index.d.ts +5 -0
  8. package/dist/async/index.js +6 -0
  9. package/dist/async/index.js.map +1 -0
  10. package/dist/async/mock-timer.d.ts +19 -0
  11. package/dist/async/mock-timer.js +87 -0
  12. package/dist/async/mock-timer.js.map +1 -0
  13. package/dist/async/retry.d.ts +8 -0
  14. package/dist/async/retry.js +36 -0
  15. package/dist/async/retry.js.map +1 -0
  16. package/dist/async/wait.d.ts +10 -0
  17. package/dist/async/wait.js +35 -0
  18. package/dist/async/wait.js.map +1 -0
  19. package/dist/docker/database-manager.d.ts +22 -0
  20. package/dist/docker/database-manager.js +97 -0
  21. package/dist/docker/database-manager.js.map +1 -0
  22. package/dist/docker/docker-test-manager.d.ts +44 -0
  23. package/dist/docker/docker-test-manager.js +805 -0
  24. package/dist/docker/docker-test-manager.js.map +1 -0
  25. package/dist/docker/index.d.ts +4 -0
  26. package/dist/docker/index.js +4 -0
  27. package/dist/docker/index.js.map +1 -0
  28. package/dist/docker/redis-manager.d.ts +12 -0
  29. package/dist/docker/redis-manager.js +453 -0
  30. package/dist/docker/redis-manager.js.map +1 -0
  31. package/dist/docker/types.d.ts +103 -0
  32. package/dist/docker/types.js +2 -0
  33. package/dist/docker/types.js.map +1 -0
  34. package/dist/errors.d.ts +15 -0
  35. package/dist/errors.js +28 -0
  36. package/dist/errors.js.map +1 -0
  37. package/dist/helpers/assertions.d.ts +6 -0
  38. package/dist/helpers/assertions.js +37 -0
  39. package/dist/helpers/assertions.js.map +1 -0
  40. package/dist/helpers/console.d.ts +1 -0
  41. package/dist/helpers/console.js +23 -0
  42. package/dist/helpers/console.js.map +1 -0
  43. package/dist/helpers/index.d.ts +3 -0
  44. package/dist/helpers/index.js +4 -0
  45. package/dist/helpers/index.js.map +1 -0
  46. package/dist/helpers/temp-dir.d.ts +2 -0
  47. package/dist/helpers/temp-dir.js +13 -0
  48. package/dist/helpers/temp-dir.js.map +1 -0
  49. package/dist/index.d.ts +2 -0
  50. package/dist/index.js +2 -0
  51. package/dist/index.js.map +1 -1
  52. package/dist/performance/index.d.ts +2 -0
  53. package/dist/performance/index.js +3 -0
  54. package/dist/performance/index.js.map +1 -0
  55. package/dist/performance/memory-detector.d.ts +9 -0
  56. package/dist/performance/memory-detector.js +61 -0
  57. package/dist/performance/memory-detector.js.map +1 -0
  58. package/dist/performance/perf-timer.d.ts +9 -0
  59. package/dist/performance/perf-timer.js +49 -0
  60. package/dist/performance/perf-timer.js.map +1 -0
  61. package/dist/titan/container-utils.d.ts +20 -0
  62. package/dist/titan/container-utils.js +78 -0
  63. package/dist/titan/container-utils.js.map +1 -0
  64. package/dist/titan/database/database-testing.module.d.ts +81 -0
  65. package/dist/titan/database/database-testing.module.js +419 -0
  66. package/dist/titan/database/database-testing.module.js.map +1 -0
  67. package/dist/titan/database/index.d.ts +3 -0
  68. package/dist/titan/database/index.js +4 -0
  69. package/dist/titan/database/index.js.map +1 -0
  70. package/dist/titan/database/test-utilities.d.ts +31 -0
  71. package/dist/titan/database/test-utilities.js +145 -0
  72. package/dist/titan/database/test-utilities.js.map +1 -0
  73. package/dist/titan/index.d.ts +14 -0
  74. package/dist/titan/index.js +10 -0
  75. package/dist/titan/index.js.map +1 -0
  76. package/dist/titan/nexus/index.d.ts +3 -0
  77. package/dist/titan/nexus/index.js +4 -0
  78. package/dist/titan/nexus/index.js.map +1 -0
  79. package/dist/titan/nexus/mock-provider.d.ts +41 -0
  80. package/dist/titan/nexus/mock-provider.js +127 -0
  81. package/dist/titan/nexus/mock-provider.js.map +1 -0
  82. package/dist/titan/nexus/test-container.d.ts +120 -0
  83. package/dist/titan/nexus/test-container.js +559 -0
  84. package/dist/titan/nexus/test-container.js.map +1 -0
  85. package/dist/titan/nexus/test-utilities.d.ts +135 -0
  86. package/dist/titan/nexus/test-utilities.js +484 -0
  87. package/dist/titan/nexus/test-utilities.js.map +1 -0
  88. package/dist/titan/pm/index.d.ts +4 -0
  89. package/dist/titan/pm/index.js +4 -0
  90. package/dist/titan/pm/index.js.map +1 -0
  91. package/dist/titan/pm/mock-process-spawner.d.ts +60 -0
  92. package/dist/titan/pm/mock-process-spawner.js +321 -0
  93. package/dist/titan/pm/mock-process-spawner.js.map +1 -0
  94. package/dist/titan/pm/mock-spawner.d.ts +27 -0
  95. package/dist/titan/pm/mock-spawner.js +99 -0
  96. package/dist/titan/pm/mock-spawner.js.map +1 -0
  97. package/dist/titan/pm/test-process-manager.d.ts +49 -0
  98. package/dist/titan/pm/test-process-manager.js +188 -0
  99. package/dist/titan/pm/test-process-manager.js.map +1 -0
  100. package/dist/titan/redis-test-utils.d.ts +33 -0
  101. package/dist/titan/redis-test-utils.js +414 -0
  102. package/dist/titan/redis-test-utils.js.map +1 -0
  103. package/dist/titan/test-application.d.ts +21 -0
  104. package/dist/titan/test-application.js +90 -0
  105. package/dist/titan/test-application.js.map +1 -0
  106. package/dist/titan/test-fixtures.d.ts +97 -0
  107. package/dist/titan/test-fixtures.js +223 -0
  108. package/dist/titan/test-fixtures.js.map +1 -0
  109. package/dist/titan/test-mocks.d.ts +29 -0
  110. package/dist/titan/test-mocks.js +262 -0
  111. package/dist/titan/test-mocks.js.map +1 -0
  112. package/dist/titan/test-module.d.ts +46 -0
  113. package/dist/titan/test-module.js +120 -0
  114. package/dist/titan/test-module.js.map +1 -0
  115. package/package.json +41 -4
  116. package/src/async/README.md +336 -0
  117. package/src/async/deferred.ts +98 -0
  118. package/src/async/events.ts +386 -0
  119. package/src/async/index.ts +48 -0
  120. package/src/async/mock-timer.ts +220 -0
  121. package/src/async/retry.ts +118 -0
  122. package/src/async/wait.ts +151 -0
  123. package/src/docker/database-manager.ts +242 -0
  124. package/src/docker/docker-test-manager.ts +1186 -0
  125. package/src/docker/index.ts +48 -0
  126. package/src/docker/redis-manager.ts +738 -0
  127. package/src/docker/types.ts +216 -0
  128. package/src/errors.ts +31 -0
  129. package/src/helpers/assertions.ts +171 -0
  130. package/src/helpers/console.ts +74 -0
  131. package/src/helpers/index.ts +15 -0
  132. package/src/helpers/temp-dir.ts +50 -0
  133. package/src/index.ts +2 -0
  134. package/src/performance/index.ts +11 -0
  135. package/src/performance/memory-detector.ts +166 -0
  136. package/src/performance/perf-timer.ts +146 -0
  137. package/src/titan/container-utils.ts +128 -0
  138. package/src/titan/database/database-testing.module.ts +831 -0
  139. package/src/titan/database/index.ts +30 -0
  140. package/src/titan/database/test-utilities.ts +337 -0
  141. package/src/titan/index.ts +147 -0
  142. package/src/titan/nexus/index.ts +3 -0
  143. package/src/titan/nexus/mock-provider.ts +189 -0
  144. package/src/titan/nexus/test-container.ts +887 -0
  145. package/src/titan/nexus/test-utilities.ts +796 -0
  146. package/src/titan/pm/index.ts +22 -0
  147. package/src/titan/pm/mock-process-spawner.ts +481 -0
  148. package/src/titan/pm/mock-spawner.ts +156 -0
  149. package/src/titan/pm/test-process-manager.ts +345 -0
  150. package/src/titan/redis-test-utils.ts +566 -0
  151. package/src/titan/test-application.ts +154 -0
  152. package/src/titan/test-fixtures.ts +304 -0
  153. package/src/titan/test-mocks.ts +336 -0
  154. package/src/titan/test-module.ts +232 -0
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Memory leak detector for identifying potential memory leaks in tests.
3
+ *
4
+ * This class monitors heap memory usage over time and can detect memory growth
5
+ * trends that may indicate memory leaks. It requires Node.js to be run with the
6
+ * `--expose-gc` flag to enable manual garbage collection.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Run Node with: node --expose-gc test.js
11
+ *
12
+ * const detector = new MemoryLeakDetector();
13
+ *
14
+ * // Run test iterations
15
+ * for (let i = 0; i < 100; i++) {
16
+ * await doWork();
17
+ * detector.measure();
18
+ * }
19
+ *
20
+ * // Check for leaks
21
+ * if (detector.hasLeak()) {
22
+ * console.warn('Potential memory leak detected!');
23
+ * console.log('Trend:', detector.getTrend());
24
+ * }
25
+ * ```
26
+ */
27
+ export class MemoryLeakDetector {
28
+ private initialMemory: number;
29
+ private measurements: number[] = [];
30
+
31
+ /**
32
+ * Creates a new memory leak detector.
33
+ *
34
+ * Triggers garbage collection (if available) and records the initial heap usage.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const detector = new MemoryLeakDetector();
39
+ * ```
40
+ */
41
+ constructor() {
42
+ if (global.gc) {
43
+ global.gc();
44
+ }
45
+ this.initialMemory = process.memoryUsage().heapUsed;
46
+ }
47
+
48
+ /**
49
+ * Measure current memory usage relative to the initial baseline.
50
+ *
51
+ * Triggers garbage collection (if available) before measuring to get
52
+ * accurate readings.
53
+ *
54
+ * @returns The memory delta in bytes (positive indicates growth)
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const delta = detector.measure();
59
+ * console.log(`Memory delta: ${delta / 1024 / 1024}MB`);
60
+ * ```
61
+ */
62
+ measure(): number {
63
+ if (global.gc) {
64
+ global.gc();
65
+ }
66
+ const current = process.memoryUsage().heapUsed;
67
+ const delta = current - this.initialMemory;
68
+ this.measurements.push(delta);
69
+ return delta;
70
+ }
71
+
72
+ /**
73
+ * Check if there's a potential memory leak.
74
+ *
75
+ * A leak is detected if the average of the last 5 measurements exceeds
76
+ * the specified threshold.
77
+ *
78
+ * @param threshold - The memory growth threshold in bytes (default: 10MB)
79
+ * @returns `true` if average memory growth exceeds the threshold
80
+ *
81
+ * @example
82
+ * ```typescript
83
+ * // Check with default 10MB threshold
84
+ * if (detector.hasLeak()) {
85
+ * console.warn('Potential leak detected');
86
+ * }
87
+ *
88
+ * // Check with custom 5MB threshold
89
+ * if (detector.hasLeak(5 * 1024 * 1024)) {
90
+ * console.warn('Memory grew by more than 5MB');
91
+ * }
92
+ * ```
93
+ */
94
+ hasLeak(threshold = 10 * 1024 * 1024): boolean {
95
+ // Consider it a leak if memory grows by more than threshold
96
+ if (this.measurements.length < 2) {
97
+ return false;
98
+ }
99
+
100
+ const recent = this.measurements.slice(-5);
101
+ const average = recent.reduce((sum, val) => sum + val, 0) / recent.length;
102
+ return average > threshold;
103
+ }
104
+
105
+ /**
106
+ * Get the memory growth trend.
107
+ *
108
+ * Analyzes the last 5 measurements to determine if memory is consistently
109
+ * growing, shrinking, or stable.
110
+ *
111
+ * @returns The memory trend: 'growing', 'shrinking', or 'stable'
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const trend = detector.getTrend();
116
+ * console.log(`Memory trend: ${trend}`);
117
+ * ```
118
+ */
119
+ getTrend(): 'stable' | 'growing' | 'shrinking' {
120
+ if (this.measurements.length < 3) {
121
+ return 'stable';
122
+ }
123
+
124
+ const recent = this.measurements.slice(-5);
125
+ let growing = 0;
126
+ let shrinking = 0;
127
+
128
+ for (let i = 1; i < recent.length; i++) {
129
+ const current = recent[i];
130
+ const previous = recent[i - 1];
131
+ if (current !== undefined && previous !== undefined) {
132
+ if (current > previous) {
133
+ growing++;
134
+ } else if (current < previous) {
135
+ shrinking++;
136
+ }
137
+ }
138
+ }
139
+
140
+ if (growing > shrinking * 2) {
141
+ return 'growing';
142
+ } else if (shrinking > growing * 2) {
143
+ return 'shrinking';
144
+ }
145
+ return 'stable';
146
+ }
147
+
148
+ /**
149
+ * Reset the detector to start fresh measurements.
150
+ *
151
+ * Triggers garbage collection (if available) and sets a new baseline.
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * detector.reset();
156
+ * // Start new test run
157
+ * ```
158
+ */
159
+ reset(): void {
160
+ if (global.gc) {
161
+ global.gc();
162
+ }
163
+ this.initialMemory = process.memoryUsage().heapUsed;
164
+ this.measurements = [];
165
+ }
166
+ }
@@ -0,0 +1,146 @@
1
+ import { NotFoundError } from '../errors.js';
2
+
3
+ /**
4
+ * Performance timer for measuring operation performance with percentile calculation.
5
+ *
6
+ * This class provides utilities for timing operations and calculating statistics
7
+ * like averages and percentiles across multiple measurements.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const timer = new PerfTimer();
12
+ *
13
+ * // Mark start and end points
14
+ * timer.mark('start');
15
+ * await doWork();
16
+ * timer.mark('end');
17
+ *
18
+ * // Measure duration
19
+ * const duration = timer.measure('work', 'start', 'end');
20
+ * console.log(`Duration: ${duration}ms`);
21
+ *
22
+ * // Get statistics
23
+ * const avg = timer.getAverage('work');
24
+ * const p95 = timer.getPercentile('work', 95);
25
+ * ```
26
+ */
27
+ export class PerfTimer {
28
+ private marks = new Map<string, number>();
29
+ private measures = new Map<string, number[]>();
30
+
31
+ /**
32
+ * Mark a point in time.
33
+ *
34
+ * @param name - The name of the mark
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * timer.mark('start');
39
+ * await doWork();
40
+ * timer.mark('end');
41
+ * ```
42
+ */
43
+ mark(name: string): void {
44
+ this.marks.set(name, performance.now());
45
+ }
46
+
47
+ /**
48
+ * Measure the duration between two marks.
49
+ *
50
+ * @param name - The name of the measurement (used for statistics)
51
+ * @param startMark - The name of the start mark
52
+ * @param endMark - The name of the end mark (defaults to current time if not provided)
53
+ * @returns The duration in milliseconds
54
+ * @throws {NotFoundError} If the start or end mark is not found
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * timer.mark('start');
59
+ * await doWork();
60
+ * timer.mark('end');
61
+ * const duration = timer.measure('work', 'start', 'end');
62
+ * ```
63
+ */
64
+ measure(name: string, startMark: string, endMark?: string): number {
65
+ const start = this.marks.get(startMark);
66
+ if (!start) {
67
+ throw new NotFoundError('performance mark', startMark);
68
+ }
69
+
70
+ const end = endMark ? this.marks.get(endMark) : performance.now();
71
+ if (!end) {
72
+ throw new NotFoundError('performance mark', endMark);
73
+ }
74
+
75
+ const duration = end - start;
76
+
77
+ let measures = this.measures.get(name);
78
+ if (!measures) {
79
+ measures = [];
80
+ this.measures.set(name, measures);
81
+ }
82
+ measures.push(duration);
83
+
84
+ return duration;
85
+ }
86
+
87
+ /**
88
+ * Get the average duration for a measurement.
89
+ *
90
+ * @param name - The name of the measurement
91
+ * @returns The average duration in milliseconds, or 0 if no measurements exist
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * // After multiple measurements
96
+ * const avg = timer.getAverage('work');
97
+ * console.log(`Average: ${avg}ms`);
98
+ * ```
99
+ */
100
+ getAverage(name: string): number {
101
+ const measures = this.measures.get(name);
102
+ if (!measures || measures.length === 0) {
103
+ return 0;
104
+ }
105
+ return measures.reduce((sum, val) => sum + val, 0) / measures.length;
106
+ }
107
+
108
+ /**
109
+ * Get a percentile value for a measurement.
110
+ *
111
+ * @param name - The name of the measurement
112
+ * @param percentile - The percentile to calculate (0-100)
113
+ * @returns The percentile value in milliseconds, or 0 if no measurements exist
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // Get 95th percentile
118
+ * const p95 = timer.getPercentile('work', 95);
119
+ * console.log(`P95: ${p95}ms`);
120
+ * ```
121
+ */
122
+ getPercentile(name: string, percentile: number): number {
123
+ const measures = this.measures.get(name);
124
+ if (!measures || measures.length === 0) {
125
+ return 0;
126
+ }
127
+
128
+ const sorted = [...measures].sort((a, b) => a - b);
129
+ const index = Math.min(Math.floor(sorted.length * (percentile / 100)), sorted.length - 1);
130
+ return sorted[index] ?? 0;
131
+ }
132
+
133
+ /**
134
+ * Clear all marks and measures.
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * timer.clear();
139
+ * // Start fresh measurements
140
+ * ```
141
+ */
142
+ clear(): void {
143
+ this.marks.clear();
144
+ this.measures.clear();
145
+ }
146
+ }
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Container Utilities for Testing
3
+ *
4
+ * Provides utilities for working with containers in tests
5
+ */
6
+
7
+ import { Container, type InjectionToken, type Provider } from '@omnitron-dev/titan/nexus';
8
+
9
+ /**
10
+ * Register providers from a module into a container
11
+ *
12
+ * Handles tuple format [token, provider] and direct constructors
13
+ */
14
+ export function registerModuleProviders(container: Container, providers: any[] | undefined): void {
15
+ if (!providers) return;
16
+
17
+ for (const provider of providers) {
18
+ if (Array.isArray(provider) && provider.length === 2) {
19
+ // Tuple format [token, provider]
20
+ const [token, providerDef] = provider;
21
+ container.register(token, providerDef);
22
+ } else if (typeof provider === 'function') {
23
+ // Constructor - register as class provider
24
+ container.register(provider, { useClass: provider });
25
+ } else {
26
+ // Provider object without token - shouldn't happen
27
+ console.warn('Provider without token:', provider);
28
+ }
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Register a single provider in the container
34
+ */
35
+ export function registerProvider(container: Container, token: InjectionToken<any>, provider: Provider): void {
36
+ // Always use register - Container will handle the provider type
37
+ container.register(token, provider);
38
+ }
39
+
40
+ /**
41
+ * Create a test container with providers
42
+ */
43
+ export function createTestContainer(providers?: any[]): Container {
44
+ const container = new Container();
45
+ registerModuleProviders(container, providers);
46
+ return container;
47
+ }
48
+
49
+ /**
50
+ * Helper to resolve a service from a module
51
+ */
52
+ export async function resolveFromModule<T>(module: { providers?: any[] }, token: InjectionToken<T>): Promise<T> {
53
+ const container = createTestContainer(module.providers);
54
+ return container.resolveAsync(token);
55
+ }
56
+
57
+ /**
58
+ * Helper to check if a provider exists in a module
59
+ */
60
+ export function hasProvider(module: { providers?: any[] }, token: InjectionToken<any>): boolean {
61
+ if (!module.providers) return false;
62
+
63
+ for (const provider of module.providers) {
64
+ if (Array.isArray(provider)) {
65
+ if (provider[0] === token) return true;
66
+ } else if (provider === token) {
67
+ return true;
68
+ }
69
+ }
70
+
71
+ return false;
72
+ }
73
+
74
+ /**
75
+ * Extract all tokens from a module's providers
76
+ */
77
+ export function getModuleTokens(module: { providers?: any[] }): InjectionToken<any>[] {
78
+ if (!module.providers) return [];
79
+
80
+ const tokens: InjectionToken<any>[] = [];
81
+
82
+ for (const provider of module.providers) {
83
+ if (Array.isArray(provider)) {
84
+ tokens.push(provider[0]);
85
+ } else if (typeof provider === 'function') {
86
+ tokens.push(provider);
87
+ }
88
+ }
89
+
90
+ return tokens;
91
+ }
92
+
93
+ /**
94
+ * Create a mock container for testing
95
+ */
96
+ export class MockContainer extends Container {
97
+ private mocks = new Map<InjectionToken<any>, any>();
98
+
99
+ /**
100
+ * Register a mock for a token
101
+ */
102
+ mock<T>(token: InjectionToken<T>, value: T): this {
103
+ this.mocks.set(token, value);
104
+ this.register(token, { useValue: value });
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * Get a mock by token
110
+ */
111
+ getMock<T>(token: InjectionToken<T>): T | undefined {
112
+ return this.mocks.get(token);
113
+ }
114
+
115
+ /**
116
+ * Clear all mocks
117
+ */
118
+ clearMocks(): void {
119
+ this.mocks.clear();
120
+ }
121
+
122
+ /**
123
+ * Check if a token has a mock
124
+ */
125
+ hasMock(token: InjectionToken<any>): boolean {
126
+ return this.mocks.has(token);
127
+ }
128
+ }