-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.test.ts
More file actions
277 lines (238 loc) · 8.73 KB
/
Copy pathstorage.test.ts
File metadata and controls
277 lines (238 loc) · 8.73 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Unit tests for Storage operations and auto-initialization
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { storage } from '@lib/storage';
import type { Feed, Article } from '@models/Feed';
import 'fake-indexeddb/auto';
describe('Storage', () => {
beforeEach(async () => {
await storage.init();
await storage.clear('feeds');
await storage.clear('articles');
});
describe('auto-initialization', () => {
it('should auto-initialize when calling put without explicit init', async () => {
// Create a fresh Storage instance to test auto-init
const { storage: freshStorage } = await import('@lib/storage');
const feed: Feed = {
id: 'auto-init-feed',
url: 'https://example.com/feed.xml',
title: 'Auto Init Feed',
description: 'Test',
lastFetchedAt: null,
refreshIntervalMinutes: 60,
paused: false,
errorCount: 0,
createdAt: new Date(),
deletedAt: null,
};
// Should NOT throw "Database not initialized"
await freshStorage.put('feeds', feed);
const stored = await freshStorage.get('feeds', 'auto-init-feed');
expect(stored).toBeDefined();
expect(stored?.title).toBe('Auto Init Feed');
});
it('should auto-initialize when calling getAll without explicit init', async () => {
const { storage: freshStorage } = await import('@lib/storage');
// Should NOT throw "Database not initialized"
const feeds = await freshStorage.getAll('feeds');
expect(Array.isArray(feeds)).toBe(true);
});
it('should auto-initialize when calling get without explicit init', async () => {
const { storage: freshStorage } = await import('@lib/storage');
// Should NOT throw "Database not initialized"
const result = await freshStorage.get('feeds', 'non-existent');
expect(result).toBeUndefined();
});
it('should handle multiple concurrent init calls safely', async () => {
const { storage: freshStorage } = await import('@lib/storage');
// Call init multiple times concurrently
await expect(
Promise.all([
freshStorage.init(),
freshStorage.init(),
freshStorage.init(),
])
).resolves.not.toThrow();
});
it('should handle concurrent storage operations without explicit init', async () => {
const { storage: freshStorage } = await import('@lib/storage');
// Multiple operations in parallel should all auto-init safely
const results = await Promise.all([
freshStorage.getAll('feeds'),
freshStorage.getAll('articles'),
freshStorage.getAll('categories'),
]);
expect(results).toHaveLength(3);
results.forEach(r => expect(Array.isArray(r)).toBe(true));
});
});
describe('bulkPut', () => {
it('should insert multiple items efficiently', async () => {
const feeds: Feed[] = Array.from({ length: 10 }, (_, i) => ({
id: `feed-${i}`,
url: `https://example.com/feed-${i}.xml`,
title: `Feed ${i}`,
description: `Description ${i}`,
lastFetchedAt: null,
refreshIntervalMinutes: 60,
paused: false,
errorCount: 0,
createdAt: new Date(),
deletedAt: null,
}));
await storage.bulkPut('feeds', feeds);
const allFeeds = await storage.getAll('feeds');
expect(allFeeds).toHaveLength(10);
});
it('should update existing items', async () => {
const feed: Feed = {
id: 'feed-1',
url: 'https://example.com/feed.xml',
title: 'Original Title',
description: 'Description',
lastFetchedAt: null,
refreshIntervalMinutes: 60,
paused: false,
errorCount: 0,
createdAt: new Date(),
deletedAt: null,
};
await storage.put('feeds', feed);
const updatedFeed = { ...feed, title: 'Updated Title' };
await storage.bulkPut('feeds', [updatedFeed]);
const result = await storage.get('feeds', 'feed-1');
expect(result?.title).toBe('Updated Title');
});
it('should handle empty array', async () => {
await expect(storage.bulkPut('feeds', [])).resolves.not.toThrow();
const allFeeds = await storage.getAll('feeds');
expect(allFeeds).toHaveLength(0);
});
it('should handle large batches', async () => {
const articles: Article[] = Array.from({ length: 100 }, (_, i) => ({
id: `article-${i}`,
feedId: 'feed-1',
title: `Article ${i}`,
summary: `Summary ${i}`,
link: `https://example.com/article-${i}`,
publishedAt: new Date(),
readAt: null,
isFavorite: false,
createdAt: new Date(),
}));
await storage.bulkPut('articles', articles);
const allArticles = await storage.getAll('articles');
expect(allArticles).toHaveLength(100);
});
});
describe('getQuota', () => {
it('should return storage quota information', async () => {
const quota = await storage.getQuota();
expect(quota).toHaveProperty('usage');
expect(quota).toHaveProperty('quota');
expect(quota).toHaveProperty('percentUsed');
expect(quota).toHaveProperty('available');
expect(typeof quota.usage).toBe('number');
expect(typeof quota.quota).toBe('number');
expect(typeof quota.percentUsed).toBe('number');
expect(typeof quota.available).toBe('number');
});
it('should calculate percentUsed correctly', async () => {
const quota = await storage.getQuota();
if (quota.quota > 0) {
const expectedPercent = (quota.usage / quota.quota) * 100;
expect(quota.percentUsed).toBeCloseTo(expectedPercent, 2);
}
});
it('should calculate available space correctly', async () => {
const quota = await storage.getQuota();
const expectedAvailable = quota.quota - quota.usage;
expect(quota.available).toBe(expectedAvailable);
});
});
describe('isQuotaExceeded', () => {
it('should return boolean', async () => {
const isExceeded = await storage.isQuotaExceeded();
expect(typeof isExceeded).toBe('boolean');
});
it('should return false for normal usage', async () => {
const isExceeded = await storage.isQuotaExceeded();
expect(isExceeded).toBe(false);
});
});
describe('getDatabaseSize', () => {
it('should return database size as number', async () => {
const size = await storage.getDatabaseSize();
expect(typeof size).toBe('number');
expect(size).toBeGreaterThanOrEqual(0);
});
it('should increase with added data', async () => {
const sizeBefore = await storage.getDatabaseSize();
const feeds: Feed[] = Array.from({ length: 50 }, (_, i) => ({
id: `feed-${i}`,
url: `https://example.com/feed-${i}.xml`,
title: `Feed ${i}`,
description: `Description ${i}`,
lastFetchedAt: null,
refreshIntervalMinutes: 60,
paused: false,
errorCount: 0,
createdAt: new Date(),
deletedAt: null,
}));
await storage.bulkPut('feeds', feeds);
const sizeAfter = await storage.getDatabaseSize();
// Size should increase (or stay same in test environment)
expect(sizeAfter).toBeGreaterThanOrEqual(sizeBefore);
});
});
describe('getAllByIndex', () => {
it('should query by feedId index', async () => {
const articles: Article[] = [
{
id: 'article-1',
feedId: 'feed-1',
title: 'Article 1',
summary: 'Summary',
link: 'https://example.com/1',
publishedAt: new Date(),
readAt: null,
isFavorite: false,
createdAt: new Date(),
},
{
id: 'article-2',
feedId: 'feed-1',
title: 'Article 2',
summary: 'Summary',
link: 'https://example.com/2',
publishedAt: new Date(),
readAt: null,
isFavorite: false,
createdAt: new Date(),
},
{
id: 'article-3',
feedId: 'feed-2',
title: 'Article 3',
summary: 'Summary',
link: 'https://example.com/3',
publishedAt: new Date(),
readAt: null,
isFavorite: false,
createdAt: new Date(),
},
];
await storage.bulkPut('articles', articles);
const feed1Articles = await storage.getAllByIndex('articles', 'feedId', 'feed-1');
expect(feed1Articles).toHaveLength(2);
expect(feed1Articles.every(a => a.feedId === 'feed-1')).toBe(true);
});
it('should return empty array for non-existent index value', async () => {
const results = await storage.getAllByIndex('articles', 'feedId', 'non-existent');
expect(results).toHaveLength(0);
});
});
});