@@ -212,3 +212,98 @@ test("KVKeyInstance: parse error on invalid range format", () => {
212
212
KVKeyInstance . parse ( "users.>=abc.<=123" , false ) ;
213
213
} , TypeError ) ;
214
214
} ) ;
215
+
216
+ test ( "KVKeyInstance: Throws if trying to use something other than an array for a key" , ( ) => {
217
+ assertThrows (
218
+ ( ) => new KVKeyInstance ( "users.data.user123" as unknown as KVKey ) ,
219
+ TypeError ,
220
+ "Key must be an array" ,
221
+ ) ;
222
+ } ) ;
223
+
224
+ test ( "KVKeyInstance: Throws if trying to use something other than a string or number for a key element" , ( ) => {
225
+ assertThrows (
226
+ // @ts -ignore expected to be wrong
227
+ ( ) => new KVKeyInstance ( [ "users" , null ] ) ,
228
+ TypeError ,
229
+ "Key ranges are only allowed in queries" ,
230
+ ) ;
231
+ } ) ;
232
+
233
+ test ( "KVKeyInstance: Throws if trying to use an object with extra properties for a key element" , ( ) => {
234
+ assertThrows (
235
+ // @ts -ignore extected to be wrong
236
+ ( ) => new KVKeyInstance ( [ "users" , { from : 100 , extra : "value" } ] , true ) ,
237
+ TypeError ,
238
+ "Ranges must have only" ,
239
+ ) ;
240
+ } ) ;
241
+
242
+ test ( "KVKeyInstance: Throws if trying to mix strings and numbers in a range for a key element" , ( ) => {
243
+ assertThrows (
244
+ ( ) => new KVKeyInstance ( [ "users" , { from : "abc" , to : 123 } ] , true ) ,
245
+ TypeError ,
246
+ "Cannot mix string and number in ranges" ,
247
+ ) ;
248
+ } ) ;
249
+
250
+ test ( "KVKeyInstance: Throws if trying to use an object with invalid range values for a key element" , ( ) => {
251
+ assertThrows (
252
+ ( ) => new KVKeyInstance ( [ "users" , { from : 123 , to : "abc" } ] , true , true ) ,
253
+ TypeError ,
254
+ "Cannot mix string and number in ranges" ,
255
+ ) ;
256
+ } ) ;
257
+
258
+ test ( "KVKeyInstance: throws when parsing a key with empty elements" , ( ) => {
259
+ assertThrows (
260
+ ( ) => KVKeyInstance . parse ( "users..profile" , false ) ,
261
+ TypeError ,
262
+ "Key ranges are only allowed in queries" ,
263
+ ) ;
264
+ } ) ;
265
+
266
+ test ( "KVKeyInstance: stringify and parse with empty elements" , ( ) => {
267
+ // Empty object represents an empty element
268
+ const queryWithEmpty : KVQuery = [ "users" , { } , "profile" ] ;
269
+ const queryWithRangeAndEmpty : KVQuery = [ "data" , { from : 10 , to : 20 } , { } ] ;
270
+
271
+ const parsedKey = KVKeyInstance . parse ( "users..profile" , true ) ;
272
+ assertEquals ( parsedKey , queryWithEmpty ) ;
273
+
274
+ const queryInstance = new KVKeyInstance ( queryWithRangeAndEmpty , true ) ;
275
+ assertEquals ( queryInstance . stringify ( ) , "data.>=#10<=#20." ) ;
276
+
277
+ const parsedQuery = KVKeyInstance . parse ( "data.>=#10<=#20." , true ) ;
278
+ assertEquals ( parsedQuery , queryWithRangeAndEmpty ) ;
279
+ } ) ;
280
+
281
+ test ( "KVKeyInstance: parse with leading dots should throw" , ( ) => {
282
+ assertThrows (
283
+ ( ) => KVKeyInstance . parse ( `.data.>=a<=z` , false ) ,
284
+ TypeError ,
285
+ "Ranges are not allowed in keys." ,
286
+ ) ;
287
+ } ) ;
288
+
289
+ test ( "KVKeyInstance: stringify and parse mixed-type keys" , ( ) => {
290
+ const mixedKey : KVKey = [ "users" , 123 , "profile" ] ;
291
+ const instance = new KVKeyInstance ( mixedKey ) ;
292
+
293
+ const stringified = instance . stringify ( ) ;
294
+ assertEquals ( stringified , "users.#123.profile" ) ;
295
+
296
+ const parsed = KVKeyInstance . parse ( stringified , false ) ;
297
+ assertEquals ( parsed , mixedKey ) ;
298
+ } ) ;
299
+
300
+ test ( "KVKeyInstance: toUint8Array and fromUint8Array roundtrip" , ( ) => {
301
+ const key : KVKey = [ "users" , "john_doe" , 42 ] ;
302
+ const instance = new KVKeyInstance ( key ) ;
303
+ const uint8Array = instance . toUint8Array ( ) ;
304
+
305
+ const dataView = new DataView ( uint8Array . buffer ) ;
306
+ const newKeyInstance = new KVKeyInstance ( dataView ) ;
307
+
308
+ assertEquals ( newKeyInstance . get ( ) , key ) ;
309
+ } ) ;
0 commit comments