#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
#[cfg(target_arch = "wasm32")]
use absurder_sql::storage::{BlockStorage, vfs_sync_database, vfs_sync_database_blocking};
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_current_vfs_sync_behavior_basic_sync() {
let db_name = "vfs_sync_basic_test.db";
#[cfg(target_arch = "wasm32")]
{
use absurder_sql::storage::vfs_sync::{with_global_commit_marker, with_global_storage};
use absurder_sql::vfs::indexeddb_vfs::STORAGE_REGISTRY;
with_global_storage(|gs| gs.borrow_mut().clear());
with_global_commit_marker(|cm| cm.borrow_mut().clear());
STORAGE_REGISTRY.with(|sr| unsafe { &mut *sr.get() }.clear());
}
let storage = BlockStorage::new(db_name)
.await
.expect("Should create storage");
let mut test_data = vec![0u8; 4096];
test_data[0..15].copy_from_slice(b"vfs sync test ");
storage
.write_block(1, test_data.clone())
.await
.expect("Should write block");
storage.sync().await.expect("Should sync");
vfs_sync_database(db_name).expect("VFS sync should succeed");
let storage2 = BlockStorage::new(db_name)
.await
.expect("Should create second storage");
let read_data = storage2
.read_block(1)
.await
.expect("Should read block after VFS sync");
assert_eq!(read_data, test_data, "Data should persist through VFS sync");
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_current_vfs_sync_behavior_blocking_sync() {
let db_name = "vfs_sync_blocking_test.db";
#[cfg(target_arch = "wasm32")]
{
use absurder_sql::storage::vfs_sync::{with_global_commit_marker, with_global_storage};
use absurder_sql::vfs::indexeddb_vfs::STORAGE_REGISTRY;
with_global_storage(|gs| gs.borrow_mut().clear());
with_global_commit_marker(|cm| cm.borrow_mut().clear());
STORAGE_REGISTRY.with(|sr| unsafe { &mut *sr.get() }.clear());
}
let storage = BlockStorage::new(db_name)
.await
.expect("Should create storage");
for i in 1..=3 {
let mut data = vec![0u8; 4096];
let text = format!("blocking sync block {}", i);
data[0..text.len()].copy_from_slice(text.as_bytes());
storage
.write_block(i, data)
.await
.expect(&format!("Should write block {}", i));
}
storage.sync().await.expect("Should sync");
vfs_sync_database_blocking(db_name).expect("Blocking VFS sync should succeed");
let storage2 = BlockStorage::new(db_name)
.await
.expect("Should create second storage");
for i in 1..=3 {
let mut expected_data = vec![0u8; 4096];
let text = format!("blocking sync block {}", i);
expected_data[0..text.len()].copy_from_slice(text.as_bytes());
let read_data = storage2
.read_block(i)
.await
.expect(&format!("Should read block {}", i));
assert_eq!(
read_data, expected_data,
"Block {} should persist through blocking VFS sync",
i
);
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_current_vfs_sync_behavior_commit_marker_advancement() {
let db_name = "vfs_sync_commit_marker_test.db";
#[cfg(target_arch = "wasm32")]
{
use absurder_sql::storage::vfs_sync::{with_global_commit_marker, with_global_storage};
use absurder_sql::vfs::indexeddb_vfs::STORAGE_REGISTRY;
with_global_storage(|gs| gs.borrow_mut().clear());
with_global_commit_marker(|cm| cm.borrow_mut().clear());
STORAGE_REGISTRY.with(|sr| unsafe { &mut *sr.get() }.clear());
}
let storage = BlockStorage::new(db_name)
.await
.expect("Should create storage");
let initial_marker = storage.get_commit_marker();
let mut test_data = vec![0u8; 4096];
test_data[0..20].copy_from_slice(b"commit marker test ");
storage
.write_block(10, test_data)
.await
.expect("Should write block");
storage.sync().await.expect("Should sync");
let marker_before_vfs = storage.get_commit_marker();
vfs_sync_database(db_name).expect("VFS sync should succeed");
let marker_after_vfs = storage.get_commit_marker();
web_sys::console::log_1(
&format!(
"Commit markers: initial={}, before_vfs={}, after_vfs={}",
initial_marker, marker_before_vfs, marker_after_vfs
)
.into(),
);
assert!(
marker_after_vfs >= marker_before_vfs,
"Commit marker should advance or stay same during VFS sync"
);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_current_vfs_sync_behavior_cross_instance_visibility() {
let db_name = "vfs_sync_cross_instance_test.db";
#[cfg(target_arch = "wasm32")]
{
use absurder_sql::storage::vfs_sync::{with_global_commit_marker, with_global_storage};
use absurder_sql::vfs::indexeddb_vfs::STORAGE_REGISTRY;
with_global_storage(|gs| gs.borrow_mut().clear());
with_global_commit_marker(|cm| cm.borrow_mut().clear());
STORAGE_REGISTRY.with(|sr| unsafe { &mut *sr.get() }.clear());
}
{
let storage1 = BlockStorage::new(db_name)
.await
.expect("Should create first storage");
let mut data = vec![0u8; 4096];
data[0..25].copy_from_slice(b"cross instance vfs data ");
storage1
.write_block(20, data)
.await
.expect("Should write block");
storage1.sync().await.expect("Should sync");
vfs_sync_database(db_name).expect("VFS sync should succeed");
}
{
let storage2 = BlockStorage::new(db_name)
.await
.expect("Should create second storage");
let read_data = storage2
.read_block(20)
.await
.expect("Should read block from second instance");
let mut expected_data = vec![0u8; 4096];
expected_data[0..25].copy_from_slice(b"cross instance vfs data ");
assert_eq!(
read_data, expected_data,
"Data should be visible across instances after VFS sync"
);
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_current_vfs_sync_behavior_storage_registration() {
let db_name = "vfs_sync_registration_test.db";
#[cfg(target_arch = "wasm32")]
{
use absurder_sql::storage::vfs_sync::{with_global_commit_marker, with_global_storage};
use absurder_sql::vfs::indexeddb_vfs::STORAGE_REGISTRY;
with_global_storage(|gs| gs.borrow_mut().clear());
with_global_commit_marker(|cm| cm.borrow_mut().clear());
STORAGE_REGISTRY.with(|sr| unsafe { &mut *sr.get() }.clear());
}
let storage = BlockStorage::new(db_name)
.await
.expect("Should create storage");
let mut test_data = vec![0u8; 4096];
test_data[0..20].copy_from_slice(b"registration test ");
storage
.write_block(30, test_data)
.await
.expect("Should write block");
storage.sync().await.expect("Should sync");
vfs_sync_database(db_name).expect("VFS sync should work");
let storage2 = BlockStorage::new(db_name)
.await
.expect("Should create verification storage");
let read_data = storage2.read_block(30).await.expect("Should read block");
let mut expected_data = vec![0u8; 4096];
expected_data[0..20].copy_from_slice(b"registration test ");
assert_eq!(
read_data, expected_data,
"Data should persist through registered storage VFS sync"
);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
async fn test_current_vfs_sync_behavior_empty_database() {
let db_name = "vfs_sync_empty_test.db";
#[cfg(target_arch = "wasm32")]
{
use absurder_sql::storage::vfs_sync::{with_global_commit_marker, with_global_storage};
use absurder_sql::vfs::indexeddb_vfs::STORAGE_REGISTRY;
with_global_storage(|gs| gs.borrow_mut().clear());
with_global_commit_marker(|cm| cm.borrow_mut().clear());
STORAGE_REGISTRY.with(|sr| unsafe { &mut *sr.get() }.clear());
}
let _storage = BlockStorage::new(db_name)
.await
.expect("Should create storage");
vfs_sync_database(db_name).expect("VFS sync should succeed on empty database");
vfs_sync_database_blocking(db_name)
.expect("Blocking VFS sync should succeed on empty database");
let storage2 = BlockStorage::new(db_name)
.await
.expect("Should create storage after empty VFS sync");
let mut test_data = vec![0u8; 4096];
test_data[0..16].copy_from_slice(b"after empty sync");
storage2
.write_block(1, test_data.clone())
.await
.expect("Should write after empty VFS sync");
let read_data = storage2
.read_block(1)
.await
.expect("Should read after empty VFS sync");
assert_eq!(
read_data, test_data,
"Should work normally after empty VFS sync"
);
}