@geekmidas/cache 0.1.0 → 1.0.0

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.
@@ -3,280 +3,280 @@ import type { Cache } from '../';
3
3
  import { InMemoryCache } from '../memory';
4
4
 
5
5
  describe('Cache Integration Tests', () => {
6
- let memoryCache: Cache;
7
-
8
- beforeEach(() => {
9
- memoryCache = new InMemoryCache();
10
- });
11
-
12
- describe('cross-cache behavior consistency', () => {
13
- const testCases = [
14
- { name: 'InMemoryCache', cache: () => new InMemoryCache() },
15
- ];
16
-
17
- testCases.forEach(({ name, cache }) => {
18
- describe(name, () => {
19
- let cacheInstance: Cache;
20
-
21
- beforeEach(() => {
22
- cacheInstance = cache();
23
- });
24
-
25
- it('should handle basic CRUD operations', async () => {
26
- // Create
27
- await cacheInstance.set('user:1', { id: 1, name: 'John' });
28
-
29
- // Read
30
- const user = await cacheInstance.get('user:1');
31
- expect(user).toEqual({ id: 1, name: 'John' });
32
-
33
- // Update
34
- await cacheInstance.set('user:1', { id: 1, name: 'Jane' });
35
- const updatedUser = await cacheInstance.get('user:1');
36
- expect(updatedUser).toEqual({ id: 1, name: 'Jane' });
37
-
38
- // Delete
39
- await cacheInstance.delete('user:1');
40
- const deletedUser = await cacheInstance.get('user:1');
41
- expect(deletedUser).toBeUndefined();
42
- });
43
-
44
- it('should handle concurrent operations', async () => {
45
- const operations = [
46
- cacheInstance.set('key1', 'value1'),
47
- cacheInstance.set('key2', 'value2'),
48
- cacheInstance.set('key3', 'value3'),
49
- ];
50
-
51
- await Promise.all(operations);
52
-
53
- const results = await Promise.all([
54
- cacheInstance.get('key1'),
55
- cacheInstance.get('key2'),
56
- cacheInstance.get('key3'),
57
- ]);
58
-
59
- expect(results).toEqual(['value1', 'value2', 'value3']);
60
- });
61
-
62
- it('should handle data type preservation', async () => {
63
- const testData = {
64
- string: 'hello',
65
- number: 42,
66
- boolean: true,
67
- object: { nested: true },
68
- array: [1, 2, 3],
69
- null: null,
70
- };
71
-
72
- // Store all data types
73
- await Promise.all(
74
- Object.entries(testData).map(([key, value]) =>
75
- cacheInstance.set(key, value),
76
- ),
77
- );
78
-
79
- // Retrieve and verify
80
- const results = await Promise.all(
81
- Object.keys(testData).map((key) => cacheInstance.get(key)),
82
- );
83
-
84
- expect(results[0]).toBe('hello');
85
- expect(results[1]).toBe(42);
86
- expect(results[2]).toBe(true);
87
- expect(results[3]).toEqual({ nested: true });
88
- expect(results[4]).toEqual([1, 2, 3]);
89
- expect(results[5]).toBeNull();
90
- });
91
-
92
- it('should handle key isolation', async () => {
93
- await cacheInstance.set('user:1', 'User One');
94
- await cacheInstance.set('user:2', 'User Two');
95
- await cacheInstance.set('session:1', 'Session One');
96
-
97
- await cacheInstance.delete('user:1');
98
-
99
- expect(await cacheInstance.get('user:1')).toBeUndefined();
100
- expect(await cacheInstance.get('user:2')).toBe('User Two');
101
- expect(await cacheInstance.get('session:1')).toBe('Session One');
102
- });
103
- });
104
- });
105
- });
106
-
107
- describe('cache migration scenarios', () => {
108
- it('should support migrating data between cache implementations', async () => {
109
- const sourceCache = new InMemoryCache();
110
- const targetCache = new InMemoryCache();
111
-
112
- // Populate source cache
113
- const testData = {
114
- 'user:1': { id: 1, name: 'John' },
115
- 'user:2': { id: 2, name: 'Jane' },
116
- 'config:app': { theme: 'dark', notifications: true },
117
- };
118
-
119
- for (const [key, value] of Object.entries(testData)) {
120
- await sourceCache.set(key, value);
121
- }
122
-
123
- // Simulate migration by reading from source and writing to target
124
- const migrationKeys = Object.keys(testData);
125
- for (const key of migrationKeys) {
126
- const value = await sourceCache.get(key);
127
- if (value !== undefined) {
128
- await targetCache.set(key, value);
129
- }
130
- }
131
-
132
- // Verify migration
133
- for (const [key, expectedValue] of Object.entries(testData)) {
134
- const migratedValue = await targetCache.get(key);
135
- expect(migratedValue).toEqual(expectedValue);
136
- }
137
- });
138
- });
139
-
140
- describe('cache abstraction patterns', () => {
141
- it('should support cache factory pattern', async () => {
142
- const createCache = (type: 'memory'): Cache => {
143
- switch (type) {
144
- case 'memory':
145
- return new InMemoryCache();
146
- default:
147
- throw new Error(`Unknown cache type: ${type}`);
148
- }
149
- };
150
-
151
- const userCache = createCache('memory');
152
- const sessionCache = createCache('memory');
153
-
154
- await userCache.set<{ id: number; name: string }>('user:1', {
155
- id: 1,
156
- name: 'John',
157
- });
158
- await sessionCache.set<string>('session:abc', 'active');
159
-
160
- expect(
161
- await userCache.get<{ id: number; name: string }>('user:1'),
162
- ).toEqual({ id: 1, name: 'John' });
163
- expect(await sessionCache.get<string>('session:abc')).toBe('active');
164
- });
165
-
166
- it('should support cache wrapper pattern', async () => {
167
- class LoggingCache implements Cache {
168
- private logs: string[] = [];
169
-
170
- constructor(private underlying: Cache) {}
171
-
172
- async get<T>(key: string): Promise<T | undefined> {
173
- this.logs.push(`GET ${key}`);
174
- return this.underlying.get<T>(key);
175
- }
176
-
177
- async set<T>(key: string, value: T, ttl?: number): Promise<void> {
178
- this.logs.push(`SET ${key}`);
179
- return this.underlying.set<T>(key, value, ttl);
180
- }
181
-
182
- async delete(key: string): Promise<void> {
183
- this.logs.push(`DELETE ${key}`);
184
- return this.underlying.delete(key);
185
- }
186
-
187
- async ttl(key: string): Promise<number> {
188
- return this.underlying.ttl(key);
189
- }
190
-
191
- getLogs(): string[] {
192
- return [...this.logs];
193
- }
194
- }
195
-
196
- const baseCache = new InMemoryCache();
197
- const loggingCache = new LoggingCache(baseCache);
198
-
199
- await loggingCache.set('test', 'value');
200
- await loggingCache.get('test');
201
- await loggingCache.delete('test');
202
-
203
- const logs = loggingCache.getLogs();
204
- expect(logs).toEqual(['SET test', 'GET test', 'DELETE test']);
205
- });
206
- });
207
-
208
- describe('performance and stress tests', () => {
209
- it('should handle large number of operations', async () => {
210
- const cache = new InMemoryCache();
211
- const operationCount = 1000;
212
-
213
- // Bulk insert
214
- const insertPromises = Array.from({ length: operationCount }, (_, i) =>
215
- cache.set(`key:${i}`, i),
216
- );
217
- await Promise.all(insertPromises);
218
-
219
- // Bulk read
220
- const readPromises = Array.from({ length: operationCount }, (_, i) =>
221
- cache.get(`key:${i}`),
222
- );
223
- const results = await Promise.all(readPromises);
224
-
225
- // Verify all values
226
- for (let i = 0; i < operationCount; i++) {
227
- expect(results[i]).toBe(i);
228
- }
229
-
230
- // Bulk delete
231
- const deletePromises = Array.from({ length: operationCount }, (_, i) =>
232
- cache.delete(`key:${i}`),
233
- );
234
- await Promise.all(deletePromises);
235
-
236
- // Verify deletion
237
- const postDeleteRead = await cache.get('key:0');
238
- expect(postDeleteRead).toBeUndefined();
239
- });
240
-
241
- it('should handle large objects efficiently', async () => {
242
- const cache = new InMemoryCache();
243
-
244
- const largeObject = {
245
- id: 1,
246
- data: Array.from({ length: 1000 }, (_, i) => ({
247
- index: i,
248
- value: `value-${i}`,
249
- metadata: {
250
- timestamp: Date.now(),
251
- random: Math.random(),
252
- },
253
- })),
254
- };
255
-
256
- const startTime = Date.now();
257
- await cache.set('large-object', largeObject);
258
- const result = await cache.get('large-object');
259
- const endTime = Date.now();
260
-
261
- expect(result).toEqual(largeObject);
262
- expect(endTime - startTime).toBeLessThan(100); // Should complete within 100ms
263
- });
264
- });
265
-
266
- describe('error scenarios', () => {
267
- it('should handle operations after errors gracefully', async () => {
268
- const cache = new InMemoryCache();
269
-
270
- // Successful operations
271
- await cache.set('valid-key', 'valid-value');
272
- expect(await cache.get('valid-key')).toBe('valid-value');
273
-
274
- // Operations should continue working normally
275
- await cache.set('another-key', 'another-value');
276
- expect(await cache.get('another-key')).toBe('another-value');
277
-
278
- await cache.delete('valid-key');
279
- expect(await cache.get('valid-key')).toBeUndefined();
280
- });
281
- });
6
+ let _memoryCache: Cache;
7
+
8
+ beforeEach(() => {
9
+ _memoryCache = new InMemoryCache();
10
+ });
11
+
12
+ describe('cross-cache behavior consistency', () => {
13
+ const testCases = [
14
+ { name: 'InMemoryCache', cache: () => new InMemoryCache() },
15
+ ];
16
+
17
+ testCases.forEach(({ name, cache }) => {
18
+ describe(name, () => {
19
+ let cacheInstance: Cache;
20
+
21
+ beforeEach(() => {
22
+ cacheInstance = cache();
23
+ });
24
+
25
+ it('should handle basic CRUD operations', async () => {
26
+ // Create
27
+ await cacheInstance.set('user:1', { id: 1, name: 'John' });
28
+
29
+ // Read
30
+ const user = await cacheInstance.get('user:1');
31
+ expect(user).toEqual({ id: 1, name: 'John' });
32
+
33
+ // Update
34
+ await cacheInstance.set('user:1', { id: 1, name: 'Jane' });
35
+ const updatedUser = await cacheInstance.get('user:1');
36
+ expect(updatedUser).toEqual({ id: 1, name: 'Jane' });
37
+
38
+ // Delete
39
+ await cacheInstance.delete('user:1');
40
+ const deletedUser = await cacheInstance.get('user:1');
41
+ expect(deletedUser).toBeUndefined();
42
+ });
43
+
44
+ it('should handle concurrent operations', async () => {
45
+ const operations = [
46
+ cacheInstance.set('key1', 'value1'),
47
+ cacheInstance.set('key2', 'value2'),
48
+ cacheInstance.set('key3', 'value3'),
49
+ ];
50
+
51
+ await Promise.all(operations);
52
+
53
+ const results = await Promise.all([
54
+ cacheInstance.get('key1'),
55
+ cacheInstance.get('key2'),
56
+ cacheInstance.get('key3'),
57
+ ]);
58
+
59
+ expect(results).toEqual(['value1', 'value2', 'value3']);
60
+ });
61
+
62
+ it('should handle data type preservation', async () => {
63
+ const testData = {
64
+ string: 'hello',
65
+ number: 42,
66
+ boolean: true,
67
+ object: { nested: true },
68
+ array: [1, 2, 3],
69
+ null: null,
70
+ };
71
+
72
+ // Store all data types
73
+ await Promise.all(
74
+ Object.entries(testData).map(([key, value]) =>
75
+ cacheInstance.set(key, value),
76
+ ),
77
+ );
78
+
79
+ // Retrieve and verify
80
+ const results = await Promise.all(
81
+ Object.keys(testData).map((key) => cacheInstance.get(key)),
82
+ );
83
+
84
+ expect(results[0]).toBe('hello');
85
+ expect(results[1]).toBe(42);
86
+ expect(results[2]).toBe(true);
87
+ expect(results[3]).toEqual({ nested: true });
88
+ expect(results[4]).toEqual([1, 2, 3]);
89
+ expect(results[5]).toBeNull();
90
+ });
91
+
92
+ it('should handle key isolation', async () => {
93
+ await cacheInstance.set('user:1', 'User One');
94
+ await cacheInstance.set('user:2', 'User Two');
95
+ await cacheInstance.set('session:1', 'Session One');
96
+
97
+ await cacheInstance.delete('user:1');
98
+
99
+ expect(await cacheInstance.get('user:1')).toBeUndefined();
100
+ expect(await cacheInstance.get('user:2')).toBe('User Two');
101
+ expect(await cacheInstance.get('session:1')).toBe('Session One');
102
+ });
103
+ });
104
+ });
105
+ });
106
+
107
+ describe('cache migration scenarios', () => {
108
+ it('should support migrating data between cache implementations', async () => {
109
+ const sourceCache = new InMemoryCache();
110
+ const targetCache = new InMemoryCache();
111
+
112
+ // Populate source cache
113
+ const testData = {
114
+ 'user:1': { id: 1, name: 'John' },
115
+ 'user:2': { id: 2, name: 'Jane' },
116
+ 'config:app': { theme: 'dark', notifications: true },
117
+ };
118
+
119
+ for (const [key, value] of Object.entries(testData)) {
120
+ await sourceCache.set(key, value);
121
+ }
122
+
123
+ // Simulate migration by reading from source and writing to target
124
+ const migrationKeys = Object.keys(testData);
125
+ for (const key of migrationKeys) {
126
+ const value = await sourceCache.get(key);
127
+ if (value !== undefined) {
128
+ await targetCache.set(key, value);
129
+ }
130
+ }
131
+
132
+ // Verify migration
133
+ for (const [key, expectedValue] of Object.entries(testData)) {
134
+ const migratedValue = await targetCache.get(key);
135
+ expect(migratedValue).toEqual(expectedValue);
136
+ }
137
+ });
138
+ });
139
+
140
+ describe('cache abstraction patterns', () => {
141
+ it('should support cache factory pattern', async () => {
142
+ const createCache = (type: 'memory'): Cache => {
143
+ switch (type) {
144
+ case 'memory':
145
+ return new InMemoryCache();
146
+ default:
147
+ throw new Error(`Unknown cache type: ${type}`);
148
+ }
149
+ };
150
+
151
+ const userCache = createCache('memory');
152
+ const sessionCache = createCache('memory');
153
+
154
+ await userCache.set<{ id: number; name: string }>('user:1', {
155
+ id: 1,
156
+ name: 'John',
157
+ });
158
+ await sessionCache.set<string>('session:abc', 'active');
159
+
160
+ expect(
161
+ await userCache.get<{ id: number; name: string }>('user:1'),
162
+ ).toEqual({ id: 1, name: 'John' });
163
+ expect(await sessionCache.get<string>('session:abc')).toBe('active');
164
+ });
165
+
166
+ it('should support cache wrapper pattern', async () => {
167
+ class LoggingCache implements Cache {
168
+ private logs: string[] = [];
169
+
170
+ constructor(private underlying: Cache) {}
171
+
172
+ async get<T>(key: string): Promise<T | undefined> {
173
+ this.logs.push(`GET ${key}`);
174
+ return this.underlying.get<T>(key);
175
+ }
176
+
177
+ async set<T>(key: string, value: T, ttl?: number): Promise<void> {
178
+ this.logs.push(`SET ${key}`);
179
+ return this.underlying.set<T>(key, value, ttl);
180
+ }
181
+
182
+ async delete(key: string): Promise<void> {
183
+ this.logs.push(`DELETE ${key}`);
184
+ return this.underlying.delete(key);
185
+ }
186
+
187
+ async ttl(key: string): Promise<number> {
188
+ return this.underlying.ttl(key);
189
+ }
190
+
191
+ getLogs(): string[] {
192
+ return [...this.logs];
193
+ }
194
+ }
195
+
196
+ const baseCache = new InMemoryCache();
197
+ const loggingCache = new LoggingCache(baseCache);
198
+
199
+ await loggingCache.set('test', 'value');
200
+ await loggingCache.get('test');
201
+ await loggingCache.delete('test');
202
+
203
+ const logs = loggingCache.getLogs();
204
+ expect(logs).toEqual(['SET test', 'GET test', 'DELETE test']);
205
+ });
206
+ });
207
+
208
+ describe('performance and stress tests', () => {
209
+ it('should handle large number of operations', async () => {
210
+ const cache = new InMemoryCache();
211
+ const operationCount = 1000;
212
+
213
+ // Bulk insert
214
+ const insertPromises = Array.from({ length: operationCount }, (_, i) =>
215
+ cache.set(`key:${i}`, i),
216
+ );
217
+ await Promise.all(insertPromises);
218
+
219
+ // Bulk read
220
+ const readPromises = Array.from({ length: operationCount }, (_, i) =>
221
+ cache.get(`key:${i}`),
222
+ );
223
+ const results = await Promise.all(readPromises);
224
+
225
+ // Verify all values
226
+ for (let i = 0; i < operationCount; i++) {
227
+ expect(results[i]).toBe(i);
228
+ }
229
+
230
+ // Bulk delete
231
+ const deletePromises = Array.from({ length: operationCount }, (_, i) =>
232
+ cache.delete(`key:${i}`),
233
+ );
234
+ await Promise.all(deletePromises);
235
+
236
+ // Verify deletion
237
+ const postDeleteRead = await cache.get('key:0');
238
+ expect(postDeleteRead).toBeUndefined();
239
+ });
240
+
241
+ it('should handle large objects efficiently', async () => {
242
+ const cache = new InMemoryCache();
243
+
244
+ const largeObject = {
245
+ id: 1,
246
+ data: Array.from({ length: 1000 }, (_, i) => ({
247
+ index: i,
248
+ value: `value-${i}`,
249
+ metadata: {
250
+ timestamp: Date.now(),
251
+ random: Math.random(),
252
+ },
253
+ })),
254
+ };
255
+
256
+ const startTime = Date.now();
257
+ await cache.set('large-object', largeObject);
258
+ const result = await cache.get('large-object');
259
+ const endTime = Date.now();
260
+
261
+ expect(result).toEqual(largeObject);
262
+ expect(endTime - startTime).toBeLessThan(100); // Should complete within 100ms
263
+ });
264
+ });
265
+
266
+ describe('error scenarios', () => {
267
+ it('should handle operations after errors gracefully', async () => {
268
+ const cache = new InMemoryCache();
269
+
270
+ // Successful operations
271
+ await cache.set('valid-key', 'valid-value');
272
+ expect(await cache.get('valid-key')).toBe('valid-value');
273
+
274
+ // Operations should continue working normally
275
+ await cache.set('another-key', 'another-value');
276
+ expect(await cache.get('another-key')).toBe('another-value');
277
+
278
+ await cache.delete('valid-key');
279
+ expect(await cache.get('valid-key')).toBeUndefined();
280
+ });
281
+ });
282
282
  });