wal-backup 0.8.11

Embedded SQLite WAL replication to S3-compatible object storage. Not a sidecar — a library. Like Litestream, but embedded.
Documentation
use wal_backup::error::Error;
use wal_backup::restore::read_page_size_from_db;
use wal_backup::util::compute_sha256;
use wal_backup::util::compute_sha256_file;

#[test]
fn test_compute_sha256_file_matches_compute_sha256() {
    let dir = tempfile::tempdir().unwrap();
    let file_path = dir.path().join("test_data.bin");
    let data = b"hello world";
    std::fs::write(&file_path, data).unwrap();
    let file_hash = compute_sha256_file(&file_path).unwrap();
    let mem_hash = compute_sha256(data);
    assert_eq!(file_hash, mem_hash);
    assert_eq!(
        file_hash,
        "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
    );
}

#[test]
fn test_compute_sha256_file_empty() {
    let dir = tempfile::tempdir().unwrap();
    let file_path = dir.path().join("empty.bin");
    std::fs::write(&file_path, b"").unwrap();
    let file_hash = compute_sha256_file(&file_path).unwrap();
    let mem_hash = compute_sha256(b"");
    assert_eq!(file_hash, mem_hash);
}

#[test]
fn test_compute_sha256_file_large() {
    let dir = tempfile::tempdir().unwrap();
    let file_path = dir.path().join("large.bin");
    let data = vec![0xABu8; 100_000];
    std::fs::write(&file_path, &data).unwrap();
    let file_hash = compute_sha256_file(&file_path).unwrap();
    let mem_hash = compute_sha256(&data);
    assert_eq!(file_hash, mem_hash);
}

#[test]
fn test_compute_sha256_file_nonexistent() {
    let result = compute_sha256_file(std::path::Path::new("/nonexistent/path/file.bin"));
    assert!(result.is_err());
}

#[test]
fn test_error_is_io_not_found_true() {
    let err = Error::Io(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "not found",
    ));
    assert!(err.is_io_not_found());
}

#[test]
fn test_error_is_io_not_found_false() {
    let err = Error::Io(std::io::Error::new(
        std::io::ErrorKind::PermissionDenied,
        "denied",
    ));
    assert!(!err.is_io_not_found());
}

#[test]
fn test_error_is_io_not_found_false_for_non_io() {
    let err = Error::Config("bad".into());
    assert!(!err.is_io_not_found());
}

#[test]
fn test_compute_sha256_known_input() {
    let hash = compute_sha256(b"hello world");
    assert_eq!(
        hash,
        "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
    );
}

#[test]
fn test_compute_sha256_empty() {
    let hash = compute_sha256(b"");
    assert_eq!(
        hash,
        "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    );
}

#[test]
fn test_compute_sha256_has_prefix() {
    let hash = compute_sha256(b"data");
    assert!(hash.starts_with("sha256:"));
}

#[test]
fn test_read_page_size_from_db_valid_4096() {
    let mut data = vec![0u8; 100];
    data[16..18].copy_from_slice(&4096u16.to_be_bytes());
    assert_eq!(read_page_size_from_db(&data).unwrap(), 4096);
}

#[test]
fn test_read_page_size_from_db_valid_8192() {
    let mut data = vec![0u8; 100];
    data[16..18].copy_from_slice(&8192u16.to_be_bytes());
    assert_eq!(read_page_size_from_db(&data).unwrap(), 8192);
}

#[test]
fn test_read_page_size_from_db_special_1_means_65536() {
    let mut data = vec![0u8; 100];
    data[16..18].copy_from_slice(&1u16.to_be_bytes());
    assert_eq!(read_page_size_from_db(&data).unwrap(), 65536);
}

#[test]
fn test_read_page_size_from_db_too_small() {
    let data = vec![0u8; 10];
    assert!(read_page_size_from_db(&data).is_err());
}

#[test]
fn test_error_is_not_found_true() {
    let err = Error::ObjectStore(object_store::Error::NotFound {
        path: "/test".to_string(),
        source: Box::new(std::io::Error::new(std::io::ErrorKind::NotFound, "test")),
    });
    assert!(err.is_not_found());
}

#[test]
fn test_error_is_not_found_false_for_other() {
    let err = Error::Other("something else".to_string());
    assert!(!err.is_not_found());
}

#[test]
fn test_error_config_variant() {
    let err = Error::Config("bad config".to_string());
    assert!(err.to_string().contains("bad config"));
    assert!(!err.is_not_found());
}

#[test]
fn test_error_wal_parse_display() {
    let err = Error::WalParse {
        offset: 42,
        detail: "truncated frame".to_string(),
    };
    let s = err.to_string();
    assert!(s.contains("42"));
    assert!(s.contains("truncated frame"));
}

#[test]
fn test_error_wal_checksum_display() {
    let err = Error::WalChecksum {
        frame: 5,
        stored_1: 0x0000_dead_cafe,
        computed_1: 0x0000_beef_f00d,
        stored_2: 0x12345678,
        computed_2: 0x9abcdef0,
    };
    let s = err.to_string();
    assert!(s.contains("5"), "should contain frame number 5: {s}");
}

#[test]
fn test_error_restore_display() {
    let err = Error::Restore("missing manifest".to_string());
    assert!(err.to_string().contains("missing manifest"));
}

#[test]
fn test_error_io_display() {
    let err = Error::Io(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "file not found",
    ));
    assert!(err.to_string().contains("file not found"));
}

