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 std::path::{Path, PathBuf};
use std::time::Duration;
use wal_backup::{Config, RestoreBehavior, SyncMode};

#[test]
fn test_config_default_values() {
    let config = Config::new("/tmp/test.db");
    assert_eq!(config.poll_interval(), Duration::from_millis(100));
    assert_eq!(config.snapshot_threshold_bytes(), 50 * 1024 * 1024);
    assert_eq!(config.snapshot_interval(), Duration::from_secs(3600));
    assert_eq!(
        config.restore_behavior(),
        &RestoreBehavior::UploadLocalIfNewer
    );
    assert_eq!(config.max_frames_per_segment(), 1000);
    assert_eq!(config.sync_mode(), &SyncMode::default());
}

#[test]
fn test_config_default_sync_mode_is_immediate() {
    let config = Config::new("/tmp/test.db");
    assert_eq!(config.sync_mode(), &SyncMode::Immediate);
}

#[test]
fn test_config_wal_path_derived_from_db_path() {
    let config = Config::new("/tmp/mydb.db");
    assert_eq!(config.wal_path(), &PathBuf::from("/tmp/mydb.db-wal"));
}

#[test]
fn test_config_with_poll_interval() {
    let config = Config::new("/tmp/test.db").with_poll_interval(Duration::from_secs(5));
    assert_eq!(config.poll_interval(), Duration::from_secs(5));
}

#[test]
fn test_config_with_snapshot_threshold() {
    let config = Config::new("/tmp/test.db").with_snapshot_threshold(100 * 1024 * 1024);
    assert_eq!(config.snapshot_threshold_bytes(), 100 * 1024 * 1024);
}

#[test]
fn test_config_with_snapshot_interval() {
    let config = Config::new("/tmp/test.db").with_snapshot_interval(Duration::from_secs(7200));
    assert_eq!(config.snapshot_interval(), Duration::from_secs(7200));
}

#[test]
fn test_config_with_restore_behavior() {
    let config =
        Config::new("/tmp/test.db").with_restore_behavior(RestoreBehavior::AlwaysRestoreFromBucket);
    assert_eq!(
        config.restore_behavior(),
        &RestoreBehavior::AlwaysRestoreFromBucket
    );
}

#[test]
fn test_config_with_max_frames_per_segment() {
    let config = Config::new("/tmp/test.db").with_max_frames_per_segment(500);
    assert_eq!(config.max_frames_per_segment(), 500);
}

#[test]
fn test_config_with_sync_mode_lazy() {
    let config = Config::new("/tmp/test.db").with_sync_mode(SyncMode::Lazy);
    assert_eq!(config.sync_mode(), &SyncMode::Lazy);
}

#[test]
fn test_config_with_immediate_poll_interval_clamped_low() {
    let config = Config::new("/tmp/test.db").with_immediate_poll_interval(Duration::from_millis(1));
    assert_eq!(config.immediate_poll_interval(), Duration::from_millis(10));
}

#[test]
fn test_config_with_immediate_poll_interval_clamped_high() {
    let config = Config::new("/tmp/test.db").with_immediate_poll_interval(Duration::from_secs(10));
    assert_eq!(config.immediate_poll_interval(), Duration::from_millis(500));
}

#[test]
fn test_config_with_immediate_poll_interval_valid() {
    let config =
        Config::new("/tmp/test.db").with_immediate_poll_interval(Duration::from_millis(100));
    assert_eq!(config.immediate_poll_interval(), Duration::from_millis(100));
}

#[test]
fn test_config_chain_multiple_options() {
    let config = Config::new("/tmp/test.db")
        .with_poll_interval(Duration::from_millis(200))
        .with_snapshot_threshold(200 * 1024 * 1024)
        .with_snapshot_interval(Duration::from_secs(7200))
        .with_sync_mode(SyncMode::Lazy)
        .with_restore_behavior(RestoreBehavior::ErrorIfConflict)
        .with_max_frames_per_segment(500)
        .with_immediate_poll_interval(Duration::from_millis(200))
        .with_salt_change_timeout(Duration::from_secs(30));

    assert_eq!(config.poll_interval(), Duration::from_millis(200));
    assert_eq!(config.snapshot_threshold_bytes(), 200 * 1024 * 1024);
    assert_eq!(config.snapshot_interval(), Duration::from_secs(7200));
    assert_eq!(config.sync_mode(), &SyncMode::Lazy);
    assert_eq!(config.restore_behavior(), &RestoreBehavior::ErrorIfConflict);
    assert_eq!(config.max_frames_per_segment(), 500);
    assert_eq!(config.immediate_poll_interval(), Duration::from_millis(200));
    assert_eq!(config.salt_change_timeout(), Duration::from_secs(30));
}

