From 20867384e36fba1d33113dbdbb0bdd94aabf6760 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 23 May 2022 18:19:39 +0300 Subject: templates: render like and bookmark posts correctly They really use the same framework, so for now a unit test for like posts is sufficient. Of course, for proper coverage, one can introduce tests for bookmarks too, especially if one chooses to render them differently. The logic will be pretty much the same though. Replies might use the same logic, since those are also Webmention-oriented posts. (It looks like another way to classify MF2 documents is slowly forming in my brain. Maybe I should write about it on my blog.) --- templates/src/lib.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'templates/src/lib.rs') diff --git a/templates/src/lib.rs b/templates/src/lib.rs index 45d1894..39f1075 100644 --- a/templates/src/lib.rs +++ b/templates/src/lib.rs @@ -7,6 +7,7 @@ pub use login::LoginPage; #[cfg(test)] mod tests { + use faker_rand::lorem::Word; use serde_json::json; use microformats::types::{Document, Item, PropertyValue, Url}; use std::cell::RefCell; @@ -37,7 +38,7 @@ mod tests { } fn gen_random_post(domain: &str, kind: PostType) -> serde_json::Value { - use faker_rand::lorem::{Paragraph, Word, Sentence}; + use faker_rand::lorem::{Paragraph, Sentence}; fn html(content: Paragraph) -> serde_json::Value { json!({ @@ -285,4 +286,62 @@ mod tests { unreachable!() } } + + #[test] + fn test_like_of() { + test_logger::ensure_env_logger_initialized(); + + for likeof in [ + PostType::LikeOf(gen_random_post( + &rand::random::().to_string(), + PostType::Note + )), + PostType::LikeOfLink(format!( + "https://{}/posts/{}-{}-{}", + &rand::random::(), + &rand::random::(), + &rand::random::(), + &rand::random::(), + )) + ] { + let mf2 = gen_random_post( + &rand::random::().to_string(), + likeof + ); + let url: Url = mf2.pointer("/properties/uid/0") + .and_then(|i| i.as_str()) + .and_then(|u| u.parse().ok()) + .unwrap(); + let html = crate::templates::Entry { + post: &mf2 + }.to_string(); + let parsed: Document = microformats::from_html(&html, url.clone()).unwrap(); + + if let Some(item) = parsed.items.get(0) { + let _item = item.borrow(); + let props = _item.properties.borrow(); + + check_dt_published(&mf2, item); + assert!(props.contains_key("like-of")); + match props.get("like-of").and_then(|v| v.first()) { + Some(PropertyValue::Url(url)) => { + assert_eq!( + url, + &mf2.pointer("/properties/like-of/0") + .and_then(|i| i.as_str()) + .or_else(|| mf2.pointer("/properties/like-of/0/properties/uid/0").and_then(|i| i.as_str())) + .and_then(|u| u.parse::().ok()) + .unwrap() + ); + } + Some(PropertyValue::Item(_cite)) => { + todo!() + } + other => panic!("Unexpected value in like-of: {:?}", other) + } + } else { + unreachable!() + } + } + } } -- cgit 1.4.1