use std::collections::BTreeMap;
use std::sync::Arc;
use async_trait::async_trait;
use crate::error::DriverError;
use crate::outbound::{Disclosure, ReplyTemplates, Suppression, SuppressionScope};
use crate::social::{InMemorySocial, SocialDriver, SocialItem, SocialOutbox, social_key};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Published {
pub in_reply_to: Option<String>,
pub text: String,
pub visibility: String,
}
#[async_trait]
pub trait SocialServer: Send + Sync {
async fn receive(&self, item: &SocialItem);
async fn published(&self) -> Vec<Published>;
}
#[async_trait]
pub trait SocialHarness: Send + Sync {
async fn start(&self) -> (Arc<dyn SocialDriver>, Arc<dyn SocialServer>);
}
#[derive(Debug, Default, Clone, Copy)]
pub struct InMemorySocialHarness;
struct InMemoryServer(Arc<InMemorySocial>);
#[async_trait]
impl SocialServer for InMemoryServer {
async fn receive(&self, item: &SocialItem) {
self.0.receive(item.clone());
}
async fn published(&self) -> Vec<Published> {
self.0
.published()
.into_iter()
.map(|(in_reply_to, item)| Published {
in_reply_to,
text: item.text,
visibility: item.visibility,
})
.collect()
}
}
#[async_trait]
impl SocialHarness for InMemorySocialHarness {
async fn start(&self) -> (Arc<dyn SocialDriver>, Arc<dyn SocialServer>) {
let social = Arc::new(InMemorySocial::new());
(social.clone(), Arc::new(InMemoryServer(social)))
}
}
fn fields(hours: &str) -> BTreeMap<String, String> {
BTreeMap::from([("hours".to_string(), hours.to_string())])
}
pub async fn social_driver_contract(harness: &dyn SocialHarness) {
let (social, server) = harness.start().await;
server
.receive(&SocialItem::new("101", "ana@social.example", "@site open on Sunday?", "public"))
.await;
server
.receive(&SocialItem::new("102", "bob@social.example", "Can I order a cake?", "direct"))
.await;
server
.receive(&SocialItem::new("103", "cy@social.example", "@site love the bread", "public"))
.await;
let mentions = social.mentions(None, 20).await.expect("mentions: read");
let ids: Vec<&str> = mentions.items.iter().map(|item| item.id.as_str()).collect();
assert_eq!(ids, ["101", "103"], "mentions: public mentions, oldest first");
assert_eq!(mentions.items[0].author, "ana@social.example", "mentions: the author");
assert!(
mentions.items[0].text.contains("open on Sunday?"),
"mentions: the text without markup"
);
let messages = social.messages(None, 20).await.expect("messages: read");
assert_eq!(messages.items.len(), 1, "messages: the direct message only");
assert!(messages.items[0].is_direct(), "messages: direct");
let after =
social.mentions(mentions.cursor.as_deref(), 20).await.expect("mentions: after the cursor");
assert!(after.items.is_empty(), "mentions: nothing after the cursor");
server.receive(&SocialItem::new("104", "dee@social.example", "@site hello", "public")).await;
let next = social.mentions(mentions.cursor.as_deref(), 20).await.expect("mentions: the next");
assert_eq!(
next.items.iter().map(|item| item.id.as_str()).collect::<Vec<_>>(),
["104"],
"mentions: only the new one"
);
social.reply(&messages.items[0], "Yes, order by Friday.").await.expect("reply: sent");
social.post("Fresh sourdough today.").await.expect("post: sent");
let published = server.published().await;
assert_eq!(published.len(), 2, "publish: a reply and a post");
assert_eq!(published[0].in_reply_to.as_deref(), Some("102"), "reply: answers its item");
assert!(published[0].text.contains("@bob@social.example"), "reply: names its author");
assert_eq!(published[0].visibility, "direct", "reply: a direct message stays direct");
assert_eq!(published[1].in_reply_to, None, "post: answers nothing");
assert_eq!(published[1].visibility, "public", "post: public");
let (social, server) = harness.start().await;
let disclosure = Disclosure::new("Wanjiru's Bakery").unwrap();
let outbox = SocialOutbox::builder(social, disclosure.clone())
.templates(ReplyTemplates::new().template("hours", "Yes, {hours} on Sundays."))
.build();
server
.receive(&SocialItem::new("201", "ana@social.example", "@site open on Sunday?", "public"))
.await;
server.receive(&SocialItem::new("202", "eve@social.example", "@site hi", "public")).await;
let error =
outbox.reply("201", "hours", &fields("8–12")).await.expect_err("outbox: an unread item");
assert!(
matches!(error, DriverError::Refused { .. }),
"outbox: only an item the inbox read: {error:?}"
);
let page = outbox.read_mentions(None, 20).await.expect("outbox: reads");
assert_eq!(page.items.len(), 2, "outbox: two mentions");
outbox.reply("201", "hours", &fields("8–12")).await.expect("outbox: reply sent");
outbox.post("Hot cross buns are back.").await.expect("outbox: post sent");
let published = server.published().await;
assert!(
published.iter().all(|p| disclosure.is_present(&p.text)),
"outbox: every text ends with the disclosure: {published:?}"
);
outbox
.suppressions()
.suppress(Suppression::new(
&social_key("eve@social.example"),
SuppressionScope::All,
"owner",
))
.await
.expect("outbox: revocation recorded");
let error = outbox.reply("202", "hours", &fields("8–12")).await.expect_err("outbox: revoked");
assert!(
matches!(error, DriverError::Refused { .. }),
"outbox: a revoked author gets no reply: {error:?}"
);
let long = "bread ".repeat(200);
let error = outbox.post(&long).await.expect_err("outbox: too long");
assert!(
matches!(error, DriverError::Invalid { .. }),
"outbox: too long with the disclosure: {error:?}"
);
assert_eq!(server.published().await.len(), 2, "outbox: nothing more published");
}