diff --git a/zomes/trust_atom/src/trust_atom.rs b/zomes/trust_atom/src/trust_atom.rs index 05822b1..698ea1e 100644 --- a/zomes/trust_atom/src/trust_atom.rs +++ b/zomes/trust_atom/src/trust_atom.rs @@ -26,19 +26,37 @@ pub fn create( let bucket = create_bucket()?; - let extra_entry_hash_string = match extra.clone() { - Some(x) => Some(create_extra(x)?), - None => None, + let overflow: bool = match content.clone() { + Some(c) => { + if c.len() > 900 { + true + } else { + false + } + } + None => false, + }; + + let extra_entry_hash_string = match overflow { + true => match extra.clone() { + Some(x) => Some(create_extra(content, Some(x))?), + None => Some(create_extra(content, None)?), + }, + false => match extra.clone() { + Some(x) => Some(create_extra(None, Some(x))?), + None => None, + }, }; - let chunks = [ + let chunks = vec![ content.clone(), normalize_value(value.clone())?, Some(bucket), extra_entry_hash_string, ]; - let forward_link_tag = create_link_tag(&LinkDirection::Forward, &chunks); - let reverse_link_tag = create_link_tag(&LinkDirection::Reverse, &chunks); + + let forward_link_tag = create_link_tag(&LinkDirection::Forward, chunks); + let reverse_link_tag = create_link_tag(&LinkDirection::Reverse, chunks); create_link( agent_address.clone(), @@ -65,10 +83,10 @@ pub fn create( fn create_bucket() -> ExternResult { let bucket_bytes = random_bytes(9)?.into_vec(); - Ok(create_bucket_string(&bucket_bytes)) + Ok(create_bucket_string(bucket_bytes)) } -fn create_bucket_string(bucket_bytes: &[u8]) -> String { +fn create_bucket_string(bucket_bytes: Vec) -> String { let mut bucket = String::new(); for chunk in bucket_bytes { let val = chunk; @@ -77,8 +95,14 @@ fn create_bucket_string(bucket_bytes: &[u8]) -> String { bucket } -fn create_extra(input: BTreeMap) -> ExternResult { - let entry = Extra { fields: input }; +fn create_extra( + content_overflow: Option, + extra_fields: Option>, +) -> ExternResult { + let entry = Extra { + content_overflow, + extra_fields, + }; create_entry(EntryTypes::Extra(entry.clone()))?; @@ -124,10 +148,20 @@ fn normalize_value(value_str: Option) -> ExternResult> { } } -fn create_link_tag(link_direction: &LinkDirection, chunk_options: &[Option]) -> LinkTag { +fn create_link_tag(link_direction: &LinkDirection, chunk_options: Vec>) -> LinkTag { let mut chunks: Vec = vec![]; + if let Some(content) = chunk_options[0] { + if content.len() > 900 { + let mut max_content = content.clone(); + max_content.truncate(898); // leave 2 bytes for `…` + max_content.push_str("…"); + chunks.push(max_content); + } else { + chunks.push(content); + } + } - for i in 0..chunk_options.len() { + for i in 1..chunk_options.len() { if let Some(chunk) = chunk_options[i].clone() { chunks.push(chunk); } @@ -256,16 +290,16 @@ pub fn query( } (Some(content_full), None, Some(value_starts_with)) => Some(create_link_tag( &link_direction, - &[Some(content_full), Some(value_starts_with)], + Some(content_full), + Some(value_starts_with), )), (Some(content_full), None, None) => Some(create_link_tag_metal( &link_direction, vec![content_full, UNICODE_NUL_STR.to_string()], )), - (None, Some(content_starts_with), None) => Some(create_link_tag( - &link_direction, - &[Some(content_starts_with)], - )), + (None, Some(content_starts_with), None) => { + Some(create_link_tag(&link_direction, Some(content_starts_with))) + } (None, None, Some(value_starts_with)) => Some(create_link_tag( &link_direction, &[None, Some(value_starts_with)], diff --git a/zomes/trust_atom/tests/trust_atom_tests.rs b/zomes/trust_atom/tests/trust_atom_tests.rs index 48b2033..9d0fa5b 100644 --- a/zomes/trust_atom/tests/trust_atom_tests.rs +++ b/zomes/trust_atom/tests/trust_atom_tests.rs @@ -1,31 +1,38 @@ #![warn(warnings)] -use futures::future; use std::collections::BTreeMap; -use trust_atom_types::DeleteReport; +use futures::future; + +// use hc_zome_trust_atom::*; + +use hc_zome_trust_atom::*; +use hdk::prelude::holo_hash::EntryHashB64; use hdk::prelude::*; use holochain::sweettest::{ SweetAgents, SweetAppBatch, SweetCell, SweetConductor, SweetConductorBatch, SweetDnaFile, }; -const DNA_FILEPATH: &str = "../../workdir/dna/trust_atom_dna.dna"; +const DNA_FILEPATH: &str = "../../workdir/dna/trust_atom.dna"; #[tokio::test] pub async fn test_unicode_null() { let unicode_nul: &str = std::str::from_utf8(&[0]).unwrap(); - assert_eq!(unicode_nul.as_bytes(), &[0]); + assert_eq!( + unicode_nul.as_bytes(), + &[0] // '\u{00}' // .to_string() // .replace("\u{00}", "�") + // .as_str() + ); } #[tokio::test(flavor = "multi_thread")] pub async fn test_create_trust_atom() { let unicode_nul: &str = std::str::from_utf8(&[0]).unwrap(); - let (conductor, agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; + let (conductor, agent, cell1) = setup_1_conductor().await; // CREATE TARGET ENTRY - let target_hash: EntryHash = conductor + let target_entry_hash: EntryHash = conductor .call( &cell1.zome("trust_atom"), "create_string_target", @@ -43,14 +50,14 @@ pub async fn test_create_trust_atom() { .into(), )]); - let trust_atom_input = trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), + let trust_atom_input = TrustAtomInput { + target: target_entry_hash.clone(), content: Some(content.clone()), value: Some(value.clone()), extra: Some(extra.clone()), }; - let _result: trust_atom_types::TrustAtom = conductor + let _result: TrustAtom = conductor .call( &cell1.zome("trust_atom"), "create_trust_atom", @@ -73,8 +80,8 @@ pub async fn test_create_trust_atom() { assert_eq!(forward_links.len(), 1); let link = &forward_links[0]; - let target_from_link = link.clone().target; - assert_eq!(target_from_link, AnyLinkableHash::from(target_hash.clone())); + let target_from_link: EntryHash = link.clone().target; + assert_eq!(target_from_link, target_entry_hash); let link_tag_bytes = link.clone().tag.into_inner(); let relevant_link_bytes = link_tag_bytes.to_vec(); @@ -88,7 +95,7 @@ pub async fn test_create_trust_atom() { let bucket = chunks[2]; assert_eq!(bucket.chars().count(), 9); - assert!(bucket.chars().all(|c| c.is_ascii_digit())); + assert!(bucket.chars().all(|c| c.is_digit(10))); let expected_entry_hash = "uhCEkto76kYgGIZMzU6AbEzCx1HMRNzurwPaOdF2utJaP-33mdcdN"; let expected_link_tag_string = format!( @@ -111,15 +118,15 @@ pub async fn test_create_trust_atom() { .call( &cell1.zome("trust_atom"), "test_helper_list_links_for_base", - target_hash.clone(), + target_entry_hash.clone(), ) .await; assert_eq!(backward_links.len(), 1); let link = &backward_links[0]; - // let agent_entry_hash = EntryHash::from(EntryHash::from(agent.clone())); - // assert_eq!(target_from_link, agent_entry_hash); + // let agent_entry_hash_b64 = EntryHashB64::from(EntryHash::from(agent.clone())); + // assert_eq!(target_from_link, agent_entry_hash_b64); let link_tag_bytes = link.clone().tag.into_inner(); let relevant_link_bytes = link_tag_bytes.to_vec(); @@ -145,251 +152,14 @@ pub async fn test_create_trust_atom() { assert_eq!(chunks[2], bucket); assert_eq!(chunks[3], expected_entry_hash); } -#[tokio::test(flavor = "multi_thread")] -pub async fn test_create_trust_atom_with_empty_chunks() { - let unicode_nul: &str = std::str::from_utf8(&[0]).unwrap(); - let (conductor, agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; - - // CREATE TARGET ENTRY - - let target_hash: EntryHash = conductor - .call( - &cell1.zome("trust_atom"), - "create_string_target", - "Nuka Sushi", - ) - .await; - - // CREATE TRUST ATOM - - let trust_atom_input = trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: None, - value: None, - extra: None, - }; - - let _result: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_input, - ) - .await; - - // CHECK FORWARD LINK - - let agent_address: EntryHash = agent.clone().into(); - - let forward_links: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "test_helper_list_links_for_base", - agent_address, - ) - .await; - - assert_eq!(forward_links.len(), 1); - let link = &forward_links[0]; - - let target_from_link = link.clone().target; - assert_eq!(target_from_link, AnyLinkableHash::from(target_hash.clone())); - - let link_tag_bytes = link.clone().tag.into_inner(); - let relevant_link_bytes = link_tag_bytes.to_vec(); - let relevant_link_string = String::from_utf8(relevant_link_bytes).unwrap(); - - let chunks: Vec<&str> = relevant_link_string.split(unicode_nul).collect(); - assert_eq!(chunks.len(), 4); - assert_eq!(chunks[0], "Ŧ→"); - assert_eq!(chunks[1], ""); - - let bucket = chunks[2]; - - assert_eq!(bucket.chars().count(), 9); - assert!(bucket.chars().all(|c| c.is_ascii_digit())); - - let expected_link_tag_string = format!( - "{}{}{}{}{}{}", - "Ŧ", "→", unicode_nul, unicode_nul, bucket, unicode_nul - ); - assert_eq!(relevant_link_string, expected_link_tag_string); - - // CHECK BACKWARD LINK - - let backward_links: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "test_helper_list_links_for_base", - target_hash.clone(), - ) - .await; - - assert_eq!(backward_links.len(), 1); - let link = &backward_links[0]; - - let link_tag_bytes = link.clone().tag.into_inner(); - let relevant_link_bytes = link_tag_bytes.to_vec(); - let relevant_link_string = String::from_utf8(relevant_link_bytes).unwrap(); - let expected_link_tag_string = format!( - "{}{}{}{}{}{}", - "Ŧ", "↩", unicode_nul, unicode_nul, bucket, unicode_nul - ); - assert_eq!(relevant_link_string, expected_link_tag_string); - - let chunks: Vec<&str> = relevant_link_string.split(unicode_nul).collect(); - assert_eq!(chunks.len(), 4); - assert_eq!(chunks[0], "Ŧ↩"); - assert_eq!(chunks[1], ""); - assert_eq!(chunks[2], bucket); -} - -#[tokio::test(flavor = "multi_thread")] -pub async fn test_delete_trust_atom() { - let (conductor, _agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; - - let target_hash: EntryHash = conductor - .call( - &cell1.zome("trust_atom"), - "create_string_target", - "Nuka Sushi", - ) - .await; - - // TRUST ATOM INPUT - - let content: String = "sushi".into(); - let value: String = ".8".into(); - let extra: BTreeMap = BTreeMap::new(); - - let target = AnyLinkableHash::from(target_hash.clone()); - - let trust_atom_input = trust_atom_types::TrustAtomInput { - target: target.clone(), - content: Some(content.clone()), - value: Some(value.clone()), - extra: Some(extra.clone()), - }; - - // CREATE 2 TRUST ATOMS - - let _result: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_input.clone(), - ) - .await; - - let _result: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_input, - ) - .await; - - // SANITY CHECK: 2 "FORWARD" TRUST ATOMS EXIST - - let trust_atom_links: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query", - trust_atom_types::QueryInput { - source: None, - target: Some(target.clone()), - content_full: None, - content_starts_with: None, - content_not_starts_with: None, - value_starts_with: None, - }, - ) - .await; - - assert_eq!(trust_atom_links.len(), 2); - - // SANITY CHECK: 2 "BACKWARD" TRUST ATOMS EXIST - - let trust_atom_links: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query", - trust_atom_types::QueryInput { - source: Some(target.clone()), - target: None, - content_full: None, - content_starts_with: None, - content_not_starts_with: None, - value_starts_with: None, - }, - ) - .await; - - assert_eq!(trust_atom_links.len(), 2); - - // DELETE TRUST ATOM - - let delete_report: DeleteReport = conductor - .call( - &cell1.zome("trust_atom"), - "delete_trust_atoms", - target.clone(), - ) - .await; - - assert_eq!(delete_report.trust_atoms_deleted, 2); - assert_eq!(delete_report.forward_links_deleted, 2); - assert_eq!(delete_report.backward_links_deleted, 2); - - // SHOULD BE ZERO "FORWARD" TRUST ATOMS - - let trust_atom_links: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query", - trust_atom_types::QueryInput { - source: None, - target: Some(target.clone()), - content_full: None, - content_starts_with: None, - content_not_starts_with: None, - value_starts_with: None, - }, - ) - .await; - - assert_eq!(trust_atom_links.len(), 0); - - // SHOULD BE ZERO "BACKWARD" TRUST ATOMS - - let trust_atom_links: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query", - trust_atom_types::QueryInput { - source: Some(target.clone()), - target: None, - content_full: None, - content_starts_with: None, - content_not_starts_with: None, - value_starts_with: None, - }, - ) - .await; - - assert_eq!(trust_atom_links.len(), 0); -} #[tokio::test(flavor = "multi_thread")] pub async fn test_query_mine() { - let (conductor, agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; + let (conductor, agent, cell1) = setup_1_conductor().await; // CREATE TARGET ENTRY - let target_hash: EntryHash = conductor + let target_entry_hash: EntryHash = conductor .call( &cell1.zome("trust_atom"), "create_string_target", @@ -399,12 +169,12 @@ pub async fn test_query_mine() { // CREATE TRUST ATOMS - let _result: trust_atom_types::TrustAtom = conductor + let _result: TrustAtom = conductor .call( &cell1.zome("trust_atom"), "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), + TrustAtomInput { + target: target_entry_hash.clone(), content: Some("sushi".to_string()), value: Some("0.8".to_string()), extra: Some(BTreeMap::new()), @@ -417,14 +187,13 @@ pub async fn test_query_mine() { // QUERY MY TRUST ATOMS - let trust_atoms_from_query: Vec = conductor + let trust_atoms_from_query: Vec = conductor .call( &cell1.zome("trust_atom"), "query_mine", - trust_atom_types::QueryMineInput { + QueryMineInput { target: None, content_starts_with: None, - content_not_starts_with: None, content_full: None, value_starts_with: None, }, @@ -433,17 +202,19 @@ pub async fn test_query_mine() { assert_eq!(trust_atoms_from_query.len(), 1); - // let source_hash = EntryHash::from(EntryHash::from(agent.clone())); - // let target_hash = EntryHash::from(target_hash); + let source_entry_hash_b64 = EntryHashB64::from(EntryHash::from(agent.clone())); + let target_entry_hash_b64 = EntryHashB64::from(target_entry_hash); let trust_atom = &trust_atoms_from_query[0]; assert_eq!( *trust_atom, - trust_atom_types::TrustAtom { - source_hash: AnyLinkableHash::from(agent.clone()), - target_hash: AnyLinkableHash::from(target_hash), + TrustAtom { + source: source_entry_hash_b64.to_string(), + target: target_entry_hash_b64.to_string(), content: Some("sushi".to_string()), value: Some(".800000000".to_string()), + source_entry_hash: source_entry_hash_b64, + target_entry_hash: target_entry_hash_b64, extra: Some(BTreeMap::new()), } ); @@ -455,7 +226,7 @@ pub async fn test_query_mine_with_content_starts_with() { // CREATE TARGET ENTRY - let target_hash: EntryHash = conductor + let target_entry_hash: EntryHash = conductor .call( &cell1.zome("trust_atom"), "create_string_target", @@ -468,12 +239,12 @@ pub async fn test_query_mine_with_content_starts_with() { let contents = vec!["sushi", "sushi joint", "sush"]; for content in contents { - let _result: trust_atom_types::TrustAtom = conductor + let _result: TrustAtom = conductor .call( &cell1.zome("trust_atom"), "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), + TrustAtomInput { + target: target_entry_hash.clone(), content: Some(content.into()), value: Some("0.8".into()), extra: Some(BTreeMap::new()), @@ -483,15 +254,14 @@ pub async fn test_query_mine_with_content_starts_with() { } // QUERY MY TRUST ATOMS - let trust_atoms_from_query: Vec = conductor + let trust_atoms_from_query: Vec = conductor .call( &cell1.zome("trust_atom"), "query_mine", - trust_atom_types::QueryMineInput { + QueryMineInput { target: None, content_full: None, content_starts_with: Some("sushi".into()), - content_not_starts_with: None, value_starts_with: None, // value_starts_with: Some("0.0".into()), }, @@ -512,90 +282,13 @@ pub async fn test_query_mine_with_content_starts_with() { ); } -#[tokio::test(flavor = "multi_thread")] -pub async fn test_query_mine_with_content_not_starts_with() { - let (conductor, _agent, cell1) = setup_1_conductor().await; - - // CREATE TARGET ENTRY - - let target_hash: EntryHash = conductor - .call( - &cell1.zome("trust_atom"), - "create_string_target", - "Sushi Ran", - ) - .await; - - // CREATE TRUST ATOMS - - let contents = vec![ - "sushi", - "sushi __not joint", - "sush not__", - "__not_example", // should be matched - "__ starts", // should be matched - "reg_example", - ]; - - for content in contents { - let _result: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: Some(content.into()), - value: Some("0.8".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; - } - // QUERY MY TRUST ATOMS - - let trust_atoms_from_query: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query_mine", - trust_atom_types::QueryMineInput { - target: None, - content_full: None, - content_starts_with: None, - content_not_starts_with: Some("__".into()), - value_starts_with: None, - }, - ) - .await; - - assert_eq!(trust_atoms_from_query.len(), 4); - - let mut actual = [ - trust_atoms_from_query[0].clone().content, - trust_atoms_from_query[1].clone().content, - trust_atoms_from_query[2].clone().content, - trust_atoms_from_query[3].clone().content, - ]; - actual.sort(); - - assert_eq!( - actual, - [ - Some("reg_example".to_string()), - Some("sush not__".to_string()), - Some("sushi".to_string()), - Some("sushi __not joint".to_string()), - ] - ); -} - #[tokio::test(flavor = "multi_thread")] pub async fn test_query_mine_with_content_full() { - let (conductor, _agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; + let (conductor, _agent, cell1) = setup_1_conductor().await; // CREATE TARGET ENTRY - let target_hash: EntryHash = conductor + let target_entry_hash: EntryHash = conductor .call( &cell1.zome("trust_atom"), "create_string_target", @@ -608,12 +301,12 @@ pub async fn test_query_mine_with_content_full() { let content_fulls = vec!["sushi", "sushi joint", "sush"]; for content_full in content_fulls { - let _result: trust_atom_types::TrustAtom = conductor + let _result: TrustAtom = conductor .call( &cell1.zome("trust_atom"), "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), + TrustAtomInput { + target: target_entry_hash.clone(), content: Some(content_full.into()), value: Some("0.8".into()), extra: Some(BTreeMap::new()), @@ -621,18 +314,16 @@ pub async fn test_query_mine_with_content_full() { ) .await; } - // QUERY MY TRUST ATOMS - let trust_atoms_from_query: Vec = conductor + let trust_atoms_from_query: Vec = conductor .call( &cell1.zome("trust_atom"), "query_mine", - trust_atom_types::QueryMineInput { + QueryMineInput { target: None, content_full: Some("sushi".into()), content_starts_with: None, - content_not_starts_with: None, value_starts_with: None, // value_starts_with: Some("0.0".into()), }, @@ -647,200 +338,49 @@ pub async fn test_query_mine_with_content_full() { ); } -#[tokio::test(flavor = "multi_thread")] -pub async fn test_query_mine_with_content_full_and_value_starts_with() { - let (conductor, _agent, cell1) = setup_1_conductor().await; - - // CREATE TARGET ENTRY - - let target_hash: EntryHash = conductor - .call( - &cell1.zome("trust_atom"), - "create_string_target", - "Sushi Ran", - ) - .await; - - // CREATE TRUST ATOMS - - let _trust_atom_1: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: None, - value: Some("0.88".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; - - let _trust_atom_2: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: Some("sushi".into()), - value: Some("0.71".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; - - let _trust_atom_3: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: Some("sushi".into()), - value: Some("0.7".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; +// TESTING UTILITY FUNCTIONS - // QUERY MY TRUST ATOMS +async fn setup_1_conductor() -> (SweetConductor, AgentPubKey, SweetCell) { + let dna = SweetDnaFile::from_bundle(std::path::Path::new(DNA_FILEPATH)) + .await + .unwrap(); - let trust_atoms_from_query: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query_mine", - trust_atom_types::QueryMineInput { - target: None, - content_full: Some("sushi".into()), - content_starts_with: None, - content_not_starts_with: None, - value_starts_with: Some(".7".into()), - }, - ) - .await; + let mut conductor = SweetConductor::from_standard_config().await; - assert_eq!(trust_atoms_from_query.len(), 2); + let agent = SweetAgents::one(conductor.keystore()).await; + let app1 = conductor + .setup_app_for_agent("app", agent.clone(), &[dna.clone()]) + .await + .unwrap(); - let mut actual = [ - trust_atoms_from_query[0].clone().value, - trust_atoms_from_query[1].clone().value, - ]; - actual.sort(); + let cell1 = app1.into_cells()[0].clone(); - assert_eq!( - actual, - [ - Some(".700000000".to_string()), - Some(".710000000".to_string()), - ] - ); + (conductor, agent, cell1) } -#[tokio::test(flavor = "multi_thread")] -pub async fn test_query_mine_with_value_starts_with() { - let (conductor, _agent, cell1) = setup_1_conductor().await; - - // CREATE TARGET ENTRY - - let target_hash: EntryHash = conductor - .call( - &cell1.zome("trust_atom"), - "create_string_target", - "Sushi Ran", - ) - .await; - - // CREATE TRUST ATOMS - - let _trust_atom_1: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: None, - value: Some("0.88".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; - - let _trust_atom_2: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: None, - value: Some("0.81".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; - - let _trust_atom_3: trust_atom_types::TrustAtom = conductor - .call( - &cell1.zome("trust_atom"), - "create_trust_atom", - trust_atom_types::TrustAtomInput { - target: AnyLinkableHash::from(target_hash.clone()), - content: None, - value: Some("0.7".into()), - extra: Some(BTreeMap::new()), - }, - ) - .await; - - // let _links: Vec = conductor - // .call( - // &cell1.zome("trust_atom"), - // "test_helper_list_links_for_base", - // target_hash, - // ) - // .await; - - // for link in links { - // println!("{:#?}", String::from_utf8(link.tag.into_inner())); - // } - - // QUERY MY TRUST ATOMS - - let trust_atoms_from_query: Vec = conductor - .call( - &cell1.zome("trust_atom"), - "query_mine", - trust_atom_types::QueryMineInput { - target: None, - content_full: None, - content_starts_with: None, - content_not_starts_with: None, - value_starts_with: Some(".8".into()), - }, - ) - .await; +pub async fn setup_conductors(n: usize) -> (SweetConductorBatch, Vec, SweetAppBatch) { + let dna = SweetDnaFile::from_bundle(std::path::Path::new(DNA_FILEPATH)) + .await + .unwrap(); - assert_eq!(trust_atoms_from_query.len(), 2); + let mut conductors = SweetConductorBatch::from_standard_config(n).await; - let mut actual = [ - trust_atoms_from_query[0].clone().value, - trust_atoms_from_query[1].clone().value, - ]; - actual.sort(); + let all_agents: Vec = + future::join_all(conductors.iter().map(|c| SweetAgents::one(c.keystore()))).await; + let apps = conductors + .setup_app_for_zipped_agents("app", &all_agents, &[dna]) + .await + .unwrap(); - assert_eq!( - actual, - [ - Some(".810000000".to_string()), - Some(".880000000".to_string()) - ] - ); + conductors.exchange_peer_info().await; + (conductors, all_agents, apps) } #[tokio::test(flavor = "multi_thread")] pub async fn test_get_extra() { - let (conductor, _agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; + let (conductor, _agent, cell1) = setup_1_conductor().await; - let target_hash = conductor + let target_entry_hash = conductor .call( &cell1.zome("trust_atom"), "create_string_target", @@ -848,8 +388,8 @@ pub async fn test_get_extra() { ) .await; - let mock_input = trust_atom_types::TrustAtomInput { - target: target_hash, + let mock_input = TrustAtomInput { + target: target_entry_hash, content: Some("sushi".to_string()), value: Some("0.9871".to_string()), extra: Some(BTreeMap::from([ @@ -864,7 +404,7 @@ pub async fn test_get_extra() { ])), }; - let _mock_trust_atom: trust_atom_types::TrustAtom = conductor + let _mock_trust_atom: TrustAtom = conductor .call( &cell1.zome("trust_atom"), "create_trust_atom", @@ -872,14 +412,15 @@ pub async fn test_get_extra() { ) .await; - let mock_entry = trust_atom_integrity::entries::Extra { - fields: mock_input.extra.unwrap(), + let mock_entry = Extra { + content_overflow: None, + extra_fields: mock_input.extra }; let mock_extra_entry_hash: EntryHash = conductor .call(&cell1.zome("trust_atom"), "calc_extra_hash", mock_entry) .await; - let mock_extra_data: trust_atom_integrity::entries::Extra = conductor + let mock_extra_data: Extra = conductor .call( &cell1.zome("trust_atom"), "get_extra", @@ -888,11 +429,11 @@ pub async fn test_get_extra() { .await; let field1 = mock_extra_data - .fields + .extra_fields .get_key_value(&"extra_stuff".to_string()) .unwrap(); let field2 = mock_extra_data - .fields + .extra_fields .get_key_value(&"another_thing".to_string()) .unwrap(); @@ -911,109 +452,3 @@ pub async fn test_get_extra() { ) ); } - -#[tokio::test(flavor = "multi_thread")] -pub async fn test_get_entry_by_actionhash() { - let (conductor, _agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = - setup_1_conductor().await; - - let test_entry = trust_atom_integrity::entries::Example { - example_field: "test".to_string(), - }; - - let action_hash: ActionHash = conductor - .call(&cell1.zome("trust_atom"), "create_test_entry", test_entry) - .await; - - let retrieval: trust_atom_integrity::entries::Example = conductor - .call( - &cell1.zome("trust_atom"), - "test_get_entry_by_action", - action_hash, - ) - .await; - assert_eq!("test".to_string(), retrieval.example_field); -} - -// #[tokio::test(flavor = "multi_thread")] -// pub async fn test_fetch_external() { -// let (conductor, agent, cell1): (SweetConductor, AgentPubKey, SweetCell) = setup_1_conductor().await; - -// //// WIP - -// let ipfs_address = ExternalHash::from("https://ipfs.io/ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu".to_string()); -// let hc_linked_ipfs_address = AnyLinkableHash::from(ipfs_address.clone()); - -// let mock_input = TrustAtomInput { -// target: AnyLinkableHash::from(ipfs_address), -// content: Some("ipfs".to_string()), -// value: None, -// extra: None, -// }; - -// let mock_trust_atom: TrustAtom = conductor -// .call( -// &cell1.zome("trust_atom"), -// "create_trust_atom", -// mock_input.clone(), -// ) -// .await; - -// let external_link = get_links(AnyLinkableHash::from(ipfs_address), None); - -// let fetched_atom = convert_link_to_trust_atom(external_link, LinkDirection::Reverse, AnyLinkableHash::from(agent))?; - -// println!("external_link: {:?}", external_link); -// assert_eq!(mock_trust_atom, fetched_atom); -// assert_eq!(fetched_atom.target, ipfs_address); -// } - -// TESTING UTILITY FUNCTIONS - -async fn setup_1_conductor() -> (SweetConductor, AgentPubKey, SweetCell) { - let dna = SweetDnaFile::from_bundle(std::path::Path::new(DNA_FILEPATH)) - .await - .unwrap(); - - let mut conductor = SweetConductor::from_standard_config().await; - - let holo_core_agent = SweetAgents::one(conductor.keystore()).await; - let app1 = conductor - .setup_app_for_agent("app", holo_core_agent.clone(), &[dna.clone()]) - .await - .unwrap(); - - let cell1 = app1.into_cells()[0].clone(); - - let agent_hash = holo_core_agent.into_inner(); - let agent = AgentPubKey::from_raw_39(agent_hash).unwrap(); - - (conductor, agent, cell1) -} - -pub async fn setup_conductors(n: usize) -> (SweetConductorBatch, Vec, SweetAppBatch) { - let dna = SweetDnaFile::from_bundle(std::path::Path::new(DNA_FILEPATH)) - .await - .unwrap(); - - let mut conductors = SweetConductorBatch::from_standard_config(n).await; - - let all_agents1: Vec = - future::join_all(conductors.iter().map(|c| SweetAgents::one(c.keystore()))).await; - - let all_agents2: Vec = all_agents1 - .iter() - .map(|holo_core_agent| { - let agent_hash = holo_core_agent.clone().into_inner(); - AgentPubKey::from_raw_39(agent_hash).unwrap() - }) - .collect(); - - let apps = conductors - .setup_app_for_zipped_agents("app", &all_agents1, &[dna]) - .await - .unwrap(); - - conductors.exchange_peer_info().await; - (conductors, all_agents2, apps) -}