#[test]
fn test_sync_mode_clone_copy() {
    let mode = SyncMode::Immediate;
    let _ = mode;
    let mode2 = SyncMode::Lazy;
    let _ = mode2;
}

#[test]
fn test_restore_behavior_clone_copy() {
    let behavior = RestoreBehavior::UploadLocalIfNewer;
    let _ = behavior;
    let behavior2 = RestoreBehavior::AlwaysRestoreFromBucket;
    let _ = behavior2;
    let behavior3 = RestoreBehavior::ErrorIfConflict;
    let _ = behavior3;
    let behavior4 = RestoreBehavior::Recoverable;
    let _ = behavior4;
}

#[test]
fn test_restore_behavior_all_variants_equality() {
    assert_eq!(
        RestoreBehavior::UploadLocalIfNewer,
        RestoreBehavior::UploadLocalIfNewer
    );
    assert_eq!(
        RestoreBehavior::AlwaysRestoreFromBucket,
        RestoreBehavior::AlwaysRestoreFromBucket
    );
    assert_eq!(
        RestoreBehavior::ErrorIfConflict,
        RestoreBehavior::ErrorIfConflict
    );
    assert_eq!(RestoreBehavior::Recoverable, RestoreBehavior::Recoverable);
    assert_ne!(
        RestoreBehavior::UploadLocalIfNewer,
        RestoreBehavior::Recoverable
    );
}

#[test]
fn test_sync_mode_all_variants_equality() {
    assert_eq!(SyncMode::Immediate, SyncMode::Immediate);
    assert_eq!(SyncMode::Lazy, SyncMode::Lazy);
    assert_ne!(SyncMode::Immediate, SyncMode::Lazy);
}

#[test]
fn test_restore_behavior_equality() {
    assert_eq!(RestoreBehavior::Recoverable, RestoreBehavior::Recoverable);
    assert_ne!(
        RestoreBehavior::Recoverable,
        RestoreBehavior::UploadLocalIfNewer
    );
    assert_ne!(
        RestoreBehavior::ErrorIfConflict,
        RestoreBehavior::AlwaysRestoreFromBucket
    );
}

#[test]
fn test_restore_behavior_debug() {
    assert_eq!(format!("{:?}", RestoreBehavior::Recoverable), "Recoverable");
    assert_eq!(
        format!("{:?}", RestoreBehavior::UploadLocalIfNewer),
        "UploadLocalIfNewer"
    );
}

