#![cfg(not(target_arch = "wasm32"))]
use absurder_sql::storage::{BLOCK_SIZE, BlockStorage};
use serial_test::serial;
use tempfile::TempDir;
#[path = "common/mod.rs"]
mod common;
#[tokio::test(flavor = "current_thread")]
#[serial]
async fn test_batch_write_and_read_basic() {
let tmp = TempDir::new().expect("tempdir");
common::set_var("ABSURDERSQL_FS_BASE", tmp.path());
let mut storage = BlockStorage::new_with_capacity("test_batch_basic", 3)
.await
.expect("Should create storage");
let d1 = vec![1u8; BLOCK_SIZE];
let d2 = vec![2u8; BLOCK_SIZE];
let d3 = vec![3u8; BLOCK_SIZE];
storage
.write_blocks(vec![(1, d1.clone()), (2, d2.clone()), (3, d3.clone())])
.await
.expect("batch write");
storage.sync().await.expect("sync to clear dirty");
let out = storage.read_blocks(&[1, 2, 3]).await.expect("batch read");
assert_eq!(out.len(), 3);
assert_eq!(out[0], d1);
assert_eq!(out[1], d2);
assert_eq!(out[2], d3);
assert!(storage.is_cached(1));
assert!(storage.is_cached(2));
assert!(storage.is_cached(3));
}
#[tokio::test(flavor = "current_thread")]
#[serial]
async fn test_batch_respects_lru_and_dirty() {
let tmp = TempDir::new().expect("tempdir");
common::set_var("ABSURDERSQL_FS_BASE", tmp.path());
let mut storage = BlockStorage::new_with_capacity("test_batch_lru_dirty", 2)
.await
.expect("Should create storage");
let d1 = vec![1u8; BLOCK_SIZE];
let d2 = vec![2u8; BLOCK_SIZE];
let d3 = vec![3u8; BLOCK_SIZE];
storage
.write_blocks(vec![(1, d1.clone()), (2, d2.clone())])
.await
.expect("batch write 1,2");
let _ = storage.read_block(1).await.expect("read 1");
storage
.write_blocks(vec![(3, d3.clone())])
.await
.expect("write 3");
assert!(storage.is_cached(1));
assert!(storage.is_cached(2));
assert!(storage.is_cached(3));
assert!(storage.get_cache_size() >= 3);
storage.sync().await.expect("sync");
assert!(storage.is_cached(1), "block 1 should remain cached");
assert!(storage.is_cached(3), "block 3 should remain cached");
assert!(
!storage.is_cached(2),
"block 2 should be evicted as LRU clean after sync"
);
}