beamdb 0.17.0

BEAM — distributed graph database syncing over WebSocket, WebRTC, and multicast. Successor to rod.
Documentation
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
//! End-to-end tests for the `beam migrate` tool.
//!
//! # What this proves
//!
//! The migration tool successfully translates data between all supported
//! storage backends (redb, persy, fjall), preserving the underlying
//! `Children` graph structure byte-for-byte.
//!
//! ## Test coverage by feature combination
//!
//! | Features enabled       | Tests that run                          |
//! |------------------------|-----------------------------------------|
//! | `persy`                | redb↔persy (6 tests)                    |
//! | `fjall`                | redb↔fjall (5 tests)                     |
//! | `persy,fjall`          | all above + fjall↔persy (2 tests)        |
//!
//! # Why `--test-threads=1`
//!
//! Persy 1.x holds an exclusive OS lock on the data file for its lifetime.
//! Parallel test execution would cause `AlreadyInUse` failures when two tests
//! try to open the same file. Serial execution also makes `force` flag
//! testing deterministic.
//!
//! # Feature gate
//!
//! ```bash
//! cargo test -p beam --features persy --test migration_e2e -- --test-threads=1
//! cargo test -p beam --features fjall --test migration_e2e -- --test-threads=1
//! cargo test -p beam --features fjall,persy --test migration_e2e -- --test-threads=1
//! ```

#![cfg(any(feature = "persy", feature = "fjall"))]

use arena_btreemap::BTreeMap;
use beam::migration::{Backend, MigrateOpts, migrate};
use beam::types::{NodeData, Value};
use std::env;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

/// Atomic counter to ensure unique temp file paths across parallel test runs.
static TEST_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Generate a unique temp file path with the given extension.
fn temp_path(name: &str, ext: &str) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    env::temp_dir().join(format!(
        "beam-migrate-{}-{}-{}-{}{}",
        name,
        std::process::id(),
        nanos,
        counter,
        ext
    ))
}

/// Helper: write `count` records to a redb file at `path`. Each record is
/// a separate row in the `beam_nodes_v1` table with key `node_NNNN` and value
/// being a single-child `Children` map (so we test N records, not 1 record
/// with N children).
fn write_redb_records(path: &std::path::Path, count: usize) -> Result<usize, String> {
    use redb::{Database, TableDefinition};
    const BEAM_NODES: TableDefinition<&str, &[u8]> = TableDefinition::new("beam_nodes_v1");

    let db = Database::create(path).map_err(|e| format!("create: {:?}", e))?;
    let txn = db
        .begin_write()
        .map_err(|e| format!("begin_write: {:?}", e))?;
    {
        let mut table = txn
            .open_table(BEAM_NODES)
            .map_err(|e| format!("open_table: {:?}", e))?;

        for i in 0..count {
            let mut children: BTreeMap<String, NodeData> = BTreeMap::default();
            children.insert(
                format!("leaf_{:04}", i),
                NodeData {
                    value: Value::Text(format!("test-{}", i)),
                    updated_at: 1_700_000_000.0 + i as f64,
                },
            );
            let bytes =
                postcard::to_allocvec(&children).map_err(|e| format!("postcard: {:?}", e))?;
            let key = format!("node_{:04}", i);
            table
                .insert(key.as_str(), bytes.as_slice())
                .map_err(|e| format!("insert {}: {:?}", key, e))?;
        }
    }
    txn.commit().map_err(|e| format!("commit: {:?}", e))?;
    drop(db);
    Ok(count)
}

/// Helper: read all records from a Persy file at `path` and count them.
#[cfg(feature = "persy")]
fn count_persy_records(path: &std::path::Path) -> Result<usize, String> {
    use persy::Persy;
    let db = Persy::open(path, persy::Config::default()).map_err(|e| format!("open: {:?}", e))?;
    let segment_id = db
        .solve_segment_id("beam_nodes_v1")
        .map_err(|e| format!("solve_segment_id: {:?}", e))?;
    // CRITICAL: db.scan() returns Result<SegmentIter, PE<SegmentError>>.
    // If we naively `for entry in db.scan(&segment_id)`, Rust iterates over
    // BOTH Ok and Err variants — a single Err yields count=1 (the Err itself),
    // not "scan failed". We must unwrap the Result first.
    let iter = db.scan(segment_id).map_err(|e| format!("scan: {:?}", e))?;
    Ok(iter.count())
}

