@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.
- package/dist/{chunk-DWy1uDak.cjs → chunk-CUT6urMc.cjs} +0 -9
- package/dist/expo.cjs +4 -3
- package/dist/expo.cjs.map +1 -0
- package/dist/expo.d.cts +8 -5
- package/dist/expo.d.cts.map +1 -0
- package/dist/expo.d.mts +8 -5
- package/dist/expo.d.mts.map +1 -0
- package/dist/expo.mjs +3 -2
- package/dist/expo.mjs.map +1 -0
- package/dist/{index-B6eF5Yxi.d.cts → index-BSYYdwRm.d.cts} +8 -5
- package/dist/index-BSYYdwRm.d.cts.map +1 -0
- package/dist/{index-Bv61mxVT.d.mts → index-Dx96gr-9.d.mts} +8 -5
- package/dist/index-Dx96gr-9.d.mts.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/memory.cjs +35 -3
- package/dist/memory.cjs.map +1 -0
- package/dist/memory.d.cts +8 -5
- package/dist/memory.d.cts.map +1 -0
- package/dist/memory.d.mts +8 -5
- package/dist/memory.d.mts.map +1 -0
- package/dist/memory.mjs +34 -3
- package/dist/memory.mjs.map +1 -0
- package/dist/{src-DVpoP03o.mjs → src-CrE4Nl5T.mjs} +2 -1
- package/dist/src-CrE4Nl5T.mjs.map +1 -0
- package/dist/{src-C7d6HaTp.cjs → src-zfYb7mdS.cjs} +3 -2
- package/dist/src-zfYb7mdS.cjs.map +1 -0
- package/dist/upstash.cjs +32 -2
- package/dist/upstash.cjs.map +1 -0
- package/dist/upstash.d.cts +8 -5
- package/dist/upstash.d.cts.map +1 -0
- package/dist/upstash.d.mts +8 -5
- package/dist/upstash.d.mts.map +1 -0
- package/dist/upstash.mjs +31 -2
- package/dist/upstash.mjs.map +1 -0
- package/package.json +6 -2
- package/src/__benchmarks__/memory.bench.ts +51 -0
- package/src/__tests__/cache.spec.ts +138 -105
- package/src/__tests__/integration.spec.ts +277 -268
- package/src/__tests__/memory.spec.ts +129 -129
- package/src/__tests__/upstash.spec.ts +209 -180
- package/src/expo.ts +45 -46
- package/src/index.ts +37 -36
- package/src/memory.ts +28 -27
- package/src/upstash.ts +28 -28
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +1 -1
- package/dist/__tests__/cache.spec.cjs +0 -77
- package/dist/__tests__/cache.spec.d.cts +0 -1
- package/dist/__tests__/cache.spec.d.mts +0 -1
- package/dist/__tests__/cache.spec.mjs +0 -77
- package/dist/__tests__/integration.spec.cjs +0 -235
- package/dist/__tests__/integration.spec.d.cts +0 -1
- package/dist/__tests__/integration.spec.d.mts +0 -1
- package/dist/__tests__/integration.spec.mjs +0 -235
- package/dist/__tests__/memory.spec.cjs +0 -126
- package/dist/__tests__/memory.spec.d.cts +0 -1
- package/dist/__tests__/memory.spec.d.mts +0 -1
- package/dist/__tests__/memory.spec.mjs +0 -126
- package/dist/__tests__/upstash.spec.cjs +0 -201
- package/dist/__tests__/upstash.spec.d.cts +0 -1
- package/dist/__tests__/upstash.spec.d.mts +0 -1
- package/dist/__tests__/upstash.spec.mjs +0 -201
- package/dist/magic-string.es-CBih0MJw.mjs +0 -1014
- package/dist/magic-string.es-KiPEzMtt.cjs +0 -1015
- package/dist/memory-Csy6J-Ak.cjs +0 -39
- package/dist/memory-QDTnSEX5.mjs +0 -33
- package/dist/upstash-CXy_duOa.mjs +0 -31
- package/dist/upstash-u8Hs4sZG.cjs +0 -37
- package/dist/vi.bdSIJ99Y-B-qkABPq.cjs +0 -14955
- 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 '../
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
});
|