155 lines
5.2 KiB
Rust

use std::sync::Arc;
use offline_web_model::Reference;
use crate::{ReferenceStore, StoreError};
pub async fn test_store_and_retrieve_reference_impl<T: ReferenceStore>(store: &T) {
// Create a test reference
let reference = Reference::new(
Some("test_content_address".to_string()),
"test_reference".to_string(),
);
// Store the reference
store.store_reference(&reference).await.unwrap();
// Retrieve the reference by ID
let retrieved = store.get_reference(&reference.id).await.unwrap();
// Verify the retrieved reference matches the original
assert_eq!(retrieved.id, reference.id);
assert_eq!(retrieved.content_address, reference.content_address);
assert_eq!(retrieved.name, reference.name);
assert_eq!(retrieved.dependents.len(), reference.dependents.len());
}
pub async fn test_store_and_retrieve_content_impl<T: ReferenceStore>(store: &T) {
let content = b"Hello, World!";
let content_address = "test_content_address";
// Store content
store.store_content(content_address, content).await.unwrap();
// Create a reference pointing to this content
let reference = Reference::new(
Some(content_address.to_string()),
"test_reference".to_string(),
);
// Retrieve content using the reference
let retrieved_content = store.get_content_for_reference(reference).await.unwrap();
// Verify the content matches
assert_eq!(retrieved_content, String::from_utf8(content.to_vec()).unwrap());
}
pub async fn test_reference_with_dependents_impl<T: ReferenceStore>(store: &T) {
// Create a leaf reference (no dependents)
let leaf_ref = Reference::new(
Some("leaf_content_address".to_string()),
"leaf_reference".to_string(),
);
// Store the leaf reference
store.store_reference(&leaf_ref).await.unwrap();
// Create a parent reference that depends on the leaf
let parent_ref = Reference::new(
Some("parent_content_address".to_string()),
"parent_reference".to_string(),
).add_dep(Arc::new(leaf_ref.clone()));
// Store the parent reference
store.store_reference(&parent_ref).await.unwrap();
// Retrieve the parent reference
let retrieved_parent = store.get_reference(&parent_ref.id).await.unwrap();
// Verify the parent has the correct dependent
assert_eq!(retrieved_parent.dependents.len(), 1);
assert_eq!(retrieved_parent.dependents[0].name, leaf_ref.name);
}
pub async fn test_get_graph_impl<T: ReferenceStore>(store: &T) {
// Create a hierarchy of references
let leaf1 = Reference::new(
Some("leaf1_content".to_string()),
"leaf1".to_string(),
);
let leaf2 = Reference::new(
Some("leaf2_content".to_string()),
"leaf2".to_string(),
);
let parent = Reference::new(
Some("parent_content".to_string()),
"parent".to_string(),
)
.add_dep(Arc::new(leaf1.clone()))
.add_dep(Arc::new(leaf2.clone()));
let root = Reference::new(
Some("root_content".to_string()),
"root".to_string(),
).add_dep(Arc::new(parent.clone()));
// Store all references
store.store_reference(&leaf1).await.unwrap();
store.store_reference(&leaf2).await.unwrap();
store.store_reference(&parent).await.unwrap();
store.store_reference(&root).await.unwrap();
// Get the graph starting from root
let graph = store.get_graph("root").await.unwrap();
// Verify we got all references in the graph
assert_eq!(graph.len(), 4);
// Verify we have all the expected references
let names: Vec<_> = graph.iter().map(|r| &r.name).collect();
assert!(names.contains(&&"root".to_string()));
assert!(names.contains(&&"parent".to_string()));
assert!(names.contains(&&"leaf1".to_string()));
assert!(names.contains(&&"leaf2".to_string()));
}
pub async fn test_nonexistent_reference_impl<T: ReferenceStore>(store: &T) {
// Try to retrieve a reference that doesn't exist
let result = store.get_reference("nonexistent_id").await;
// Should return NoSuchReference error
assert!(matches!(result, Err(StoreError::NoSuchReference)));
}
pub async fn test_nonexistent_content_impl<T: ReferenceStore>(store: &T) {
// Create a reference with a content address that doesn't exist
let reference = Reference::new(
Some("nonexistent_content_address".to_string()),
"test_reference".to_string(),
);
// Try to retrieve content
let result = store.get_content_for_reference(reference).await;
// Should return NoSuchContentAddress error
assert!(matches!(result, Err(StoreError::NoSuchContentAddress)));
}
pub async fn test_reference_without_content_address_impl<T: ReferenceStore>(store: &T) {
// Create a reference without a content address
let reference = Reference::new(None, "test_reference".to_string());
// Try to retrieve content
let result = store.get_content_for_reference(reference).await;
// Should return NoSuchContentAddress error
assert!(matches!(result, Err(StoreError::NoSuchContentAddress)));
}
#[cfg(all(test, feature="native"))]
mod sqlite;
#[cfg(all(test, feature="wasm"))]
mod indexeddb;