@geekmidas/cache 0.0.7 → 0.2.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.
Files changed (73) hide show
  1. package/dist/{chunk-DWy1uDak.cjs → chunk-CUT6urMc.cjs} +0 -9
  2. package/dist/expo.cjs +4 -3
  3. package/dist/expo.cjs.map +1 -0
  4. package/dist/expo.d.cts +8 -5
  5. package/dist/expo.d.cts.map +1 -0
  6. package/dist/expo.d.mts +8 -5
  7. package/dist/expo.d.mts.map +1 -0
  8. package/dist/expo.mjs +3 -2
  9. package/dist/expo.mjs.map +1 -0
  10. package/dist/{index-B6eF5Yxi.d.cts → index-BSYYdwRm.d.cts} +8 -5
  11. package/dist/index-BSYYdwRm.d.cts.map +1 -0
  12. package/dist/{index-Bv61mxVT.d.mts → index-Dx96gr-9.d.mts} +8 -5
  13. package/dist/index-Dx96gr-9.d.mts.map +1 -0
  14. package/dist/index.cjs +1 -1
  15. package/dist/index.d.cts +1 -1
  16. package/dist/index.d.mts +1 -1
  17. package/dist/index.mjs +1 -1
  18. package/dist/memory.cjs +35 -3
  19. package/dist/memory.cjs.map +1 -0
  20. package/dist/memory.d.cts +8 -5
  21. package/dist/memory.d.cts.map +1 -0
  22. package/dist/memory.d.mts +8 -5
  23. package/dist/memory.d.mts.map +1 -0
  24. package/dist/memory.mjs +34 -3
  25. package/dist/memory.mjs.map +1 -0
  26. package/dist/{src-DVpoP03o.mjs → src-CrE4Nl5T.mjs} +2 -1
  27. package/dist/src-CrE4Nl5T.mjs.map +1 -0
  28. package/dist/{src-C7d6HaTp.cjs → src-zfYb7mdS.cjs} +3 -2
  29. package/dist/src-zfYb7mdS.cjs.map +1 -0
  30. package/dist/upstash.cjs +32 -2
  31. package/dist/upstash.cjs.map +1 -0
  32. package/dist/upstash.d.cts +8 -5
  33. package/dist/upstash.d.cts.map +1 -0
  34. package/dist/upstash.d.mts +8 -5
  35. package/dist/upstash.d.mts.map +1 -0
  36. package/dist/upstash.mjs +31 -2
  37. package/dist/upstash.mjs.map +1 -0
  38. package/package.json +6 -2
  39. package/src/__benchmarks__/memory.bench.ts +51 -0
  40. package/src/__tests__/cache.spec.ts +138 -105
  41. package/src/__tests__/integration.spec.ts +277 -268
  42. package/src/__tests__/memory.spec.ts +129 -129
  43. package/src/__tests__/upstash.spec.ts +209 -180
  44. package/src/expo.ts +45 -46
  45. package/src/index.ts +37 -36
  46. package/src/memory.ts +28 -27
  47. package/src/upstash.ts +28 -28
  48. package/tsconfig.json +9 -0
  49. package/tsdown.config.ts +1 -1
  50. package/dist/__tests__/cache.spec.cjs +0 -77
  51. package/dist/__tests__/cache.spec.d.cts +0 -1
  52. package/dist/__tests__/cache.spec.d.mts +0 -1
  53. package/dist/__tests__/cache.spec.mjs +0 -77
  54. package/dist/__tests__/integration.spec.cjs +0 -235
  55. package/dist/__tests__/integration.spec.d.cts +0 -1
  56. package/dist/__tests__/integration.spec.d.mts +0 -1
  57. package/dist/__tests__/integration.spec.mjs +0 -235
  58. package/dist/__tests__/memory.spec.cjs +0 -126
  59. package/dist/__tests__/memory.spec.d.cts +0 -1
  60. package/dist/__tests__/memory.spec.d.mts +0 -1
  61. package/dist/__tests__/memory.spec.mjs +0 -126
  62. package/dist/__tests__/upstash.spec.cjs +0 -201
  63. package/dist/__tests__/upstash.spec.d.cts +0 -1
  64. package/dist/__tests__/upstash.spec.d.mts +0 -1
  65. package/dist/__tests__/upstash.spec.mjs +0 -201
  66. package/dist/magic-string.es-CBih0MJw.mjs +0 -1014
  67. package/dist/magic-string.es-KiPEzMtt.cjs +0 -1015
  68. package/dist/memory-Csy6J-Ak.cjs +0 -39
  69. package/dist/memory-QDTnSEX5.mjs +0 -33
  70. package/dist/upstash-CXy_duOa.mjs +0 -31
  71. package/dist/upstash-u8Hs4sZG.cjs +0 -37
  72. package/dist/vi.bdSIJ99Y-B-qkABPq.cjs +0 -14955
  73. package/dist/vi.bdSIJ99Y-CB5havZF.mjs +0 -14951
@@ -1,111 +1,144 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import type { Cache } from '../cache';
2
+ import type { Cache } from '../';
3
+ import { getExpirationInSeconds } from '../';
3
4
  import { InMemoryCache } from '../memory';
4
5
  import { UpstashCache } from '../upstash';
5
6
 
6
7
  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