/// Helper: read all records from a redb file at `path` and count them.
fn count_redb_records(path: &std::path::Path) -> Result<usize, String> {
    use redb::{Database, ReadableDatabase, ReadableTable, TableDefinition};
    const BEAM_NODES: TableDefinition<&str, &[u8]> = TableDefinition::new("beam_nodes_v1");

    let db = Database::open(path).map_err(|e| format!("open: {:?}", e))?;
    let txn = db
        .begin_read()
        .map_err(|e| format!("begin_read: {:?}", e))?;
    let table = match txn.open_table(BEAM_NODES) {
        Ok(t) => t,
        Err(_) => return Ok(0), // empty database
    };
    let count = table.iter().map_err(|e| format!("iter: {:?}", e))?.count();
    drop(table);
    drop(txn);
    drop(db);
    Ok(count)
}

/// Helper: delete a file, ignoring "not found" errors.
fn cleanup(path: &PathBuf) {
    let _ = std::fs::remove_file(path);
}

/// Test 1: Basic redb → persy migration preserves record count.
#[cfg(feature = "persy")]
#[tokio::test]
async fn e2e_redb_to_persy_basic() {
    let source = temp_path("redb-source", ".redb");
    let target = temp_path("persy-target", ".persy");

    cleanup(&target);

    // Write 100 records to redb source
    let written = write_redb_records(&source, 100).expect("write redb");
    assert_eq!(written, 100);

    // Migrate
    let opts = MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 50,
        force: false,
        dry_run: false,
    };
    let report = migrate(&opts).expect("migrate redb→persy");
    assert_eq!(report.records_migrated, 100);
    assert_eq!(report.source_count, 100);

    // `migrate()` commits all batches before returning; `drop(opts)` releases
    // the migration's Persy handles so we can open a fresh reader. No sleep
    // needed — the sibling test `diag_persy_count_basic` does the same
    // migration and counts immediately without sleeping, proving the
    // commit is synchronous.
    drop(opts);

    let target_count = count_persy_records(&target).expect("count persy");
    assert_eq!(target_count, 100, "Persy target should have 100 records");

    cleanup(&source);
    cleanup(&target);
}

/// Test 2: Reverse direction (persy → redb) for rollback paths.
#[cfg(feature = "persy")]
#[tokio::test]
async fn e2e_persy_to_redb_basic() {
    let source = temp_path("persy-source", ".persy");
    let target = temp_path("redb-target", ".redb");

    cleanup(&target);

    // Write 50 records to redb, then migrate to persy to create source
    write_redb_records(&source, 50).expect("write initial redb");
    let intermediate = temp_path("persy-intermediate", ".persy");
    cleanup(&intermediate);
    let _ = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: intermediate.clone(),
        batch_size: 25,
        force: true,
        dry_run: false,
    })
    .expect("redb→persy intermediate");

    // Now migrate back from persy → redb
    let report = migrate(&MigrateOpts {
        from: Backend::Persy,
        to: Backend::Redb,
        source_path: intermediate.clone(),
        target_path: target.clone(),
        batch_size: 25,
        force: false,
        dry_run: false,
    })
    .expect("persy→redb");
    assert_eq!(report.records_migrated, 50);

    let target_count = count_redb_records(&target).expect("count redb target");
    assert_eq!(target_count, 50);

    cleanup(&source);
    cleanup(&intermediate);
    cleanup(&target);
}

