diff options
| -rw-r--r-- | test_utils/src/lib.rs | 35 | ||||
| -rw-r--r-- | tests/git_remote_helper.rs | 107 |
2 files changed, 141 insertions, 1 deletions
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( | |||
| 150 | } | 150 | } |
| 151 | 151 | ||
| 152 | pub fn generate_repo_ref_event() -> nostr::Event { | 152 | pub fn generate_repo_ref_event() -> nostr::Event { |
| 153 | generate_repo_ref_event_with_git_server("git:://123.gitexample.com/test".to_string()) | ||
| 154 | } | ||
| 155 | |||
| 156 | pub fn generate_repo_ref_event_with_git_server(git_server: String) -> nostr::Event { | ||
| 153 | // taken from test git_repo | 157 | // taken from test git_repo |
| 154 | // TODO - this may not be consistant across computers as it might take the | 158 | // TODO - this may not be consistant across computers as it might take the |
| 155 | // author and committer from global git config | 159 | // author and committer from global git config |
| @@ -167,7 +171,7 @@ pub fn generate_repo_ref_event() -> nostr::Event { | |||
| 167 | Tag::from_standardized(TagStandard::Description("example description".into())), | 171 | Tag::from_standardized(TagStandard::Description("example description".into())), |
| 168 | Tag::custom( | 172 | Tag::custom( |
| 169 | nostr::TagKind::Custom(std::borrow::Cow::Borrowed("clone")), | 173 | nostr::TagKind::Custom(std::borrow::Cow::Borrowed("clone")), |
| 170 | vec!["git:://123.gitexample.com/test".to_string()], | 174 | vec![git_server], |
| 171 | ), | 175 | ), |
| 172 | Tag::custom( | 176 | Tag::custom( |
| 173 | nostr::TagKind::Custom(std::borrow::Cow::Borrowed("web")), | 177 | nostr::TagKind::Custom(std::borrow::Cow::Borrowed("web")), |
| @@ -728,6 +732,14 @@ impl CliTester { | |||
| 728 | } | 732 | } |
| 729 | } | 733 | } |
| 730 | 734 | ||
| 735 | pub fn new_remote_helper_from_dir(dir: &PathBuf, nostr_remote_url: &str) -> Self { | ||
| 736 | Self { | ||
| 737 | rexpect_session: remote_helper_rexpect_with_from_dir(dir, nostr_remote_url, 3000) | ||
| 738 | .expect("rexpect to spawn new process"), | ||
| 739 | formatter: ColorfulTheme::default(), | ||
| 740 | } | ||
| 741 | } | ||
| 742 | |||
| 731 | pub fn restart_with<I, S>(&mut self, args: I) -> &mut Self | 743 | pub fn restart_with<I, S>(&mut self, args: I) -> &mut Self |
| 732 | where | 744 | where |
| 733 | I: IntoIterator<Item = S>, | 745 | I: IntoIterator<Item = S>, |
| @@ -943,6 +955,27 @@ where | |||
| 943 | ) | 955 | ) |
| 944 | } | 956 | } |
| 945 | 957 | ||
| 958 | pub fn remote_helper_rexpect_with_from_dir( | ||
| 959 | dir: &PathBuf, | ||
| 960 | nostr_remote_url: &str, | ||
| 961 | timeout_ms: u64, | ||
| 962 | ) -> Result<PtySession, rexpect::error::Error> { | ||
| 963 | let mut cmd = std::process::Command::new(assert_cmd::cargo::cargo_bin("git-remote-nostr")); | ||
| 964 | cmd.env("NGITTEST", "TRUE"); | ||
| 965 | cmd.env("GIT_DIR", dir); | ||
| 966 | cmd.env("RUST_BACKTRACE", "0"); | ||
| 967 | cmd.current_dir(dir); | ||
| 968 | cmd.args([dir.as_os_str().to_str().unwrap(), nostr_remote_url]); | ||
| 969 | // using branch for PR https://github.com/rust-cli/rexpect/pull/103 to strip ansi escape codes | ||
| 970 | rexpect::session::spawn_with_options( | ||
| 971 | cmd, | ||
| 972 | Options { | ||
| 973 | timeout_ms: Some(timeout_ms), | ||
| 974 | strip_ansi_escape_codes: true, | ||
| 975 | }, | ||
| 976 | ) | ||
| 977 | } | ||
| 978 | |||
| 946 | /** copied from client.rs */ | 979 | /** copied from client.rs */ |
| 947 | async fn get_local_cache_database(git_repo_path: &Path) -> Result<SQLiteDatabase> { | 980 | async fn get_local_cache_database(git_repo_path: &Path) -> Result<SQLiteDatabase> { |
| 948 | SQLiteDatabase::open(git_repo_path.join(".git/nostr-cache.sqlite")) | 981 | 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 @@ | |||
| 1 | use anyhow::{Context, Result}; | ||
| 2 | use futures::join; | ||
| 3 | use nostr::nips::nip01::Coordinate; | ||
| 4 | use nostr_sdk::{Kind, ToBech32}; | ||
| 5 | use relay::Relay; | ||
| 6 | use serial_test::serial; | ||
| 7 | use test_utils::{git::GitTestRepo, *}; | ||
| 8 | |||
| 9 | static NOSTR_REMOTE_NAME: &str = "nostr"; | ||
| 10 | |||
| 11 | fn get_nostr_remote_url() -> Result<String> { | ||
| 12 | let repo_event = generate_repo_ref_event(); | ||
| 13 | let naddr = Coordinate { | ||
| 14 | kind: Kind::GitRepoAnnouncement, | ||
| 15 | public_key: repo_event.author(), | ||
| 16 | identifier: repo_event.identifier().unwrap().to_string(), | ||
| 17 | relays: vec![ | ||
| 18 | "ws://localhost:8055".to_string(), | ||
| 19 | "ws://localhost:8056".to_string(), | ||
| 20 | ], | ||
| 21 | } | ||
| 22 | .to_bech32()?; | ||
| 23 | Ok(format!("nostr://{naddr}")) | ||
| 24 | } | ||
| 25 | |||
| 26 | fn prep_git_repo() -> Result<GitTestRepo> { | ||
| 27 | let test_repo = GitTestRepo::without_repo_in_git_config(); | ||
| 28 | let mut config = test_repo | ||
| 29 | .git_repo | ||
| 30 | .config() | ||
| 31 | .context("cannot open git config")?; | ||
| 32 | config.set_str("nostr.nsec", TEST_KEY_1_NSEC)?; | ||
| 33 | config.set_str("nostr.npub", TEST_KEY_1_NPUB)?; | ||
| 34 | test_repo.add_remote(NOSTR_REMOTE_NAME, &get_nostr_remote_url()?)?; | ||
| 35 | test_repo.populate()?; | ||
| 36 | Ok(test_repo) | ||
| 37 | } | ||
| 38 | |||
| 39 | fn cli_tester(git_repo: &GitTestRepo) -> CliTester { | ||
| 40 | CliTester::new_remote_helper_from_dir(&git_repo.dir, &get_nostr_remote_url().unwrap()) | ||
| 41 | } | ||
| 42 | |||
| 43 | fn cli_tester_after_fetch(git_repo: &GitTestRepo) -> Result<CliTester> { | ||
| 44 | let mut p = cli_tester(git_repo); | ||
| 45 | p.expect("fetching updates...\r\n")?; | ||
| 46 | p.expect_eventually("updates")?; // some updates | ||
| 47 | p.expect_eventually("\r\n")?; | ||
| 48 | Ok(p) | ||
| 49 | } | ||
| 50 | |||
| 51 | mod initially_runs_fetch { | ||
| 52 | |||
| 53 | use relay::ListenerReqFunc; | ||
| 54 | |||
| 55 | use super::*; | ||
| 56 | async fn async_run_test() -> Result<()> { | ||
| 57 | let source_git_repo = prep_git_repo()?; | ||
| 58 | let source_git_url = format!("git://{}", source_git_repo.dir.to_str().unwrap()); | ||
| 59 | let git_repo = prep_git_repo()?; | ||
| 60 | let events = vec![ | ||
| 61 | generate_test_key_1_metadata_event("fred"), | ||
| 62 | generate_test_key_1_relay_list_event(), | ||
| 63 | generate_repo_ref_event_with_git_server(source_git_url), | ||
| 64 | ]; | ||
| 65 | let responder: ListenerReqFunc = &|relay, client_id, subscription_id, _| -> Result<()> { | ||
| 66 | relay.respond_events(client_id, &subscription_id, &events)?; | ||
| 67 | Ok(()) | ||
| 68 | }; | ||
| 69 | // fallback (51,52) user write (53, 55) repo (55, 56) blaster (57) | ||
| 70 | let (mut r51, mut r52, mut r53, mut r55, mut r56, mut r57) = ( | ||
| 71 | Relay::new(8051, None, Some(&responder)), | ||
| 72 | Relay::new(8052, None, None), | ||
| 73 | Relay::new(8053, None, None), | ||
| 74 | Relay::new(8055, None, None), | ||
| 75 | Relay::new(8056, None, None), | ||
| 76 | Relay::new(8057, None, None), | ||
| 77 | ); | ||
| 78 | |||
| 79 | // // check relay had the right number of events | ||
| 80 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 81 | let mut p = cli_tester_after_fetch(&git_repo)?; | ||
| 82 | p.exit()?; | ||
| 83 | for p in [51, 52, 53, 55, 56, 57] { | ||
| 84 | relay::shutdown_relay(8000 + p)?; | ||
| 85 | } | ||
| 86 | Ok(()) | ||
| 87 | }); | ||
| 88 | |||
| 89 | // launch relay | ||
| 90 | let _ = join!( | ||
| 91 | r51.listen_until_close(), | ||
| 92 | r52.listen_until_close(), | ||
| 93 | r53.listen_until_close(), | ||
| 94 | r55.listen_until_close(), | ||
| 95 | r56.listen_until_close(), | ||
| 96 | r57.listen_until_close(), | ||
| 97 | ); | ||
| 98 | cli_tester_handle.join().unwrap()?; | ||
| 99 | Ok(()) | ||
| 100 | } | ||
| 101 | |||
| 102 | #[tokio::test] | ||
| 103 | #[serial] | ||
| 104 | async fn runs_fetch_and_reports() -> Result<()> { | ||
| 105 | async_run_test().await | ||
| 106 | } | ||
| 107 | } | ||