#[test]
fn test_config_validate_rejects_empty_db_path() {
    let config = Config::new(Path::new(""));
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_accepts_valid_config() {
    let config = Config::new(Path::new("/tmp/test.db"));
    assert!(config.validate().is_ok());
}

#[test]
fn test_config_with_health_timeout() {
    let config =
        Config::new(Path::new("/tmp/test.db")).with_health_timeout(Duration::from_secs(60));
    assert_eq!(config.health_timeout(), Duration::from_secs(60));
}

#[test]
fn test_config_with_sync_timeout() {
    let config = Config::new(Path::new("/tmp/test.db")).with_sync_timeout(Duration::from_secs(30));
    assert_eq!(config.sync_timeout(), Duration::from_secs(30));
}

#[test]
fn test_config_validate_rejects_zero_sync_timeout() {
    let config = Config::new(Path::new("/tmp/test.db")).with_invalid_sync_timeout(Duration::ZERO);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_zero_health_timeout() {
    let config = Config::new(Path::new("/tmp/test.db")).with_invalid_health_timeout(Duration::ZERO);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_zero_snapshot_threshold() {
    let config = Config::new(Path::new("/tmp/test.db")).with_invalid_snapshot_threshold(0);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_zero_snapshot_interval() {
    let config =
        Config::new(Path::new("/tmp/test.db")).with_invalid_snapshot_interval(Duration::ZERO);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_zero_poll_interval() {
    let config = Config::new(Path::new("/tmp/test.db")).with_invalid_poll_interval(Duration::ZERO);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_zero_max_frames_per_segment() {
    let config = Config::new(Path::new("/tmp/test.db")).with_invalid_max_frames(0);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_zero_salt_change_timeout() {
    let config =
        Config::new(Path::new("/tmp/test.db")).with_invalid_salt_change_timeout(Duration::ZERO);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_salt_change_timeout_over_5min() {
    let config = Config::new(Path::new("/tmp/test.db"))
        .with_invalid_salt_change_timeout(Duration::from_secs(400));
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_accepts_min_salt_change_timeout() {
    let config = Config::new(Path::new("/tmp/test.db"))
        .with_invalid_salt_change_timeout(Duration::from_secs(1));
    assert!(config.validate().is_ok());
}

#[test]
fn test_config_validate_accepts_max_salt_change_timeout() {
    let config = Config::new(Path::new("/tmp/test.db"))
        .with_invalid_salt_change_timeout(Duration::from_secs(300));
    assert!(config.validate().is_ok());
}

#[test]
fn test_config_validate_rejects_immediate_poll_interval_too_low() {
    let config = Config::new(Path::new("/tmp/test.db"))
        .with_invalid_immediate_poll_interval(Duration::from_millis(5));
    assert!(config.validate().is_err());
}

#[test]
fn test_config_validate_rejects_immediate_poll_interval_too_high() {
    let config = Config::new(Path::new("/tmp/test.db"))
        .with_invalid_immediate_poll_interval(Duration::from_millis(1000));
    assert!(config.validate().is_err());
}

#[test]
fn test_config_with_salt_change_timeout_builder() {
    let config =
        Config::new(Path::new("/tmp/test.db")).with_salt_change_timeout(Duration::from_secs(45));
    assert_eq!(config.salt_change_timeout(), Duration::from_secs(45));
}

#[test]
fn test_config_with_salt_change_timeout_clamped_low() {
    // Below 1s gets clamped to 1s
    let config =
        Config::new(Path::new("/tmp/test.db")).with_salt_change_timeout(Duration::from_millis(500));
    assert_eq!(config.salt_change_timeout(), Duration::from_secs(1));
}

#[test]
fn test_config_with_salt_change_timeout_clamped_high() {
    // Above 5min gets clamped to 5min
    let config =
        Config::new(Path::new("/tmp/test.db")).with_salt_change_timeout(Duration::from_secs(600));
    assert_eq!(config.salt_change_timeout(), Duration::from_secs(300));
}

#[test]
fn test_config_with_snapshot_check_interval() {
    let config =
        Config::new(Path::new("/tmp/test.db")).with_snapshot_check_interval(Duration::from_secs(5));
    assert_eq!(config.snapshot_check_interval(), Duration::from_secs(5));
}

#[test]
fn test_config_validate_rejects_zero_snapshot_check_interval() {
    let config =
        Config::new(Path::new("/tmp/test.db")).with_invalid_snapshot_check_interval(Duration::ZERO);
    assert!(config.validate().is_err());
}

#[test]
fn test_config_db_path() {
    let config = Config::new("/tmp/test.db");
    assert_eq!(config.db_path(), &PathBuf::from("/tmp/test.db"));
}

#[test]
fn test_config_wal_path() {
    let config = Config::new("/tmp/test.db");
    assert_eq!(config.wal_path(), &PathBuf::from("/tmp/test.db-wal"));
}

#[test]
fn test_config_default_salt_change_timeout() {
    let config = Config::new("/tmp/test.db");
    assert_eq!(config.salt_change_timeout(), Duration::from_secs(60));
}

#[test]
fn test_config_default_snapshot_check_interval() {
    let config = Config::new("/tmp/test.db");
    assert_eq!(config.snapshot_check_interval(), Duration::from_secs(10));
}