@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.
- package/dist/async/deferred.d.ts +15 -0
- package/dist/async/deferred.js +35 -0
- package/dist/async/deferred.js.map +1 -0
- package/dist/async/events.d.ts +32 -0
- package/dist/async/events.js +176 -0
- package/dist/async/events.js.map +1 -0
- package/dist/async/index.d.ts +5 -0
- package/dist/async/index.js +6 -0
- package/dist/async/index.js.map +1 -0
- package/dist/async/mock-timer.d.ts +19 -0
- package/dist/async/mock-timer.js +87 -0
- package/dist/async/mock-timer.js.map +1 -0
- package/dist/async/retry.d.ts +8 -0
- package/dist/async/retry.js +36 -0
- package/dist/async/retry.js.map +1 -0
- package/dist/async/wait.d.ts +10 -0
- package/dist/async/wait.js +35 -0
- package/dist/async/wait.js.map +1 -0
- package/dist/docker/database-manager.d.ts +22 -0
- package/dist/docker/database-manager.js +97 -0
- package/dist/docker/database-manager.js.map +1 -0
- package/dist/docker/docker-test-manager.d.ts +44 -0
- package/dist/docker/docker-test-manager.js +805 -0
- package/dist/docker/docker-test-manager.js.map +1 -0
- package/dist/docker/index.d.ts +4 -0
- package/dist/docker/index.js +4 -0
- package/dist/docker/index.js.map +1 -0
- package/dist/docker/redis-manager.d.ts +12 -0
- package/dist/docker/redis-manager.js +453 -0
- package/dist/docker/redis-manager.js.map +1 -0
- package/dist/docker/types.d.ts +103 -0
- package/dist/docker/types.js +2 -0
- package/dist/docker/types.js.map +1 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.js +28 -0
- package/dist/errors.js.map +1 -0
- package/dist/helpers/assertions.d.ts +6 -0
- package/dist/helpers/assertions.js +37 -0
- package/dist/helpers/assertions.js.map +1 -0
- package/dist/helpers/console.d.ts +1 -0
- package/dist/helpers/console.js +23 -0
- package/dist/helpers/console.js.map +1 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/helpers/temp-dir.d.ts +2 -0
- package/dist/helpers/temp-dir.js +13 -0
- package/dist/helpers/temp-dir.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/performance/index.d.ts +2 -0
- package/dist/performance/index.js +3 -0
- package/dist/performance/index.js.map +1 -0
- package/dist/performance/memory-detector.d.ts +9 -0
- package/dist/performance/memory-detector.js +61 -0
- package/dist/performance/memory-detector.js.map +1 -0
- package/dist/performance/perf-timer.d.ts +9 -0
- package/dist/performance/perf-timer.js +49 -0
- package/dist/performance/perf-timer.js.map +1 -0
- package/dist/titan/container-utils.d.ts +20 -0
- package/dist/titan/container-utils.js +78 -0
- package/dist/titan/container-utils.js.map +1 -0
- package/dist/titan/database/database-testing.module.d.ts +81 -0
- package/dist/titan/database/database-testing.module.js +419 -0
- package/dist/titan/database/database-testing.module.js.map +1 -0
- package/dist/titan/database/index.d.ts +3 -0
- package/dist/titan/database/index.js +4 -0
- package/dist/titan/database/index.js.map +1 -0
- package/dist/titan/database/test-utilities.d.ts +31 -0
- package/dist/titan/database/test-utilities.js +145 -0
- package/dist/titan/database/test-utilities.js.map +1 -0
- package/dist/titan/index.d.ts +14 -0
- package/dist/titan/index.js +10 -0
- package/dist/titan/index.js.map +1 -0
- package/dist/titan/nexus/index.d.ts +3 -0
- package/dist/titan/nexus/index.js +4 -0
- package/dist/titan/nexus/index.js.map +1 -0
- package/dist/titan/nexus/mock-provider.d.ts +41 -0
- package/dist/titan/nexus/mock-provider.js +127 -0
- package/dist/titan/nexus/mock-provider.js.map +1 -0
- package/dist/titan/nexus/test-container.d.ts +120 -0
- package/dist/titan/nexus/test-container.js +559 -0
- package/dist/titan/nexus/test-container.js.map +1 -0
- package/dist/titan/nexus/test-utilities.d.ts +135 -0
- package/dist/titan/nexus/test-utilities.js +484 -0
- package/dist/titan/nexus/test-utilities.js.map +1 -0
- package/dist/titan/pm/index.d.ts +4 -0
- package/dist/titan/pm/index.js +4 -0
- package/dist/titan/pm/index.js.map +1 -0
- package/dist/titan/pm/mock-process-spawner.d.ts +60 -0
- package/dist/titan/pm/mock-process-spawner.js +321 -0
- package/dist/titan/pm/mock-process-spawner.js.map +1 -0
- package/dist/titan/pm/mock-spawner.d.ts +27 -0
- package/dist/titan/pm/mock-spawner.js +99 -0
- package/dist/titan/pm/mock-spawner.js.map +1 -0
- package/dist/titan/pm/test-process-manager.d.ts +49 -0
- package/dist/titan/pm/test-process-manager.js +188 -0
- package/dist/titan/pm/test-process-manager.js.map +1 -0
- package/dist/titan/redis-test-utils.d.ts +33 -0
- package/dist/titan/redis-test-utils.js +414 -0
- package/dist/titan/redis-test-utils.js.map +1 -0
- package/dist/titan/test-application.d.ts +21 -0
- package/dist/titan/test-application.js +90 -0
- package/dist/titan/test-application.js.map +1 -0
- package/dist/titan/test-fixtures.d.ts +97 -0
- package/dist/titan/test-fixtures.js +223 -0
- package/dist/titan/test-fixtures.js.map +1 -0
- package/dist/titan/test-mocks.d.ts +29 -0
- package/dist/titan/test-mocks.js +262 -0
- package/dist/titan/test-mocks.js.map +1 -0
- package/dist/titan/test-module.d.ts +46 -0
- package/dist/titan/test-module.js +120 -0
- package/dist/titan/test-module.js.map +1 -0
- package/package.json +41 -4
- package/src/async/README.md +336 -0
- package/src/async/deferred.ts +98 -0
- package/src/async/events.ts +386 -0
- package/src/async/index.ts +48 -0
- package/src/async/mock-timer.ts +220 -0
- package/src/async/retry.ts +118 -0
- package/src/async/wait.ts +151 -0
- package/src/docker/database-manager.ts +242 -0
- package/src/docker/docker-test-manager.ts +1186 -0
- package/src/docker/index.ts +48 -0
- package/src/docker/redis-manager.ts +738 -0
- package/src/docker/types.ts +216 -0
- package/src/errors.ts +31 -0
- package/src/helpers/assertions.ts +171 -0
- package/src/helpers/console.ts +74 -0
- package/src/helpers/index.ts +15 -0
- package/src/helpers/temp-dir.ts +50 -0
- package/src/index.ts +2 -0
- package/src/performance/index.ts +11 -0
- package/src/performance/memory-detector.ts +166 -0
- package/src/performance/perf-timer.ts +146 -0
- package/src/titan/container-utils.ts +128 -0
- package/src/titan/database/database-testing.module.ts +831 -0
- package/src/titan/database/index.ts +30 -0
- package/src/titan/database/test-utilities.ts +337 -0
- package/src/titan/index.ts +147 -0
- package/src/titan/nexus/index.ts +3 -0
- package/src/titan/nexus/mock-provider.ts +189 -0
- package/src/titan/nexus/test-container.ts +887 -0
- package/src/titan/nexus/test-utilities.ts +796 -0
- package/src/titan/pm/index.ts +22 -0
- package/src/titan/pm/mock-process-spawner.ts +481 -0
- package/src/titan/pm/mock-spawner.ts +156 -0
- package/src/titan/pm/test-process-manager.ts +345 -0
- package/src/titan/redis-test-utils.ts +566 -0
- package/src/titan/test-application.ts +154 -0
- package/src/titan/test-fixtures.ts +304 -0
- package/src/titan/test-mocks.ts +336 -0
- package/src/titan/test-module.ts +232 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test Fixtures
|
|
3
|
+
*
|
|
4
|
+
* Reusable test data and configurations
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { Module, Injectable } from '@omnitron-dev/titan/decorators';
|
|
9
|
+
import type { IConfigModuleOptions as ConfigModuleOptions } from '@omnitron-dev/titan/module/config';
|
|
10
|
+
import type { RedisOptions } from 'ioredis';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Test schemas for configuration
|
|
14
|
+
*/
|
|
15
|
+
export const TestSchemas = {
|
|
16
|
+
AppConfig: z.object({
|
|
17
|
+
app: z.object({
|
|
18
|
+
name: z.string(),
|
|
19
|
+
port: z.number(),
|
|
20
|
+
version: z.string().optional(),
|
|
21
|
+
}),
|
|
22
|
+
}),
|
|
23
|
+
|
|
24
|
+
DatabaseConfig: z.object({
|
|
25
|
+
database: z.object({
|
|
26
|
+
host: z.string(),
|
|
27
|
+
port: z.number(),
|
|
28
|
+
name: z.string(),
|
|
29
|
+
user: z.string().optional(),
|
|
30
|
+
password: z.string().optional(),
|
|
31
|
+
}),
|
|
32
|
+
}),
|
|
33
|
+
|
|
34
|
+
RedisConfig: z.object({
|
|
35
|
+
redis: z.object({
|
|
36
|
+
host: z.string(),
|
|
37
|
+
port: z.number(),
|
|
38
|
+
password: z.string().optional(),
|
|
39
|
+
db: z.number().optional(),
|
|
40
|
+
}),
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
FullConfig: z.object({
|
|
44
|
+
app: z.object({
|
|
45
|
+
name: z.string(),
|
|
46
|
+
port: z.number(),
|
|
47
|
+
version: z.string().optional(),
|
|
48
|
+
}),
|
|
49
|
+
database: z.object({
|
|
50
|
+
host: z.string(),
|
|
51
|
+
port: z.number(),
|
|
52
|
+
name: z.string(),
|
|
53
|
+
}),
|
|
54
|
+
redis: z.object({
|
|
55
|
+
host: z.string(),
|
|
56
|
+
port: z.number(),
|
|
57
|
+
}),
|
|
58
|
+
}),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Test configuration fixtures
|
|
63
|
+
*/
|
|
64
|
+
export const TestConfigs = {
|
|
65
|
+
minimal: {
|
|
66
|
+
sources: [
|
|
67
|
+
{
|
|
68
|
+
type: 'object' as const,
|
|
69
|
+
data: {
|
|
70
|
+
app: {
|
|
71
|
+
name: 'test',
|
|
72
|
+
port: 3000,
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
} as ConfigModuleOptions,
|
|
78
|
+
|
|
79
|
+
standard: {
|
|
80
|
+
sources: [
|
|
81
|
+
{
|
|
82
|
+
type: 'object' as const,
|
|
83
|
+
data: {
|
|
84
|
+
app: {
|
|
85
|
+
name: 'test-app',
|
|
86
|
+
port: 3000,
|
|
87
|
+
version: '1.0.0',
|
|
88
|
+
},
|
|
89
|
+
database: {
|
|
90
|
+
host: 'localhost',
|
|
91
|
+
port: 5432,
|
|
92
|
+
name: 'testdb',
|
|
93
|
+
},
|
|
94
|
+
redis: {
|
|
95
|
+
host: 'localhost',
|
|
96
|
+
port: 6379,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
} as ConfigModuleOptions,
|
|
102
|
+
|
|
103
|
+
withEnv: {
|
|
104
|
+
sources: [
|
|
105
|
+
{
|
|
106
|
+
type: 'object' as const,
|
|
107
|
+
data: {
|
|
108
|
+
app: {
|
|
109
|
+
name: 'default-app',
|
|
110
|
+
port: 3000,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
type: 'env' as const,
|
|
116
|
+
prefix: 'TEST_',
|
|
117
|
+
separator: '__',
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
} as ConfigModuleOptions,
|
|
121
|
+
|
|
122
|
+
withValidation: {
|
|
123
|
+
schema: TestSchemas.FullConfig,
|
|
124
|
+
sources: [
|
|
125
|
+
{
|
|
126
|
+
type: 'object' as const,
|
|
127
|
+
data: {
|
|
128
|
+
app: {
|
|
129
|
+
name: 'validated-app',
|
|
130
|
+
port: 3000,
|
|
131
|
+
version: '2.0.0',
|
|
132
|
+
},
|
|
133
|
+
database: {
|
|
134
|
+
host: 'localhost',
|
|
135
|
+
port: 5432,
|
|
136
|
+
name: 'validdb',
|
|
137
|
+
},
|
|
138
|
+
redis: {
|
|
139
|
+
host: 'localhost',
|
|
140
|
+
port: 6379,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
validateOnStartup: true,
|
|
146
|
+
} as ConfigModuleOptions,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Test Redis configurations
|
|
151
|
+
*/
|
|
152
|
+
export const TestRedisConfigs = {
|
|
153
|
+
local: {
|
|
154
|
+
host: 'localhost',
|
|
155
|
+
port: 6379,
|
|
156
|
+
retryStrategy: () => null, // Disable retries in tests
|
|
157
|
+
maxRetriesPerRequest: 1,
|
|
158
|
+
enableReadyCheck: false,
|
|
159
|
+
lazyConnect: true,
|
|
160
|
+
} as RedisOptions,
|
|
161
|
+
|
|
162
|
+
cluster: [
|
|
163
|
+
{ host: 'localhost', port: 7000 },
|
|
164
|
+
{ host: 'localhost', port: 7001 },
|
|
165
|
+
{ host: 'localhost', port: 7002 },
|
|
166
|
+
],
|
|
167
|
+
|
|
168
|
+
sentinel: {
|
|
169
|
+
sentinels: [
|
|
170
|
+
{ host: 'localhost', port: 26379 },
|
|
171
|
+
{ host: 'localhost', port: 26380 },
|
|
172
|
+
],
|
|
173
|
+
name: 'mymaster',
|
|
174
|
+
retryStrategy: () => null,
|
|
175
|
+
maxRetriesPerRequest: 1,
|
|
176
|
+
} as RedisOptions,
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Test module definitions
|
|
181
|
+
*/
|
|
182
|
+
export const TestModules = {
|
|
183
|
+
/**
|
|
184
|
+
* Create a simple test module
|
|
185
|
+
*/
|
|
186
|
+
createSimple: (name: string, providers: any[] = []) => {
|
|
187
|
+
@Module({
|
|
188
|
+
providers,
|
|
189
|
+
exports: providers.map((p) => (Array.isArray(p) ? p[0] : p)),
|
|
190
|
+
})
|
|
191
|
+
class TestModule {
|
|
192
|
+
static __name = name;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return TestModule;
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Create a test service
|
|
200
|
+
*/
|
|
201
|
+
createService: (name: string, methods: Record<string, any> = {}) => {
|
|
202
|
+
@Injectable()
|
|
203
|
+
class TestService {
|
|
204
|
+
static __name = name;
|
|
205
|
+
|
|
206
|
+
constructor() {
|
|
207
|
+
Object.assign(this, methods);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return TestService;
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Test data generators
|
|
217
|
+
*/
|
|
218
|
+
export const TestData = {
|
|
219
|
+
/**
|
|
220
|
+
* Generate random string
|
|
221
|
+
*/
|
|
222
|
+
randomString: (length = 10): string => {
|
|
223
|
+
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
224
|
+
let result = '';
|
|
225
|
+
for (let i = 0; i < length; i++) {
|
|
226
|
+
result += chars[Math.floor(Math.random() * chars.length)];
|
|
227
|
+
}
|
|
228
|
+
return result;
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Generate random port number
|
|
233
|
+
*/
|
|
234
|
+
randomPort: (): number => Math.floor(Math.random() * (65535 - 1024) + 1024),
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Generate test user object
|
|
238
|
+
*/
|
|
239
|
+
user: (overrides = {}) => ({
|
|
240
|
+
id: TestData.randomString(),
|
|
241
|
+
name: 'Test User',
|
|
242
|
+
email: 'test@example.com',
|
|
243
|
+
createdAt: new Date(),
|
|
244
|
+
...overrides,
|
|
245
|
+
}),
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Generate test task object
|
|
249
|
+
*/
|
|
250
|
+
task: (overrides = {}) => ({
|
|
251
|
+
id: TestData.randomString(),
|
|
252
|
+
title: 'Test Task',
|
|
253
|
+
description: 'Test Description',
|
|
254
|
+
status: 'pending',
|
|
255
|
+
priority: 'medium',
|
|
256
|
+
createdAt: new Date(),
|
|
257
|
+
...overrides,
|
|
258
|
+
}),
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Test timing utilities
|
|
263
|
+
*/
|
|
264
|
+
export const TestTiming = {
|
|
265
|
+
/**
|
|
266
|
+
* Short delay for immediate async operations
|
|
267
|
+
*/
|
|
268
|
+
IMMEDIATE: 0,
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Minimal delay for fast async operations
|
|
272
|
+
*/
|
|
273
|
+
MINIMAL: 10,
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Short delay for quick operations
|
|
277
|
+
*/
|
|
278
|
+
SHORT: 50,
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Standard delay for most operations
|
|
282
|
+
*/
|
|
283
|
+
STANDARD: 100,
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Long delay for slow operations
|
|
287
|
+
*/
|
|
288
|
+
LONG: 500,
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Very long delay for very slow operations
|
|
292
|
+
*/
|
|
293
|
+
VERY_LONG: 1000,
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Default timeout for async operations
|
|
297
|
+
*/
|
|
298
|
+
DEFAULT_TIMEOUT: 5000,
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Extended timeout for slow operations
|
|
302
|
+
*/
|
|
303
|
+
EXTENDED_TIMEOUT: 10000,
|
|
304
|
+
};
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test Mocks
|
|
3
|
+
*
|
|
4
|
+
* Mock implementations for common Titan framework components
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { EventEmitter } from '@omnitron-dev/eventemitter';
|
|
8
|
+
import type { Logger } from 'pino';
|
|
9
|
+
import type { ConfigService } from '@omnitron-dev/titan/module/config';
|
|
10
|
+
import type { RedisManager } from '@omnitron-dev/titan/module/redis';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create mock logger
|
|
14
|
+
*/
|
|
15
|
+
export function createMockLogger(): Logger {
|
|
16
|
+
return {
|
|
17
|
+
trace: jest.fn(),
|
|
18
|
+
debug: jest.fn(),
|
|
19
|
+
info: jest.fn(),
|
|
20
|
+
warn: jest.fn(),
|
|
21
|
+
error: jest.fn(),
|
|
22
|
+
fatal: jest.fn(),
|
|
23
|
+
child: jest.fn(() => createMockLogger()),
|
|
24
|
+
level: 'info',
|
|
25
|
+
silent: jest.fn(),
|
|
26
|
+
bindings: jest.fn(() => ({})),
|
|
27
|
+
flush: jest.fn(),
|
|
28
|
+
isLevelEnabled: jest.fn(() => true),
|
|
29
|
+
} as any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Create mock ConfigService
|
|
34
|
+
*/
|
|
35
|
+
export function createMockConfigService(data: Record<string, any> = {}): ConfigService {
|
|
36
|
+
const emitter = new EventEmitter();
|
|
37
|
+
const configData = { ...data };
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
get: jest.fn((path?: string) => {
|
|
41
|
+
if (!path) return configData;
|
|
42
|
+
|
|
43
|
+
const keys = path.split('.');
|
|
44
|
+
let result = configData;
|
|
45
|
+
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
result = result?.[key];
|
|
48
|
+
if (result === undefined) return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
}),
|
|
53
|
+
|
|
54
|
+
set: jest.fn((path: string, value: any) => {
|
|
55
|
+
const keys = path.split('.');
|
|
56
|
+
const lastKey = keys.pop()!;
|
|
57
|
+
let target = configData;
|
|
58
|
+
|
|
59
|
+
for (const key of keys) {
|
|
60
|
+
if (!target[key]) {
|
|
61
|
+
target[key] = {};
|
|
62
|
+
}
|
|
63
|
+
target = target[key];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
target[lastKey] = value;
|
|
67
|
+
emitter.emit('config:changed', { path, value });
|
|
68
|
+
}),
|
|
69
|
+
|
|
70
|
+
has: jest.fn((path: string) => {
|
|
71
|
+
const keys = path.split('.');
|
|
72
|
+
let result = configData;
|
|
73
|
+
|
|
74
|
+
for (const key of keys) {
|
|
75
|
+
result = result?.[key];
|
|
76
|
+
if (result === undefined) return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
}),
|
|
81
|
+
|
|
82
|
+
delete: jest.fn((path: string) => {
|
|
83
|
+
const keys = path.split('.');
|
|
84
|
+
const lastKey = keys.pop()!;
|
|
85
|
+
let target = configData;
|
|
86
|
+
|
|
87
|
+
for (const key of keys) {
|
|
88
|
+
target = target?.[key];
|
|
89
|
+
if (!target) return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (lastKey in target) {
|
|
93
|
+
delete target[lastKey];
|
|
94
|
+
emitter.emit('config:changed', { path, value: undefined });
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return false;
|
|
99
|
+
}),
|
|
100
|
+
|
|
101
|
+
watch: jest.fn((path: string, callback: (event: any) => void) => {
|
|
102
|
+
const handler = (event: any) => {
|
|
103
|
+
if (event.path.startsWith(path)) {
|
|
104
|
+
callback(event);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
emitter.on('config:changed', handler);
|
|
108
|
+
return () => emitter.off('config:changed', handler);
|
|
109
|
+
}),
|
|
110
|
+
|
|
111
|
+
validate: jest.fn(() => true),
|
|
112
|
+
reload: jest.fn(async () => {}),
|
|
113
|
+
initialize: jest.fn(async () => {}),
|
|
114
|
+
dispose: jest.fn(async () => {}),
|
|
115
|
+
|
|
116
|
+
on: emitter.on.bind(emitter),
|
|
117
|
+
off: emitter.off.bind(emitter),
|
|
118
|
+
emit: emitter.emit.bind(emitter),
|
|
119
|
+
once: emitter.once.bind(emitter),
|
|
120
|
+
|
|
121
|
+
// Internal access for testing
|
|
122
|
+
__data: configData,
|
|
123
|
+
__emitter: emitter,
|
|
124
|
+
} as any;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Create mock RedisManager
|
|
129
|
+
*/
|
|
130
|
+
export function createMockRedisManager(): RedisManager {
|
|
131
|
+
const clients = new Map<string, any>();
|
|
132
|
+
const emitter = new EventEmitter();
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
getClient: jest.fn((name = 'default') => {
|
|
136
|
+
if (!clients.has(name)) {
|
|
137
|
+
const mockClient = createMockRedisClient();
|
|
138
|
+
clients.set(name, mockClient);
|
|
139
|
+
}
|
|
140
|
+
return clients.get(name);
|
|
141
|
+
}),
|
|
142
|
+
|
|
143
|
+
hasClient: jest.fn((name: string) => clients.has(name)),
|
|
144
|
+
|
|
145
|
+
addClient: jest.fn((name: string, client: any) => {
|
|
146
|
+
clients.set(name, client);
|
|
147
|
+
emitter.emit('client:added', { name, client });
|
|
148
|
+
}),
|
|
149
|
+
|
|
150
|
+
removeClient: jest.fn(async (name: string) => {
|
|
151
|
+
const client = clients.get(name);
|
|
152
|
+
if (client) {
|
|
153
|
+
await client.quit();
|
|
154
|
+
clients.delete(name);
|
|
155
|
+
emitter.emit('client:removed', { name });
|
|
156
|
+
}
|
|
157
|
+
}),
|
|
158
|
+
|
|
159
|
+
getAllClients: jest.fn(() => Array.from(clients.entries())),
|
|
160
|
+
|
|
161
|
+
connect: jest.fn(async () => {
|
|
162
|
+
for (const [name, client] of clients) {
|
|
163
|
+
await client.connect();
|
|
164
|
+
emitter.emit('client:connected', { name, client });
|
|
165
|
+
}
|
|
166
|
+
}),
|
|
167
|
+
|
|
168
|
+
disconnect: jest.fn(async () => {
|
|
169
|
+
for (const [name, client] of clients) {
|
|
170
|
+
await client.disconnect();
|
|
171
|
+
emitter.emit('client:disconnected', { name });
|
|
172
|
+
}
|
|
173
|
+
}),
|
|
174
|
+
|
|
175
|
+
isHealthy: jest.fn(async () => true),
|
|
176
|
+
|
|
177
|
+
getMetrics: jest.fn(() => ({
|
|
178
|
+
connections: clients.size,
|
|
179
|
+
commands: 0,
|
|
180
|
+
errors: 0,
|
|
181
|
+
})),
|
|
182
|
+
|
|
183
|
+
on: emitter.on.bind(emitter),
|
|
184
|
+
off: emitter.off.bind(emitter),
|
|
185
|
+
emit: emitter.emit.bind(emitter),
|
|
186
|
+
once: emitter.once.bind(emitter),
|
|
187
|
+
|
|
188
|
+
// Internal access for testing
|
|
189
|
+
__clients: clients,
|
|
190
|
+
__emitter: emitter,
|
|
191
|
+
} as any;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Create mock Redis client (simplified version)
|
|
196
|
+
*/
|
|
197
|
+
function createMockRedisClient(): any {
|
|
198
|
+
const data = new Map<string, any>();
|
|
199
|
+
const emitter = new EventEmitter();
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
get: jest.fn(async (key: string) => data.get(key) ?? null),
|
|
203
|
+
set: jest.fn(async (key: string, value: any) => {
|
|
204
|
+
data.set(key, value);
|
|
205
|
+
return 'OK';
|
|
206
|
+
}),
|
|
207
|
+
del: jest.fn(async (...keys: string[]) => {
|
|
208
|
+
let count = 0;
|
|
209
|
+
for (const key of keys) {
|
|
210
|
+
if (data.delete(key)) count++;
|
|
211
|
+
}
|
|
212
|
+
return count;
|
|
213
|
+
}),
|
|
214
|
+
exists: jest.fn(async (...keys: string[]) => keys.filter((k) => data.has(k)).length),
|
|
215
|
+
ping: jest.fn(async () => 'PONG'),
|
|
216
|
+
connect: jest.fn(async () => {
|
|
217
|
+
emitter.emit('connect');
|
|
218
|
+
emitter.emit('ready');
|
|
219
|
+
}),
|
|
220
|
+
disconnect: jest.fn(() => {
|
|
221
|
+
emitter.emit('close');
|
|
222
|
+
}),
|
|
223
|
+
quit: jest.fn(async () => {
|
|
224
|
+
emitter.emit('close');
|
|
225
|
+
}),
|
|
226
|
+
on: emitter.on.bind(emitter),
|
|
227
|
+
off: emitter.off.bind(emitter),
|
|
228
|
+
duplicate: jest.fn((): any => createMockRedisClient()),
|
|
229
|
+
__data: data,
|
|
230
|
+
__emitter: emitter,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Create mock scheduler service
|
|
236
|
+
*/
|
|
237
|
+
export function createMockSchedulerService() {
|
|
238
|
+
const jobs = new Map<string, any>();
|
|
239
|
+
const emitter = new EventEmitter();
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
schedule: jest.fn((name: string, pattern: string, handler: () => void) => {
|
|
243
|
+
jobs.set(name, { pattern, handler, enabled: true });
|
|
244
|
+
emitter.emit('job:scheduled', { name, pattern });
|
|
245
|
+
return name;
|
|
246
|
+
}),
|
|
247
|
+
|
|
248
|
+
unschedule: jest.fn((name: string) => {
|
|
249
|
+
if (jobs.delete(name)) {
|
|
250
|
+
emitter.emit('job:unscheduled', { name });
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
254
|
+
}),
|
|
255
|
+
|
|
256
|
+
getJob: jest.fn((name: string) => jobs.get(name)),
|
|
257
|
+
getAllJobs: jest.fn(() => Array.from(jobs.entries())),
|
|
258
|
+
|
|
259
|
+
start: jest.fn(() => {
|
|
260
|
+
emitter.emit('scheduler:started');
|
|
261
|
+
}),
|
|
262
|
+
|
|
263
|
+
stop: jest.fn(() => {
|
|
264
|
+
emitter.emit('scheduler:stopped');
|
|
265
|
+
}),
|
|
266
|
+
|
|
267
|
+
on: emitter.on.bind(emitter),
|
|
268
|
+
off: emitter.off.bind(emitter),
|
|
269
|
+
|
|
270
|
+
// Test helpers
|
|
271
|
+
__jobs: jobs,
|
|
272
|
+
__emitter: emitter,
|
|
273
|
+
__trigger: (name: string) => {
|
|
274
|
+
const job = jobs.get(name);
|
|
275
|
+
if (job?.enabled) {
|
|
276
|
+
job.handler();
|
|
277
|
+
emitter.emit('job:executed', { name });
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Create mock event bus
|
|
285
|
+
*/
|
|
286
|
+
export function createMockEventBus() {
|
|
287
|
+
const emitter = new EventEmitter();
|
|
288
|
+
const handlers = new Map<string, Set<(...args: any[]) => any>>();
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
emit: jest.fn(async (event: string, data?: any) => {
|
|
292
|
+
emitter.emit(event, data);
|
|
293
|
+
const eventHandlers = handlers.get(event);
|
|
294
|
+
if (eventHandlers) {
|
|
295
|
+
for (const handler of eventHandlers) {
|
|
296
|
+
await handler(data);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}),
|
|
300
|
+
|
|
301
|
+
on: jest.fn((event: string, handler: (...args: any[]) => any) => {
|
|
302
|
+
if (!handlers.has(event)) {
|
|
303
|
+
handlers.set(event, new Set());
|
|
304
|
+
}
|
|
305
|
+
handlers.get(event)!.add(handler);
|
|
306
|
+
emitter.on(event, handler);
|
|
307
|
+
return () => {
|
|
308
|
+
handlers.get(event)?.delete(handler);
|
|
309
|
+
emitter.off(event, handler);
|
|
310
|
+
};
|
|
311
|
+
}),
|
|
312
|
+
|
|
313
|
+
once: jest.fn((event: string, handler: (...args: any[]) => any) => {
|
|
314
|
+
const wrapper = (...args: any[]) => {
|
|
315
|
+
handlers.get(event)?.delete(wrapper);
|
|
316
|
+
return handler(...args);
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
if (!handlers.has(event)) {
|
|
320
|
+
handlers.set(event, new Set());
|
|
321
|
+
}
|
|
322
|
+
handlers.get(event)!.add(wrapper);
|
|
323
|
+
emitter.once(event, wrapper);
|
|
324
|
+
}),
|
|
325
|
+
|
|
326
|
+
off: jest.fn((event: string, handler: (...args: any[]) => any) => {
|
|
327
|
+
handlers.get(event)?.delete(handler);
|
|
328
|
+
emitter.off(event, handler);
|
|
329
|
+
}),
|
|
330
|
+
|
|
331
|
+
// Test helpers
|
|
332
|
+
__emitter: emitter,
|
|
333
|
+
__handlers: handlers,
|
|
334
|
+
__getHandlerCount: (event: string) => handlers.get(event)?.size ?? 0,
|
|
335
|
+
};
|
|
336
|
+
}
|