@@ -446,3 +446,43 @@ func TestCloseNodeDB(t *testing.T) {
446
446
require .NoError (t , ndb .Close ())
447
447
require .NoError (t , ndb .Close ()) // must not block or fail on second call
448
448
}
449
+
450
+ func TestGetFirstNonLegacyVersion (t * testing.T ) {
451
+ db := dbm .NewMemDB ()
452
+ ndb := newNodeDB (db , 0 , DefaultOptions (), NewNopLogger ())
453
+
454
+ // Test case 1: Empty database
455
+ firstVersion , err := ndb .getFirstNonLegacyVersion ()
456
+ require .NoError (t , err )
457
+ require .Equal (t , int64 (0 ), firstVersion )
458
+
459
+ // Test case 2: Database with only legacy versions
460
+ // Create a legacy version at version 1
461
+ legacyRoot := GetRootKey (1 )
462
+ require .NoError (t , ndb .batch .Set (ndb .legacyRootKey (1 ), legacyRoot ))
463
+ require .NoError (t , ndb .batch .Write ())
464
+
465
+ firstVersion , err = ndb .getFirstNonLegacyVersion ()
466
+ require .NoError (t , err )
467
+ require .Equal (t , int64 (0 ), firstVersion )
468
+
469
+ // Test case 3: Database with both legacy and non-legacy versions
470
+ // Create a non-legacy version at version 2
471
+ nonLegacyRoot := GetRootKey (2 )
472
+ require .NoError (t , ndb .batch .Set (ndb .nodeKey (nonLegacyRoot ), []byte {}))
473
+ require .NoError (t , ndb .batch .Write ())
474
+
475
+ firstVersion , err = ndb .getFirstNonLegacyVersion ()
476
+ require .NoError (t , err )
477
+ require .Equal (t , int64 (2 ), firstVersion )
478
+
479
+ // Test case 4: Database with multiple non-legacy versions
480
+ // Create another non-legacy version at version 3
481
+ nonLegacyRoot3 := GetRootKey (3 )
482
+ require .NoError (t , ndb .batch .Set (ndb .nodeKey (nonLegacyRoot3 ), []byte {}))
483
+ require .NoError (t , ndb .batch .Write ())
484
+
485
+ firstVersion , err = ndb .getFirstNonLegacyVersion ()
486
+ require .NoError (t , err )
487
+ require .Equal (t , int64 (2 ), firstVersion ) // Should still return the first non-legacy version
488
+ }
0 commit comments