/// Test 3: Migration preserves nested children structure (3-level deep graph).
#[cfg(feature = "persy")]
#[tokio::test]
async fn e2e_migration_preserves_children() {
    let source = temp_path("redb-nested-source", ".redb");
    let target = temp_path("persy-nested-target", ".persy");
    cleanup(&target);

    // Write a 3-level nested graph:
    // root → { level1_a → { level2_a → { leaf1, leaf2 } } }
    use redb::{Database, TableDefinition};
    const BEAM_NODES: TableDefinition<&str, &[u8]> = TableDefinition::new("beam_nodes_v1");

    let db = Database::create(&source).expect("create");
    let txn = db.begin_write().expect("begin_write");
    {
        let mut table = txn.open_table(BEAM_NODES).expect("open_table");

        // root node with child "level1_a"
        let mut root_children: BTreeMap<String, NodeData> = BTreeMap::default();
        root_children.insert(
            "level1_a".to_string(),
            NodeData {
                value: Value::Null,
                updated_at: 1_700_000_000.0,
            },
        );
        let bytes = postcard::to_allocvec(&root_children).expect("postcard root");
        table.insert("root", bytes.as_slice()).expect("insert root");

        // level1_a node with child "level2_a"
        let mut l1_children: BTreeMap<String, NodeData> = BTreeMap::default();
        l1_children.insert(
            "level2_a".to_string(),
            NodeData {
                value: Value::Null,
                updated_at: 1_700_000_001.0,
            },
        );
        let bytes = postcard::to_allocvec(&l1_children).expect("postcard l1");
        table
            .insert("level1_a", bytes.as_slice())
            .expect("insert l1");

        // level2_a node with 2 leaf children
        let mut l2_children: BTreeMap<String, NodeData> = BTreeMap::default();
        l2_children.insert(
            "leaf1".to_string(),
            NodeData {
                value: Value::Text("deep value 1".into()),
                updated_at: 1_700_000_002.0,
            },
        );
        l2_children.insert(
            "leaf2".to_string(),
            NodeData {
                value: Value::Text("deep value 2".into()),
                updated_at: 1_700_000_003.0,
            },
        );
        let bytes = postcard::to_allocvec(&l2_children).expect("postcard l2");
        table
            .insert("level2_a", bytes.as_slice())
            .expect("insert l2");
    }
    txn.commit().expect("commit");
    drop(db);

    // Migrate
    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 100,
        force: false,
        dry_run: false,
    })
    .expect("migrate nested");
    assert_eq!(report.records_migrated, 3, "3 top-level records");

    // Verify target has 3 records (root, level1_a, level2_a)
    let target_count = count_persy_records(&target).expect("count");
    assert_eq!(target_count, 3);

    cleanup(&source);
    cleanup(&target);
}

/// Test 4: Empty source dataset is a successful no-op.
#[cfg(feature = "persy")]
#[tokio::test]
async fn e2e_migration_empty_dataset() {
    let source = temp_path("redb-empty-source", ".redb");
    let target = temp_path("persy-empty-target", ".persy");
    cleanup(&target);

    // Create empty redb database (no records)
    use redb::Database;
    let db = Database::create(&source).expect("create empty");
    drop(db);

    // Migrate
    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 100,
        force: false,
        dry_run: false,
    })
    .expect("migrate empty");
    assert_eq!(report.records_migrated, 0);
    assert_eq!(report.source_count, 0);

    cleanup(&source);
    cleanup(&target);
}

/// Test 5: Dry run doesn't write target file.
#[cfg(feature = "persy")]
#[tokio::test]
async fn e2e_migration_dry_run_no_write() {
    let source = temp_path("redb-dry-source", ".redb");
    let target = temp_path("persy-dry-target", ".persy");
    cleanup(&target);

    write_redb_records(&source, 10).expect("write");

    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 100,
        force: false,
        dry_run: true,
    })
    .expect("dry run migrate");
    assert_eq!(report.records_migrated, 10);
    assert!(report.dry_run);

    // Target should NOT exist after dry run
    assert!(!target.exists(), "dry run should not create target file");

    cleanup(&source);
}

