use std::sync::Arc;
use async_trait::async_trait;
use awaken_runtime::builder::AgentRuntimeBuilder;
use awaken_runtime_contract::contract::commit_coordinator::{
CommitCoordinator, CommitError, ThreadCommit, ThreadCommitOutcome, TransactionScopeId,
};
use awaken_runtime_contract::contract::storage::RuntimeCheckpointStore;
use awaken_server_contract::contract::storage::{ThreadRunCheckpointStore, ThreadRunStore};
use awaken_stores::InMemoryStore;
struct NoopCoord {
thread_run: Arc<InMemoryStore>,
}
impl Default for NoopCoord {
fn default() -> Self {
Self {
thread_run: Arc::new(InMemoryStore::new()),
}
}
}
#[async_trait]
impl CommitCoordinator for NoopCoord {
fn scope(&self) -> TransactionScopeId {
TransactionScopeId::new("test::noop").unwrap()
}
fn reader(&self) -> Arc<dyn RuntimeCheckpointStore> {
Arc::new(ThreadRunCheckpointStore::new(
Arc::clone(&self.thread_run) as Arc<dyn ThreadRunStore>
))
}
async fn commit_checkpoint(
&self,
_plan: ThreadCommit,
) -> Result<ThreadCommitOutcome, CommitError> {
Ok(ThreadCommitOutcome)
}
}
#[test]
fn builder_wires_commit_coordinator_into_runtime() {
let runtime = AgentRuntimeBuilder::new()
.with_commit_coordinator(Arc::new(NoopCoord::default()))
.build()
.unwrap();
let coord = runtime
.commit_coordinator()
.expect("coordinator should be wired");
assert_eq!(coord.scope().as_str(), "test::noop");
}
#[test]
fn run_activation_accepts_commit_coordinator_override() {
use awaken_runtime::RunActivation;
let coordinator: Arc<dyn CommitCoordinator> = Arc::new(NoopCoord::default());
let activation = RunActivation::new("t-override", Vec::new())
.with_commit_coordinator_override(Arc::clone(&coordinator));
let attached = activation
.control
.commit_coordinator_override
.as_ref()
.expect("override must be attached to RunActivation");
assert!(Arc::ptr_eq(attached, &coordinator));
let neutral = RunActivation::new("t-neutral", Vec::new());
assert!(neutral.control.commit_coordinator_override.is_none());
}