Repository navigation
Expand file tree
/
Copy pathcache.test.ts
More file actions
184 lines (161 loc) · 5.56 KB
/
Copy pathcache.test.ts
File metadata and controls
184 lines (161 loc) · 5.56 KB
1
2
3
4
5
6
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
111
112
113
114
115
116
117
118
119
120
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const { mockConfig } = vi.hoisted(() => ({
mockConfig: { cache: { enabled: true, ttlSeconds: 3600 } }
}));
vi.mock('$lib/config', () => ({ forumConfig: mockConfig }));
let cache: typeof import('$lib/cache');
beforeEach(async () => {
vi.resetModules();
localStorage.clear();
mockConfig.cache = { enabled: true, ttlSeconds: 3600 };
cache = await import('$lib/cache');
});
afterEach(() => {
vi.restoreAllMocks();
vi.useRealTimers();
});
describe('read/write round-trip', () => {
it('stores and retrieves values', () => {
cache.writeCache('k', { a: 1 });
expect(cache.readCache('k')).toEqual({ a: 1 });
});
it('returns null for missing keys', () => {
expect(cache.readCache('missing')).toBeNull();
});
it('is a no-op when disabled', () => {
mockConfig.cache.enabled = false;
cache.writeCache('k', 1);
expect(localStorage.length).toBe(0);
localStorage.setItem('dk:cache:v3:anon:k', JSON.stringify({ t: Date.now(), data: 1 }));
expect(cache.readCache('k')).toBeNull();
});
it('drops corrupt entries', () => {
localStorage.setItem('dk:cache:v3:anon:bad', 'not json');
expect(cache.readCache('bad')).toBeNull();
expect(localStorage.getItem('dk:cache:v3:anon:bad')).toBeNull();
});
it('drops entries without a numeric timestamp', () => {
localStorage.setItem('dk:cache:v3:anon:odd', JSON.stringify({ data: 1 }));
expect(cache.readCache('odd')).toBeNull();
});
it('expires entries past the TTL', () => {
vi.useFakeTimers();
cache.writeCache('k', 'v');
vi.advanceTimersByTime(3601 * 1000);
expect(cache.readCache('k')).toBeNull();
expect(localStorage.getItem('dk:cache:v3:anon:k')).toBeNull();
});
});
describe('user scoping', () => {
it('isolates entries per user and falls back to anon', () => {
cache.setCacheScope('alice');
cache.writeCache('k', 'alice-data');
cache.setCacheScope('bob');
expect(cache.readCache('k')).toBeNull();
cache.setCacheScope('alice');
expect(cache.readCache('k')).toBe('alice-data');
cache.setCacheScope(null);
expect(cache.readCache('k')).toBeNull();
});
});
describe('quota handling', () => {
it('prunes the oldest entries and retries the write', () => {
vi.useFakeTimers();
cache.writeCache('old', 1);
vi.advanceTimersByTime(1000);
cache.writeCache('new', 2);
localStorage.setItem('dk:cache:v3:anon:corrupt', 'junk'); // treated as oldest
localStorage.setItem('dk:cache:v3:anon:no-t', JSON.stringify({ data: 1 })); // also oldest
localStorage.setItem('unrelated', 'kept');
const original = Storage.prototype.setItem;
const spy = vi
.spyOn(Storage.prototype, 'setItem')
.mockImplementationOnce(() => {
throw new DOMException('quota', 'QuotaExceededError');
});
spy.mockImplementation(original as never);
cache.writeCache('k', 3);
expect(cache.readCache('k')).toBe(3);
// oldest half evicted (the two unreadable zero-timestamp entries)
expect(localStorage.getItem('dk:cache:v3:anon:corrupt')).toBeNull();
expect(localStorage.getItem('dk:cache:v3:anon:no-t')).toBeNull();
expect(cache.readCache('old')).toBe(1);
expect(cache.readCache('new')).toBe(2);
expect(localStorage.getItem('unrelated')).toBe('kept');
});
it('degrades to network-only when storage stays full', () => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new DOMException('quota', 'QuotaExceededError');
});
expect(() => cache.writeCache('k', 1)).not.toThrow();
expect(cache.readCache('k')).toBeNull();
});
});
describe('invalidateCache', () => {
it('removes matching prefixes for the current user only', () => {
cache.writeCache('discussions:all', 1);
cache.writeCache('discussions:cat:X', 2);
cache.writeCache('meta', 3);
cache.setCacheScope('alice');
cache.writeCache('discussions:all', 4);
cache.setCacheScope(null);
cache.invalidateCache('discussions');
expect(cache.readCache('discussions:all')).toBeNull();
expect(cache.readCache('discussions:cat:X')).toBeNull();
expect(cache.readCache('meta')).toBe(3);
cache.setCacheScope('alice');
expect(cache.readCache('discussions:all')).toBe(4);
});
});
describe('clearCache', () => {
it('wipes every scope and version but nothing else', () => {
cache.writeCache('k', 1);
cache.setCacheScope('alice');
cache.writeCache('k', 2);
localStorage.setItem('dk:cache:v0:anon:legacy', 'x');
localStorage.setItem('dk:token', 'keep');
cache.clearCache();
expect(Object.keys(localStorage)).toEqual(['dk:token']);
});
});
describe('swr', () => {
it('applies cached data first, then the fresh result', async () => {
cache.writeCache('k', 'stale');
const apply = vi.fn();
await cache.swr('k', async () => 'fresh', apply);
expect(apply.mock.calls).toEqual([
['stale', { fromCache: true }],
['fresh', { fromCache: false }]
]);
expect(cache.readCache('k')).toBe('fresh');
});
it('applies only the fresh result on a cold cache', async () => {
const apply = vi.fn();
await cache.swr('k', async () => 'fresh', apply);
expect(apply.mock.calls).toEqual([['fresh', { fromCache: false }]]);
});
it('keeps stale data when revalidation fails', async () => {
cache.writeCache('k', 'stale');
const apply = vi.fn();
await cache.swr(
'k',
async () => {
throw new Error('offline');
},
apply
);
expect(apply).toHaveBeenCalledTimes(1);
expect(apply).toHaveBeenCalledWith('stale', { fromCache: true });
});
it('rethrows when there is nothing cached to fall back on', async () => {
await expect(
cache.swr(
'k',
async () => {
throw new Error('offline');
},
vi.fn()
)
).rejects.toThrow('offline');
});
});