use std::convert::TryFrom;
use wasm_bindgen_test::*;
use crate::document::DocumentViewId;
use crate::hash::Hash;
use crate::operation::{OperationEncoded, OperationFields, OperationValue};
use crate::schema::SchemaId;
use crate::test_utils::fixtures::operation;
use crate::wasm::{decode_entry, sign_encode_entry, KeyPair, SignEncodeEntryResult};
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn encodes_decodes_entries() {
let key_pair = KeyPair::new();
let schema = SchemaId::Application(
"profile".to_string(),
"0020c65567ae37efea293e34a9c7d13f8f2bf23dbdc3b5c7b9ab46293111c48fc78b"
.parse::<DocumentViewId>()
.unwrap(),
);
let mut fields = OperationFields::new();
fields
.add("name", OperationValue::Text("Hello!".to_string()))
.unwrap();
let operation = operation(Some(fields), None, Some(schema));
let operation_encoded = OperationEncoded::try_from(&operation).unwrap();
let encode_result = sign_encode_entry(
&key_pair,
operation_encoded.as_str().into(),
None,
None,
1,
1,
);
assert!(encode_result.is_ok());
let encoded_entry_result: SignEncodeEntryResult =
serde_wasm_bindgen::from_value(encode_result.unwrap()).unwrap();
let decode_result = decode_entry(
encoded_entry_result.entry_encoded,
Some(operation_encoded.as_str().into()),
);
assert!(decode_result.is_ok());
let result = sign_encode_entry(
&key_pair,
operation_encoded.as_str().into(),
Some(
Hash::new_from_bytes(vec![0, 1, 2])
.unwrap()
.as_str()
.to_string(),
),
Some(
Hash::new_from_bytes(vec![1, 2, 3])
.unwrap()
.as_str()
.to_string(),
),
7,
1,
);
assert!(result.is_ok());
let result = sign_encode_entry(
&key_pair,
operation_encoded.as_str().into(),
None,
None,
3,
1,
);
assert!(result.is_err());
}