/// Diagnostic: write 100 records to Persy via migration, count via fresh open.
#[cfg(feature = "persy")]
#[tokio::test]
async fn diag_persy_count_basic() {
    use redb::{Database, TableDefinition};

    const BEAM_NODES: TableDefinition<&str, &[u8]> = TableDefinition::new("beam_nodes_v1");

    let source = temp_path("diag-source", ".redb");
    let target = temp_path("diag-target", ".persy");
    cleanup(&source);
    cleanup(&target);

    // Write 100 records to redb
    let db = Database::create(&source).unwrap();
    let txn = db.begin_write().unwrap();
    {
        let mut table = txn.open_table(BEAM_NODES).unwrap();
        for i in 0..100 {
            let mut children: BTreeMap<String, NodeData> = BTreeMap::default();
            children.insert(
                format!("leaf_{:04}", i),
                NodeData {
                    value: Value::Text(format!("test-{}", i)),
                    updated_at: 1_700_000_000.0 + i as f64,
                },
            );
            let bytes = postcard::to_allocvec(&children).unwrap();
            let key = format!("node_{:04}", i);
            table.insert(key.as_str(), bytes.as_slice()).unwrap();
        }
    }
    txn.commit().unwrap();
    drop(db);

    // Migrate
    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 50,
        force: false,
        dry_run: false,
    })
    .expect("migrate");
    eprintln!(
        "DIAG: migration reports records_migrated={}",
        report.records_migrated
    );
    eprintln!("DIAG: source_count={}", report.source_count);

    // Open target as separate Persy instance to verify persistence.
    // Without the `target_db.commit()` checkpoint in `flush_batch`, the
    // second batch's commit would report success but not reach the file,
    // so this count would return 50 instead of 100.
    let target_count = count_persy_records(&target).expect("count persy");
    assert_eq!(
        target_count, 100,
        "all 100 records must persist across batches (post-fix checkpoint)"
    );
    assert_eq!(
        report.records_migrated, 100,
        "migration report must match actual persisted count"
    );

    cleanup(&source);
    cleanup(&target);
}

// ============================================================================
// Fjall helpers and E2E tests
// ============================================================================

/// Helper: write `count` records to a fjall database directory.
///
/// Each record is a separate key in the `beam_nodes_v1` keyspace with
/// key `node_NNNN` and value being a single-child `Children` map.
#[cfg(feature = "fjall")]
fn write_fjall_records(path: &std::path::Path, count: usize) -> Result<usize, String> {
    use beam::migration::redb_to_fjall_key;

    let db = fjall::Database::builder(path)
        .open()
        .map_err(|e| format!("create: {:?}", e))?;
    let keyspace = db
        .keyspace("beam_nodes_v1", fjall::KeyspaceCreateOptions::default)
        .map_err(|e| format!("keyspace: {:?}", e))?;

    for i in 0..count {
        let mut children: BTreeMap<String, NodeData> = BTreeMap::default();
        children.insert(
            format!("leaf_{:04}", i),
            NodeData {
                value: Value::Text(format!("test-{}", i)),
                updated_at: 1_700_000_000.0 + i as f64,
            },
        );
        let bytes = postcard::to_allocvec(&children).map_err(|e| format!("postcard: {:?}", e))?;
        let key = redb_to_fjall_key(&format!("node_{:04}", i));
        keyspace
            .insert(key, bytes.as_slice())
            .map_err(|e| format!("insert: {:?}", e))?;
    }
    db.persist(fjall::PersistMode::SyncAll)
        .map_err(|e| format!("persist: {:?}", e))?;
    drop(db);
    Ok(count)
}

/// Helper: count records in a fjall database directory.
#[cfg(feature = "fjall")]
fn count_fjall_records(path: &std::path::Path) -> Result<usize, String> {
    let db = fjall::Database::builder(path)
        .open()
        .map_err(|e| format!("open: {:?}", e))?;
    let keyspace = db
        .keyspace("beam_nodes_v1", fjall::KeyspaceCreateOptions::default)
        .map_err(|e| format!("keyspace: {:?}", e))?;
    Ok(keyspace.iter().count())
}

/// Helper: remove a file or directory, ignoring "not found" errors.
#[cfg(feature = "fjall")]
fn cleanup_dir(path: &std::path::Path) {
    if path.is_dir() {
        let _ = std::fs::remove_dir_all(path);
    } else {
        let _ = std::fs::remove_file(path);
    }
}

/// Test: redb → fjall migration preserves record count.
#[cfg(feature = "fjall")]
#[tokio::test]
async fn e2e_redb_to_fjall_basic() {
    let source = temp_path("redb-src-fjall", ".redb");
    let target = temp_path("fjall-tgt", ".fjall");
    cleanup_dir(&target);

    let written = write_redb_records(&source, 100).expect("write redb");
    assert_eq!(written, 100);

    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Fjall,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 50,
        force: false,
        dry_run: false,
    })
    .expect("migrate redb→fjall");
    assert_eq!(report.records_migrated, 100);
    assert_eq!(report.source_count, 100);

    let target_count = count_fjall_records(&target).expect("count fjall");
    assert_eq!(target_count, 100, "fjall target should have 100 records");

    cleanup(&source);
    cleanup_dir(&target);
}

