use crate::actor::state::ActorStateClient;
use crate::actor::{ActorContext, ActorKey};
use std::sync::Arc;
pub async fn create_test_context(actor_name: &str, key: &str) -> ActorContext {
let actor_key = ActorKey::new(actor_name, key);
let state_client = Arc::new(ActorStateClient::new_mock());
let execution_id = uuid::Uuid::new_v4().to_string();
ActorContext::new(actor_key, state_client, execution_id)
}
pub async fn create_test_context_with_execution_id(
actor_name: &str,
key: &str,
execution_id: &str,
) -> ActorContext {
let actor_key = ActorKey::new(actor_name, key);
let state_client = Arc::new(ActorStateClient::new_mock());
ActorContext::new(actor_key, state_client, execution_id.to_string())
}
pub async fn create_test_context_with_shared_state(
actor_name: &str,
key: &str,
state_client: Arc<ActorStateClient>,
) -> ActorContext {
let actor_key = ActorKey::new(actor_name, key);
let execution_id = uuid::Uuid::new_v4().to_string();
ActorContext::new(actor_key, state_client, execution_id)
}
pub fn create_mock_state_client() -> Arc<ActorStateClient> {
Arc::new(ActorStateClient::new_mock())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_create_test_context() {
let ctx = create_test_context("TestActor", "test-key").await;
assert_eq!(ctx.actor_name(), "TestActor");
assert_eq!(ctx.key(), "test-key");
assert!(!ctx.execution_id().is_empty());
}
#[tokio::test]
async fn test_create_test_context_with_execution_id() {
let ctx = create_test_context_with_execution_id("TestActor", "test-key", "exec-123").await;
assert_eq!(ctx.execution_id(), "exec-123");
}
#[tokio::test]
async fn test_create_test_context_with_shared_state() {
let state_client = create_mock_state_client();
let ctx1 =
create_test_context_with_shared_state("TestActor", "key-1", state_client.clone()).await;
let ctx2 =
create_test_context_with_shared_state("TestActor", "key-2", state_client.clone()).await;
ctx1.state().set("test", &42i64).await.unwrap();
let value: Option<i64> = ctx2.state().get("test").await.unwrap();
assert_eq!(value, None);
let ctx3 =
create_test_context_with_shared_state("TestActor", "key-1", state_client.clone()).await;
let value: Option<i64> = ctx3.state().get("test").await.unwrap();
assert_eq!(value, Some(42));
}
#[tokio::test]
async fn test_state_persistence_across_contexts() {
let state_client = create_mock_state_client();
{
let ctx =
create_test_context_with_shared_state("Counter", "user-123", state_client.clone())
.await;
ctx.state().set("count", &10i64).await.unwrap();
}
{
let ctx =
create_test_context_with_shared_state("Counter", "user-123", state_client.clone())
.await;
let count: Option<i64> = ctx.state().get("count").await.unwrap();
assert_eq!(count, Some(10));
}
}
#[tokio::test]
async fn test_state_isolation_between_keys() {
let state_client = create_mock_state_client();
let ctx1 =
create_test_context_with_shared_state("Counter", "key-1", state_client.clone()).await;
let ctx2 =
create_test_context_with_shared_state("Counter", "key-2", state_client.clone()).await;
ctx1.state().set("count", &10i64).await.unwrap();
ctx2.state().set("count", &20i64).await.unwrap();
let count1: Option<i64> = ctx1.state().get("count").await.unwrap();
let count2: Option<i64> = ctx2.state().get("count").await.unwrap();
assert_eq!(count1, Some(10));
assert_eq!(count2, Some(20));
}
#[tokio::test]
async fn test_state_isolation_between_actors() {
let state_client = create_mock_state_client();
let ctx1 =
create_test_context_with_shared_state("Counter", "key-1", state_client.clone()).await;
let ctx2 =
create_test_context_with_shared_state("ShoppingCart", "key-1", state_client.clone())
.await;
ctx1.state().set("data", &10i64).await.unwrap();
ctx2.state().set("data", &20i64).await.unwrap();
let data1: Option<i64> = ctx1.state().get("data").await.unwrap();
let data2: Option<i64> = ctx2.state().get("data").await.unwrap();
assert_eq!(data1, Some(10));
assert_eq!(data2, Some(20));
}
}