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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
import * as fs from "fs";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { ASSET_TYPES, AssetStore } from "@karakeep/shared/assetdb";
import {
assertAssetExists,
assertAssetNotExists,
cleanupTempDirectory,
cleanupTestBucket,
createLocalFileSystemStore,
createS3Store,
createTempDirectory,
createTempFile,
createTestAssetData,
createTestBucket,
createTestImageData,
createTestPdfData,
generateLargeBuffer,
getAllAssetsArray,
streamToBuffer,
} from "./assetdb-utils";
interface TestContext {
store: AssetStore;
cleanup: () => Promise<void>;
}
async function createLocalContext(): Promise<TestContext> {
const tempDir = await createTempDirectory();
const store = createLocalFileSystemStore(tempDir);
return {
store,
cleanup: async () => {
await cleanupTempDirectory(tempDir);
},
};
}
async function createS3Context(): Promise<TestContext> {
const bucketName = `test-bucket-${Math.random().toString(36).substring(7)}`;
const s3Client = await createTestBucket(bucketName);
const store = createS3Store(bucketName);
return {
store,
cleanup: async () => {
await cleanupTestBucket(s3Client, bucketName);
},
};
}
describe.each([
{ name: "LocalFileSystemAssetStore", createContext: createLocalContext },
{ name: "S3AssetStore", createContext: createS3Context },
])("AssetStore Interface Compliance - $name", ({ createContext }) => {
let context: TestContext;
beforeEach(async () => {
context = await createContext();
});
afterEach(async () => {
await context.cleanup();
});
describe("Basic CRUD Operations", () => {
it("should save and retrieve an asset", async () => {
const testData = createTestAssetData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { asset, metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset).toEqual(testData.content);
expect(metadata).toEqual(testData.metadata);
});
it("should delete an asset", async () => {
const testData = createTestAssetData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
await assertAssetExists(context.store, testData.userId, testData.assetId);
await context.store.deleteAsset({
userId: testData.userId,
assetId: testData.assetId,
});
await assertAssetNotExists(
context.store,
testData.userId,
testData.assetId,
);
});
it("should get asset size", async () => {
const testData = createTestAssetData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const size = await context.store.getAssetSize({
userId: testData.userId,
assetId: testData.assetId,
});
expect(size).toBe(testData.content.length);
});
it("should read asset metadata", async () => {
const testData = createTestAssetData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const metadata = await context.store.readAssetMetadata({
userId: testData.userId,
assetId: testData.assetId,
});
expect(metadata).toEqual(testData.metadata);
});
});
describe("Streaming Operations", () => {
it("should create readable stream", async () => {
const testData = createTestAssetData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const stream = await context.store.createAssetReadStream({
userId: testData.userId,
assetId: testData.assetId,
});
const streamedContent = await streamToBuffer(stream);
expect(streamedContent).toEqual(testData.content);
});
it("should support range requests in streams", async () => {
const content = Buffer.from("0123456789abcdef");
const testData = createTestAssetData({ content });
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const stream = await context.store.createAssetReadStream({
userId: testData.userId,
assetId: testData.assetId,
start: 5,
end: 10,
});
const streamedContent = await streamToBuffer(stream);
expect(streamedContent.toString()).toBe("56789a");
});
});
describe("Asset Types Support", () => {
it("should support all required asset types", async () => {
const testCases = [
{ contentType: ASSET_TYPES.IMAGE_JPEG, fileName: "test.jpg" },
{ contentType: ASSET_TYPES.IMAGE_PNG, fileName: "test.png" },
{ contentType: ASSET_TYPES.IMAGE_WEBP, fileName: "test.webp" },
{ contentType: ASSET_TYPES.APPLICATION_PDF, fileName: "test.pdf" },
{ contentType: ASSET_TYPES.TEXT_HTML, fileName: "test.html" },
{ contentType: ASSET_TYPES.VIDEO_MP4, fileName: "test.mp4" },
];
for (const { contentType, fileName } of testCases) {
const testData = createTestAssetData({
metadata: { contentType, fileName },
});
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(metadata.contentType).toBe(contentType);
expect(metadata.fileName).toBe(fileName);
}
});
it("should handle large assets", async () => {
const largeContent = generateLargeBuffer(5); // 5MB
const testData = createTestAssetData({ content: largeContent });
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { asset } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset.length).toBe(largeContent.length);
expect(asset).toEqual(largeContent);
});
it("should reject unsupported asset types", async () => {
const testData = createTestAssetData({
metadata: {
contentType: "unsupported/type",
fileName: "test.unsupported",
},
});
await expect(
context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
}),
).rejects.toThrow("Unsupported asset type");
});
});
describe("Bulk Operations", () => {
it("should delete all user assets", async () => {
const userId = "bulk-test-user";
const testAssets = [
createTestAssetData({ userId }),
createTestAssetData({ userId }),
createTestAssetData({ userId }),
];
const otherUserAsset = createTestAssetData(); // Different user
// Save all assets
await Promise.all([
...testAssets.map((testData) =>
context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
}),
),
context.store.saveAsset({
userId: otherUserAsset.userId,
assetId: otherUserAsset.assetId,
asset: otherUserAsset.content,
metadata: otherUserAsset.metadata,
}),
]);
// Delete user assets
await context.store.deleteUserAssets({ userId });
// Verify user assets are deleted
for (const testData of testAssets) {
await assertAssetNotExists(
context.store,
testData.userId,
testData.assetId,
);
}
// Verify other user's asset still exists
await assertAssetExists(
context.store,
otherUserAsset.userId,
otherUserAsset.assetId,
);
});
it("should list all assets", async () => {
const testAssets = [
createTestAssetData(),
createTestImageData(),
createTestPdfData(),
];
// Save all assets
await Promise.all(
testAssets.map((testData) =>
context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
}),
),
);
const assets = await getAllAssetsArray(context.store);
expect(assets).toHaveLength(3);
// Verify all assets are present
const assetIds = assets.map((a) => a.assetId);
for (const testData of testAssets) {
expect(assetIds).toContain(testData.assetId);
}
// Verify asset structure
for (const asset of assets) {
expect(asset).toHaveProperty("userId");
expect(asset).toHaveProperty("assetId");
expect(asset).toHaveProperty("contentType");
expect(asset).toHaveProperty("size");
expect(typeof asset.size).toBe("number");
expect(asset.size).toBeGreaterThan(0);
}
});
});
describe("File Operations", () => {
it("should save asset from file and delete original file", async () => {
const testData = createTestAssetData();
const tempFile = await createTempFile(testData.content, "temp-asset.bin");
// Verify temp file exists before operation
expect(
await fs.promises
.access(tempFile)
.then(() => true)
.catch(() => false),
).toBe(true);
await context.store.saveAssetFromFile({
userId: testData.userId,
assetId: testData.assetId,
assetPath: tempFile,
metadata: testData.metadata,
});
// Verify temp file was deleted
expect(
await fs.promises
.access(tempFile)
.then(() => true)
.catch(() => false),
).toBe(false);
// Verify asset was saved correctly
const { asset, metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset).toEqual(testData.content);
expect(metadata).toEqual(testData.metadata);
});
});
describe("Error Handling", () => {
it("should throw error for non-existent asset read", async () => {
await expect(
context.store.readAsset({
userId: "non-existent-user",
assetId: "non-existent-asset",
}),
).rejects.toThrow();
});
it("should throw error for non-existent asset metadata", async () => {
await expect(
context.store.readAssetMetadata({
userId: "non-existent-user",
assetId: "non-existent-asset",
}),
).rejects.toThrow();
});
it("should throw error for non-existent asset size", async () => {
await expect(
context.store.getAssetSize({
userId: "non-existent-user",
assetId: "non-existent-asset",
}),
).rejects.toThrow();
});
it("should handle deleting non-existent asset gracefully", async () => {
// Filesystem implementation throws errors for non-existent files
await expect(
context.store.deleteAsset({
userId: "non-existent-user",
assetId: "non-existent-asset",
}),
).resolves.not.toThrow();
});
it("should handle deletion of non-existent user directory gracefully", async () => {
// Should not throw error when user directory doesn't exist
await expect(
context.store.deleteUserAssets({ userId: "non-existent-user" }),
).resolves.not.toThrow();
});
it("should handle non-existent asset stream appropriately", async () => {
const streamResult = context.store.createAssetReadStream({
userId: "non-existent-user",
assetId: "non-existent-asset",
});
await expect(streamResult).rejects.toThrow();
});
});
describe("Data Integrity", () => {
it("should preserve binary data integrity", async () => {
const testData = createTestImageData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { asset } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
// Verify exact binary match
expect(asset).toEqual(testData.content);
// Verify PNG header is intact
expect(asset[0]).toBe(0x89);
expect(asset[1]).toBe(0x50);
expect(asset[2]).toBe(0x4e);
expect(asset[3]).toBe(0x47);
});
it("should preserve metadata exactly", async () => {
const testData = createTestAssetData({
metadata: {
contentType: ASSET_TYPES.APPLICATION_PDF,
fileName: "test-document.pdf",
},
});
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(metadata).toEqual(testData.metadata);
expect(metadata.contentType).toBe(ASSET_TYPES.APPLICATION_PDF);
expect(metadata.fileName).toBe("test-document.pdf");
});
it("should handle null fileName correctly", async () => {
const testData = createTestAssetData({
metadata: {
contentType: ASSET_TYPES.TEXT_HTML,
fileName: null,
},
});
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(metadata.fileName).toBeNull();
});
});
describe("Concurrent Operations", () => {
it("should handle concurrent saves safely", async () => {
const testAssets = Array.from({ length: 5 }, () => createTestAssetData());
await Promise.all(
testAssets.map((testData) =>
context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
}),
),
);
// Verify all assets were saved correctly
for (const testData of testAssets) {
const { asset, metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset).toEqual(testData.content);
expect(metadata).toEqual(testData.metadata);
}
});
it("should handle concurrent reads safely", async () => {
const testData = createTestAssetData();
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
// Perform multiple concurrent reads
const readPromises = Array.from({ length: 10 }, () =>
context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
}),
);
const results = await Promise.all(readPromises);
// Verify all reads returned the same data
for (const { asset, metadata } of results) {
expect(asset).toEqual(testData.content);
expect(metadata).toEqual(testData.metadata);
}
});
});
describe("Edge Cases", () => {
it("should handle empty assets", async () => {
const testData = createTestAssetData({
content: Buffer.alloc(0),
});
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { asset } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset.length).toBe(0);
const size = await context.store.getAssetSize({
userId: testData.userId,
assetId: testData.assetId,
});
expect(size).toBe(0);
});
it("should handle special characters in user and asset IDs", async () => {
const testData = createTestAssetData({
userId: "user-with-special_chars.123",
assetId: "asset_with-special.chars_456",
});
await context.store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const { asset, metadata } = await context.store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset).toEqual(testData.content);
expect(metadata).toEqual(testData.metadata);
});
});
});
|