@geekmidas/cache 0.0.1

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/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@geekmidas/cache",
3
+ "version": "0.0.1",
4
+ "description": "Type-safe email client with SMTP support and React templates",
5
+ "private": false,
6
+ "type": "module",
7
+ "exports": {
8
+ "./*": {
9
+ "import": "./dist/*.mjs",
10
+ "require": "./dist/*.cjs",
11
+ "types": "./src/*.ts"
12
+ }
13
+ },
14
+ "publishConfig": {
15
+ "registry": "https://registry.npmjs.org/",
16
+ "access": "public"
17
+ },
18
+ "dependencies": {},
19
+ "devDependencies": {},
20
+ "peerDependencies": {
21
+ "@upstash/redis": "~1.35.1"
22
+ }
23
+ }
@@ -0,0 +1,111 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import type { Cache } from '../cache';
3
+ import { InMemoryCache } from '../memory';
4
+ import { UpstashCache } from '../upstash';
5
+
6
+ describe('Cache Interface', () => {
7
+ describe('interface compliance', () => {
8
+ it('InMemoryCache should implement Cache interface', () => {
9
+ const cache: Cache<string> = new InMemoryCache<string>();
10
+
11
+ expect(typeof cache.get).toBe('function');
12
+ expect(typeof cache.set).toBe('function');
13
+ expect(typeof cache.delete).toBe('function');
14
+ });
15
+
16
+ it('UpstashCache should implement Cache interface', () => {
17
+ const cache = new UpstashCache('http://localhost:8079', 'example_token');
18
+
19
+ expect(typeof cache.get).toBe('function');
20
+ expect(typeof cache.set).toBe('function');
21
+ expect(typeof cache.delete).toBe('function');
22
+ });
23
+ });
24
+
25
+ describe('method signatures', () => {
26
+ interface TestCache extends Cache<any> {
27
+ get(key: string): Promise<any>;
28
+ set(key: string, value: any): Promise<void>;
29
+ delete(key: string): Promise<void>;
30
+ }
31
+
32
+ it('should have correct get method signature', () => {
33
+ const cache = new InMemoryCache() as TestCache;
34
+
35
+ // These should compile without errors
36
+ const getResult: Promise<any> = cache.get('test');
37
+ expect(getResult).toBeInstanceOf(Promise);
38
+ });
39
+
40
+ it('should have correct set method signature', () => {
41
+ const cache = new InMemoryCache() as TestCache;
42
+
43
+ // These should compile without errors
44
+ const setResult: Promise<void> = cache.set('test', 'value');
45
+ expect(setResult).toBeInstanceOf(Promise);
46
+ });
47
+
48
+ it('should have correct delete method signature', () => {
49
+ const cache = new InMemoryCache() as TestCache;
50
+
51
+ // These should compile without errors
52
+ const deleteResult: Promise<void> = cache.delete('test');
53
+ expect(deleteResult).toBeInstanceOf(Promise);
54
+ });
55
+ });
56
+
57
+ describe('generic type support', () => {
58
+ it('should support string types', async () => {
59
+ const cache: Cache<string> = new InMemoryCache<string>();
60
+
61
+ await cache.set('key', 'value');
62
+ const result = await cache.get('key');
63
+
64
+ expect(typeof result === 'string' || result === undefined).toBe(true);
65
+ });
66
+
67
+ it('should support number types', async () => {
68
+ const cache: Cache<number> = new InMemoryCache<number>();
69
+
70
+ await cache.set('key', 42);
71
+ const result = await cache.get('key');
72
+
73
+ expect(typeof result === 'number' || result === undefined).toBe(true);
74
+ });
75
+
76
+ it('should support object types', async () => {
77
+ interface User {
78
+ id: number;
79
+ name: string;
80
+ }
81
+
82
+ const cache: Cache<User> = new InMemoryCache<User>();
83
+ const user: User = { id: 1, name: 'John' };
84
+
85
+ await cache.set('user', user);
86
+ const result = await cache.get('user');
87
+
88
+ expect(
89
+ result === undefined ||
90
+ (typeof result === 'object' && 'id' in result && 'name' in result),
91
+ ).toBe(true);
92
+ });
93
+
94
+ it('should support union types', async () => {
95
+ type CacheValue = string | number | boolean;
96
+ const cache: Cache<CacheValue> = new InMemoryCache<CacheValue>();
97
+
98
+ await cache.set('string', 'text');
99
+ await cache.set('number', 42);
100
+ await cache.set('boolean', true);
101
+
102
+ const stringResult = await cache.get('string');
103
+ const numberResult = await cache.get('number');
104
+ const booleanResult = await cache.get('boolean');
105
+
106
+ expect(stringResult).toBe('text');
107
+ expect(numberResult).toBe(42);
108
+ expect(booleanResult).toBe(true);
109
+ });
110
+ });
111
+ });
@@ -0,0 +1,273 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+ import type { Cache } from '../cache';
3
+ import { InMemoryCache } from '../memory';
4
+
5
+ describe('Cache Integration Tests', () => {
6
+ let memoryCache: Cache<any>;
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<any>;
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<any>();
110
+ const targetCache = new InMemoryCache<any>();
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 = <T>(type: 'memory'): Cache<T> => {
143
+ switch (type) {
144
+ case 'memory':
145
+ return new InMemoryCache<T>();
146
+ default:
147
+ throw new Error(`Unknown cache type: ${type}`);
148
+ }
149
+ };
150
+
151
+ const userCache = createCache<{ id: number; name: string }>('memory');
152
+ const sessionCache = createCache<string>('memory');
153
+
154
+ await userCache.set('user:1', { id: 1, name: 'John' });
155
+ await sessionCache.set('session:abc', 'active');
156
+
157
+ expect(await userCache.get('user:1')).toEqual({ id: 1, name: 'John' });
158
+ expect(await sessionCache.get('session:abc')).toBe('active');
159
+ });
160
+
161
+ it('should support cache wrapper pattern', async () => {
162
+ class LoggingCache<T> implements Cache<T> {
163
+ private logs: string[] = [];
164
+
165
+ constructor(private underlying: Cache<T>) {}
166
+
167
+ async get(key: string): Promise<T | undefined> {
168
+ this.logs.push(`GET ${key}`);
169
+ return this.underlying.get(key);
170
+ }
171
+
172
+ async set(key: string, value: T): Promise<void> {
173
+ this.logs.push(`SET ${key}`);
174
+ return this.underlying.set(key, value);
175
+ }
176
+
177
+ async delete(key: string): Promise<void> {
178
+ this.logs.push(`DELETE ${key}`);
179
+ return this.underlying.delete(key);
180
+ }
181
+
182
+ getLogs(): string[] {
183
+ return [...this.logs];
184
+ }
185
+ }
186
+
187
+ const baseCache = new InMemoryCache<string>();
188
+ const loggingCache = new LoggingCache(baseCache);
189
+
190
+ await loggingCache.set('test', 'value');
191
+ await loggingCache.get('test');
192
+ await loggingCache.delete('test');
193
+
194
+ const logs = loggingCache.getLogs();
195
+ expect(logs).toEqual(['SET test', 'GET test', 'DELETE test']);
196
+ });
197
+ });
198
+
199
+ describe('performance and stress tests', () => {
200
+ it('should handle large number of operations', async () => {
201
+ const cache = new InMemoryCache<number>();
202
+ const operationCount = 1000;
203
+
204
+ // Bulk insert
205
+ const insertPromises = Array.from({ length: operationCount }, (_, i) =>
206
+ cache.set(`key:${i}`, i),
207
+ );
208
+ await Promise.all(insertPromises);
209
+
210
+ // Bulk read
211
+ const readPromises = Array.from({ length: operationCount }, (_, i) =>
212
+ cache.get(`key:${i}`),
213
+ );
214
+ const results = await Promise.all(readPromises);
215
+
216
+ // Verify all values
217
+ for (let i = 0; i < operationCount; i++) {
218
+ expect(results[i]).toBe(i);
219
+ }
220
+
221
+ // Bulk delete
222
+ const deletePromises = Array.from({ length: operationCount }, (_, i) =>
223
+ cache.delete(`key:${i}`),
224
+ );
225
+ await Promise.all(deletePromises);
226
+
227
+ // Verify deletion
228
+ const postDeleteRead = await cache.get('key:0');
229
+ expect(postDeleteRead).toBeUndefined();
230
+ });
231
+
232
+ it('should handle large objects efficiently', async () => {
233
+ const cache = new InMemoryCache<any>();
234
+
235
+ const largeObject = {
236
+ id: 1,
237
+ data: Array.from({ length: 1000 }, (_, i) => ({
238
+ index: i,
239
+ value: `value-${i}`,
240
+ metadata: {
241
+ timestamp: Date.now(),
242
+ random: Math.random(),
243
+ },
244
+ })),
245
+ };
246
+
247
+ const startTime = Date.now();
248
+ await cache.set('large-object', largeObject);
249
+ const result = await cache.get('large-object');
250
+ const endTime = Date.now();
251
+
252
+ expect(result).toEqual(largeObject);
253
+ expect(endTime - startTime).toBeLessThan(100); // Should complete within 100ms
254
+ });
255
+ });
256
+
257
+ describe('error scenarios', () => {
258
+ it('should handle operations after errors gracefully', async () => {
259
+ const cache = new InMemoryCache<string>();
260
+
261
+ // Successful operations
262
+ await cache.set('valid-key', 'valid-value');
263
+ expect(await cache.get('valid-key')).toBe('valid-value');
264
+
265
+ // Operations should continue working normally
266
+ await cache.set('another-key', 'another-value');
267
+ expect(await cache.get('another-key')).toBe('another-value');
268
+
269
+ await cache.delete('valid-key');
270
+ expect(await cache.get('valid-key')).toBeUndefined();
271
+ });
272
+ });
273
+ });
@@ -0,0 +1,134 @@
1
+ import { beforeEach, describe, expect, it } from 'vitest';
2
+ import { InMemoryCache } from '../memory';
3
+
4
+ describe('InMemoryCache', () => {
5
+ let cache: InMemoryCache<any>;
6
+
7
+ beforeEach(() => {
8
+ cache = new InMemoryCache();
9
+ });
10
+
11
+ describe('get', () => {
12
+ it('should return undefined for non-existent key', async () => {
13
+ const result = await cache.get('non-existent');
14
+ expect(result).toBeUndefined();
15
+ });
16
+
17
+ it('should return value for existing key', async () => {
18
+ await cache.set('test-key', 'test-value');
19
+ const result = await cache.get('test-key');
20
+ expect(result).toBe('test-value');
21
+ });
22
+
23
+ it('should handle different data types', async () => {
24
+ const objectValue = { id: 1, name: 'test' };
25
+ const arrayValue = [1, 2, 3];
26
+ const numberValue = 42;
27
+ const booleanValue = true;
28
+
29
+ await cache.set('object', objectValue);
30
+ await cache.set('array', arrayValue);
31
+ await cache.set('number', numberValue);
32
+ await cache.set('boolean', booleanValue);
33
+
34
+ expect(await cache.get('object')).toEqual(objectValue);
35
+ expect(await cache.get('array')).toEqual(arrayValue);
36
+ expect(await cache.get('number')).toBe(numberValue);
37
+ expect(await cache.get('boolean')).toBe(booleanValue);
38
+ });
39
+ });
40
+
41
+ describe('set', () => {
42
+ it('should store string value', async () => {
43
+ await cache.set('string-key', 'string-value');
44
+ const result = await cache.get('string-key');
45
+ expect(result).toBe('string-value');
46
+ });
47
+
48
+ it('should store object value', async () => {
49
+ const value = { id: 1, data: 'test' };
50
+ await cache.set('object-key', value);
51
+ const result = await cache.get('object-key');
52
+ expect(result).toEqual(value);
53
+ });
54
+
55
+ it('should overwrite existing value', async () => {
56
+ await cache.set('key', 'initial-value');
57
+ await cache.set('key', 'updated-value');
58
+ const result = await cache.get('key');
59
+ expect(result).toBe('updated-value');
60
+ });
61
+
62
+ it('should handle null values', async () => {
63
+ await cache.set('null-key', null);
64
+ const result = await cache.get('null-key');
65
+ expect(result).toBeNull();
66
+ });
67
+ });
68
+
69
+ describe('delete', () => {
70
+ it('should remove existing key', async () => {
71
+ await cache.set('to-delete', 'value');
72
+ await cache.delete('to-delete');
73
+ const result = await cache.get('to-delete');
74
+ expect(result).toBeUndefined();
75
+ });
76
+
77
+ it('should not throw error for non-existent key', async () => {
78
+ await expect(cache.delete('non-existent')).resolves.not.toThrow();
79
+ });
80
+
81
+ it('should only delete specified key', async () => {
82
+ await cache.set('key1', 'value1');
83
+ await cache.set('key2', 'value2');
84
+ await cache.delete('key1');
85
+
86
+ expect(await cache.get('key1')).toBeUndefined();
87
+ expect(await cache.get('key2')).toBe('value2');
88
+ });
89
+ });
90
+
91
+ describe('multiple operations', () => {
92
+ it('should handle multiple keys independently', async () => {
93
+ await cache.set('key1', 'value1');
94
+ await cache.set('key2', 'value2');
95
+ await cache.set('key3', 'value3');
96
+
97
+ expect(await cache.get('key1')).toBe('value1');
98
+ expect(await cache.get('key2')).toBe('value2');
99
+ expect(await cache.get('key3')).toBe('value3');
100
+ });
101
+
102
+ it('should maintain data integrity across operations', async () => {
103
+ const data = { count: 0 };
104
+ await cache.set('counter', data);
105
+
106
+ const retrieved = await cache.get('counter');
107
+ retrieved.count = 5;
108
+
109
+ const original = await cache.get('counter');
110
+ expect(original.count).toBe(5); // Reference equality in memory
111
+ });
112
+ });
113
+
114
+ describe('edge cases', () => {
115
+ it('should handle empty string as key', async () => {
116
+ await cache.set('', 'empty-key-value');
117
+ const result = await cache.get('');
118
+ expect(result).toBe('empty-key-value');
119
+ });
120
+
121
+ it('should handle special characters in keys', async () => {
122
+ const specialKey = 'key!@#$%^&*()_+{}|:"<>?[]\\;\',./?`~';
123
+ await cache.set(specialKey, 'special-value');
124
+ const result = await cache.get(specialKey);
125
+ expect(result).toBe('special-value');
126
+ });
127
+
128
+ it('should handle undefined values', async () => {
129
+ await cache.set('undefined-key', undefined);
130
+ const result = await cache.get('undefined-key');
131
+ expect(result).toBeUndefined();
132
+ });
133
+ });
134
+ });