@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,345 @@
1
+ /**
2
+ * Test Process Manager
3
+ *
4
+ * Specialized ProcessManager for testing with additional features
5
+ * for simulating failures, controlling timing, and verifying behavior.
6
+ */
7
+
8
+ import { Errors } from '@omnitron-dev/titan/errors';
9
+ import { ProcessManager } from '@omnitron-dev/titan/module/pm';
10
+ import { ProcessStatus } from '@omnitron-dev/titan/module/pm';
11
+ import type { IProcessOptions, IProcessInfo, ServiceProxy, IProcessManagerConfig } from '@omnitron-dev/titan/module/pm';
12
+ import { createNullLogger } from '@omnitron-dev/titan/module/logger';
13
+
14
+ /**
15
+ * Test process manager configuration
16
+ */
17
+ export interface ITestProcessManagerConfig extends IProcessManagerConfig {
18
+ /** Use mock spawner for faster tests */
19
+ mock?: boolean;
20
+ /** Control time for testing */
21
+ controlTime?: boolean;
22
+ /** Record all operations for verification */
23
+ recordOperations?: boolean;
24
+ }
25
+
26
+ /**
27
+ * Operation record for verification
28
+ */
29
+ export interface IOperationRecord {
30
+ type: 'spawn' | 'kill' | 'restart' | 'health' | 'metrics';
31
+ processId?: string;
32
+ processClass?: string;
33
+ timestamp: number;
34
+ data?: any;
35
+ }
36
+
37
+ /**
38
+ * Test Process Manager with additional testing capabilities
39
+ */
40
+ export class TestProcessManager extends ProcessManager {
41
+ private operations: IOperationRecord[] = [];
42
+ private simulatedFailures = new Map<string, Error>();
43
+ private simulatedMetrics = new Map<string, any>();
44
+ private simulatedHealth = new Map<string, any>();
45
+ private timeOffset = 0;
46
+ private crashHandlers = new Map<string, () => void>();
47
+ private recoveryHandlers = new Map<string, () => void>();
48
+
49
+ constructor(config: ITestProcessManagerConfig = {}) {
50
+ // Use null logger for tests - silent by default
51
+ const logger = createNullLogger();
52
+
53
+ super(logger, {
54
+ ...config,
55
+ testing: {
56
+ ...config.testing,
57
+ useMockSpawner: config.mock !== false,
58
+ },
59
+ });
60
+
61
+ // Setup test event handlers
62
+ this.setupTestHandlers();
63
+ }
64
+
65
+ /**
66
+ * Setup test-specific event handlers
67
+ */
68
+ private setupTestHandlers(): void {
69
+ this.on('process:crash', (info, error) => {
70
+ const handler = this.crashHandlers.get(info.id);
71
+ if (handler) {
72
+ handler();
73
+ }
74
+ });
75
+
76
+ this.on('process:ready', (info) => {
77
+ const handler = this.recoveryHandlers.get(info.id);
78
+ if (handler) {
79
+ handler();
80
+ }
81
+ });
82
+ }
83
+
84
+ /**
85
+ * Spawn a process with test recording
86
+ */
87
+ override async spawn<T>(
88
+ ProcessClass: new (...args: any[]) => T,
89
+ options: IProcessOptions = {}
90
+ ): Promise<ServiceProxy<T>> {
91
+ this.recordOperation('spawn', undefined, ProcessClass.name, options);
92
+
93
+ // Check for simulated failure
94
+ const failureKey = `spawn:${ProcessClass.name}`;
95
+ if (this.simulatedFailures.has(failureKey)) {
96
+ throw this.simulatedFailures.get(failureKey)!;
97
+ }
98
+
99
+ return super.spawn(ProcessClass, options);
100
+ }
101
+
102
+ /**
103
+ * Simulate a process crash
104
+ */
105
+ async simulateCrash(processOrId: ServiceProxy<any> | string): Promise<void> {
106
+ const processId = typeof processOrId === 'string' ? processOrId : (processOrId as any).__processId;
107
+
108
+ const info = this.getProcess(processId);
109
+ if (!info) {
110
+ throw Errors.notFound('Process', processId);
111
+ }
112
+
113
+ // Emit crash event
114
+ const error = new Error('Simulated crash for testing');
115
+ this.emit('process:crash', info, error);
116
+
117
+ // Kill the actual process if it exists but preserve CRASHED status
118
+ try {
119
+ await this.kill(processId, 'SIGKILL');
120
+ } catch (e) {
121
+ // Ignore errors during simulated crash
122
+ }
123
+
124
+ // Update status AFTER kill to ensure it stays CRASHED
125
+ (info as any).status = ProcessStatus.CRASHED;
126
+ }
127
+
128
+ /**
129
+ * Wait for a process to recover
130
+ */
131
+ async waitForRecovery(processOrId: ServiceProxy<any> | string, timeout: number = 5000): Promise<boolean> {
132
+ const processId = typeof processOrId === 'string' ? processOrId : (processOrId as any).__processId;
133
+
134
+ return new Promise((resolve) => {
135
+ const timer = setTimeout(() => {
136
+ this.recoveryHandlers.delete(processId);
137
+ resolve(false);
138
+ }, timeout);
139
+
140
+ this.recoveryHandlers.set(processId, () => {
141
+ clearTimeout(timer);
142
+ this.recoveryHandlers.delete(processId);
143
+ resolve(true);
144
+ });
145
+
146
+ // Check if already recovered
147
+ const info = this.getProcess(processId);
148
+ if (info && info.status === ProcessStatus.RUNNING) {
149
+ clearTimeout(timer);
150
+ this.recoveryHandlers.delete(processId);
151
+ resolve(true);
152
+ }
153
+ });
154
+ }
155
+
156
+ /**
157
+ * Simulate metrics for a process
158
+ */
159
+ setMetrics(processId: string, metrics: any): void {
160
+ this.simulatedMetrics.set(processId, metrics);
161
+ }
162
+
163
+ /**
164
+ * Get metrics with simulated values
165
+ */
166
+ override async getMetrics(processId: string): Promise<any> {
167
+ if (this.simulatedMetrics.has(processId)) {
168
+ return this.simulatedMetrics.get(processId);
169
+ }
170
+ return super.getMetrics(processId);
171
+ }
172
+
173
+ /**
174
+ * Simulate health status for a process
175
+ */
176
+ setHealth(processId: string, health: any): void {
177
+ this.simulatedHealth.set(processId, health);
178
+ }
179
+
180
+ /**
181
+ * Get health with simulated values
182
+ */
183
+ override async getHealth(processId: string): Promise<any> {
184
+ if (this.simulatedHealth.has(processId)) {
185
+ return this.simulatedHealth.get(processId);
186
+ }
187
+ return super.getHealth(processId);
188
+ }
189
+
190
+ /**
191
+ * Simulate a failure for next operation
192
+ */
193
+ simulateFailure(operation: string, error: Error): void {
194
+ this.simulatedFailures.set(operation, error);
195
+ }
196
+
197
+ /**
198
+ * Clear simulated failures
199
+ */
200
+ clearFailures(): void {
201
+ this.simulatedFailures.clear();
202
+ }
203
+
204
+ /**
205
+ * Advance simulated time
206
+ */
207
+ advanceTime(ms: number): void {
208
+ this.timeOffset += ms;
209
+ }
210
+
211
+ /**
212
+ * Reset simulated time
213
+ */
214
+ resetTime(): void {
215
+ this.timeOffset = 0;
216
+ }
217
+
218
+ /**
219
+ * Get current time with offset
220
+ */
221
+ getCurrentTime(): number {
222
+ return Date.now() + this.timeOffset;
223
+ }
224
+
225
+ /**
226
+ * Get operation history
227
+ */
228
+ getOperations(): IOperationRecord[] {
229
+ return [...this.operations];
230
+ }
231
+
232
+ /**
233
+ * Clear operation history
234
+ */
235
+ clearOperations(): void {
236
+ this.operations = [];
237
+ }
238
+
239
+ /**
240
+ * Verify operation occurred
241
+ */
242
+ verifyOperation(type: string, predicate?: (op: IOperationRecord) => boolean): boolean {
243
+ return this.operations.some((op) => op.type === type && (!predicate || predicate(op)));
244
+ }
245
+
246
+ /**
247
+ * Wait for a specific operation
248
+ */
249
+ async waitForOperation(type: string, timeout: number = 5000): Promise<IOperationRecord | null> {
250
+ const startTime = Date.now();
251
+
252
+ while (Date.now() - startTime < timeout) {
253
+ const op = this.operations.find((o) => o.type === type);
254
+ if (op) return op;
255
+ await new Promise((resolve) => setTimeout(resolve, 100));
256
+ }
257
+
258
+ return null;
259
+ }
260
+
261
+ /**
262
+ * Record an operation for verification
263
+ */
264
+ private recordOperation(type: any, processId?: string, processClass?: string, data?: any): void {
265
+ this.operations.push({
266
+ type,
267
+ processId,
268
+ processClass,
269
+ timestamp: this.getCurrentTime(),
270
+ data,
271
+ });
272
+ }
273
+
274
+ /**
275
+ * Clean up test resources
276
+ */
277
+ async cleanup(): Promise<void> {
278
+ this.clearFailures();
279
+ this.clearOperations();
280
+ this.resetTime();
281
+ this.simulatedMetrics.clear();
282
+ this.simulatedHealth.clear();
283
+ this.crashHandlers.clear();
284
+ this.recoveryHandlers.clear();
285
+ await this.shutdown();
286
+ }
287
+
288
+ /**
289
+ * Create a spy for a process method
290
+ */
291
+ spyOnMethod<T>(proxy: ServiceProxy<T>, methodName: keyof T): jest.SpyInstance | any {
292
+ const original = (proxy as any)[methodName];
293
+ const calls: any[] = [];
294
+
295
+ (proxy as any)[methodName] = async (...args: any[]) => {
296
+ calls.push({ args, timestamp: this.getCurrentTime() });
297
+ return original.apply(proxy, args);
298
+ };
299
+
300
+ // Return spy-like interface
301
+ return {
302
+ calls,
303
+ mockClear: () => (calls.length = 0),
304
+ mockRestore: () => ((proxy as any)[methodName] = original),
305
+ };
306
+ }
307
+
308
+ /**
309
+ * Assert process state
310
+ */
311
+ assertProcessState(processId: string, expectedStatus: ProcessStatus): void {
312
+ const info = this.getProcess(processId);
313
+ if (!info) {
314
+ throw Errors.notFound('Process', processId);
315
+ }
316
+ if (info.status !== expectedStatus) {
317
+ throw Errors.badRequest(`Expected process ${processId} to have status ${expectedStatus}, but got ${info.status}`);
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Get all processes of a specific class
323
+ */
324
+ getProcessesByClass<T>(ProcessClass: new (...args: any[]) => T): IProcessInfo[] {
325
+ return this.listProcesses().filter((info) => info.name === ProcessClass.name);
326
+ }
327
+
328
+ /**
329
+ * Count processes by status
330
+ */
331
+ countByStatus(status: ProcessStatus): number {
332
+ return this.listProcesses().filter((info) => info.status === status).length;
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Create a test process manager with defaults
338
+ */
339
+ export function createTestProcessManager(config?: ITestProcessManagerConfig): TestProcessManager {
340
+ return new TestProcessManager({
341
+ mock: true,
342
+ recordOperations: true,
343
+ ...config,
344
+ });
345
+ }