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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* eslint-disable */
// TODO: Make these more compatible with eslint
window.kvTests = [
    {
        name: "testSetKeyWithValue",
        description: "Test setting a key-value pair and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set('testKey', 'testValue');
                assert(result === true, "Failed to set key with value");
                pass("testSetKeyWithValue passed");
            } catch (error) {
                fail("testSetKeyWithValue failed:", error);
            }
        }
    },
    {
        name: "testUpdateKey",
        description: "Test updating an existing key with a new value and verify it returns true",
        test: async function() {
            try {
                await puter.kv.set('updateKey', 'initialValue');
                const result = await puter.kv.set('updateKey', 'updatedValue');
                assert(result === true, "Failed to update existing key");
                pass("testUpdateKey passed");
            } catch (error) {
                fail("testUpdateKey failed:", error);
            }
        }
    },
    {
        name: "testKeySizeLimit",
        description: "Test setting a key that exceeds the size limit and verify it throws an error",
        test: async function() {
            try {
                const largeKey = 'a'.repeat(1025); // 1 KB + 1 byte
                await puter.kv.set(largeKey, 'value');
                fail("testKeySizeLimit failed: No error thrown for large key");
            } catch (error) {
                pass("testKeySizeLimit passed:", error.message);
            }
        }
    },
    {
        name: "testInvalidParameters",
        description: "Test setting a key with invalid parameters and verify it throws an error",
        test: async function() {
            try {
                await puter.kv.set(undefined, 'value');
                fail("testInvalidParameters failed: No error thrown for undefined key");
            } catch (error) {
                pass("testInvalidParameters passed:", error.message);
            }
        }
    },
    {
        name: "testEmptyKey",
        description: "Test setting an empty key and verify it throws an error",
        test: async function() {
            try {
                await puter.kv.set('', 'value');
                fail("testEmptyKey failed: No error thrown for empty key");
            } catch (error) {
                pass("testEmptyKey passed:", error.message);
            }
        }
    },
    {
        name: "testSetNullValue",
        description: "Test setting a null value and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set('nullValueKey', null);
                assert(result === true, "Failed to set null value");
                pass("testSetNullValue passed");
            } catch (error) {
                fail("testSetNullValue failed:", error);
            }
        }
    },
    {
        name: "testSetObjectValue",
        description: "Test setting an object as a value and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set('objectKey', { a: 1 });
                assert(result === true, "Failed to set object as value");
                pass("testSetObjectValue passed");
            } catch (error) {
                fail("testSetObjectValue failed:", error);
            }
        }
    },
    {
        name: "testSetKeyWithSpecialCharacters",
        description: "Test setting a key with special characters and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set('special@Key#', 'value');
                assert(result === true, "Failed to set key with special characters");
                pass("testSetKeyWithSpecialCharacters passed");
            } catch (error) {
                fail("testSetKeyWithSpecialCharacters failed:", error);
            }
        }
    },
    {
        name: "testSetLargeValue",
        description: "Test setting a large value and verify it returns true",
        test: async function() {
            try {
                const largeValue = 'a'.repeat(10000); // 10 KB
                const result = await puter.kv.set('largeValueKey', largeValue);
                assert(result === true, "Failed to set large value");
                pass("testSetLargeValue passed");
            } catch (error) {
                fail("testSetLargeValue failed:", error);
            }
        }
    },
    {
        name: "testSetBooleanValue",
        description: "Test setting a boolean value and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set('booleanKey', true);
                assert(result === true, "Failed to set boolean value");
                pass("testSetBooleanValue passed");
            } catch (error) {
                fail("testSetBooleanValue failed:", error);
            }
        }
    },
    {
        name: "testSetNumericKey",
        description: "Test setting a numeric key and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set(123, 'value');
                assert(result === true, "Failed to set numeric key");
                pass("testSetNumericKey passed");
            } catch (error) {
                fail("testSetNumericKey failed:", error);
            }
        }
    },
    {
        name: "testSetConcurrentKeys",
        description: "Test setting multiple keys concurrently and verify all return true",
        test: async function() {
            try {
                const promises = [puter.kv.set('key1', 'value1'), puter.kv.set('key2', 'value2')];
                const results = await Promise.all(promises);
                assert(results.every(result => result === true), "Failed to set concurrent keys");
                pass("testSetConcurrentKeys passed");
            } catch (error) {
                fail("testSetConcurrentKeys failed:", error);
            }
        }
    },
    {
        name: "testSetValueAndRetrieve",
        description: "Test setting a value and then retrieving it to verify it matches",
        test: async function() {
            try {
                await puter.kv.set('retrieveKey', 'testValue');
                const value = await puter.kv.get('retrieveKey');
                assert(value === 'testValue', "Failed to retrieve correct value");
                pass("testSetValueAndRetrieve passed");
            } catch (error) {
                fail("testSetValueAndRetrieve failed:", error);
            }
        }
    },
    {
        name: "testUpdateValueAndRetrieve",
        description: "Test updating a value and then retrieving it to verify it matches the updated value",
        test: async function() {
            try {
                await puter.kv.set('updateKey', 'initialValue');
                await puter.kv.set('updateKey', 'updatedValue');
                const value = await puter.kv.get('updateKey');
                assert(value === 'updatedValue', "Failed to retrieve updated value");
                pass("testUpdateValueAndRetrieve passed");
            } catch (error) {
                fail("testUpdateValueAndRetrieve failed:", error);
            }
        }
    },
    {
        name: "testSetNumericValueAndRetrieve",
        description: "Test setting a numeric value and then retrieving it to verify it matches",
        test: async function() {
            try {
                await puter.kv.set('numericKey', 123);
                const value = await puter.kv.get('numericKey');
                assert(value === 123, "Failed to retrieve numeric value");
                pass("testSetNumericValueAndRetrieve passed");
            } catch (error) {
                fail("testSetNumericValueAndRetrieve failed:", error);
            }
        }
    },
    {
        name: "testSetBooleanValueAndRetrieve",
        description: "Test setting a boolean value and then retrieving it to verify it matches",
        test: async function() {
            try {
                await puter.kv.set('booleanKey', true);
                const value = await puter.kv.get('booleanKey');
                assert(value === true, "Failed to retrieve boolean value");
                pass("testSetBooleanValueAndRetrieve passed");
            } catch (error) {
                fail("testSetBooleanValueAndRetrieve failed:", error);
            }
        }
    },
    {
        name: "testSetAndDeleteKey",
        description: "Test setting a key and then deleting it to verify it returns true",
        test: async function() {
            try {
                await puter.kv.set('deleteKey', 'value');
                const result = await puter.kv.del('deleteKey');
                assert(result === true, "Failed to delete key");
                pass("testSetAndDeleteKey passed");
            } catch (error) {
                fail("testSetAndDeleteKey failed:", error);
            }
        }
    },
    {
        name: "testGetNonexistentKey",
        description: "Test getting a non-existent key and verify it returns null",
        test: async function() {
            try {
                const value = await puter.kv.get('nonexistentKey_102mk');
                assert(value === null, "Failed to return `null` for nonexistent key");
                pass("testGetNonexistentKey passed");
            } catch (error) {
                fail("testGetNonexistentKey failed:", error);
            }
        }
    },
    {
        name: "testSetObjectValueAndRetrieve",
        description: "Test setting an object value and then retrieving it to verify it matches",
        test: async function() {
            try {
                const result = await puter.kv.set('objectKey', { a: 1 });
                assert(result === true, "Failed to set object as value");
                const value = await puter.kv.get('objectKey');
                assert(value.a === 1, "Failed to retrieve object value");
                pass("testSetObjectValueAndRetrieve passed");
            } catch (error) {
                fail("testSetObjectValueAndRetrieve failed:", error);
            }
        }
    },
    {
        name: "testSetArrayValue",
        description: "Test setting an array as a value and verify it returns true",
        test: async function() {
            try {
                const result = await puter.kv.set('arrayKey', [1, 2, 3]);
                assert(result === true, "Failed to set array as value");
                const value = await puter.kv.get('arrayKey');
                assert(value[0] === 1, "Failed to retrieve array value");
                pass("testSetArrayValue passed");
            } catch (error) {
                fail("testSetArrayValue failed:", error);
            }
        }
    },
    {
        name: "testSetKeyWithSpecialCharactersAndRetrieve",
        description: "Test setting a key with special characters and then retrieving it to verify it matches",
        test: async function() {
            try {
                await puter.kv.set('special@Key#', 'value');
                const value = await puter.kv.get('special@Key#');
                assert(value === 'value', "Failed to retrieve value for key with special characters");
                pass("testSetKeyWithSpecialCharactersAndRetrieve passed");
            } catch (error) {
                fail("testSetKeyWithSpecialCharactersAndRetrieve failed:", error);
            }
        }
    },
    {
        name: "testConcurrentSetOperations",
        description: "Test setting multiple keys concurrently and verify all return true",
        test: async function() {
            try {
                const promises = [puter.kv.set('key1', 'value1'), puter.kv.set('key2', 'value2')];
                const results = await Promise.all(promises);
                assert(results.every(result => result === true), "Failed to set concurrent keys");
                pass("testConcurrentSetOperations passed");
            } catch (error) {
                fail("testConcurrentSetOperations failed:", error);
            }
        }
    },
    {
        name: "testFlush",
        description: "Test flushing a bunch of keys and verify they no longer exist",
        test: async function() {
            try {
                const keys = [];
                for(let i = 0; i < 10; i++){
                    keys.push('key' + i);
                }
                await Promise.all(keys.map(key => puter.kv.set(key, 'value')));
                await puter.kv.flush();
                const results = await Promise.all(keys.map(key => puter.kv.get(key)));
                assert(results.every(result => result === null), "Failed to flush keys");
                pass("testFlush passed");
            } catch (error) {
                fail("testFlush failed:", error);
            }
        }
    },
    {
        name: "testIncr",
        description: "Test incrementing a key and verify it returns 1",
        test: async function() {
            try {
                const result = await puter.kv.incr(puter.randName());
                assert(result === 1, "Failed to increment key");
                pass("testIncr passed");
            } catch (error) {
                fail("testIncr failed:", error);
            }
        }
    },
    {
        name: "testDecr",
        description: "Test decrementing a key and verify it returns -1",
        test: async function() {
            try {
                const result = await puter.kv.decr(puter.randName());
                assert(result === -1, "Failed to decrement key");
                pass("testDecr passed");
            } catch (error) {
                fail("testDecr failed:", error);
            }
        }
    },
    {
        name: "testIncrExistingKey",
        description: "Test incrementing an existing key and verify it returns 2",
        test: async function() {
            try {
                await puter.kv.set('incrKey', 1);
                const result = await puter.kv.incr('incrKey');
                assert(result === 2, "Failed to increment existing key");
                pass("testIncrExistingKey passed");
            } catch (error) {
                fail("testIncrExistingKey failed:", error);
            }
        }
    },
    {
        name: "testDecrExistingKey",
        description: "Test decrementing an existing key and verify it returns 1",
        test: async function() {
            try {
                await puter.kv.set('decrKey', 2);
                const result = await puter.kv.decr('decrKey');
                assert(result === 1, "Failed to decrement existing key");
                pass("testDecrExistingKey passed");
            } catch (error) {
                fail("testDecrExistingKey failed:", error);
            }
        }
    },
    {
        name: "testIncrByAmount",
        description: "Test incrementing a key by a specified amount and verify it returns the correct value",
        test: async function() {
            try {
                await puter.kv.set('incrKey', 1);
                const result = await puter.kv.incr('incrKey', 5);
                assert(result === 6, "Failed to increment key by amount");
                pass("testIncrByAmount passed");
            } catch (error) {
                fail("testIncrByAmount failed:", error);
            }
        }
    },
    {
        name: "testDecrByAmount",
        description: "Test decrementing a key by a specified amount and verify it returns the correct value",
        test: async function() {
            try {
                await puter.kv.set('decrKey', 10);
                const result = await puter.kv.decr('decrKey', 5);
                assert(result === 5, "Failed to decrement key by amount");
                pass("testDecrByAmount passed");
            } catch (error) {
                fail("testDecrByAmount failed:", error);
            }
        }
    },
    {
        name: "testIncrByAmountExistingKey",
        description: "Test incrementing an existing key by a specified amount and verify it returns the correct value",
        test: async function() {
            try {
                await puter.kv.set('incrKey', 1);
                const result = await puter.kv.incr('incrKey', 5);
                assert(result === 6, "Failed to increment existing key by amount");
                pass("testIncrByAmountExistingKey passed");
            } catch (error) {
                fail("testIncrByAmountExistingKey failed:", error);
            }
        }
    },
    {
        name: "testDecrByAmountExistingKey",
        description: "Test decrementing an existing key by a specified amount and verify it returns the correct value",
        test: async function() {
            try {
                await puter.kv.set('decrKey', 10);
                const result = await puter.kv.decr('decrKey', 5);
                assert(result === 5, "Failed to decrement existing key by amount");
                pass("testDecrByAmountExistingKey passed");
            } catch (error) {
                fail("testDecrByAmountExistingKey failed:", error);
            }
        }
    },
    {
        name: "testIncrByNegativeAmount",
        description: "Test incrementing a key by a negative amount and verify it returns the correct value",
        test: async function() {
            try {
                await puter.kv.set('incrKey', 1);
                const result = await puter.kv.incr('incrKey', -5);
                assert(result === -4, "Failed to increment key by negative amount");
                pass("testIncrByNegativeAmount passed");
            } catch (error) {
                fail("testIncrByNegativeAmount failed:", error);
            }
        }
    },
    {
        name: "testDecrByNegativeAmount",
        description: "Test decrementing a key by a negative amount and verify it returns the correct value",
        test: async function() {
            try {
                await puter.kv.set('decrKey', 10);
                const result = await puter.kv.decr('decrKey', -5);
                assert(result === 15, "Failed to decrement key by negative amount");
                pass("testDecrByNegativeAmount passed");
            } catch (error) {
                fail("testDecrByNegativeAmount failed:", error);
            }
        }
    },
    {
        name: "testListKeys",
        description: "Test listing all keys and verify the count is correct",
        test: async function() {
            try {
                const keys = [];
                // flush first
                await puter.kv.flush();
                // create 10 keys
                for(let i = 0; i < 10; i++){
                    keys.push('key' + i);
                }
                // set all keys
                await Promise.all(keys.map(key => puter.kv.set(key, 'value')));
                // list keys
                const result = await puter.kv.list();
                assert(result.length === 10, "Failed to list keys");
                pass("testListKeys passed");
            } catch (error) {
                fail("testListKeys failed:", error);
            }
        }
    },
    {
        name: "testListKeysGlob",
        description: "Test listing keys using a glob pattern and verify the count is correct",
        test: async function() {
            try {
                const keys = [];
                // flush first
                await puter.kv.flush();
                // create 10 keys
                for(let i = 0; i < 10; i++){
                    keys.push('key' + i);
                }
                // set all keys
                await Promise.all(keys.map(key => puter.kv.set(key, 'value')));
                // list keys
                const result = await puter.kv.list('k*');
                assert(result.length === 10, "Failed to list keys using glob");
                pass("testListKeysGlob passed");
            } catch (error) {
                fail("testListKeysGlob failed:", error);
            }
        }
    },
    {
        name: "testGetPerformance",
        description: "Test that get method takes less than 100ms",
        test: async function() {
            try {
                // Set up a key-value pair first
                await puter.kv.set('performanceTestKey', 'testValue');
                
                // Measure the time it takes to get the value
                const startTime = performance.now();
                const value = await puter.kv.get('performanceTestKey');
                const endTime = performance.now();
                
                const duration = endTime - startTime;
                
                // Assert that the value is correct and timing is under 100ms
                assert(value === 'testValue', "Failed to retrieve correct value");
                assert(duration < 100, `Get method took ${duration}ms, which exceeds the 100ms limit`);
                
                pass(`testGetPerformance passed: get took ${duration.toFixed(2)}ms`);
            } catch (error) {
                fail("testGetPerformance failed:", error);
            }
        }
    },
    {
        name: "testSetPerformance",
        description: "Test that set method takes less than 100ms",
        test: async function() {
            try {
                // Set up a key-value pair first
                const startTime = performance.now();
                await puter.kv.set('performanceTestKey', 'testValue');
                const endTime = performance.now();
                const duration = endTime - startTime;
                assert(duration < 100, `Set method took ${duration}ms, which exceeds the 100ms limit`);
                pass(`testSetPerformance passed: set took ${duration.toFixed(2)}ms`);
            } catch (error) {
                fail("testSetPerformance failed:", error);
            }
        }
    }
]