From 771f944af447c202eba045936a36dee71ab797ac Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 4 Sep 2024 11:32:05 +0100 Subject: refactor: fix imports, etc based on restructure move some functions out of ngit and into lib/mod and lib/git_events remove MockConnect from binaries so it is only used in the library. this was done: * mainly because automocks were not being imported from lib into each binary * but also because the these functions were being tested with MockConnect --- src/bin/git_remote_nostr/main.rs | 307 +++------------------------------------ 1 file changed, 22 insertions(+), 285 deletions(-) (limited to 'src/bin/git_remote_nostr/main.rs') diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index a5244bf..3e08efe 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -15,12 +15,19 @@ use std::{ use anyhow::{anyhow, bail, Context, Result}; use auth_git2::GitAuthenticator; use client::{ - consolidate_fetch_reports, get_events_from_cache, get_repo_ref_from_cache, - get_state_from_cache, sign_event, Connect, STATE_KIND, + consolidate_fetch_reports, get_all_proposal_patch_events_from_cache, get_events_from_cache, + get_proposals_and_revisions_from_cache, get_repo_ref_from_cache, get_state_from_cache, + send_events, sign_event, Connect, STATE_KIND, }; use console::Term; -use git::{sha1_to_oid, NostrUrlDecoded, RepoActions}; +use git::{nostr_url::NostrUrlDecoded, sha1_to_oid, RepoActions}; use git2::{Oid, Repository}; +use git_events::{ + event_is_revision_root, event_to_cover_letter, generate_cover_letter_and_patch_events, + generate_patch_event, get_commit_id_from_patch, get_most_recent_patch_with_ancestors, + status_kinds, tag_value, +}; +use ngit::{client, git, git_events, login, repo_ref, repo_state}; use nostr::nips::{nip01::Coordinate, nip10::Marker}; use nostr_sdk::{ hashes::sha1::Hash as Sha1Hash, Event, EventBuilder, EventId, Kind, PublicKey, Tag, Url, @@ -28,34 +35,8 @@ use nostr_sdk::{ use nostr_signer::NostrSigner; use repo_ref::RepoRef; use repo_state::RepoState; -use sub_commands::{ - list::{ - get_all_proposal_patch_events_from_cache, get_commit_id_from_patch, - get_most_recent_patch_with_ancestors, get_proposals_and_revisions_from_cache, status_kinds, - tag_value, - }, - send::{ - event_is_revision_root, event_to_cover_letter, generate_cover_letter_and_patch_events, - generate_patch_event, send_events, - }, -}; -#[cfg(not(test))] -use crate::client::Client; -#[cfg(test)] -use crate::client::MockConnect; -use crate::git::Repo; - -mod cli; -mod cli_interactor; -mod client; -mod config; -mod git; -mod key_handling; -mod login; -mod repo_ref; -mod repo_state; -mod sub_commands; +use crate::{client::Client, git::Repo}; #[tokio::main] async fn main() -> Result<()> { @@ -76,10 +57,7 @@ async fn main() -> Result<()> { ))?; let git_repo_path = git_repo.get_path()?; - #[cfg(not(test))] let client = Client::default(); - #[cfg(test)] - let client = ::default(); let decoded_nostr_url = NostrUrlDecoded::from_str(nostr_remote_url).context("invalid nostr url")?; @@ -155,8 +133,7 @@ pub(crate) fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Resu async fn fetching_with_report_for_helper( git_repo_path: &Path, - #[cfg(test)] client: &crate::client::MockConnect, - #[cfg(not(test))] client: &Client, + client: &Client, repo_coordinates: &HashSet, ) -> Result<()> { let term = console::Term::stderr(); @@ -662,8 +639,7 @@ async fn push( nostr_remote_url: &str, stdin: &Stdin, initial_refspec: &str, - #[cfg(test)] client: &crate::client::MockConnect, - #[cfg(not(test))] client: &Client, + client: &Client, list_outputs: Option>>, ) -> Result<()> { let refspecs = get_refspecs_from_push_batch(stdin, initial_refspec)?; @@ -1613,8 +1589,15 @@ fn get_refspecs_from_push_batch(stdin: &Stdin, initial_refspec: &str) -> Result< Ok(refspecs) } -impl RepoState { - pub async fn build( +trait BuildRepoState { + async fn build( + identifier: String, + state: HashMap, + signer: &NostrSigner, + ) -> Result; +} +impl BuildRepoState for RepoState { + async fn build( identifier: String, state: HashMap, signer: &NostrSigner, @@ -1639,252 +1622,6 @@ impl RepoState { mod tests { use super::*; - mod nostr_git_url_paramemters_from_str { - use git::ServerProtocol; - use nostr_sdk::PublicKey; - - use super::*; - - fn get_model_coordinate(relays: bool) -> Coordinate { - Coordinate { - identifier: "ngit".to_string(), - public_key: PublicKey::parse( - "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", - ) - .unwrap(), - kind: nostr_sdk::Kind::GitRepoAnnouncement, - relays: if relays { - vec!["wss://nos.lol/".to_string()] - } else { - vec![] - }, - } - } - - #[test] - fn from_naddr() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://naddr1qqzxuemfwsqs6amnwvaz7tmwdaejumr0dspzpgqgmmc409hm4xsdd74sf68a2uyf9pwel4g9mfdg8l5244t6x4jdqvzqqqrhnym0k2qj" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([Coordinate { - identifier: "ngit".to_string(), - public_key: PublicKey::parse( - "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", - ) - .unwrap(), - kind: nostr_sdk::Kind::GitRepoAnnouncement, - relays: vec!["wss://nos.lol".to_string()], // wont add the slash - }]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - mod from_npub_slash_identifier { - use super::*; - - #[test] - fn without_relay() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(false)]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - - mod with_url_parameters { - - use super::*; - - #[test] - fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay=nos.lol" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(true)]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - - #[test] - fn with_encoded_relay() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}", - urlencoding::encode("wss://nos.lol") - ))?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(true)]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - #[test] - fn with_multiple_encoded_relays() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?relay={}&relay1={}", - urlencoding::encode("wss://nos.lol"), - urlencoding::encode("wss://relay.damus.io"), - ))?, - NostrUrlDecoded { - coordinates: HashSet::from([Coordinate { - identifier: "ngit".to_string(), - public_key: PublicKey::parse( - "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", - ) - .unwrap(), - kind: nostr_sdk::Kind::GitRepoAnnouncement, - relays: vec![ - "wss://nos.lol/".to_string(), - "wss://relay.damus.io/".to_string(), - ], - }]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - - #[test] - fn with_server_protocol() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(false)]), - protocol: Some(ServerProtocol::Ssh), - user: None, - }, - ); - Ok(()) - } - #[test] - fn with_server_protocol_and_user() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit?protocol=ssh&user=fred" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(false)]), - protocol: Some(ServerProtocol::Ssh), - user: Some("fred".to_string()), - }, - ); - Ok(()) - } - } - mod with_parameters_embedded_with_slashes { - use super::*; - - #[test] - fn with_relay_without_scheme_defaults_to_wss() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/nos.lol/ngit" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(true)]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - - #[test] - fn with_encoded_relay() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/ngit", - urlencoding::encode("wss://nos.lol") - ))?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(true)]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - #[test] - fn with_multiple_encoded_relays() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str(&format!( - "nostr://npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/{}/{}/ngit", - urlencoding::encode("wss://nos.lol"), - urlencoding::encode("wss://relay.damus.io"), - ))?, - NostrUrlDecoded { - coordinates: HashSet::from([Coordinate { - identifier: "ngit".to_string(), - public_key: PublicKey::parse( - "npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr", - ) - .unwrap(), - kind: nostr_sdk::Kind::GitRepoAnnouncement, - relays: vec![ - "wss://nos.lol/".to_string(), - "wss://relay.damus.io/".to_string(), - ], - }]), - protocol: None, - user: None, - }, - ); - Ok(()) - } - - #[test] - fn with_server_protocol() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(false)]), - protocol: Some(ServerProtocol::Ssh), - user: None, - }, - ); - Ok(()) - } - #[test] - fn with_server_protocol_and_user() -> Result<()> { - assert_eq!( - NostrUrlDecoded::from_str( - "nostr://fred@ssh/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit" - )?, - NostrUrlDecoded { - coordinates: HashSet::from([get_model_coordinate(false)]), - protocol: Some(ServerProtocol::Ssh), - user: Some("fred".to_string()), - }, - ); - Ok(()) - } - } - } - } - mod refspec_to_from_to { use super::*; -- cgit v1.2.3