From fea2e0a7c06acb2bd6223d24b80b2509c13e460d Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 31 Jul 2024 09:36:42 +0100 Subject: test(remote): test helpers and basic fetch create test helpers and setup basic test to verifiy fetch was ran --- test_utils/src/lib.rs | 35 ++++++++++++++- tests/git_remote_helper.rs | 107 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tests/git_remote_helper.rs diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index bccfaf5..7697e6f 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs @@ -150,6 +150,10 @@ pub fn make_event_old_or_change_user( } pub fn generate_repo_ref_event() -> nostr::Event { + generate_repo_ref_event_with_git_server("git:://123.gitexample.com/test".to_string()) +} + +pub fn generate_repo_ref_event_with_git_server(git_server: String) -> nostr::Event { // taken from test git_repo // TODO - this may not be consistant across computers as it might take the // author and committer from global git config @@ -167,7 +171,7 @@ pub fn generate_repo_ref_event() -> nostr::Event { Tag::from_standardized(TagStandard::Description("example description".into())), Tag::custom( nostr::TagKind::Custom(std::borrow::Cow::Borrowed("clone")), - vec!["git:://123.gitexample.com/test".to_string()], + vec![git_server], ), Tag::custom( nostr::TagKind::Custom(std::borrow::Cow::Borrowed("web")), @@ -728,6 +732,14 @@ impl CliTester { } } + pub fn new_remote_helper_from_dir(dir: &PathBuf, nostr_remote_url: &str) -> Self { + Self { + rexpect_session: remote_helper_rexpect_with_from_dir(dir, nostr_remote_url, 3000) + .expect("rexpect to spawn new process"), + formatter: ColorfulTheme::default(), + } + } + pub fn restart_with(&mut self, args: I) -> &mut Self where I: IntoIterator, @@ -943,6 +955,27 @@ where ) } +pub fn remote_helper_rexpect_with_from_dir( + dir: &PathBuf, + nostr_remote_url: &str, + timeout_ms: u64, +) -> Result { + let mut cmd = std::process::Command::new(assert_cmd::cargo::cargo_bin("git-remote-nostr")); + cmd.env("NGITTEST", "TRUE"); + cmd.env("GIT_DIR", dir); + cmd.env("RUST_BACKTRACE", "0"); + cmd.current_dir(dir); + cmd.args([dir.as_os_str().to_str().unwrap(), nostr_remote_url]); + // using branch for PR https://github.com/rust-cli/rexpect/pull/103 to strip ansi escape codes + rexpect::session::spawn_with_options( + cmd, + Options { + timeout_ms: Some(timeout_ms), + strip_ansi_escape_codes: true, + }, + ) +} + /** copied from client.rs */ async fn get_local_cache_database(git_repo_path: &Path) -> Result { SQLiteDatabase::open(git_repo_path.join(".git/nostr-cache.sqlite")) diff --git a/tests/git_remote_helper.rs b/tests/git_remote_helper.rs new file mode 100644 index 0000000..9bbd10f --- /dev/null +++ b/tests/git_remote_helper.rs @@ -0,0 +1,107 @@ +use anyhow::{Context, Result}; +use futures::join; +use nostr::nips::nip01::Coordinate; +use nostr_sdk::{Kind, ToBech32}; +use relay::Relay; +use serial_test::serial; +use test_utils::{git::GitTestRepo, *}; + +static NOSTR_REMOTE_NAME: &str = "nostr"; + +fn get_nostr_remote_url() -> Result { + let repo_event = generate_repo_ref_event(); + let naddr = Coordinate { + kind: Kind::GitRepoAnnouncement, + public_key: repo_event.author(), + identifier: repo_event.identifier().unwrap().to_string(), + relays: vec![ + "ws://localhost:8055".to_string(), + "ws://localhost:8056".to_string(), + ], + } + .to_bech32()?; + Ok(format!("nostr://{naddr}")) +} + +fn prep_git_repo() -> Result { + let test_repo = GitTestRepo::without_repo_in_git_config(); + let mut config = test_repo + .git_repo + .config() + .context("cannot open git config")?; + config.set_str("nostr.nsec", TEST_KEY_1_NSEC)?; + config.set_str("nostr.npub", TEST_KEY_1_NPUB)?; + test_repo.add_remote(NOSTR_REMOTE_NAME, &get_nostr_remote_url()?)?; + test_repo.populate()?; + Ok(test_repo) +} + +fn cli_tester(git_repo: &GitTestRepo) -> CliTester { + CliTester::new_remote_helper_from_dir(&git_repo.dir, &get_nostr_remote_url().unwrap()) +} + +fn cli_tester_after_fetch(git_repo: &GitTestRepo) -> Result { + let mut p = cli_tester(git_repo); + p.expect("fetching updates...\r\n")?; + p.expect_eventually("updates")?; // some updates + p.expect_eventually("\r\n")?; + Ok(p) +} + +mod initially_runs_fetch { + + use relay::ListenerReqFunc; + + use super::*; + async fn async_run_test() -> Result<()> { + let source_git_repo = prep_git_repo()?; + let source_git_url = format!("git://{}", source_git_repo.dir.to_str().unwrap()); + let git_repo = prep_git_repo()?; + let events = vec![ + generate_test_key_1_metadata_event("fred"), + generate_test_key_1_relay_list_event(), + generate_repo_ref_event_with_git_server(source_git_url), + ]; + let responder: ListenerReqFunc = &|relay, client_id, subscription_id, _| -> Result<()> { + relay.respond_events(client_id, &subscription_id, &events)?; + Ok(()) + }; + // fallback (51,52) user write (53, 55) repo (55, 56) blaster (57) + let (mut r51, mut r52, mut r53, mut r55, mut r56, mut r57) = ( + Relay::new(8051, None, Some(&responder)), + Relay::new(8052, None, None), + Relay::new(8053, None, None), + Relay::new(8055, None, None), + Relay::new(8056, None, None), + Relay::new(8057, None, None), + ); + + // // check relay had the right number of events + let cli_tester_handle = std::thread::spawn(move || -> Result<()> { + let mut p = cli_tester_after_fetch(&git_repo)?; + p.exit()?; + for p in [51, 52, 53, 55, 56, 57] { + relay::shutdown_relay(8000 + p)?; + } + Ok(()) + }); + + // launch relay + let _ = join!( + r51.listen_until_close(), + r52.listen_until_close(), + r53.listen_until_close(), + r55.listen_until_close(), + r56.listen_until_close(), + r57.listen_until_close(), + ); + cli_tester_handle.join().unwrap()?; + Ok(()) + } + + #[tokio::test] + #[serial] + async fn runs_fetch_and_reports() -> Result<()> { + async_run_test().await + } +} -- cgit v1.2.3