All basic functions plus guide README to use

This commit is contained in:
Malte Schröder
2025-12-19 20:25:39 +01:00
parent db48f07b78
commit 397635d43e
12 changed files with 486 additions and 22 deletions

93
src/bin/testall.rs Normal file
View File

@@ -0,0 +1,93 @@
use easy_nostr::EasyNostr;
use nostr_sdk::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("=== EasyNostr Production Test Suite ===");
// 1. Identity
let keys = Keys::generate();
let nsec = keys.secret_key().to_bech32()?;
let npub = keys.public_key().to_bech32()?;
println!("[INFO] Identity: {}", npub);
// 2. Init
let ez = EasyNostr::new(&nsec).await?;
// 3. Relays (More reliable set)
println!("\n[STEP 1] Connecting to Relays...");
let relays = vec![
"wss://relay.damus.io",
"wss://nos.lol",
"wss://relay.primal.net",
];
ez.add_relays(relays).await?;
// Give relays a moment to fully welcome us
sleep(Duration::from_secs(2)).await;
println!(" [OK] Connected.");
// 4. Follow Self (Critical for Timeline Test reliability)
println!("\n[STEP 2] Following Self (for robust timeline testing)...");
ez.create_contact(&npub, Some("Me Myself".to_string()))
.await?;
println!(" [OK] Followed self.");
// 5. Publish Post
println!("\n[STEP 3] Publishing Post...");
let secret_code = format!("Test Run {}", Timestamp::now().as_secs());
let event_id = ez
.post_text(&format!("Hello Nostr! {}", secret_code))
.await?;
println!(" [OK] Posted: {}", event_id.to_bech32()?);
// 6. Verify Timeline (with Retry)
println!("\n[STEP 4] verifying Timeline (expecting own post)...");
let mut found_post = false;
for i in 1..=5 {
sleep(Duration::from_secs(2)).await; // Wait for propagation
let posts = ez.get_timeline(vec![npub.clone()]).await?;
// Look for our specific post
if posts.iter().any(|p| p.content.contains(&secret_code)) {
println!(" [OK] Found our post in timeline after {}s!", i * 2);
found_post = true;
break;
} else {
println!(" ... attempt {}/5: Post not yet visible...", i);
}
}
if !found_post {
println!(" [ERR] Verified Failed: Post did not appear in timeline.");
}
// 7. DM Test (with Retry)
println!("\n[STEP 5] Testing Direct Messages...");
let dm_msg = format!("Secret DM {}", secret_code);
ez.send_private_message(&npub, &dm_msg).await?;
let mut found_dm = false;
for i in 1..=5 {
sleep(Duration::from_secs(2)).await;
let dms = ez.get_private_messages(&npub).await?;
if dms.iter().any(|m| m.content == dm_msg) {
println!(" [OK] Found DM after {}s!", i * 2);
found_dm = true;
break;
} else {
println!(" ... attempt {}/5: DM not yet visible...", i);
}
}
if !found_dm {
println!(" [ERR] DM propagation failed.");
// Non-fatal, DMs are slower
}
println!("\n=== Test Complete ===");
Ok(())
}