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
|
import { HeadObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { S3AssetStore } from "@karakeep/shared/assetdb";
import {
assertAssetExists,
cleanupTestBucket,
createS3Store,
createTestAssetData,
createTestBucket,
} from "./assetdb-utils";
describe("S3AssetStore - S3-Specific Behaviors", () => {
let bucketName: string;
let s3Client: S3Client;
let store: S3AssetStore;
beforeEach(async () => {
bucketName = `test-bucket-${Math.random().toString(36).substring(7)}`;
s3Client = await createTestBucket(bucketName);
store = createS3Store(bucketName);
});
afterEach(async () => {
await cleanupTestBucket(s3Client, bucketName);
});
describe("S3 Object Structure", () => {
it("should store asset as single object with user-defined metadata", async () => {
const testData = createTestAssetData();
await store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
// Verify the object exists with the expected key structure
const objectKey = `${testData.userId}/${testData.assetId}`;
const headResponse = await s3Client.send(
new HeadObjectCommand({
Bucket: bucketName,
Key: objectKey,
}),
);
// Verify metadata is stored in S3 user-defined metadata
expect(headResponse.Metadata).toBeDefined();
expect(headResponse.Metadata!["x-amz-meta-content-type"]).toBe(
testData.metadata.contentType,
);
expect(headResponse.Metadata!["x-amz-meta-file-name"]).toBe(
testData.metadata.fileName || "",
);
// Verify content type is set correctly on the S3 object
expect(headResponse.ContentType).toBe(testData.metadata.contentType);
});
it("should handle null fileName in metadata correctly", async () => {
const testData = createTestAssetData({
metadata: {
contentType: "text/html",
fileName: null,
},
});
await store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
const objectKey = `${testData.userId}/${testData.assetId}`;
const headResponse = await s3Client.send(
new HeadObjectCommand({
Bucket: bucketName,
Key: objectKey,
}),
);
// Verify null fileName are not stored in S3 metadata
expect(headResponse.Metadata!["x-amz-meta-file-name"]).toBeUndefined();
// Verify reading back gives us null fileName
const metadata = await store.readAssetMetadata({
userId: testData.userId,
assetId: testData.assetId,
});
expect(metadata.fileName).toBeNull();
});
});
describe("S3 Key Structure", () => {
it("should use clean userId/assetId key structure", async () => {
const userId = "user123";
const assetId = "asset456";
const testData = createTestAssetData({ userId, assetId });
await store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
// Verify the exact key structure
const expectedKey = `${userId}/${assetId}`;
const headResponse = await s3Client.send(
new HeadObjectCommand({
Bucket: bucketName,
Key: expectedKey,
}),
);
expect(headResponse.ContentLength).toBe(testData.content.length);
});
it("should handle special characters in user and asset IDs", async () => {
const userId = "user/with/slashes";
const assetId = "asset-with-special-chars_123";
const testData = createTestAssetData({ userId, assetId });
await store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
await assertAssetExists(store, testData.userId, testData.assetId);
});
});
describe("S3 Eventual Consistency", () => {
it("should handle immediate read after write (MinIO strong consistency)", async () => {
const testData = createTestAssetData();
await store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
// Immediately try to read - should work with MinIO's strong consistency
const { asset, metadata } = await store.readAsset({
userId: testData.userId,
assetId: testData.assetId,
});
expect(asset).toEqual(testData.content);
expect(metadata).toEqual(testData.metadata);
});
});
describe("S3 Metadata Conversion", () => {
it("should correctly convert between AssetMetadata and S3 metadata", async () => {
const testCases = [
{
contentType: "image/jpeg",
fileName: "test-image.jpg",
},
{
contentType: "application/pdf",
fileName: "document.pdf",
},
{
contentType: "text/html",
fileName: null,
},
];
for (const metadata of testCases) {
const testData = createTestAssetData({ metadata });
await store.saveAsset({
userId: testData.userId,
assetId: testData.assetId,
asset: testData.content,
metadata: testData.metadata,
});
// Verify metadata round-trip
const retrievedMetadata = await store.readAssetMetadata({
userId: testData.userId,
assetId: testData.assetId,
});
expect(retrievedMetadata).toEqual(testData.metadata);
}
});
});
});
|