diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-08-01 08:16:43 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-08-01 08:16:43 +0100 |
| commit | 5c973c672f79510b19cd394a33751d93b7f9f5ee (patch) | |
| tree | d8eca2d7ba03e5d88e1046f337fbe077ceb3393d /tests | |
| parent | 6ba6177e895f7018b6b044a3d63442d1097a07dd (diff) | |
test(remote): `push` delete branch updates state
in nostr state event
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/git_remote_helper.rs | 105 |
1 files changed, 102 insertions, 3 deletions
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<CliTester> { | |||
| 53 | 53 | ||
| 54 | async fn generate_repo_with_state_event() -> Result<(nostr::Event, GitTestRepo)> { | 54 | async fn generate_repo_with_state_event() -> Result<(nostr::Event, GitTestRepo)> { |
| 55 | let git_repo = prep_git_repo()?; | 55 | let git_repo = prep_git_repo()?; |
| 56 | let commit_id = git_repo.get_tip_of_local_branch("main")?.to_string(); | 56 | git_repo.create_branch("example-branch")?; |
| 57 | let source_git_repo = GitTestRepo::recreate_as_bare(&git_repo)?; | ||
| 58 | 57 | ||
| 58 | let main_commit_id = git_repo.get_tip_of_local_branch("main")?.to_string(); | ||
| 59 | // TODO recreate_as_bare isn't creating other branches | ||
| 60 | let source_git_repo = GitTestRepo::recreate_as_bare(&git_repo)?; | ||
| 61 | let example_commit_id = source_git_repo | ||
| 62 | .get_tip_of_local_branch("example-branch")? | ||
| 63 | .to_string(); | ||
| 59 | let events = vec![ | 64 | let events = vec![ |
| 60 | generate_test_key_1_metadata_event("fred"), | 65 | generate_test_key_1_metadata_event("fred"), |
| 61 | generate_test_key_1_relay_list_event(), | 66 | generate_test_key_1_relay_list_event(), |
| @@ -112,7 +117,8 @@ async fn generate_repo_with_state_event() -> Result<(nostr::Event, GitTestRepo)> | |||
| 112 | .collect::<HashSet<Vec<String>>>(), | 117 | .collect::<HashSet<Vec<String>>>(), |
| 113 | HashSet::from([ | 118 | HashSet::from([ |
| 114 | vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], | 119 | vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], |
| 115 | vec!["refs/heads/main".to_string(), commit_id,], | 120 | vec!["refs/heads/main".to_string(), main_commit_id.clone(),], |
| 121 | vec!["refs/heads/example-branch".to_string(), example_commit_id,], | ||
| 116 | ]), | 122 | ]), |
| 117 | ); | 123 | ); |
| 118 | 124 | ||
| @@ -610,6 +616,8 @@ mod push { | |||
| 610 | let (state_event, source_git_repo) = generate_repo_with_state_event().await?; | 616 | let (state_event, source_git_repo) = generate_repo_with_state_event().await?; |
| 611 | 617 | ||
| 612 | let git_repo = prep_git_repo()?; | 618 | let git_repo = prep_git_repo()?; |
| 619 | let example_branch_commit_id = | ||
| 620 | git_repo.get_tip_of_local_branch("main")?.to_string(); // same as example | ||
| 613 | 621 | ||
| 614 | std::fs::write(git_repo.dir.join("new.md"), "some content")?; | 622 | std::fs::write(git_repo.dir.join("new.md"), "some content")?; |
| 615 | let main_commit_id = git_repo.stage_and_commit("new.md")?; | 623 | let main_commit_id = git_repo.stage_and_commit("new.md")?; |
| @@ -682,6 +690,10 @@ mod push { | |||
| 682 | HashSet::from([ | 690 | HashSet::from([ |
| 683 | vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], | 691 | vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], |
| 684 | vec!["refs/heads/main".to_string(), main_commit_id.to_string()], | 692 | vec!["refs/heads/main".to_string(), main_commit_id.to_string()], |
| 693 | vec![ | ||
| 694 | "refs/heads/example-branch".to_string(), | ||
| 695 | example_branch_commit_id.to_string() | ||
| 696 | ], | ||
| 685 | vec!["refs/heads/vnext".to_string(), vnext_commit_id.to_string()], | 697 | vec!["refs/heads/vnext".to_string(), vnext_commit_id.to_string()], |
| 686 | ]), | 698 | ]), |
| 687 | ); | 699 | ); |
| @@ -866,5 +878,92 @@ mod push { | |||
| 866 | async_run_test().await | 878 | async_run_test().await |
| 867 | } | 879 | } |
| 868 | } | 880 | } |
| 881 | mod existing_state_event { | ||
| 882 | use super::*; | ||
| 883 | |||
| 884 | mod state_on_git_server_published_in_nostr_state_event { | ||
| 885 | |||
| 886 | use super::*; | ||
| 887 | |||
| 888 | async fn async_run_test() -> Result<()> { | ||
| 889 | let (state_event, source_git_repo) = generate_repo_with_state_event().await?; | ||
| 890 | |||
| 891 | let git_repo = prep_git_repo()?; | ||
| 892 | let main_commit_id = git_repo.get_tip_of_local_branch("main")?.to_string(); // same as example | ||
| 893 | |||
| 894 | let events = vec![ | ||
| 895 | generate_test_key_1_metadata_event("fred"), | ||
| 896 | generate_test_key_1_relay_list_event(), | ||
| 897 | generate_repo_ref_event_with_git_server( | ||
| 898 | source_git_repo.dir.to_str().unwrap(), | ||
| 899 | ), | ||
| 900 | state_event.clone(), | ||
| 901 | ]; | ||
| 902 | |||
| 903 | // fallback (51,52) user write (53, 55) repo (55, 56) blaster (57) | ||
| 904 | let (mut r51, mut r52, mut r53, mut r55, mut r56, mut r57) = ( | ||
| 905 | Relay::new(8051, None, None), | ||
| 906 | Relay::new(8052, None, None), | ||
| 907 | Relay::new(8053, None, None), | ||
| 908 | Relay::new(8055, None, None), | ||
| 909 | Relay::new(8056, None, None), | ||
| 910 | Relay::new(8057, None, None), | ||
| 911 | ); | ||
| 912 | r51.events = events.clone(); | ||
| 913 | r55.events = events; | ||
| 914 | |||
| 915 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 916 | let mut p = cli_tester_after_fetch(&git_repo)?; | ||
| 917 | p.send_line("push :refs/heads/example-branch")?; | ||
| 918 | p.send_line("")?; | ||
| 919 | p.expect("ok refs/heads/example-branch\r\n")?; | ||
| 920 | p.expect("\r\n")?; | ||
| 921 | p.exit()?; | ||
| 922 | for p in [51, 52, 53, 55, 56, 57] { | ||
| 923 | relay::shutdown_relay(8000 + p)?; | ||
| 924 | } | ||
| 925 | Ok(()) | ||
| 926 | }); | ||
| 927 | // launch relays | ||
| 928 | let _ = join!( | ||
| 929 | r51.listen_until_close(), | ||
| 930 | r52.listen_until_close(), | ||
| 931 | r53.listen_until_close(), | ||
| 932 | r55.listen_until_close(), | ||
| 933 | r56.listen_until_close(), | ||
| 934 | r57.listen_until_close(), | ||
| 935 | ); | ||
| 936 | |||
| 937 | cli_tester_handle.join().unwrap()?; | ||
| 938 | |||
| 939 | let state_event = r56 | ||
| 940 | .events | ||
| 941 | .iter() | ||
| 942 | .find(|e| e.kind().eq(&STATE_KIND)) | ||
| 943 | .context("state event not created")?; | ||
| 944 | |||
| 945 | // println!("{:#?}", state_event); | ||
| 946 | assert_eq!( | ||
| 947 | state_event | ||
| 948 | .tags | ||
| 949 | .iter() | ||
| 950 | .filter(|t| t.kind().to_string().as_str().ne("d")) | ||
| 951 | .map(|t| t.as_vec().to_vec()) | ||
| 952 | .collect::<HashSet<Vec<String>>>(), | ||
| 953 | HashSet::from([ | ||
| 954 | vec!["HEAD".to_string(), "ref: refs/heads/main".to_string()], | ||
| 955 | vec!["refs/heads/main".to_string(), main_commit_id.to_string()], | ||
| 956 | ]), | ||
| 957 | ); | ||
| 958 | Ok(()) | ||
| 959 | } | ||
| 960 | |||
| 961 | #[tokio::test] | ||
| 962 | #[serial] | ||
| 963 | async fn state_event_reflects_deleted_branch() -> Result<()> { | ||
| 964 | async_run_test().await | ||
| 965 | } | ||
| 966 | } | ||
| 967 | } | ||
| 869 | } | 968 | } |
| 870 | } | 969 | } |