From 49bc478fffc63a3cad0be194c6c45a868e432691 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 22 Mar 2024 15:51:40 +0000 Subject: update tag generation to reflect changes in rust-nostr --- tests/list.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'tests/list.rs') diff --git a/tests/list.rs b/tests/list.rs index 4f55645..ee5e1dc 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -190,7 +190,7 @@ mod cannot_find_repo_event { input.succeeds_with( &Coordinate { kind: nostr::Kind::Custom(REPOSITORY_KIND), - pubkey: TEST_KEY_1_KEYS.public_key(), + public_key: TEST_KEY_1_KEYS.public_key(), identifier: repo_event.identifier().unwrap().to_string(), relays: vec!["ws://localhost:8056".to_string()], } @@ -1621,7 +1621,10 @@ mod when_main_branch_is_uptodate { vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]), + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), ], Some(Duration::from_millis(500)), )?; @@ -1748,7 +1751,10 @@ mod when_main_branch_is_uptodate { vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]), + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), ], Some(Duration::from_millis(500)), )?; -- cgit v1.2.3 From 71606283bf21f0fb4931850cd69d97344dca521e Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 22 Mar 2024 15:51:42 +0000 Subject: replace blocking client in tests with async as it has been removed from nostr_sdk --- Cargo.lock | 1 - test_utils/Cargo.toml | 2 +- tests/list.rs | 235 ++++++++++++++++++++++++++------------------------ tests/pull.rs | 168 ++++++++++++++++++------------------ 4 files changed, 209 insertions(+), 197 deletions(-) (limited to 'tests/list.rs') diff --git a/Cargo.lock b/Cargo.lock index 9965f63..22927d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1837,7 +1837,6 @@ dependencies = [ "nostr-signer", "nostr-zapper", "nwc", - "once_cell", "thiserror", "tokio", "tracing", diff --git a/test_utils/Cargo.toml b/test_utils/Cargo.toml index af25b43..e07e411 100644 --- a/test_utils/Cargo.toml +++ b/test_utils/Cargo.toml @@ -10,7 +10,7 @@ dialoguer = "0.10.4" directories = "5.0.1" git2 = "0.18.1" nostr = "0.29.0" -nostr-sdk = { version = "0.29.0", features = ["blocking"] } +nostr-sdk = "0.29.0" once_cell = "1.18.0" rand = "0.8" rexpect = { git = "https://github.com/rust-cli/rexpect.git", rev = "9eb61dd" } diff --git a/tests/list.rs b/tests/list.rs index ee5e1dc..8252448 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -1,6 +1,6 @@ use anyhow::Result; use futures::join; -use nostr_sdk::client::blocking::Client; +use nostr_sdk::client::Client; use serial_test::serial; use test_utils::{git::GitTestRepo, relay::Relay, *}; @@ -1589,6 +1589,8 @@ mod when_main_branch_is_uptodate { mod when_latest_revision_rebases_branch { use std::time::Duration; + use tokio::{runtime::Handle, task::JoinHandle}; + use super::*; async fn prep_and_run() -> Result<(GitTestRepo, GitTestRepo)> { @@ -1609,26 +1611,28 @@ mod when_main_branch_is_uptodate { r55.events.push(generate_test_key_1_metadata_event("fred")); r55.events.push(generate_test_key_1_relay_list_event()); - let cli_tester_handle = std::thread::spawn( - move || -> Result<(GitTestRepo, GitTestRepo)> { + let cli_tester_handle: JoinHandle> = + tokio::task::spawn_blocking(move || { // create 3 proposals let _ = cli_tester_create_proposals()?; // get proposal id of first - let client = Client::new(&nostr::Keys::generate()); - client.add_relay("ws://localhost:8055")?; - client.connect_relay("ws://localhost:8055")?; - let proposals = client.get_events_of( + // get proposal id of first + let client = Client::default(); + Handle::current().block_on(client.add_relay("ws://localhost:8055"))?; + Handle::current() + .block_on(client.connect_relay("ws://localhost:8055"))?; + let proposals = Handle::current().block_on(client.get_events_of( vec![ - nostr::Filter::default() - .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag( - nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), - vec!["root"], - ), - ], + nostr::Filter::default() + .kind(nostr::Kind::Custom(PATCH_KIND)) + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), + ], Some(Duration::from_millis(500)), - )?; - client.disconnect()?; + ))?; + Handle::current().block_on(client.disconnect())?; let proposal_1_id = proposals .iter() @@ -1702,8 +1706,7 @@ mod when_main_branch_is_uptodate { relay::shutdown_relay(8000 + p)?; } Ok((second_originating_repo, test_repo)) - }, - ); + }); // launch relay let _ = join!( @@ -1713,7 +1716,7 @@ mod when_main_branch_is_uptodate { r55.listen_until_close(), r56.listen_until_close(), ); - let res = cli_tester_handle.join().unwrap()?; + let res = cli_tester_handle.await??; Ok(res) } @@ -1740,99 +1743,105 @@ mod when_main_branch_is_uptodate { r55.events.push(generate_test_key_1_metadata_event("fred")); r55.events.push(generate_test_key_1_relay_list_event()); - let cli_tester_handle = std::thread::spawn(move || -> Result<()> { - // create 3 proposals - let _ = cli_tester_create_proposals()?; - // get proposal id of first - let client = Client::new(&nostr::Keys::generate()); - client.add_relay("ws://localhost:8055")?; - client.connect_relay("ws://localhost:8055")?; - let proposals = client.get_events_of( - vec![ - nostr::Filter::default() - .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag( - nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), - vec!["root"], - ), - ], - Some(Duration::from_millis(500)), - )?; - client.disconnect()?; - - let proposal_1_id = proposals - .iter() - .find(|e| { - e.tags - .iter() - .any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1)) - }) - .unwrap() - .id; - // recreate proposal 1 on top of a another commit (like a rebase on top - // of one extra commit) - let second_originating_repo = GitTestRepo::default(); - second_originating_repo.populate()?; - std::fs::write( - second_originating_repo.dir.join("amazing.md"), - "some content", - )?; - second_originating_repo - .stage_and_commit("commit for rebasing on top of")?; - cli_tester_create_proposal( - &second_originating_repo, - FEATURE_BRANCH_NAME_1, - "a", - Some((PROPOSAL_TITLE_1, "proposal a description")), - Some(proposal_1_id.to_string()), - )?; - - // pretend we have downloaded the origianl version of the first proposal - let test_repo = GitTestRepo::default(); - test_repo.populate()?; - create_and_populate_branch( - &test_repo, - FEATURE_BRANCH_NAME_1, - "a", - false, - )?; - // pretend we have pulled the updated main branch - test_repo.checkout("main")?; - std::fs::write(test_repo.dir.join("amazing.md"), "some content")?; - test_repo.stage_and_commit("commit for rebasing on top of")?; - - let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); - p.expect("finding proposals...\r\n")?; - let mut c = p.expect_choice( - "all proposals", - vec![ - format!("\"{PROPOSAL_TITLE_3}\""), - format!("\"{PROPOSAL_TITLE_2}\""), - format!("\"{PROPOSAL_TITLE_1}\""), - ], - )?; - c.succeeds_with(2, true, None)?; - p.expect("finding commits...\r\n")?; - p.expect("updated proposal available (2 ahead 0 behind 'main'). existing version is 2 ahead 1 behind 'main'\r\n")?; - let mut c = p.expect_choice( - "", - vec![ - format!("checkout and overwrite existing proposal branch"), - format!("checkout existing outdated proposal branch"), - format!("apply to current branch with `git am`"), - format!("download to ./patches"), - format!("back"), - ], - )?; - c.succeeds_with(0, false, Some(0))?; - p.expect("checked out new version of proposal (2 ahead 0 behind 'main'), replacing old version (2 ahead 1 behind 'main')\r\n")?; - p.expect_end()?; - - for p in [51, 52, 53, 55, 56] { - relay::shutdown_relay(8000 + p)?; - } - Ok(()) - }); + let cli_tester_handle: JoinHandle> = tokio::task::spawn_blocking( + move || { + // create 3 proposals + let _ = cli_tester_create_proposals()?; + // get proposal id of first + let client = Client::default(); + Handle::current() + .block_on(client.add_relay("ws://localhost:8055"))?; + Handle::current() + .block_on(client.connect_relay("ws://localhost:8055"))?; + let proposals = + Handle::current().block_on(client.get_events_of( + vec![ + nostr::Filter::default() + .kind(nostr::Kind::Custom(PATCH_KIND)) + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), + ], + Some(Duration::from_millis(500)), + ))?; + Handle::current().block_on(client.disconnect())?; + + let proposal_1_id = proposals + .iter() + .find(|e| { + e.tags + .iter() + .any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1)) + }) + .unwrap() + .id; + // recreate proposal 1 on top of a another commit (like a rebase on + // top of one extra commit) + let second_originating_repo = GitTestRepo::default(); + second_originating_repo.populate()?; + std::fs::write( + second_originating_repo.dir.join("amazing.md"), + "some content", + )?; + second_originating_repo + .stage_and_commit("commit for rebasing on top of")?; + cli_tester_create_proposal( + &second_originating_repo, + FEATURE_BRANCH_NAME_1, + "a", + Some((PROPOSAL_TITLE_1, "proposal a description")), + Some(proposal_1_id.to_string()), + )?; + + // pretend we have downloaded the origianl version of the first + // proposal + let test_repo = GitTestRepo::default(); + test_repo.populate()?; + create_and_populate_branch( + &test_repo, + FEATURE_BRANCH_NAME_1, + "a", + false, + )?; + // pretend we have pulled the updated main branch + test_repo.checkout("main")?; + std::fs::write(test_repo.dir.join("amazing.md"), "some content")?; + test_repo.stage_and_commit("commit for rebasing on top of")?; + + let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); + p.expect("finding proposals...\r\n")?; + let mut c = p.expect_choice( + "all proposals", + vec![ + format!("\"{PROPOSAL_TITLE_3}\""), + format!("\"{PROPOSAL_TITLE_2}\""), + format!("\"{PROPOSAL_TITLE_1}\""), + ], + )?; + c.succeeds_with(2, true, None)?; + p.expect("finding commits...\r\n")?; + p.expect("updated proposal available (2 ahead 0 behind 'main'). existing version is 2 ahead 1 behind 'main'\r\n")?; + let mut c = p.expect_choice( + "", + vec![ + format!("checkout and overwrite existing proposal branch"), + format!("checkout existing outdated proposal branch"), + format!("apply to current branch with `git am`"), + format!("download to ./patches"), + format!("back"), + ], + )?; + c.succeeds_with(0, false, Some(0))?; + p.expect("checked out new version of proposal (2 ahead 0 behind 'main'), replacing old version (2 ahead 1 behind 'main')\r\n")?; + p.expect_end()?; + + for p in [51, 52, 53, 55, 56] { + relay::shutdown_relay(8000 + p)?; + } + Ok(()) + }, + ); // launch relay let _ = join!( @@ -1842,7 +1851,7 @@ mod when_main_branch_is_uptodate { r55.listen_until_close(), r56.listen_until_close(), ); - cli_tester_handle.join().unwrap()?; + cli_tester_handle.await??; println!("{:?}", r55.events); Ok(()) } diff --git a/tests/pull.rs b/tests/pull.rs index 853f518..bf132e7 100644 --- a/tests/pull.rs +++ b/tests/pull.rs @@ -712,7 +712,8 @@ mod when_branch_is_checked_out { mod when_latest_event_rebases_branch { use std::time::Duration; - use nostr_sdk::blocking::Client; + use nostr_sdk::Client; + use tokio::{runtime::Handle, task::JoinHandle}; use super::*; @@ -734,26 +735,26 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_metadata_event("fred")); r55.events.push(generate_test_key_1_relay_list_event()); - let cli_tester_handle = - std::thread::spawn(move || -> Result<(GitTestRepo, GitTestRepo)> { + let cli_tester_handle: JoinHandle> = + tokio::task::spawn_blocking(move || { // create 3 proposals let _ = cli_tester_create_proposals()?; // get proposal id of first - let client = Client::new(&nostr::Keys::generate()); - client.add_relay("ws://localhost:8055")?; - client.connect_relay("ws://localhost:8055")?; - let proposals = client.get_events_of( + let client = Client::default(); + Handle::current().block_on(client.add_relay("ws://localhost:8055"))?; + Handle::current().block_on(client.connect_relay("ws://localhost:8055"))?; + let proposals = Handle::current().block_on(client.get_events_of( vec![ - nostr::Filter::default() - .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag( - nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), - vec!["root"], - ), - ], + nostr::Filter::default() + .kind(nostr::Kind::Custom(PATCH_KIND)) + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), + ], Some(Duration::from_millis(500)), - )?; - client.disconnect()?; + ))?; + Handle::current().block_on(client.disconnect())?; let proposal_1_id = proposals .iter() @@ -808,7 +809,7 @@ mod when_branch_is_checked_out { r55.listen_until_close(), r56.listen_until_close(), ); - let res = cli_tester_handle.join().unwrap()?; + let res = cli_tester_handle.await??; Ok(res) } @@ -835,72 +836,75 @@ mod when_branch_is_checked_out { r55.events.push(generate_test_key_1_metadata_event("fred")); r55.events.push(generate_test_key_1_relay_list_event()); - let cli_tester_handle = std::thread::spawn(move || -> Result<()> { - // create 3 proposals - let _ = cli_tester_create_proposals()?; - // get proposal id of first - let client = Client::new(&nostr::Keys::generate()); - client.add_relay("ws://localhost:8055")?; - client.connect_relay("ws://localhost:8055")?; - let proposals = client.get_events_of( - vec![ - nostr::Filter::default() - .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag( - nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), - vec!["root"], - ), - ], - Some(Duration::from_millis(500)), - )?; - client.disconnect()?; - - let proposal_1_id = proposals - .iter() - .find(|e| { - e.tags - .iter() - .any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1)) - }) - .unwrap() - .id; - // recreate proposal 1 on top of a another commit (like a rebase on top - // of one extra commit) - let second_originating_repo = GitTestRepo::default(); - second_originating_repo.populate()?; - std::fs::write( - second_originating_repo.dir.join("amazing.md"), - "some content", - )?; - second_originating_repo.stage_and_commit("commit for rebasing on top of")?; - cli_tester_create_proposal( - &second_originating_repo, - FEATURE_BRANCH_NAME_1, - "a", - Some((PROPOSAL_TITLE_1, "proposal a description")), - Some(proposal_1_id.to_string()), - )?; - - // pretend we have downloaded the origianl version of the first proposal - let test_repo = GitTestRepo::default(); - test_repo.populate()?; - create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?; - // pretend we have pulled the updated main branch - test_repo.checkout("main")?; - std::fs::write(test_repo.dir.join("amazing.md"), "some content")?; - test_repo.stage_and_commit("commit for rebasing on top of")?; - test_repo.checkout(FEATURE_BRANCH_NAME_1)?; + let cli_tester_handle: JoinHandle> = tokio::task::spawn_blocking( + move || { + // create 3 proposals + let _ = cli_tester_create_proposals()?; + // get proposal id of first + let client = Client::default(); + Handle::current().block_on(client.add_relay("ws://localhost:8055"))?; + Handle::current().block_on(client.connect_relay("ws://localhost:8055"))?; + let proposals = Handle::current().block_on(client.get_events_of( + vec![ + nostr::Filter::default() + .kind(nostr::Kind::Custom(PATCH_KIND)) + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), + ], + Some(Duration::from_millis(500)), + ))?; + Handle::current().block_on(client.disconnect())?; + + let proposal_1_id = proposals + .iter() + .find(|e| { + e.tags + .iter() + .any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1)) + }) + .unwrap() + .id; + // recreate proposal 1 on top of a another commit (like a rebase on top + // of one extra commit) + let second_originating_repo = GitTestRepo::default(); + second_originating_repo.populate()?; + std::fs::write( + second_originating_repo.dir.join("amazing.md"), + "some content", + )?; + second_originating_repo + .stage_and_commit("commit for rebasing on top of")?; + cli_tester_create_proposal( + &second_originating_repo, + FEATURE_BRANCH_NAME_1, + "a", + Some((PROPOSAL_TITLE_1, "proposal a description")), + Some(proposal_1_id.to_string()), + )?; + + // pretend we have downloaded the origianl version of the first proposal + let test_repo = GitTestRepo::default(); + test_repo.populate()?; + create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?; + // pretend we have pulled the updated main branch + test_repo.checkout("main")?; + std::fs::write(test_repo.dir.join("amazing.md"), "some content")?; + test_repo.stage_and_commit("commit for rebasing on top of")?; + test_repo.checkout(FEATURE_BRANCH_NAME_1)?; - let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]); - p.expect("finding proposal root event...\r\n")?; - p.expect("found proposal root event. finding commits...\r\n")?; - p.expect_end_with("pulled new version of proposal (2 ahead 0 behind 'main'), replacing old version (2 ahead 1 behind 'main')\r\n")?; + let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]); + p.expect("finding proposal root event...\r\n")?; + p.expect("found proposal root event. finding commits...\r\n")?; + p.expect_end_with("pulled new version of proposal (2 ahead 0 behind 'main'), replacing old version (2 ahead 1 behind 'main')\r\n")?; - for p in [51, 52, 53, 55, 56] { - relay::shutdown_relay(8000 + p)?; - } - Ok(()) - }); + for p in [51, 52, 53, 55, 56] { + relay::shutdown_relay(8000 + p)?; + } + Ok(()) + }, + ); // launch relay let _ = join!( @@ -910,7 +914,7 @@ mod when_branch_is_checked_out { r55.listen_until_close(), r56.listen_until_close(), ); - cli_tester_handle.join().unwrap()?; + cli_tester_handle.await??; println!("{:?}", r55.events); Ok(()) } -- cgit v1.2.3 From bc1e9e7ff8d2c3747a7dbc3b649930ac5bc6425d Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 28 Mar 2024 08:26:19 +0000 Subject: test: fix intermittent test failures caused by test proposals with the same timestamp listed in an inconsistant list order; by ensuring test proposals have a different timestamps --- tests/list.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'tests/list.rs') diff --git a/tests/list.rs b/tests/list.rs index 8252448..61c2201 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -23,6 +23,7 @@ fn cli_tester_create_proposals() -> Result { Some((PROPOSAL_TITLE_1, "proposal a description")), None, )?; + std::thread::sleep(std::time::Duration::from_millis(1000)); cli_tester_create_proposal( &git_repo, FEATURE_BRANCH_NAME_2, @@ -30,6 +31,7 @@ fn cli_tester_create_proposals() -> Result { Some((PROPOSAL_TITLE_2, "proposal b description")), None, )?; + std::thread::sleep(std::time::Duration::from_millis(1000)); cli_tester_create_proposal( &git_repo, FEATURE_BRANCH_NAME_3, @@ -72,7 +74,7 @@ fn cli_tester_create_proposal( in_reply_to: Option, ) -> Result<()> { create_and_populate_branch(test_repo, branch_name, prefix, false)?; - + std::thread::sleep(std::time::Duration::from_millis(1000)); if let Some(in_reply_to) = in_reply_to { let mut p = CliTester::new_from_dir( &test_repo.dir, @@ -489,7 +491,7 @@ mod when_main_branch_is_uptodate { "checked out proposal as '{FEATURE_BRANCH_NAME_3}' branch\r\n" ))?; p.expect_end()?; - + println!("blablagothere"); for p in [51, 52, 53, 55, 56] { relay::shutdown_relay(8000 + p)?; } @@ -726,6 +728,7 @@ mod when_main_branch_is_uptodate { let cli_tester_handle = std::thread::spawn(move || -> Result<()> { let originating_repo = cli_tester_create_proposals()?; + std::thread::sleep(std::time::Duration::from_millis(1000)); cli_tester_create_proposal( &originating_repo, FEATURE_BRANCH_NAME_4, -- cgit v1.2.3