#![allow(clippy::significant_drop_tightening)]
use chaotic_semantic_memory::{ChaoticSemanticFramework, HVec10240, error::MemoryError};
use std::collections::HashMap;
const NS: &str = "_default";
async fn create_test_framework() -> ChaoticSemanticFramework {
ChaoticSemanticFramework::builder()
.without_persistence()
.build()
.await
.unwrap()
}
#[tokio::test]
async fn test_update_concept_vector() {
let framework = create_test_framework().await;
let id = "test-concept";
let vector1 = HVec10240::random();
let vector2 = HVec10240::random();
framework.inject_concept(id, vector1).await.unwrap();
let sing = framework.singularity();
let guard = sing.read().await;
let concept = guard.get(NS, id).unwrap();
assert_eq!(concept.vector, vector1);
drop(guard);
framework.update_concept_vector(id, vector2).await.unwrap();
let sing = framework.singularity();
let guard = sing.read().await;
let concept = guard.get(NS, id).unwrap();
assert_eq!(concept.vector, vector2);
}
#[tokio::test]
async fn test_update_concept_vector_not_found() {
let framework = create_test_framework().await;
let vector = HVec10240::random();
let result = framework.update_concept_vector("nonexistent", vector).await;
assert!(matches!(result.unwrap_err(), MemoryError::NotFound { .. }));
}
#[tokio::test]
async fn test_update_concept_metadata() {
let framework = create_test_framework().await;
let id = "test-concept";
let vector = HVec10240::random();
framework.inject_concept(id, vector).await.unwrap();
let mut metadata = HashMap::new();
metadata.insert("key".to_string(), serde_json::json!("value"));
framework
.update_concept_metadata(id, metadata.clone())
.await
.unwrap();
let sing = framework.singularity();
let guard = sing.read().await;
let concept = guard.get(NS, id).unwrap();
assert_eq!(
concept.metadata.get("key").unwrap(),
&serde_json::json!("value")
);
}
#[tokio::test]
async fn test_disassociate() {
let framework = create_test_framework().await;
let id1 = "concept-1";
let id2 = "concept-2";
framework
.inject_concept(id1, HVec10240::random())
.await
.unwrap();
framework
.inject_concept(id2, HVec10240::random())
.await
.unwrap();
framework.associate(id1, id2, 0.8).await.unwrap();
let sing = framework.singularity();
let associations = sing.read().await.get_associations(NS, id1);
assert_eq!(associations.len(), 1);
framework.disassociate(id1, id2).await.unwrap();
let associations = framework
.singularity()
.read()
.await
.get_associations(NS, id1);
assert!(associations.is_empty());
}
#[tokio::test]
async fn test_clear_associations() {
let framework = create_test_framework().await;
let id1 = "concept-1";
let id2 = "concept-2";
let id3 = "concept-3";
framework
.inject_concept(id1, HVec10240::random())
.await
.unwrap();
framework
.inject_concept(id2, HVec10240::random())
.await
.unwrap();
framework
.inject_concept(id3, HVec10240::random())
.await
.unwrap();
framework.associate(id1, id2, 0.8).await.unwrap();
framework.associate(id1, id3, 0.6).await.unwrap();
let sing = framework.singularity();
let associations = sing.read().await.get_associations(NS, id1);
assert_eq!(associations.len(), 2);
framework.clear_associations(id1).await.unwrap();
let associations = framework
.singularity()
.read()
.await
.get_associations(NS, id1);
assert!(associations.is_empty());
}
#[tokio::test]
async fn test_bundle_concepts_strict() {
let framework = create_test_framework().await;
let id1 = "concept-1";
let id2 = "concept-2";
framework
.inject_concept(id1, HVec10240::random())
.await
.unwrap();
framework
.inject_concept(id2, HVec10240::random())
.await
.unwrap();
let result = framework
.bundle_concepts_strict(&[id1.to_string(), id2.to_string()])
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_bundle_concepts_strict_missing() {
let framework = create_test_framework().await;
let id1 = "concept-1";
framework
.inject_concept(id1, HVec10240::random())
.await
.unwrap();
let result = framework
.bundle_concepts_strict(&[id1.to_string(), "nonexistent".to_string()])
.await;
match result {
Err(MemoryError::NotFound { entity, id }) => {
assert_eq!(entity, "Concept");
assert_eq!(id, "nonexistent");
}
_ => panic!("Expected NotFound error, got {result:?}"),
}
}
#[tokio::test]
async fn test_clear_similarity_cache() {
let framework = create_test_framework().await;
let id = "concept-1";
let vector = HVec10240::random();
framework.inject_concept(id, vector).await.unwrap();
let _ = framework.probe(vector, 10).await.unwrap();
framework.clear_similarity_cache().await;
}