use crate::utils::watcher::WatcherTest;
use anyhow::Result;
use nexus_common::{
models::notification::{Notification, NotificationBody, PostChangedSource},
types::Pagination,
};
use pubky::Keypair;
use pubky_app_specs::{PubkyAppPost, PubkyAppPostKind, PubkyAppUser};
#[tokio_shared_rt::test(shared)]
async fn test_edit_parent_post_notification() -> Result<()> {
let mut test = WatcherTest::setup().await?;
let keypair_a = Keypair::random();
let user_a = PubkyAppUser {
bio: Some("User A bio".to_string()),
image: None,
links: None,
name: "Watcher:ReplyParentEditNotification:UserA".to_string(),
status: None,
};
let user_a_id = test.create_user(&keypair_a, &user_a).await?;
let keypair_b = Keypair::random();
let user_b = PubkyAppUser {
bio: Some("User B bio".to_string()),
image: None,
links: None,
name: "Watcher:ReplyParentEditNotification:UserB".to_string(),
status: None,
};
let user_b_id = test.create_user(&keypair_b, &user_b).await?;
let mut post = PubkyAppPost {
content: "Original post by User A".to_string(),
kind: PubkyAppPostKind::Short,
parent: None,
embed: None,
attachments: None,
};
let post_id = test.create_post(&user_a_id, &post).await?;
let reply = PubkyAppPost {
content: "Reply by User B".to_string(),
kind: PubkyAppPostKind::Short,
parent: Some(format!("pubky://{user_a_id}/pub/pubky.app/posts/{post_id}")),
embed: None,
attachments: None,
};
let reply_id = test.create_post(&user_b_id, &reply).await?;
post.content = "Edited post by User A".to_string();
let edited_url = format!("pubky://{user_a_id}/pub/pubky.app/posts/{post_id}");
test.put(edited_url.as_str(), &post).await?;
let notifications = Notification::get_by_id(&user_b_id, Pagination::default())
.await
.unwrap();
assert_eq!(
notifications.len(),
1,
"User B should have exactly one notification"
);
if let NotificationBody::PostEdited {
edit_source,
edited_by,
edited_uri,
linked_uri,
} = ¬ifications[0].body
{
assert_eq!(
edited_by, &user_a_id,
"Notification should specify the correct user who edited the post"
);
assert_eq!(
edited_uri,
&format!("pubky://{user_a_id}/pub/pubky.app/posts/{post_id}"),
"Notification should contain the correct edited post URI"
);
assert_eq!(
linked_uri,
&format!("pubky://{user_b_id}/pub/pubky.app/posts/{reply_id}"),
"Notification should contain the correct reply URI"
);
assert_eq!(
edit_source,
&PostChangedSource::ReplyParent,
"Edit notification should have the correct source"
);
} else {
panic!("Expected a PostEdited notification, found something else");
}
Ok(())
}