@omnitron-dev/testing 0.3.1 → 0.3.2

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.
@@ -49,7 +49,7 @@ export interface TestRedisEndpoint {
49
49
  */
50
50
  export function resolveTestRedisEndpoint(): TestRedisEndpoint {
51
51
  try {
52
- const infoPath = join(process.cwd(), REDIS_INFO_FILE);
52
+ const infoPath = process.env['TITAN_TEST_REDIS_INFO'] ?? join(process.cwd(), REDIS_INFO_FILE);
53
53
  if (existsSync(infoPath)) {
54
54
  const info = JSON.parse(readFileSync(infoPath, 'utf-8')) as {
55
55
  port?: number;
@@ -131,12 +131,16 @@ export interface RedisTestFixture {
131
131
  * Create a Redis test fixture with proper cleanup
132
132
  */
133
133
  export async function createRedisTestFixture(options: RedisTestFixtureOptions = {}): Promise<RedisTestFixture> {
134
+ const endpoint = resolveTestRedisEndpoint();
135
+ if (endpoint.isMock && options.port === undefined) {
136
+ throw new Error('A real Redis fixture requires a test endpoint; Redis setup selected mock mode.');
137
+ }
134
138
  const {
135
139
  withManager = false,
136
140
  withService = false,
137
141
  db = 15, // Use test database
138
- host = 'localhost',
139
- port = 6379,
142
+ host = endpoint.host,
143
+ port = endpoint.port,
140
144
  namespace = 'test',
141
145
  lazyConnect = false,
142
146
  enableReadyCheck = true,
@@ -157,66 +161,83 @@ export async function createRedisTestFixture(options: RedisTestFixtureOptions =
157
161
 
158
162
  // Create primary client
159
163
  const client = new Redis(redisOptions);
160
-
161
- // Wait for connection if not lazy
162
- if (!lazyConnect) {
163
- await new Promise<void>((resolve, reject) => {
164
- const timeout = setTimeout(() => {
165
- client.removeAllListeners();
166
- reject(Errors.timeout('Redis connection in test fixture', 10000));
167
- }, 10000); // 10 second timeout
168
-
169
- client.once('ready', () => {
170
- clearTimeout(timeout);
171
- resolve();
172
- });
173
- client.once('error', (error) => {
174
- clearTimeout(timeout);
175
- reject(error);
176
- });
177
-
178
- // Ensure connection starts
179
- if (client.status === 'wait') {
180
- client.connect().catch(reject);
181
- }
182
- });
183
- }
184
-
185
164
  let manager: RedisManager | undefined;
186
165
  let service: RedisService | undefined;
187
166
 
188
- // Create manager if requested
189
- if (withManager) {
190
- // Use type assertion to avoid ioredis version compatibility issues
191
- const clientConfigs = [
192
- {
193
- namespace: 'default',
194
- ...redisOptions,
195
- db: redisOptions.db, // Use the test DB
196
- },
197
- {
198
- namespace,
199
- ...redisOptions,
200
- db: redisOptions.db, // Use the test DB
201
- },
202
- ...(namespace !== 'cache'
203
- ? [
204
- {
205
- namespace: 'cache',
206
- ...redisOptions,
207
- db: 14, // Use different DB for cache namespace
208
- },
209
- ]
210
- : []),
211
- ] as any;
212
-
213
- manager = new RedisManager({ clients: clientConfigs }, createTestNullLogger());
214
- await manager.onModuleInit();
215
- }
167
+ try {
168
+ // Wait for connection if not lazy
169
+ if (!lazyConnect) {
170
+ await new Promise<void>((resolve, reject) => {
171
+ const timeout = setTimeout(() => {
172
+ removeListeners();
173
+ reject(Errors.timeout('Redis connection in test fixture', 10000));
174
+ }, 10000); // 10 second timeout
175
+
176
+ const removeListeners = () => {
177
+ clearTimeout(timeout);
178
+ client.off('ready', onReady);
179
+ client.off('error', onError);
180
+ };
181
+ const onReady = () => {
182
+ removeListeners();
183
+ resolve();
184
+ };
185
+ const onError = (error: Error) => {
186
+ removeListeners();
187
+ reject(error);
188
+ };
189
+ client.once('ready', onReady);
190
+ client.once('error', onError);
191
+
192
+ // Ensure connection starts
193
+ if (client.status === 'wait') {
194
+ client.connect().catch(onError);
195
+ }
196
+ });
197
+ }
216
198
 
217
- // Create service if requested
218
- if (withService && manager) {
219
- service = new RedisService(manager);
199
+ // Create manager if requested
200
+ if (withManager) {
201
+ // Use type assertion to avoid ioredis version compatibility issues
202
+ const clientConfigs = [
203
+ {
204
+ namespace: 'default',
205
+ ...redisOptions,
206
+ db: redisOptions.db, // Use the test DB
207
+ },
208
+ {
209
+ namespace,
210
+ ...redisOptions,
211
+ db: redisOptions.db, // Use the test DB
212
+ },
213
+ ...(namespace !== 'cache'
214
+ ? [
215
+ {
216
+ namespace: 'cache',
217
+ ...redisOptions,
218
+ db: 14, // Use different DB for cache namespace
219
+ },
220
+ ]
221
+ : []),
222
+ ] as any;
223
+
224
+ manager = new RedisManager({ clients: clientConfigs }, createTestNullLogger());
225
+ await manager.onModuleInit();
226
+ }
227
+
228
+ // Create service if requested
229
+ if (withService && manager) {
230
+ service = new RedisService(manager);
231
+ }
232
+ } catch (error) {
233
+ try {
234
+ await manager?.onModuleDestroy();
235
+ } catch (cleanupError) {
236
+ throw new AggregateError([error, cleanupError], 'Redis fixture initialization and cleanup failed', { cause: cleanupError });
237
+ } finally {
238
+ client.disconnect();
239
+ }
240
+ throw error;
220
241
  }
221
242
 
222
243
  // Cleanup function
@@ -224,19 +245,13 @@ export async function createRedisTestFixture(options: RedisTestFixtureOptions =
224
245
  try {
225
246
  // Clear all data in test database
226
247
  await client.flushdb();
227
-
228
- // Cleanup manager
229
- if (manager) {
230
- await manager.onModuleDestroy();
248
+ } finally {
249
+ try {
250
+ await manager?.onModuleDestroy();
251
+ } finally {
252
+ client.disconnect();
253
+ client.removeAllListeners();
231
254
  }
232
-
233
- // Close client
234
- client.disconnect();
235
-
236
- // Remove all listeners
237
- client.removeAllListeners();
238
- } catch (error) {
239
- console.error('Cleanup error:', error);
240
255
  }
241
256
  };
242
257