From 5c973c672f79510b19cd394a33751d93b7f9f5ee Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 1 Aug 2024 08:16:43 +0100 Subject: test(remote): `push` delete branch updates state in nostr state event --- tests/git_remote_helper.rs | 105 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/git_remote_helper.rs b/tests/git_remote_helper.rs index fffcaa1..1ca4296 100644 --- a/tests/git_remote_helper.rs +++ b/tests/git_remote_helper.rs @@ -53,9 +53,14 @@ fn cli_tester_after_fetch(git_repo: &GitTestRepo) -> Result { async fn generate_repo_with_state_event() -> Result<(nostr::Event, GitTestRepo)> { let git_repo = prep_git_repo()?; - let commit_id = git_repo.get_tip_of_local_branch("main")?.to_string(); - let source_git_repo = GitTestRepo::recreate_as_bare(&git_repo)?; + git_repo.create_branch("example-branch")?; + let main_commit_id = git_repo.get_tip_of_local_branch("main")?.to_string(); + // TODO recreate_as_bare isn't creating other branches + let source_git_repo = GitTestRepo::recreate_as_bare(&git_repo)?; + let example_commit_id = source_git_repo + .get_tip_of_local_branch("example-branch")? + .to_string(); let events = vec![ generate_test_key_1_metadata_event("fred"), generate_test_key_1_relay_list_event(), @@ -112,7 +117,8 @@ async fn generate_repo_with_state_event() -> Result<(nostr::Event, GitTestRepo)> .collect::>>(), HashSet::from([ vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], - vec!["refs/heads/main".to_string(), commit_id,], + vec!["refs/heads/main".to_string(), main_commit_id.clone(),], + vec!["refs/heads/example-branch".to_string(), example_commit_id,], ]), ); @@ -610,6 +616,8 @@ mod push { let (state_event, source_git_repo) = generate_repo_with_state_event().await?; let git_repo = prep_git_repo()?; + let example_branch_commit_id = + git_repo.get_tip_of_local_branch("main")?.to_string(); // same as example std::fs::write(git_repo.dir.join("new.md"), "some content")?; let main_commit_id = git_repo.stage_and_commit("new.md")?; @@ -682,6 +690,10 @@ mod push { HashSet::from([ vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], vec!["refs/heads/main".to_string(), main_commit_id.to_string()], + vec![ + "refs/heads/example-branch".to_string(), + example_branch_commit_id.to_string() + ], vec!["refs/heads/vnext".to_string(), vnext_commit_id.to_string()], ]), ); @@ -866,5 +878,92 @@ mod push { async_run_test().await } } + mod existing_state_event { + use super::*; + + mod state_on_git_server_published_in_nostr_state_event { + + use super::*; + + async fn async_run_test() -> Result<()> { + let (state_event, source_git_repo) = generate_repo_with_state_event().await?; + + let git_repo = prep_git_repo()?; + let main_commit_id = git_repo.get_tip_of_local_branch("main")?.to_string(); // same as example + + 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_repo.dir.to_str().unwrap(), + ), + state_event.clone(), + ]; + + // 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, None), + 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), + ); + r51.events = events.clone(); + r55.events = events; + + let cli_tester_handle = std::thread::spawn(move || -> Result<()> { + let mut p = cli_tester_after_fetch(&git_repo)?; + p.send_line("push :refs/heads/example-branch")?; + p.send_line("")?; + p.expect("ok refs/heads/example-branch\r\n")?; + p.expect("\r\n")?; + p.exit()?; + for p in [51, 52, 53, 55, 56, 57] { + relay::shutdown_relay(8000 + p)?; + } + Ok(()) + }); + // launch relays + 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()?; + + let state_event = r56 + .events + .iter() + .find(|e| e.kind().eq(&STATE_KIND)) + .context("state event not created")?; + + // println!("{:#?}", state_event); + assert_eq!( + state_event + .tags + .iter() + .filter(|t| t.kind().to_string().as_str().ne("d")) + .map(|t| t.as_vec().to_vec()) + .collect::>>(), + HashSet::from([ + vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], + vec!["refs/heads/main".to_string(), main_commit_id.to_string()], + ]), + ); + Ok(()) + } + + #[tokio::test] + #[serial] + async fn state_event_reflects_deleted_branch() -> Result<()> { + async_run_test().await + } + } + } } } -- cgit v1.2.3