Skip to content

Commit

Permalink
chore: test json/ipld/nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanlakhani committed Mar 8, 2024
1 parent ebac601 commit 172de35
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions homestar-invocation/src/task/instruction/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()));
}
}

0 comments on commit 172de35

Please sign in to comment.