@omnitron-dev/testing 0.4.4 → 0.4.6
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/README.md +43 -0
- package/dist/async/events.js.map +1 -1
- package/dist/async/index.d.ts +5 -5
- package/dist/async/index.js +5 -5
- package/dist/async/index.js.map +1 -1
- package/dist/async/retry.js.map +1 -1
- package/dist/async/wait.js.map +1 -1
- package/dist/docker/database-manager.js +13 -15
- package/dist/docker/database-manager.js.map +1 -1
- package/dist/docker/docker-test-manager.d.ts +12 -3
- package/dist/docker/docker-test-manager.js +286 -255
- package/dist/docker/docker-test-manager.js.map +1 -1
- package/dist/docker/redis-manager.js +70 -95
- package/dist/docker/redis-manager.js.map +1 -1
- package/dist/docker/types.d.ts +7 -2
- package/dist/env.d.ts +11 -0
- package/dist/env.js +21 -0
- package/dist/env.js.map +1 -0
- package/dist/errors.js.map +1 -1
- package/dist/helpers/assertions.js +3 -3
- package/dist/helpers/assertions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/runtime/bun-adapter.d.ts +23 -1
- package/dist/runtime/bun-adapter.js +14 -1
- package/dist/runtime/bun-adapter.js.map +1 -1
- package/dist/runtime/deno-adapter.d.ts +72 -15
- package/dist/runtime/deno-adapter.js +93 -78
- package/dist/runtime/deno-adapter.js.map +1 -1
- package/dist/runtime/node-adapter.d.ts +15 -3
- package/dist/runtime/node-adapter.js +1 -1
- package/dist/runtime/node-adapter.js.map +1 -1
- package/dist/runtime/test-adapter.d.ts +1 -1
- package/dist/runtime/test-adapter.js +12 -14
- package/dist/runtime/test-adapter.js.map +1 -1
- package/dist/titan/database/database-testing.module.d.ts +7 -6
- package/dist/titan/database/database-testing.module.js +40 -26
- package/dist/titan/database/database-testing.module.js.map +1 -1
- package/dist/titan/database/index.d.ts +1 -1
- package/dist/titan/database/index.js +1 -1
- package/dist/titan/database/index.js.map +1 -1
- package/dist/titan/database/test-utilities.d.ts +2 -2
- package/dist/titan/database/test-utilities.js +3 -2
- package/dist/titan/database/test-utilities.js.map +1 -1
- package/dist/titan/index.d.ts +9 -9
- package/dist/titan/index.js +6 -6
- package/dist/titan/index.js.map +1 -1
- package/dist/titan/nexus/mock-provider.js +6 -6
- package/dist/titan/nexus/mock-provider.js.map +1 -1
- package/dist/titan/nexus/test-container.js +10 -10
- package/dist/titan/nexus/test-container.js.map +1 -1
- package/dist/titan/nexus/test-utilities.js +9 -9
- package/dist/titan/nexus/test-utilities.js.map +1 -1
- package/dist/titan/pm/mock-process-spawner.d.ts +4 -3
- package/dist/titan/pm/mock-process-spawner.js +37 -15
- package/dist/titan/pm/mock-process-spawner.js.map +1 -1
- package/dist/titan/pm/mock-spawner.d.ts +2 -1
- package/dist/titan/pm/mock-spawner.js +3 -1
- package/dist/titan/pm/mock-spawner.js.map +1 -1
- package/dist/titan/pm/test-process-manager.d.ts +4 -4
- package/dist/titan/pm/test-process-manager.js +7 -3
- package/dist/titan/pm/test-process-manager.js.map +1 -1
- package/dist/titan/redis-test-utils.d.ts +8 -2
- package/dist/titan/redis-test-utils.js +147 -92
- package/dist/titan/redis-test-utils.js.map +1 -1
- package/dist/titan/test-application.d.ts +1 -1
- package/dist/titan/test-application.js +1 -1
- package/dist/titan/test-application.js.map +1 -1
- package/dist/titan/test-fixtures.js.map +1 -1
- package/dist/titan/test-mocks.d.ts +12 -11
- package/dist/titan/test-mocks.js +49 -49
- package/dist/titan/test-mocks.js.map +1 -1
- package/dist/titan/test-module.js.map +1 -1
- package/package.json +66 -40
- package/src/async/events.ts +1 -1
- package/src/async/index.ts +5 -30
- package/src/async/retry.ts +1 -4
- package/src/async/wait.ts +1 -4
- package/src/docker/a-port-the-network-could-reach.spec.ts +66 -0
- package/src/docker/database-manager.ts +13 -15
- package/src/docker/docker-orphan-reaper.spec.ts +175 -0
- package/src/docker/docker-port-collision.spec.ts +97 -0
- package/src/docker/docker-test-manager.spec.ts +122 -0
- package/src/docker/docker-test-manager.ts +421 -327
- package/src/docker/docker-volume-cleanup.spec.ts +113 -0
- package/src/docker/redis-cluster-creation.spec.ts +52 -0
- package/src/docker/redis-manager.ts +87 -117
- package/src/docker/types.ts +11 -4
- package/src/env.spec.ts +20 -0
- package/src/env.ts +33 -0
- package/src/errors.ts +9 -2
- package/src/globals.d.ts +2 -2
- package/src/helpers/assertions.ts +4 -7
- package/src/index.ts +1 -1
- package/src/runtime/adapter-surface.spec.ts +83 -0
- package/src/runtime/bun-adapter.ts +36 -5
- package/src/runtime/deno-adapter.ts +167 -108
- package/src/runtime/node-adapter.ts +5 -5
- package/src/runtime/test-adapter.test.ts +1 -1
- package/src/runtime/test-adapter.ts +15 -17
- package/src/titan/database/database-testing.module.ts +73 -53
- package/src/titan/database/index.ts +2 -2
- package/src/titan/database/test-utilities.ts +6 -3
- package/src/titan/index.ts +13 -27
- package/src/titan/nexus/mock-provider.ts +12 -12
- package/src/titan/nexus/test-container.ts +25 -17
- package/src/titan/nexus/test-utilities.ts +13 -11
- package/src/titan/pm/mock-process-spawner.ts +80 -27
- package/src/titan/pm/mock-spawner.ts +10 -4
- package/src/titan/pm/test-process-manager.ts +26 -9
- package/src/titan/redis-test-utils.test.ts +102 -0
- package/src/titan/redis-test-utils.ts +191 -109
- package/src/titan/test-application.ts +2 -2
- package/src/titan/test-fixtures.ts +0 -1
- package/src/titan/test-mocks.ts +50 -50
- package/runtime/bun.d.ts +0 -2
- package/runtime/bun.js +0 -5
- package/runtime/bun.ts +0 -7
- package/runtime/deno.d.ts +0 -2
- package/runtime/deno.js +0 -4
- package/runtime/jest.d.ts +0 -2
- package/runtime/jest.js +0 -16
- package/runtime/jest.ts +0 -7
|
@@ -5,13 +5,80 @@
|
|
|
5
5
|
* Provides comprehensive fixtures and helpers for testing Redis functionality
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
|
|
8
11
|
import { Redis, RedisOptions } from 'ioredis';
|
|
9
|
-
import { RedisManager } from '@omnitron-dev/titan
|
|
10
|
-
import { RedisService } from '@omnitron-dev/titan
|
|
12
|
+
import { RedisManager } from '@omnitron-dev/titan-redis';
|
|
13
|
+
import { RedisService } from '@omnitron-dev/titan-redis';
|
|
11
14
|
import { EventEmitter } from '@omnitron-dev/eventemitter';
|
|
12
15
|
import { Errors } from '@omnitron-dev/titan/errors';
|
|
13
16
|
import type { ILogger } from '@omnitron-dev/titan/module/logger';
|
|
14
17
|
|
|
18
|
+
/** Where a Vitest `globalSetup` publishes the endpoint it found or started. */
|
|
19
|
+
const REDIS_INFO_FILE = '.redis-test-info.json';
|
|
20
|
+
|
|
21
|
+
export interface TestRedisEndpoint {
|
|
22
|
+
host: string;
|
|
23
|
+
port: number;
|
|
24
|
+
/** True when no real Redis was available and the caller should mock. */
|
|
25
|
+
isMock: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The Redis a test suite should talk to.
|
|
30
|
+
*
|
|
31
|
+
* Four packages had grown their own copy of this resolution, and each copy
|
|
32
|
+
* ended in a hard-coded `TEST_REDIS_PORT ?? 16379` — the `redis-test` service
|
|
33
|
+
* in the monorepo's `docker-compose.test.yml`. That is a fine default and a
|
|
34
|
+
* terrible fallback: with no `globalSetup` registered, the earlier steps can
|
|
35
|
+
* never answer, and nothing listens on 16379 unless that compose file is up.
|
|
36
|
+
* Suites then hung until the 120 s per-test timeout rather than failing, which
|
|
37
|
+
* reads as "slow" instead of "misconfigured".
|
|
38
|
+
*
|
|
39
|
+
* Order: the file a `globalSetup` published, then `globalThis.globalRedis`
|
|
40
|
+
* (set in the main process, so workers rarely see it), then `USE_MOCK_REDIS` /
|
|
41
|
+
* `SKIP_DOCKER_TESTS`, then the compose default.
|
|
42
|
+
*
|
|
43
|
+
* It deliberately does NOT fall back to 6379. A Redis answering on the default
|
|
44
|
+
* port is whatever the developer happens to be running — on a machine with the
|
|
45
|
+
* downstream stand up, that is main/storage/messaging/pricing/payments/geo in
|
|
46
|
+
* db0..db5, and these suites call `flushdb`. titan's `globalSetup` will only
|
|
47
|
+
* use 6379 when `TEST_REDIS_ALLOW_DEFAULT_PORT=true` says so, and publishes
|
|
48
|
+
* the choice here.
|
|
49
|
+
*/
|
|
50
|
+
export function resolveTestRedisEndpoint(): TestRedisEndpoint {
|
|
51
|
+
try {
|
|
52
|
+
const infoPath = process.env['TITAN_TEST_REDIS_INFO'] ?? join(process.cwd(), REDIS_INFO_FILE);
|
|
53
|
+
if (existsSync(infoPath)) {
|
|
54
|
+
const info = JSON.parse(readFileSync(infoPath, 'utf-8')) as {
|
|
55
|
+
port?: number;
|
|
56
|
+
isMock?: boolean;
|
|
57
|
+
};
|
|
58
|
+
if (info.isMock) return { host: 'localhost', port: 0, isMock: true };
|
|
59
|
+
if (info.port) return { host: 'localhost', port: info.port, isMock: false };
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// A malformed or half-written file is not a reason to fail the suite;
|
|
63
|
+
// fall through to the other sources.
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const fromGlobal = (globalThis as { globalRedis?: { host?: string; port?: number } }).globalRedis;
|
|
67
|
+
if (fromGlobal?.port) {
|
|
68
|
+
return { host: fromGlobal.host ?? 'localhost', port: fromGlobal.port, isMock: false };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (process.env['USE_MOCK_REDIS'] === 'true' || process.env['SKIP_DOCKER_TESTS'] === 'true') {
|
|
72
|
+
return { host: 'localhost', port: 0, isMock: true };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
host: process.env['TEST_REDIS_HOST'] ?? 'localhost',
|
|
77
|
+
port: Number(process.env['TEST_REDIS_PORT'] ?? 16379),
|
|
78
|
+
isMock: false,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
15
82
|
/**
|
|
16
83
|
* Create a null logger for testing
|
|
17
84
|
*/
|
|
@@ -64,12 +131,16 @@ export interface RedisTestFixture {
|
|
|
64
131
|
* Create a Redis test fixture with proper cleanup
|
|
65
132
|
*/
|
|
66
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
|
+
}
|
|
67
138
|
const {
|
|
68
139
|
withManager = false,
|
|
69
140
|
withService = false,
|
|
70
141
|
db = 15, // Use test database
|
|
71
|
-
host =
|
|
72
|
-
port =
|
|
142
|
+
host = endpoint.host,
|
|
143
|
+
port = endpoint.port,
|
|
73
144
|
namespace = 'test',
|
|
74
145
|
lazyConnect = false,
|
|
75
146
|
enableReadyCheck = true,
|
|
@@ -90,66 +161,83 @@ export async function createRedisTestFixture(options: RedisTestFixtureOptions =
|
|
|
90
161
|
|
|
91
162
|
// Create primary client
|
|
92
163
|
const client = new Redis(redisOptions);
|
|
93
|
-
|
|
94
|
-
// Wait for connection if not lazy
|
|
95
|
-
if (!lazyConnect) {
|
|
96
|
-
await new Promise<void>((resolve, reject) => {
|
|
97
|
-
const timeout = setTimeout(() => {
|
|
98
|
-
client.removeAllListeners();
|
|
99
|
-
reject(Errors.timeout('Redis connection in test fixture', 10000));
|
|
100
|
-
}, 10000); // 10 second timeout
|
|
101
|
-
|
|
102
|
-
client.once('ready', () => {
|
|
103
|
-
clearTimeout(timeout);
|
|
104
|
-
resolve();
|
|
105
|
-
});
|
|
106
|
-
client.once('error', (error) => {
|
|
107
|
-
clearTimeout(timeout);
|
|
108
|
-
reject(error);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// Ensure connection starts
|
|
112
|
-
if (client.status === 'wait') {
|
|
113
|
-
client.connect().catch(reject);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
164
|
let manager: RedisManager | undefined;
|
|
119
165
|
let service: RedisService | undefined;
|
|
120
166
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
+
}
|
|
198
|
+
|
|
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
|
+
}
|
|
149
227
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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;
|
|
153
241
|
}
|
|
154
242
|
|
|
155
243
|
// Cleanup function
|
|
@@ -157,19 +245,13 @@ export async function createRedisTestFixture(options: RedisTestFixtureOptions =
|
|
|
157
245
|
try {
|
|
158
246
|
// Clear all data in test database
|
|
159
247
|
await client.flushdb();
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
248
|
+
} finally {
|
|
249
|
+
try {
|
|
250
|
+
await manager?.onModuleDestroy();
|
|
251
|
+
} finally {
|
|
252
|
+
client.disconnect();
|
|
253
|
+
client.removeAllListeners();
|
|
164
254
|
}
|
|
165
|
-
|
|
166
|
-
// Close client
|
|
167
|
-
client.disconnect();
|
|
168
|
-
|
|
169
|
-
// Remove all listeners
|
|
170
|
-
client.removeAllListeners();
|
|
171
|
-
} catch (error) {
|
|
172
|
-
console.error('Cleanup error:', error);
|
|
173
255
|
}
|
|
174
256
|
};
|
|
175
257
|
|
|
@@ -211,17 +293,17 @@ export function createMockRedis(): any {
|
|
|
211
293
|
|
|
212
294
|
const mock = {
|
|
213
295
|
// Connection
|
|
214
|
-
connect:
|
|
215
|
-
disconnect:
|
|
296
|
+
connect: vi.fn().mockResolvedValue(undefined),
|
|
297
|
+
disconnect: vi.fn().mockImplementation(() => {
|
|
216
298
|
mock.emit('close');
|
|
217
299
|
}),
|
|
218
|
-
quit:
|
|
219
|
-
ping:
|
|
300
|
+
quit: vi.fn().mockResolvedValue('OK'),
|
|
301
|
+
ping: vi.fn().mockResolvedValue('PONG'),
|
|
220
302
|
|
|
221
303
|
// Basic operations
|
|
222
|
-
get:
|
|
304
|
+
get: vi.fn().mockImplementation(async (key: string) => data.get(key) ?? null),
|
|
223
305
|
|
|
224
|
-
set:
|
|
306
|
+
set: vi.fn().mockImplementation(async (key: string, value: any, ...args: any[]) => {
|
|
225
307
|
data.set(key, typeof value === 'object' ? JSON.stringify(value) : String(value));
|
|
226
308
|
|
|
227
309
|
// Handle TTL
|
|
@@ -234,13 +316,13 @@ export function createMockRedis(): any {
|
|
|
234
316
|
return 'OK';
|
|
235
317
|
}),
|
|
236
318
|
|
|
237
|
-
setex:
|
|
319
|
+
setex: vi.fn().mockImplementation(async (key: string, ttl: number, value: any) => {
|
|
238
320
|
data.set(key, String(value));
|
|
239
321
|
setTimeout(() => data.delete(key), ttl * 1000);
|
|
240
322
|
return 'OK';
|
|
241
323
|
}),
|
|
242
324
|
|
|
243
|
-
del:
|
|
325
|
+
del: vi.fn().mockImplementation(async (...keys: string[]) => {
|
|
244
326
|
let count = 0;
|
|
245
327
|
for (const key of keys) {
|
|
246
328
|
if (data.delete(key)) count++;
|
|
@@ -248,9 +330,9 @@ export function createMockRedis(): any {
|
|
|
248
330
|
return count;
|
|
249
331
|
}),
|
|
250
332
|
|
|
251
|
-
exists:
|
|
333
|
+
exists: vi.fn().mockImplementation(async (...keys: string[]) => keys.filter((k) => data.has(k)).length),
|
|
252
334
|
|
|
253
|
-
expire:
|
|
335
|
+
expire: vi.fn().mockImplementation(async (key: string, ttl: number) => {
|
|
254
336
|
if (data.has(key)) {
|
|
255
337
|
setTimeout(() => data.delete(key), ttl * 1000);
|
|
256
338
|
return 1;
|
|
@@ -258,22 +340,22 @@ export function createMockRedis(): any {
|
|
|
258
340
|
return 0;
|
|
259
341
|
}),
|
|
260
342
|
|
|
261
|
-
ttl:
|
|
343
|
+
ttl: vi.fn().mockImplementation(async (key: string) => (data.has(key) ? -1 : -2)),
|
|
262
344
|
|
|
263
|
-
keys:
|
|
345
|
+
keys: vi.fn().mockImplementation(async (pattern: string) => {
|
|
264
346
|
if (pattern === '*') return Array.from(data.keys());
|
|
265
347
|
const regex = new RegExp(pattern.replace('*', '.*'));
|
|
266
348
|
return Array.from(data.keys()).filter((k) => regex.test(k));
|
|
267
349
|
}),
|
|
268
350
|
|
|
269
351
|
// Hash operations
|
|
270
|
-
hget:
|
|
352
|
+
hget: vi.fn().mockImplementation(async (key: string, field: string) => {
|
|
271
353
|
const hash = data.get(key);
|
|
272
354
|
if (!hash || typeof hash !== 'object') return null;
|
|
273
355
|
return hash[field] ?? null;
|
|
274
356
|
}),
|
|
275
357
|
|
|
276
|
-
hset:
|
|
358
|
+
hset: vi.fn().mockImplementation(async (key: string, field: string, value: any) => {
|
|
277
359
|
let hash = data.get(key);
|
|
278
360
|
if (!hash || typeof hash !== 'object') {
|
|
279
361
|
hash = {};
|
|
@@ -284,13 +366,13 @@ export function createMockRedis(): any {
|
|
|
284
366
|
return isNew ? 1 : 0;
|
|
285
367
|
}),
|
|
286
368
|
|
|
287
|
-
hgetall:
|
|
369
|
+
hgetall: vi.fn().mockImplementation(async (key: string) => {
|
|
288
370
|
const hash = data.get(key);
|
|
289
371
|
return hash && typeof hash === 'object' ? hash : {};
|
|
290
372
|
}),
|
|
291
373
|
|
|
292
374
|
// List operations
|
|
293
|
-
lpush:
|
|
375
|
+
lpush: vi.fn().mockImplementation(async (key: string, ...values: any[]) => {
|
|
294
376
|
let list = data.get(key);
|
|
295
377
|
if (!Array.isArray(list)) {
|
|
296
378
|
list = [];
|
|
@@ -300,7 +382,7 @@ export function createMockRedis(): any {
|
|
|
300
382
|
return list.length;
|
|
301
383
|
}),
|
|
302
384
|
|
|
303
|
-
rpush:
|
|
385
|
+
rpush: vi.fn().mockImplementation(async (key: string, ...values: any[]) => {
|
|
304
386
|
let list = data.get(key);
|
|
305
387
|
if (!Array.isArray(list)) {
|
|
306
388
|
list = [];
|
|
@@ -310,26 +392,26 @@ export function createMockRedis(): any {
|
|
|
310
392
|
return list.length;
|
|
311
393
|
}),
|
|
312
394
|
|
|
313
|
-
lpop:
|
|
395
|
+
lpop: vi.fn().mockImplementation(async (key: string) => {
|
|
314
396
|
const list = data.get(key);
|
|
315
397
|
if (!Array.isArray(list) || list.length === 0) return null;
|
|
316
398
|
return list.shift();
|
|
317
399
|
}),
|
|
318
400
|
|
|
319
|
-
rpop:
|
|
401
|
+
rpop: vi.fn().mockImplementation(async (key: string) => {
|
|
320
402
|
const list = data.get(key);
|
|
321
403
|
if (!Array.isArray(list) || list.length === 0) return null;
|
|
322
404
|
return list.pop();
|
|
323
405
|
}),
|
|
324
406
|
|
|
325
|
-
lrange:
|
|
407
|
+
lrange: vi.fn().mockImplementation(async (key: string, start: number, stop: number) => {
|
|
326
408
|
const list = data.get(key);
|
|
327
409
|
if (!Array.isArray(list)) return [];
|
|
328
410
|
return list.slice(start, stop + 1);
|
|
329
411
|
}),
|
|
330
412
|
|
|
331
413
|
// Set operations
|
|
332
|
-
sadd:
|
|
414
|
+
sadd: vi.fn().mockImplementation(async (key: string, ...members: any[]) => {
|
|
333
415
|
let set = data.get(key);
|
|
334
416
|
if (!set || !(set instanceof Set)) {
|
|
335
417
|
set = new Set();
|
|
@@ -345,41 +427,41 @@ export function createMockRedis(): any {
|
|
|
345
427
|
return added;
|
|
346
428
|
}),
|
|
347
429
|
|
|
348
|
-
smembers:
|
|
430
|
+
smembers: vi.fn().mockImplementation(async (key: string) => {
|
|
349
431
|
const set = data.get(key);
|
|
350
432
|
if (!set || !(set instanceof Set)) return [];
|
|
351
433
|
return Array.from(set);
|
|
352
434
|
}),
|
|
353
435
|
|
|
354
|
-
sismember:
|
|
436
|
+
sismember: vi.fn().mockImplementation(async (key: string, member: any) => {
|
|
355
437
|
const set = data.get(key);
|
|
356
438
|
if (!set || !(set instanceof Set)) return 0;
|
|
357
439
|
return set.has(member) ? 1 : 0;
|
|
358
440
|
}),
|
|
359
441
|
|
|
360
442
|
// Pub/Sub
|
|
361
|
-
subscribe:
|
|
443
|
+
subscribe: vi.fn().mockImplementation(async (...channels: string[]) => {
|
|
362
444
|
for (const channel of channels) {
|
|
363
445
|
pubsub.on(channel, () => {});
|
|
364
446
|
}
|
|
365
447
|
return channels.length;
|
|
366
448
|
}),
|
|
367
449
|
|
|
368
|
-
unsubscribe:
|
|
450
|
+
unsubscribe: vi.fn().mockImplementation(async (...channels: string[]) => {
|
|
369
451
|
for (const channel of channels) {
|
|
370
452
|
pubsub.removeAllListeners(channel);
|
|
371
453
|
}
|
|
372
454
|
return channels.length;
|
|
373
455
|
}),
|
|
374
456
|
|
|
375
|
-
publish:
|
|
457
|
+
publish: vi.fn().mockImplementation(async (channel: string, message: string) => {
|
|
376
458
|
const listeners = pubsub.listenerCount(channel);
|
|
377
459
|
pubsub.emit(channel, message);
|
|
378
460
|
return listeners;
|
|
379
461
|
}),
|
|
380
462
|
|
|
381
463
|
// Transactions
|
|
382
|
-
multi:
|
|
464
|
+
multi: vi.fn().mockImplementation(() => {
|
|
383
465
|
const commands: any[] = [];
|
|
384
466
|
const multi = {
|
|
385
467
|
get: (key: string) => {
|
|
@@ -394,7 +476,7 @@ export function createMockRedis(): any {
|
|
|
394
476
|
commands.push(['del', ...keys]);
|
|
395
477
|
return multi;
|
|
396
478
|
},
|
|
397
|
-
exec:
|
|
479
|
+
exec: vi.fn().mockImplementation(async () => {
|
|
398
480
|
const results = [];
|
|
399
481
|
for (const [cmd, ...args] of commands) {
|
|
400
482
|
try {
|
|
@@ -411,7 +493,7 @@ export function createMockRedis(): any {
|
|
|
411
493
|
}),
|
|
412
494
|
|
|
413
495
|
// Lock operations (for RedisLock decorator)
|
|
414
|
-
eval:
|
|
496
|
+
eval: vi.fn().mockImplementation(async (script: string, ...args: any[]) => {
|
|
415
497
|
// Simple lock implementation
|
|
416
498
|
const [numKeys, ...params] = args;
|
|
417
499
|
const keys = params.slice(0, numKeys);
|
|
@@ -453,40 +535,40 @@ export function createMockRedis(): any {
|
|
|
453
535
|
}),
|
|
454
536
|
|
|
455
537
|
// Utility
|
|
456
|
-
flushdb:
|
|
538
|
+
flushdb: vi.fn().mockImplementation(async () => {
|
|
457
539
|
data.clear();
|
|
458
540
|
locks.clear();
|
|
459
541
|
return 'OK';
|
|
460
542
|
}),
|
|
461
543
|
|
|
462
|
-
flushall:
|
|
544
|
+
flushall: vi.fn().mockImplementation(async () => {
|
|
463
545
|
data.clear();
|
|
464
546
|
locks.clear();
|
|
465
547
|
return 'OK';
|
|
466
548
|
}),
|
|
467
549
|
|
|
468
550
|
// Event emitter methods
|
|
469
|
-
on:
|
|
551
|
+
on: vi.fn().mockImplementation((event: string, handler: (...args: any[]) => any) => {
|
|
470
552
|
pubsub.on(event, handler);
|
|
471
553
|
return mock;
|
|
472
554
|
}),
|
|
473
555
|
|
|
474
|
-
once:
|
|
556
|
+
once: vi.fn().mockImplementation((event: string, handler: (...args: any[]) => any) => {
|
|
475
557
|
pubsub.once(event, handler);
|
|
476
558
|
return mock;
|
|
477
559
|
}),
|
|
478
560
|
|
|
479
|
-
off:
|
|
561
|
+
off: vi.fn().mockImplementation((event: string, handler: (...args: any[]) => any) => {
|
|
480
562
|
pubsub.off(event, handler);
|
|
481
563
|
return mock;
|
|
482
564
|
}),
|
|
483
565
|
|
|
484
|
-
emit:
|
|
566
|
+
emit: vi.fn().mockImplementation((event: string, ...args: any[]) => {
|
|
485
567
|
pubsub.emit(event, ...args);
|
|
486
568
|
return true;
|
|
487
569
|
}),
|
|
488
570
|
|
|
489
|
-
removeAllListeners:
|
|
571
|
+
removeAllListeners: vi.fn().mockImplementation((event?: string) => {
|
|
490
572
|
if (event) {
|
|
491
573
|
pubsub.removeAllListeners(event);
|
|
492
574
|
} else {
|
|
@@ -496,7 +578,7 @@ export function createMockRedis(): any {
|
|
|
496
578
|
}),
|
|
497
579
|
|
|
498
580
|
// Duplicate for creating subscriber clients
|
|
499
|
-
duplicate:
|
|
581
|
+
duplicate: vi.fn().mockImplementation(() => createMockRedis()),
|
|
500
582
|
|
|
501
583
|
// Status
|
|
502
584
|
status: 'ready',
|
|
@@ -111,8 +111,8 @@ export class TestApplication {
|
|
|
111
111
|
/**
|
|
112
112
|
* Create spy for service method
|
|
113
113
|
*/
|
|
114
|
-
spyOn<T>(service: T, method: keyof T):
|
|
115
|
-
const spy =
|
|
114
|
+
spyOn<T>(service: T, method: keyof T): any {
|
|
115
|
+
const spy = vi.spyOn(service as any, method as any);
|
|
116
116
|
this.addCleanup(() => spy.mockRestore());
|
|
117
117
|
return spy;
|
|
118
118
|
}
|