about summary refs log tree commit diff
path: root/templates/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'templates/src/lib.rs')
-rw-r--r--templates/src/lib.rs61
1 files changed, 60 insertions, 1 deletions
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::<Domain>().to_string(),
+                PostType::Note
+            )),
+            PostType::LikeOfLink(format!(
+                "https://{}/posts/{}-{}-{}",
+                &rand::random::<Domain>(),
+                &rand::random::<Word>(),
+                &rand::random::<Word>(),
+                &rand::random::<Word>(),
+            ))
+        ] {
+            let mf2 = gen_random_post(
+                &rand::random::<Domain>().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::<Url>().ok())
+                                .unwrap()
+                        );
+                    }
+                    Some(PropertyValue::Item(_cite)) => {
+                        todo!()
+                    }
+                    other => panic!("Unexpected value in like-of: {:?}", other)
+                }
+            } else {
+                unreachable!()
+            }
+        }
+    }
 }