#[test]
fn test_error_replication_display() {
    let err = Error::Replication("sync timeout".to_string());
    assert!(err.to_string().contains("sync timeout"));
}

#[test]
fn test_error_manifest_conflict_display() {
    let err = Error::ManifestConflict {
        local_gen: 3,
        remote_gen: 7,
    };
    let s = err.to_string();
    assert!(s.contains("3"));
    assert!(s.contains("7"));
}

#[test]
fn test_error_snapshot_display() {
    let err = Error::Snapshot("checkpoint failed".to_string());
    assert!(err.to_string().contains("checkpoint failed"));
}

#[test]
fn test_error_other_display() {
    let err = Error::Other("something went wrong".to_string());
    assert!(err.to_string().contains("something went wrong"));
}

#[test]
fn test_error_sqlite_from_conversion() {
    let sqlite_err = rusqlite::Error::InvalidColumnIndex(99);
    let err: Error = Error::from(sqlite_err);
    assert!(matches!(err, Error::Sqlite(_)));
    assert!(err.to_string().contains("SQLite error"));
    assert!(!err.is_not_found());
}

#[test]
fn test_error_serialization_from_json() {
    let bad_json_result: serde_json::Result<serde_json::Value> =
        serde_json::from_str("not valid json");
    let err = bad_json_result.unwrap_err();
    let wrapped: Error = err.into();
    assert!(
        wrapped.to_string().contains("Serialization error"),
        "got: {wrapped}"
    );
    assert!(!wrapped.is_not_found());
}

#[test]
fn test_error_object_store_non_notfound() {
    let err = Error::ObjectStore(object_store::Error::NotFound {
        path: "/missing".to_string(),
        source: Box::new(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "not found",
        )),
    });
    assert!(err.to_string().contains("Object store error"), "got: {err}");
}

#[test]
fn test_error_io_is_not_found_false() {
    let err = Error::Io(std::io::Error::new(
        std::io::ErrorKind::PermissionDenied,
        "denied",
    ));
    assert!(!err.is_not_found());
}

#[test]
fn test_error_manifest_conflict_is_not_found_false() {
    let err = Error::ManifestConflict {
        local_gen: 1,
        remote_gen: 5,
    };
    assert!(!err.is_not_found());
}

#[test]
fn test_error_wal_parse_is_not_found_false() {
    let err = Error::WalParse {
        offset: 0,
        detail: "test".to_string(),
    };
    assert!(!err.is_not_found());
}

#[test]
fn test_error_sqlite_display() {
    let conn = rusqlite::Connection::open_in_memory().unwrap();
    let result = conn.execute("SELECT * FROM nonexistent_table", []);
    let err = result.unwrap_err();
    let wrapped: Error = err.into();
    assert!(
        wrapped.to_string().contains("SQLite error"),
        "Error::Sqlite Display should contain 'SQLite error', got: {wrapped}"
    );
    assert!(!wrapped.is_not_found());
}

#[test]
fn test_error_object_store_display() {
    let err = Error::ObjectStore(object_store::Error::NotFound {
        path: "/missing".to_string(),
        source: Box::new(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "not found",
        )),
    });
    assert!(err.to_string().contains("Object store error"), "got: {err}");
}