use ant_logging::LogBuilder;
use autonomi::{
Client,
client::{
graph::{GraphEntry, GraphError},
payment::PaymentOption,
},
};
use eyre::Result;
use serial_test::serial;
use test_utils::evm::get_funded_wallet;
#[tokio::test]
#[serial]
async fn graph_entry_put() -> Result<()> {
let _log_appender_guard = LogBuilder::init_single_threaded_tokio_test();
let client = Client::init_local().await?;
let wallet = get_funded_wallet();
let key = bls::SecretKey::random();
let content = [0u8; 32];
let graph_entry = GraphEntry::new(&key, vec![], content, vec![]);
let cost = client.graph_entry_cost(&key.public_key()).await?;
println!("graph_entry cost: {cost}");
let payment_option = PaymentOption::from(&wallet);
client
.graph_entry_put(graph_entry.clone(), payment_option)
.await?;
println!("graph_entry put 1");
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
let txs = client.graph_entry_get(&graph_entry.address()).await?;
assert_eq!(txs, graph_entry.clone());
println!("graph_entry got 1");
let content2 = [1u8; 32];
let graph_entry2 = GraphEntry::new(&key, vec![], content2, vec![]);
let payment_option = PaymentOption::from(&wallet);
let res = client
.graph_entry_put(graph_entry2.clone(), payment_option)
.await;
assert!(matches!(
res,
Err(GraphError::AlreadyExists(address))
if address == graph_entry2.address()
));
let key3 = bls::SecretKey::random();
let graph_entry3 = GraphEntry::new(&key3, vec![graph_entry.owner], content2, vec![]);
let payment_option = PaymentOption::from(&wallet);
client
.graph_entry_put(graph_entry3.clone(), payment_option)
.await?;
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
let txs = client.graph_entry_get(&graph_entry3.address()).await?;
assert_eq!(txs, graph_entry3.clone());
println!("graph_entry got 2");
Ok(())
}