From 172de3596148837729bd09fa1f31e9ef01c92bc9 Mon Sep 17 00:00:00 2001 From: Zeeshan Lakhani Date: Fri, 8 Mar 2024 14:04:38 -0500 Subject: [PATCH] chore: test json/ipld/nonce --- .../src/task/instruction/nonce.rs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/homestar-invocation/src/task/instruction/nonce.rs b/homestar-invocation/src/task/instruction/nonce.rs index 6022edf2..4bce1ad3 100644 --- a/homestar-invocation/src/task/instruction/nonce.rs +++ b/homestar-invocation/src/task/instruction/nonce.rs @@ -134,6 +134,7 @@ impl JsonSchema for Nonce { #[cfg(test)] mod test { use super::*; + use libipld::{json::DagJsonCodec, multibase::Base, prelude::Codec}; #[test] fn ipld_roundtrip_12() { @@ -173,4 +174,28 @@ mod test { assert_eq!(gen, de); } + + #[test] + fn json_nonce_roundtrip() { + let bytes = Base::Base64.encode(b"hell0"); + let json = json!({ + "/": {"bytes": format!("{}", bytes)} + }); + + let ipld: Ipld = DagJsonCodec.decode(json.to_string().as_bytes()).unwrap(); + let nonce = Nonce::try_from(ipld.clone()).unwrap(); + + let Ipld::Bytes(bytes) = ipld.clone() else { + panic!("IPLD is not bytes"); + }; + + assert_eq!(bytes, b"hell0"); + assert_eq!(ipld, Ipld::Bytes(b"hell0".to_vec())); + assert_eq!(nonce, Nonce::Nonce96(*GenericArray::from_slice(b"hell0"))); + assert_eq!(nonce, Nonce::try_from(ipld.clone()).unwrap()); + + let nonce: Nonce = ipld.clone().try_into().unwrap(); + let ipld = Ipld::from(nonce.clone()); + assert_eq!(ipld, Ipld::Bytes(b"hell0".to_vec())); + } }