use std::future::Future;
use std::sync::Arc;
use aidaemon_core::RunId;
use aidaemon_core::conformance::ConformanceReport;
use crate::approvals::{ApprovalStore, StoredApproval};
use crate::channel::{Conversation, ConversationKind, MessageRef};
pub mod approval_checks {
pub const STARTS_EMPTY: &str = "approvals.starts_empty";
pub const ROUND_TRIP: &str = "approvals.round_trip";
pub const SAVE_REPLACES: &str = "approvals.save_replaces";
pub const REMOVE_IDEMPOTENT: &str = "approvals.remove_idempotent";
pub const SCOPES_KEPT_APART: &str = "approvals.scopes_kept_apart";
pub const CONCURRENT_WRITES: &str = "approvals.concurrent_writes";
}
fn record(prompt_id: &str, channel: &str, conversation: &str) -> StoredApproval {
StoredApproval {
prompt_id: prompt_id.to_string(),
session_id: format!("{channel}:{conversation}"),
channel: channel.to_string(),
conversation: Conversation {
id: conversation.to_string(),
kind: ConversationKind::Direct,
},
reply_to: None,
run_id: RunId::new(),
prompt_text: format!("Allow the synthetic action {prompt_id}?"),
prompt_ref: None,
expires_at_ms: 4_000_000_000_000,
}
}
fn sorted(mut all: Vec<StoredApproval>) -> Vec<StoredApproval> {
all.sort_by(|a, b| a.prompt_id.cmp(&b.prompt_id));
all
}
pub async fn check_approval_store<F, Fut>(make: F) -> ConformanceReport
where
F: Fn() -> Fut,
Fut: Future<Output = Arc<dyn ApprovalStore>>,
{
use approval_checks::*;
let mut report = ConformanceReport::new("ApprovalStore");
let store = make().await;
report.record(
STARTS_EMPTY,
store
.load_all()
.await
.map_err(|e| e.to_string())
.and_then(|all| {
if all.is_empty() {
Ok(())
} else {
Err(format!("{} records in a fresh store", all.len()))
}
}),
);
let store = make().await;
let mut full = record("p1", "telegram", "chat-1");
full.reply_to = Some("m-7".to_string());
full.prompt_ref = Some(MessageRef {
conversation_id: "chat-1".to_string(),
message_id: "m-9".to_string(),
});
let bare = record("p2", "telegram", "chat-1");
report.record(
ROUND_TRIP,
async {
store.save(&full).await.map_err(|e| e.to_string())?;
store.save(&bare).await.map_err(|e| e.to_string())?;
let got = sorted(store.load_all().await.map_err(|e| e.to_string())?);
if got == vec![full.clone(), bare.clone()] {
Ok(())
} else {
Err(format!("loaded {got:?}"))
}
}
.await,
);
let store = make().await;
report.record(
SAVE_REPLACES,
async {
let first = record("p1", "telegram", "chat-1");
let mut second = first.clone();
second.prompt_ref = Some(MessageRef {
conversation_id: "chat-1".to_string(),
message_id: "m-2".to_string(),
});
second.expires_at_ms += 1;
store.save(&first).await.map_err(|e| e.to_string())?;
store.save(&second).await.map_err(|e| e.to_string())?;
let got = store.load_all().await.map_err(|e| e.to_string())?;
if got == vec![second] {
Ok(())
} else {
Err(format!("loaded {got:?}"))
}
}
.await,
);
let store = make().await;
report.record(
REMOVE_IDEMPOTENT,
async {
let keep = record("keep", "telegram", "chat-1");
store.save(&keep).await.map_err(|e| e.to_string())?;
store
.save(&record("drop", "telegram", "chat-1"))
.await
.map_err(|e| e.to_string())?;
store.remove("drop").await.map_err(|e| e.to_string())?;
store
.remove("drop")
.await
.map_err(|e| format!("second remove failed: {e}"))?;
store
.remove("never-saved")
.await
.map_err(|e| format!("removing a missing id failed: {e}"))?;
let got = store.load_all().await.map_err(|e| e.to_string())?;
if got == vec![keep] {
Ok(())
} else {
Err(format!("loaded {got:?}"))
}
}
.await,
);
let store = make().await;
report.record(
SCOPES_KEPT_APART,
async {
let a = record("a", "telegram", "chat-1");
let b = record("b", "telegram", "chat-2");
let c = record("c", "slack", "chat-1");
for r in [&a, &b, &c] {
store.save(r).await.map_err(|e| e.to_string())?;
}
store.remove("b").await.map_err(|e| e.to_string())?;
let got = sorted(store.load_all().await.map_err(|e| e.to_string())?);
if got == vec![a, c] {
Ok(())
} else {
Err(format!("loaded {got:?}"))
}
}
.await,
);
let store = make().await;
report.record(
CONCURRENT_WRITES,
async {
let mut tasks = Vec::new();
for i in 0..16 {
let store = store.clone();
tasks.push(tokio::spawn(async move {
let id = format!("p{i:02}");
store.save(&record(&id, "telegram", "chat-1")).await?;
if i % 2 == 1 {
store.remove(&id).await?;
store.remove(&id).await?;
}
Ok::<_, aidaemon_core::StoreError>(())
}));
}
for task in tasks {
task.await
.map_err(|e| e.to_string())?
.map_err(|e| e.to_string())?;
}
let got: Vec<String> = sorted(store.load_all().await.map_err(|e| e.to_string())?)
.into_iter()
.map(|r| r.prompt_id)
.collect();
let want: Vec<String> = (0..16)
.filter(|i| i % 2 == 0)
.map(|i| format!("p{i:02}"))
.collect();
if got == want {
Ok(())
} else {
Err(format!("loaded {got:?}, wanted {want:?}"))
}
}
.await,
);
report
}
#[cfg(test)]
mod tests {
use super::*;
use crate::MemoryApprovalStore;
#[tokio::test]
async fn the_memory_store_passes_the_kit() {
check_approval_store(|| async {
Arc::new(MemoryApprovalStore::new()) as Arc<dyn ApprovalStore>
})
.await
.assert_all_passed();
}
}