@@ -242,4 +242,145 @@ describe('entries', () => {
242242 assert . equal ( results [ i ] [ 0 ] , key )
243243 }
244244 } )
245+
246+ it ( 'lists entries in ascending order (explicit)' , async ( ) => {
247+ const empty = await ShardBlock . create ( )
248+ const blocks = new Blockstore ( )
249+ await blocks . put ( empty . cid , empty . bytes )
250+
251+ /** @type {Array<[string, API.UnknownLink]> } */
252+ const testdata = [
253+ [ 'c' , await randomCID ( 32 ) ] ,
254+ [ 'd' , await randomCID ( 32 ) ] ,
255+ [ 'a' , await randomCID ( 32 ) ] ,
256+ [ 'b' , await randomCID ( 32 ) ]
257+ ]
258+
259+ /** @type {API.ShardLink } */
260+ let root = empty . cid
261+ for ( const [ k , v ] of testdata ) {
262+ const res = await put ( blocks , root , k , v )
263+ for ( const b of res . additions ) {
264+ await blocks . put ( b . cid , b . bytes )
265+ }
266+ root = res . root
267+ }
268+
269+ const results = [ ]
270+ for await ( const entry of entries ( blocks , root , { order : 'asc' } ) ) {
271+ results . push ( entry )
272+ }
273+
274+ const sortedKeys = testdata . map ( d => d [ 0 ] ) . sort ( )
275+ for ( const [ i , key ] of sortedKeys . entries ( ) ) {
276+ assert . equal ( results [ i ] [ 0 ] , key )
277+ }
278+ } )
279+
280+ it ( 'lists entries in descending order' , async ( ) => {
281+ const empty = await ShardBlock . create ( )
282+ const blocks = new Blockstore ( )
283+ await blocks . put ( empty . cid , empty . bytes )
284+
285+ /** @type {Array<[string, API.UnknownLink]> } */
286+ const testdata = [
287+ [ 'c' , await randomCID ( 32 ) ] ,
288+ [ 'd' , await randomCID ( 32 ) ] ,
289+ [ 'a' , await randomCID ( 32 ) ] ,
290+ [ 'b' , await randomCID ( 32 ) ]
291+ ]
292+
293+ /** @type {API.ShardLink } */
294+ let root = empty . cid
295+ for ( const [ k , v ] of testdata ) {
296+ const res = await put ( blocks , root , k , v )
297+ for ( const b of res . additions ) {
298+ await blocks . put ( b . cid , b . bytes )
299+ }
300+ root = res . root
301+ }
302+
303+ const results = [ ]
304+ for await ( const entry of entries ( blocks , root , { order : 'desc' } ) ) {
305+ results . push ( entry )
306+ }
307+
308+ const sortedKeys = testdata . map ( d => d [ 0 ] ) . sort ( ) . reverse ( )
309+ for ( const [ i , key ] of sortedKeys . entries ( ) ) {
310+ assert . equal ( results [ i ] [ 0 ] , key )
311+ }
312+ } )
313+
314+ it ( 'lists entries by prefix in descending order' , async ( ) => {
315+ const empty = await ShardBlock . create ( )
316+ const blocks = new Blockstore ( )
317+ await blocks . put ( empty . cid , empty . bytes )
318+
319+ /** @type {Array<[string, API.UnknownLink]> } */
320+ const testdata = [
321+ [ 'dccc' , await randomCID ( 32 ) ] ,
322+ [ 'deee' , await randomCID ( 32 ) ] ,
323+ [ 'dooo' , await randomCID ( 32 ) ] ,
324+ [ 'daaa' , await randomCID ( 32 ) ] ,
325+ [ 'beee' , await randomCID ( 32 ) ]
326+ ]
327+
328+ /** @type {API.ShardLink } */
329+ let root = empty . cid
330+ for ( const [ k , v ] of testdata ) {
331+ const res = await put ( blocks , root , k , v )
332+ for ( const b of res . additions ) {
333+ await blocks . put ( b . cid , b . bytes )
334+ }
335+ root = res . root
336+ }
337+
338+ const prefix = 'd'
339+ const results = [ ]
340+ for await ( const entry of entries ( blocks , root , { prefix, order : 'desc' } ) ) {
341+ results . push ( entry )
342+ }
343+
344+ const filteredAndSorted = testdata . map ( d => d [ 0 ] ) . filter ( k => k . startsWith ( prefix ) ) . sort ( ) . reverse ( )
345+ for ( const [ i , key ] of filteredAndSorted . entries ( ) ) {
346+ assert . equal ( results [ i ] [ 0 ] , key )
347+ }
348+ } )
349+
350+ it ( 'lists entries by range in descending order' , async ( ) => {
351+ const empty = await ShardBlock . create ( )
352+ const blocks = new Blockstore ( )
353+ await blocks . put ( empty . cid , empty . bytes )
354+
355+ /** @type {Array<[string, API.UnknownLink]> } */
356+ const testdata = [
357+ [ 'aaaa' , await randomCID ( 32 ) ] ,
358+ [ 'cccc' , await randomCID ( 32 ) ] ,
359+ [ 'deee' , await randomCID ( 32 ) ] ,
360+ [ 'dooo' , await randomCID ( 32 ) ] ,
361+ [ 'eeee' , await randomCID ( 32 ) ]
362+ ]
363+
364+ /** @type {API.ShardLink } */
365+ let root = empty . cid
366+ for ( const [ k , v ] of testdata ) {
367+ const res = await put ( blocks , root , k , v )
368+ for ( const b of res . additions ) {
369+ await blocks . put ( b . cid , b . bytes )
370+ }
371+ root = res . root
372+ }
373+
374+ const gte = 'c'
375+ const lt = 'e'
376+ const results = [ ]
377+ for await ( const entry of entries ( blocks , root , { gte, lt, order : 'desc' } ) ) {
378+ results . push ( entry )
379+ }
380+
381+ const filteredAndSorted = testdata . map ( d => d [ 0 ] ) . filter ( k => k >= gte && k < lt ) . sort ( ) . reverse ( )
382+ for ( const [ i , key ] of filteredAndSorted . entries ( ) ) {
383+ assert . equal ( results [ i ] [ 0 ] , key )
384+ }
385+ } )
245386} )
0 commit comments