//! Each IoBackend variant must persist and read back identically.
use armdb::{Config, ConstTree, IoBackend};
use tempfile::TempDir;

fn roundtrip(io_backend: IoBackend) {
    let dir = TempDir::new().unwrap();
    let config = Config {
        shard_count: 2,
        io_backend,
        ..Config::balanced().build()
    };
    let tree = ConstTree::<[u8; 8], 16>::open(dir.path(), config).unwrap();
    for i in 0u64..1000 {
        tree.insert(&i.to_be_bytes(), &[i as u8; 16]).unwrap();
    }
    tree.flush().unwrap();
    for i in 0u64..1000 {
        let v = tree.get(&i.to_be_bytes());
        assert_eq!(v, Some([i as u8; 16]), "key {i}");
    }
}

// These two genuinely exercise the io_uring path, so they run only where it is
// actually compiled in (Linux + `io-uring` feature).
#[cfg(all(target_os = "linux", feature = "io-uring"))]
#[test]
fn sqpoll_2000_roundtrip() {
    roundtrip(IoBackend::Uring {
        sqpoll_idle_ms: Some(2000),
    });
}
#[cfg(all(target_os = "linux", feature = "io-uring"))]
#[test]
fn no_sqpoll_roundtrip() {
    roundtrip(IoBackend::Uring {
        sqpoll_idle_ms: None,
    });
}
#[test]
fn pwrite_roundtrip() {
    roundtrip(IoBackend::Pwrite);
}

/// When io_uring is unavailable (feature off, or non-Linux), requesting `Uring`
/// must transparently fall back to `Pwrite` and still round-trip data correctly.
#[cfg(not(all(target_os = "linux", feature = "io-uring")))]
#[test]
fn uring_request_falls_back_to_pwrite() {
    roundtrip(IoBackend::Uring {
        sqpoll_idle_ms: Some(2000),
    });
}