/// Test: fjall → redb migration preserves record count.
#[cfg(feature = "fjall")]
#[tokio::test]
async fn e2e_fjall_to_redb_basic() {
    let source = temp_path("fjall-src", ".fjall");
    let target = temp_path("redb-tgt-fjall", ".redb");
    cleanup(&target);

    let written = write_fjall_records(&source, 50).expect("write fjall");
    assert_eq!(written, 50);

    let report = migrate(&MigrateOpts {
        from: Backend::Fjall,
        to: Backend::Redb,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 25,
        force: false,
        dry_run: false,
    })
    .expect("migrate fjall→redb");
    assert_eq!(report.records_migrated, 50);

    let target_count = count_redb_records(&target).expect("count redb");
    assert_eq!(target_count, 50);

    cleanup_dir(&source);
    cleanup(&target);
}

/// Test: redb → fjall migration preserves nested children structure.
#[cfg(feature = "fjall")]
#[tokio::test]
async fn e2e_redb_to_fjall_preserves_children() {
    let source = temp_path("redb-nested-fjall", ".redb");
    let target = temp_path("fjall-nested", ".fjall");
    cleanup_dir(&target);

    // Write a 3-level nested graph using redb directly
    use redb::{Database, TableDefinition};
    const BEAM_NODES: TableDefinition<&str, &[u8]> = TableDefinition::new("beam_nodes_v1");

    let db = Database::create(&source).expect("create");
    let txn = db.begin_write().expect("begin_write");
    {
        let mut table = txn.open_table(BEAM_NODES).expect("open_table");

        // root → { level1_a → { level2_a → { leaf1, leaf2 } } }
        let mut root_children: BTreeMap<String, NodeData> = BTreeMap::default();
        root_children.insert(
            "level1_a".to_string(),
            NodeData {
                value: Value::Null,
                updated_at: 1_700_000_000.0,
            },
        );
        table
            .insert(
                "root",
                postcard::to_allocvec(&root_children).unwrap().as_slice(),
            )
            .expect("insert root");

        let mut l1_children: BTreeMap<String, NodeData> = BTreeMap::default();
        l1_children.insert(
            "level2_a".to_string(),
            NodeData {
                value: Value::Null,
                updated_at: 1_700_000_001.0,
            },
        );
        table
            .insert(
                "level1_a",
                postcard::to_allocvec(&l1_children).unwrap().as_slice(),
            )
            .expect("insert l1");

        let mut l2_children: BTreeMap<String, NodeData> = BTreeMap::default();
        l2_children.insert(
            "leaf1".to_string(),
            NodeData {
                value: Value::Text("deep value 1".into()),
                updated_at: 1_700_000_002.0,
            },
        );
        l2_children.insert(
            "leaf2".to_string(),
            NodeData {
                value: Value::Text("deep value 2".into()),
                updated_at: 1_700_000_003.0,
            },
        );
        table
            .insert(
                "level2_a",
                postcard::to_allocvec(&l2_children).unwrap().as_slice(),
            )
            .expect("insert l2");
    }
    txn.commit().expect("commit");
    drop(db);

    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Fjall,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 100,
        force: false,
        dry_run: false,
    })
    .expect("migrate nested");
    assert_eq!(report.records_migrated, 3, "3 top-level records");

    let target_count = count_fjall_records(&target).expect("count");
    assert_eq!(target_count, 3);

    cleanup(&source);
    cleanup_dir(&target);
}

/// Test: empty source dataset is a successful no-op for redb → fjall.
#[cfg(feature = "fjall")]
#[tokio::test]
async fn e2e_redb_to_fjall_empty_dataset() {
    let source = temp_path("redb-empty-fjall", ".redb");
    let target = temp_path("fjall-empty", ".fjall");
    cleanup_dir(&target);

    // Create empty redb database
    use redb::Database;
    let db = Database::create(&source).expect("create empty");
    drop(db);

    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Fjall,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 100,
        force: false,
        dry_run: false,
    })
    .expect("migrate empty");
    assert_eq!(report.records_migrated, 0);
    assert_eq!(report.source_count, 0);

    cleanup(&source);
    cleanup_dir(&target);
}

