From d8feeec26f2cb451576817bb83692a043eb3e49c Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 6 Jun 2024 16:22:47 +0200 Subject: [PATCH] test(apb): make sure .addressed() works --- apb/src/node.rs | 2 +- apb/src/target.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/apb/src/node.rs b/apb/src/node.rs index 0ff4eb6..7b8c941 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -113,7 +113,7 @@ impl Node { Node::Empty => vec![], Node::Link(uri) => vec![uri.href().to_string()], Node::Object(x) => x.id().map_or(vec![], |x| vec![x.to_string()]), - Node::Array(x) => x.iter().filter_map(|x| x.id().ok()).map(|x| x.to_string()).collect() + Node::Array(x) => x.iter().filter_map(|x| Some(x.id().ok()?.to_string())).collect() } } diff --git a/apb/src/target.rs b/apb/src/target.rs index 7d41475..f130dd3 100644 --- a/apb/src/target.rs +++ b/apb/src/target.rs @@ -43,3 +43,34 @@ impl Addressed for T { // to // } } + +#[cfg(test)] +mod test { + use super::Addressed; + + #[test] + #[cfg(feature = "unstructured")] + fn addressed_trait_finds_all_targets_on_json_objects() { + let obj = serde_json::json!({ + "id": "http://localhost:8080/obj/1", + "type": "Note", + "content": "hello world!", + "published": "2024-06-04T17:09:20+00:00", + "to": ["http://localhost:8080/usr/root/followers"], + "bto": ["https://localhost:8080/usr/secret"], + "cc": [crate::target::PUBLIC], + "bcc": [], + }); + + let addressed = obj.addressed(); + + assert_eq!( + addressed, + vec![ + "http://localhost:8080/usr/root/followers".to_string(), + "https://localhost:8080/usr/secret".to_string(), + crate::target::PUBLIC.to_string(), + ] + ); + } +}