use do_memory_core::{Episode, TaskContext, TaskType};
use do_memory_storage_turso::TursoStorage;
use libsql::Builder;
use tempfile::TempDir;
use tokio::time::{Duration, timeout};
#[tokio::test]
#[ignore = "Memory corruption bug in libsql native library - malloc_consolidate() unaligned fastbin chunk in CI"]
async fn test_prepared_statement_cache_integration() {
let dir = TempDir::new().unwrap();
let db_path = dir.path().join("test.db");
let db = timeout(
Duration::from_secs(10),
Builder::new_local(&db_path).build(),
)
.await
.expect("Database creation timed out")
.unwrap();
let storage = TursoStorage::from_database(db).unwrap();
timeout(Duration::from_secs(5), storage.initialize_schema())
.await
.expect("Schema initialization timed out")
.unwrap();
let stats_before = storage.prepared_cache_stats();
assert_eq!(stats_before.current_size, 0, "Cache should start empty");
assert_eq!(stats_before.hits, 0, "No hits initially");
assert_eq!(stats_before.misses, 0, "No misses initially");
let episode = Episode::new(
"Test episode for prepared cache".to_string(),
TaskContext::default(),
TaskType::CodeGeneration,
);
timeout(Duration::from_secs(5), storage.store_episode(&episode))
.await
.expect("Store episode timed out")
.unwrap();
let stats_after_store = storage.prepared_cache_stats();
assert_eq!(
stats_after_store.current_size, 0,
"Cache cleared after operation"
);
let retrieved = timeout(
Duration::from_secs(5),
storage.get_episode(episode.episode_id),
)
.await
.expect("Get episode timed out")
.unwrap();
assert!(retrieved.is_some(), "Episode should be retrievable");
assert_eq!(
retrieved.unwrap().task_description,
"Test episode for prepared cache"
);
let episode2 = Episode::new(
"Second test episode".to_string(),
TaskContext::default(),
TaskType::Debugging,
);
timeout(Duration::from_secs(5), storage.store_episode(&episode2))
.await
.expect("Store second episode timed out")
.unwrap();
let retrieved2 = timeout(
Duration::from_secs(5),
storage.get_episode(episode2.episode_id),
)
.await
.expect("Get second episode timed out")
.unwrap();
assert!(retrieved2.is_some(), "Second episode should be retrievable");
assert_eq!(retrieved2.unwrap().task_description, "Second test episode");
let stats_final = storage.prepared_cache_stats();
assert_eq!(
stats_final.current_size, 0,
"Cache cleared after operations"
);
println!("Prepared cache integration test passed!");
println!(" Episodes stored and retrieved: 2");
println!(" Cache stats (cleared after each operation):");
println!(" Current size: {}", stats_final.current_size);
println!(" Total prepares: {}", stats_final.prepared);
drop(storage);
}