#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
use absurder_sql::storage::BlockStorage;
#[cfg(target_arch = "wasm32")]
use absurder_sql::storage::SyncPolicy;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_basic_enablement() {
let storage = BlockStorage::new("wasm_auto_sync_basic")
.await
.expect("create storage");
storage.enable_auto_sync(1000);
assert!(
storage.is_auto_sync_enabled(),
"Auto-sync should be enabled"
);
storage.disable_auto_sync();
assert!(
!storage.is_auto_sync_enabled(),
"Auto-sync should be disabled"
);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_with_policy() {
let storage = BlockStorage::new("wasm_auto_sync_policy")
.await
.expect("create storage");
let policy = SyncPolicy {
interval_ms: Some(500),
max_dirty: Some(10),
max_dirty_bytes: Some(40960), debounce_ms: Some(100),
verify_after_write: false,
};
storage.enable_auto_sync_with_policy(policy.clone());
assert!(
storage.is_auto_sync_enabled(),
"Auto-sync should be enabled"
);
let retrieved_policy = storage.get_sync_policy().expect("Should have policy");
assert_eq!(retrieved_policy.interval_ms, policy.interval_ms);
assert_eq!(retrieved_policy.max_dirty, policy.max_dirty);
assert_eq!(retrieved_policy.max_dirty_bytes, policy.max_dirty_bytes);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_background_execution() {
let mut storage = BlockStorage::new("wasm_auto_sync_bg")
.await
.expect("create storage");
storage.enable_auto_sync(100);
let block1 = storage.allocate_block().await.expect("allocate block1");
let block2 = storage.allocate_block().await.expect("allocate block2");
storage
.write_block(block1, vec![1u8; 4096])
.await
.expect("write block1");
storage
.write_block(block2, vec![2u8; 4096])
.await
.expect("write block2");
assert_eq!(storage.get_dirty_count(), 2, "Should have 2 dirty blocks");
storage.sync().await.expect("manual sync");
assert_eq!(
storage.get_dirty_count(),
0,
"Sync should have cleared dirty blocks"
);
storage.disable_auto_sync();
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_threshold_triggering() {
let mut storage = BlockStorage::new("wasm_auto_sync_threshold")
.await
.expect("create storage");
let policy = SyncPolicy {
interval_ms: None, max_dirty: Some(3), max_dirty_bytes: None,
debounce_ms: None,
verify_after_write: false,
};
storage.enable_auto_sync_with_policy(policy);
let block1 = storage.allocate_block().await.expect("allocate block1");
let block2 = storage.allocate_block().await.expect("allocate block2");
let block3 = storage.allocate_block().await.expect("allocate block3");
storage
.write_block(block1, vec![1u8; 4096])
.await
.expect("write block1");
assert_eq!(storage.get_dirty_count(), 1);
storage
.write_block(block2, vec![2u8; 4096])
.await
.expect("write block2");
assert_eq!(storage.get_dirty_count(), 2);
storage
.write_block(block3, vec![3u8; 4096])
.await
.expect("write block3");
storage.sync().await.expect("sync");
assert_eq!(
storage.get_dirty_count(),
0,
"Sync should have cleared dirty blocks"
);
storage.disable_auto_sync();
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_metrics() {
let mut storage = BlockStorage::new("wasm_auto_sync_metrics")
.await
.expect("create storage");
storage.enable_auto_sync(200);
let block1 = storage.allocate_block().await.expect("allocate block1");
storage
.write_block(block1, vec![42u8; 4096])
.await
.expect("write block1");
storage.sync().await.expect("sync");
let metrics = storage.get_metrics();
assert!(
metrics.sync_count > 0,
"Should have recorded sync operations"
);
storage.disable_auto_sync();
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_multi_instance() {
let mut storage1 = BlockStorage::new("wasm_auto_sync_multi")
.await
.expect("create storage1");
let storage2 = BlockStorage::new("wasm_auto_sync_multi")
.await
.expect("create storage2");
storage1.enable_auto_sync(300);
storage2.enable_auto_sync(300);
let block1 = storage1.allocate_block().await.expect("allocate block1");
storage1
.write_block(block1, vec![1u8; 4096])
.await
.expect("write block1");
storage1.sync().await.expect("sync storage1");
assert_eq!(storage1.get_dirty_count(), 0, "Storage1 should be synced");
let data = storage2
.read_block(block1)
.await
.expect("read from storage2");
assert_eq!(data[0], 1u8, "Data should be visible across instances");
storage1.disable_auto_sync();
storage2.disable_auto_sync();
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_error_handling() {
let mut storage = BlockStorage::new("wasm_auto_sync_errors")
.await
.expect("create storage");
storage.enable_auto_sync(150);
let block1 = storage.allocate_block().await.expect("allocate block1");
storage
.write_block(block1, vec![1u8; 4096])
.await
.expect("write block1");
storage.sync().await.expect("sync");
assert_eq!(storage.get_dirty_count(), 0, "Sync should work correctly");
storage.disable_auto_sync();
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_wasm_auto_sync_shutdown() {
let mut storage = BlockStorage::new("wasm_auto_sync_shutdown")
.await
.expect("create storage");
storage.enable_auto_sync(100);
assert!(storage.is_auto_sync_enabled());
let block1 = storage.allocate_block().await.expect("allocate block1");
storage
.write_block(block1, vec![1u8; 4096])
.await
.expect("write block1");
storage.drain_and_shutdown();
assert_eq!(
storage.get_dirty_count(),
0,
"Shutdown should drain dirty blocks"
);
assert!(
!storage.is_auto_sync_enabled(),
"Shutdown should disable auto-sync"
);
}