use tempfile::tempdir;
use theater::{ContentRef, ContentStore, Label};
#[tokio::test]
async fn test_content_store_basic() {
let temp_dir = tempdir().unwrap();
let _store_path = temp_dir.path().join("test-store");
let store = ContentStore::new();
let test_content = b"test content data".to_vec();
let content_ref = store.store(test_content.clone()).await;
let content_ref_obj = ContentRef::from_content(&test_content);
let expected_hash = content_ref_obj.hash();
assert_eq!(content_ref.hash(), expected_hash);
let retrieved = store.get(&content_ref).await.unwrap();
assert_eq!(retrieved, test_content);
}
#[tokio::test]
async fn test_content_store_deduplication() {
let temp_dir = tempdir().unwrap();
let _store_path = temp_dir.path().join("test-store");
let store = ContentStore::new();
let test_content = b"duplicate content test".to_vec();
let ref1 = store.store(test_content.clone()).await;
let ref2 = store.store(test_content.clone()).await;
assert_eq!(ref1.hash(), ref2.hash());
let retrieved1 = store.get(&ref1).await.unwrap();
let retrieved2 = store.get(&ref2).await.unwrap();
assert_eq!(retrieved1, test_content);
assert_eq!(retrieved2, test_content);
}
#[tokio::test]
async fn test_content_store_labeling() {
let temp_dir = tempdir().unwrap();
let _store_path = temp_dir.path().join("test-store");
let store = ContentStore::new();
let test_content1 = b"content one".to_vec();
let test_content2 = b"content two".to_vec();
let ref1 = store.store(test_content1.clone()).await;
let ref2 = store.store(test_content2.clone()).await;
store
.label(&Label::from_str("test-label-1"), &ref1.clone())
.await
.unwrap();
store
.label(&Label::from_str("test-label-2"), &ref2.clone())
.await
.unwrap();
let found_ref1 = store
.get_by_label(&Label::from_str("test-label-1"))
.await
.unwrap()
.unwrap();
let found_ref2 = store
.get_by_label(&Label::from_str("test-label-2"))
.await
.unwrap()
.unwrap();
assert_eq!(found_ref1, ref1);
assert_eq!(found_ref2, ref2);
store
.label(&Label::from_str("test-label-1"), &ref2.clone())
.await
.unwrap();
let updated_ref = store
.get_by_label(&Label::from_str("test-label-1"))
.await
.unwrap()
.unwrap();
assert_eq!(updated_ref, ref2);
}
#[tokio::test]
async fn test_content_store_delete() {
let temp_dir = tempdir().unwrap();
let _store_path = temp_dir.path().join("test-store");
let store = ContentStore::new();
let label_name = "test-label";
let test_content = b"deletable content".to_vec();
let content_ref = store.store(test_content.clone()).await;
store
.label(&Label::from_str(label_name), &content_ref)
.await
.unwrap();
assert!(store.exists(&content_ref).await);
let label_ref = store
.get_by_label(&Label::from_str(label_name))
.await
.unwrap();
assert_eq!(Some(content_ref.clone()), label_ref);
store
.remove_label(&Label::from_str(label_name))
.await
.unwrap();
assert!(store.exists(&content_ref).await); let label_ref_after = store
.get_by_label(&Label::from_str(label_name))
.await
.unwrap();
assert_eq!(None, label_ref_after);
let result = store
.get_content_by_label(&Label::from_str(label_name))
.await
.unwrap();
assert_eq!(None, result);
}
#[tokio::test]
async fn test_content_store_from_id() {
let store = ContentStore::from_id("test-id");
let test_content = b"store from ID test".to_vec();
let content_ref = store.store(test_content.clone()).await;
let retrieved = store.get(&content_ref).await.unwrap();
assert_eq!(retrieved, test_content);
assert!(!store.id().is_empty());
}
#[tokio::test]
async fn test_content_ref_creation() {
let content = b"test content for ref".to_vec();
let content_ref = ContentRef::from_content(&content);
let content_ref2 = ContentRef::from_content(&content);
assert_eq!(content_ref.hash(), content_ref2.hash());
let different_content = b"different content".to_vec();
let different_ref = ContentRef::from_content(&different_content);
assert_ne!(content_ref.hash(), different_ref.hash());
let hash = content_ref.hash();
let from_hash = ContentRef::from_str(hash);
assert_eq!(content_ref, from_hash);
}
#[tokio::test]
async fn test_content_ref_serialization() {
let content = b"serialization test".to_vec();
let content_ref = ContentRef::from_content(&content);
let serialized = serde_json::to_string(&content_ref).unwrap();
let deserialized: ContentRef = serde_json::from_str(&serialized).unwrap();
assert_eq!(content_ref, deserialized);
}
#[tokio::test]
async fn test_label_operations() {
}