#![cfg(feature = "s3")]
use somatize_core::cache::CacheKey;
use somatize_core::store::{DataRef, DataStore};
use somatize_core::value::Value;
fn store() -> Option<somatize_core::store::S3DataStore> {
somatize_core::store::S3DataStore::from_env(
"soma-test/",
std::env::temp_dir().join("soma-s3-integration"),
)
.ok()
}
#[test]
fn roundtrip_tensor() {
let Some(store) = store() else {
eprintln!("Skipping S3 test: env vars not set");
return;
};
let key = CacheKey::hash_data(b"integration_test_tensor");
let value = Value::tensor(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]);
let data_ref = store.put(&key, &value).unwrap();
assert!(matches!(data_ref, DataRef::S3 { .. }));
println!("PUT ok: {data_ref:?}");
let local = std::env::temp_dir().join("soma-s3-integration");
let _ = std::fs::remove_dir_all(&local);
std::fs::create_dir_all(&local).ok();
let retrieved = store.get(&data_ref).unwrap();
assert_eq!(retrieved, value);
println!("GET ok: value matches");
assert!(store.exists(&data_ref).unwrap());
println!("EXISTS ok: true");
store.remove(&data_ref).unwrap();
println!("REMOVE ok");
println!("REMOVE completed");
}
#[test]
fn roundtrip_json() {
let Some(store) = store() else { return };
let key = CacheKey::hash_data(b"integration_test_json");
let value = Value::Json(serde_json::json!({
"experiment": "test_001",
"metrics": {"accuracy": 0.95, "loss": 0.05},
"tags": ["integration", "ci"]
}));
let data_ref = store.put(&key, &value).unwrap();
let local = std::env::temp_dir().join("soma-s3-integration");
let _ = std::fs::remove_dir_all(&local);
std::fs::create_dir_all(&local).ok();
let retrieved = store.get(&data_ref).unwrap();
assert_eq!(retrieved, value);
println!("JSON roundtrip ok");
store.remove(&data_ref).unwrap();
}
#[test]
fn local_cache_prevents_s3_fetch() {
let Some(store) = store() else { return };
let key = CacheKey::hash_data(b"integration_test_cache");
let value = Value::tensor(vec![42.0], vec![1]);
let data_ref = store.put(&key, &value).unwrap();
let retrieved = store.get(&data_ref).unwrap();
assert_eq!(retrieved, value);
println!("Local cache hit ok");
store.remove(&data_ref).unwrap();
}