- });
8
+ describe('interface compliance', () => {
9
+ it('InMemoryCache should implement Cache interface', () => {
10
+ const cache: Cache = new InMemoryCache();
11
+
12
+ expect(typeof cache.get).toBe('function');
13
+ expect(typeof cache.set).toBe('function');
14
+ expect(typeof cache.delete).toBe('function');
15
+ });
16
+
17
+ it('UpstashCache should implement Cache interface', () => {
18
+ const cache: Cache = new UpstashCache(
19
+ 'http://localhost:8079',
20
+ 'example_token',
21
+ );
22
+
23
+ expect(typeof cache.get).toBe('function');
24
+ expect(typeof cache.set).toBe('function');
25
+ expect(typeof cache.delete).toBe('function');
26
+ });
27
+ });
28
+
29
+ describe('method signatures', () => {
30
+ it('should have correct get method signature', () => {
31
+ const cache: Cache = new InMemoryCache();
32
+
33
+ // These should compile without errors
34
+ const getResult: Promise<unknown> = cache.get('test');
35
+ expect(getResult).toBeInstanceOf(Promise);
36
+ });
37
+
38
+ it('should have correct set method signature', () => {
39
+ const cache: Cache = new InMemoryCache();
40
+
41
+ // These should compile without errors
42
+ const setResult: Promise<void> = cache.set('test', 'value');
43
+ expect(setResult).toBeInstanceOf(Promise);
44
+ });
45
+
46
+ it('should have correct delete method signature', () => {
47
+ const cache: Cache = new InMemoryCache();
48
+
49
+ // These should compile without errors
50
+ const deleteResult: Promise<void> = cache.delete('test');
51
+ expect(deleteResult).toBeInstanceOf(Promise);
52
+ });
53
+ });
54
+
55
+ describe('function-level generic type support', () => {
56
+ it('should support string types with function-level generics', async () => {
57
+ const cache: Cache = new InMemoryCache();
58
+
59
+ await cache.set<string>('key', 'value');
60
+ const result = await cache.get<string>('key');
61
+
62
+ expect(typeof result === 'string' || result === undefined).toBe(true);
63
+ });
64
+
65
+ it('should support number types with function-level generics', async () => {
66
+ const cache: Cache = new InMemoryCache();
67
+
68
+ await cache.set<number>('key', 42);
69
+ const result = await cache.get<number>('key');
70
+
71
+ expect(typeof result === 'number' || result === undefined).toBe(true);
72
+ });
73
+
74
+ it('should support object types with function-level generics', async () => {
75
+ interface User {
76
+ id: number;
77
+ name: string;
78
+ }
79
+
80
+ const cache: Cache = new InMemoryCache();
81
+ const user: User = { id: 1, name: 'John' };
82
+
83
+ await cache.set<User>('user', user);
84
+ const result = await cache.get<User>('user');
85
+
86
+ expect(
87
+ result === undefined ||
88
+ (typeof result === 'object' && 'id' in result && 'name' in result),
89
+ ).toBe(true);
90
+ });
91
+
92
+ it('should support mixed types in same cache instance', async () => {
93
+ const cache: Cache = new InMemoryCache();
94
+
95
+ await cache.set<string>('string', 'text');
96
+ await cache.set<number>('number', 42);
97
+ await cache.set<boolean>('boolean', true);
98
+
99
+ const stringResult = await cache.get<string>('string');
100
+ const numberResult = await cache.get<number>('number');
101
+ const booleanResult = await cache.get<boolean>('boolean');
102
+
103
+ expect(stringResult).toBe('text');
104
+ expect(numberResult).toBe(42);
105
+ expect(booleanResult).toBe(true);
106
+ });
107
+ });
108
+ });
109
+
110
+ describe('getExpirationInSeconds', () => {
111
+ it('should return 0 for null', () => {
112
+ expect(getExpirationInSeconds(null)).toBe(0);
113
+ });
114
+
115
+ it('should return 0 for undefined', () => {
116
+ expect(getExpirationInSeconds(undefined)).toBe(0);
117
+ });
118
+
119
+ it('should return positive seconds for future date', () => {
120
+ const futureDate = new Date(Date.now() + 60000); // 1 minute in the future
121
+ const seconds = getExpirationInSeconds(futureDate);
122
+ expect(seconds).toBeGreaterThan(0);
123
+ expect(seconds).toBeLessThanOrEqual(60);
124
+ });
125
+
126
+ it('should return 0 for past date', () => {
127
+ const pastDate = new Date(Date.now() - 60000); // 1 minute in the past
128
+ expect(getExpirationInSeconds(pastDate)).toBe(0);
129
+ });
130
+
131
+ it('should handle ISO string dates', () => {
132
+ const futureDate = new Date(Date.now() + 120000).toISOString(); // 2 minutes in the future
133
+ const seconds = getExpirationInSeconds(futureDate);
134
+ expect(seconds).toBeGreaterThan(0);
135
+ expect(seconds).toBeLessThanOrEqual(120);
136
+ });
137
+
138
+ it('should handle timestamp numbers', () => {
139
+ const futureTimestamp = Date.now() + 30000; // 30 seconds in the future
140
+ const seconds = getExpirationInSeconds(futureTimestamp);
141
+ expect(seconds).toBeGreaterThan(0);
142
+ expect(seconds).toBeLessThanOrEqual(30);
143
+ });
111
144
  });