/// Test: dry run doesn't create target directory for redb → fjall.
#[cfg(feature = "fjall")]
#[tokio::test]
async fn e2e_redb_to_fjall_dry_run() {
    let source = temp_path("redb-dry-fjall", ".redb");
    let target = temp_path("fjall-dry", ".fjall");

    write_redb_records(&source, 10).expect("write");

    let report = migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Fjall,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 100,
        force: false,
        dry_run: true,
    })
    .expect("dry run migrate");
    assert_eq!(report.records_migrated, 10);
    assert!(report.dry_run);

    // Target should NOT exist after dry run
    assert!(
        !target.exists(),
        "dry run should not create target directory"
    );

    cleanup(&source);
    cleanup_dir(&target);
}

/// Test: redb → fjall → redb roundtrip preserves data integrity.
#[cfg(feature = "fjall")]
#[tokio::test]
async fn e2e_fjall_roundtrip() {
    let source = temp_path("rt-redb-src", ".redb");
    let intermediate = temp_path("rt-fjall", ".fjall");
    let target = temp_path("rt-redb-tgt", ".redb");
    cleanup(&target);
    cleanup_dir(&intermediate);

    // Write 50 records to redb
    write_redb_records(&source, 50).expect("write redb");

    // redb → fjall
    migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Fjall,
        source_path: source.clone(),
        target_path: intermediate.clone(),
        batch_size: 25,
        force: false,
        dry_run: false,
    })
    .expect("redb→fjall");

    // fjall → redb
    migrate(&MigrateOpts {
        from: Backend::Fjall,
        to: Backend::Redb,
        source_path: intermediate.clone(),
        target_path: target.clone(),
        batch_size: 25,
        force: false,
        dry_run: false,
    })
    .expect("fjall→redb");

    let target_count = count_redb_records(&target).expect("count");
    assert_eq!(target_count, 50, "roundtrip should preserve all 50 records");

    cleanup(&source);
    cleanup_dir(&intermediate);
    cleanup(&target);
}

/// Test: fjall → persy migration preserves record count.
#[cfg(all(feature = "fjall", feature = "persy"))]
#[tokio::test]
async fn e2e_fjall_to_persy_basic() {
    let source = temp_path("fjall-to-persy-src", ".fjall");
    let target = temp_path("fjall-to-persy-tgt", ".persy");
    cleanup(&target);

    let written = write_fjall_records(&source, 75).expect("write fjall");
    assert_eq!(written, 75);

    let report = migrate(&MigrateOpts {
        from: Backend::Fjall,
        to: Backend::Persy,
        source_path: source.clone(),
        target_path: target.clone(),
        batch_size: 50,
        force: false,
        dry_run: false,
    })
    .expect("migrate fjall→persy");
    assert_eq!(report.records_migrated, 75);

    let target_count = count_persy_records(&target).expect("count persy");
    assert_eq!(target_count, 75);

    cleanup_dir(&source);
    cleanup(&target);
}

/// Test: persy → fjall migration preserves record count.
#[cfg(all(feature = "fjall", feature = "persy"))]
#[tokio::test]
async fn e2e_persy_to_fjall_basic() {
    let redb_source = temp_path("persy-to-fjall-redb", ".redb");
    let persy_source = temp_path("persy-to-fjall-src", ".persy");
    let target = temp_path("persy-to-fjall-tgt", ".fjall");
    cleanup_dir(&target);
    cleanup(&persy_source);

    // Create persy source via redb → persy migration
    write_redb_records(&redb_source, 60).expect("write redb");
    migrate(&MigrateOpts {
        from: Backend::Redb,
        to: Backend::Persy,
        source_path: redb_source.clone(),
        target_path: persy_source.clone(),
        batch_size: 30,
        force: true,
        dry_run: false,
    })
    .expect("redb→persy");
    cleanup(&redb_source);

    // persy → fjall
    let report = migrate(&MigrateOpts {
        from: Backend::Persy,
        to: Backend::Fjall,
        source_path: persy_source.clone(),
        target_path: target.clone(),
        batch_size: 30,
        force: false,
        dry_run: false,
    })
    .expect("migrate persy→fjall");
    assert_eq!(report.records_migrated, 60);

    let target_count = count_fjall_records(&target).expect("count fjall");
    assert_eq!(target_count, 60);

    cleanup(&persy_source);
    cleanup_dir(&target);
}