Skip to content

Commit

Permalink
chore: test json/ipld/nonce (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeeshan Lakhani committed Mar 8, 2024
1 parent ebac601 commit 4118586
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 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,29 @@ mod test {

assert_eq!(gen, de);
}

#[test]
fn json_nonce_roundtrip() {
let b = b"LSS3Ftv+Gtq9965M";
let bytes = Base::Base64.encode(b);
let json = json!({
"/": {"bytes": format!("{}", bytes)}
});

let ipld: Ipld = DagJsonCodec.decode(json.to_string().as_bytes()).unwrap();
let nonce: Nonce = Nonce::try_from(ipld.clone()).unwrap();

let Ipld::Bytes(bytes) = ipld.clone() else {
panic!("IPLD is not bytes");
};

assert_eq!(bytes, b);
assert_eq!(ipld, Ipld::Bytes(b.to_vec()));
assert_eq!(nonce, Nonce::Nonce128(*GenericArray::from_slice(b)));
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.to_vec()));
}
}

0 comments on commit 4118586

Please sign in to comment.