// use super::*; use config::{ server::{Webhook, WebhookUrl}, BranchName, ForgeAlias, ForgeConfig, ForgeType, GitDir, RepoAlias, RepoBranches, RepoConfig, ServerRepoConfig, WebhookAuth, WebhookId, }; use git::{ repository::{open::MockOpenRepositoryLike, Direction}, Generation, GitRemote, MockForgeLike, RepoDetails, }; use git_next_git::repository::RepositoryLike as _; use mockall::predicate::eq; use rand::RngCore; use std::{ collections::HashMap, path::PathBuf, sync::{Arc, Mutex}, }; pub fn has_all_valid_remote_defaults( open_repository: &mut MockOpenRepositoryLike, repo_details: &RepoDetails, ) { has_remote_defaults( open_repository, HashMap::from([ (Direction::Push, Some(repo_details.git_remote())), (Direction::Fetch, Some(repo_details.git_remote())), ]), ); } pub fn has_remote_defaults( open_repository: &mut MockOpenRepositoryLike, remotes: HashMap>, ) { remotes.into_iter().for_each(|(direction, remote)| { open_repository .expect_find_default_remote() .with(eq(direction)) .return_once(|_| remote); }); } #[allow(clippy::type_complexity)] pub fn open_repository_for_loading_config_from_repo( repo_config: &RepoConfig, ) -> ( MockOpenRepositoryLike, Arc>>, ) { let mut load_config_from_repo_open_repository = MockOpenRepositoryLike::new(); let read_files = Arc::new(Mutex::new(vec![])); let read_files_ref = read_files.clone(); let branches = repo_config.branches().clone(); load_config_from_repo_open_repository .expect_read_file() .return_once(move |branch_name, file_name| { let branch_name = branch_name.clone(); let file_name = file_name.to_path_buf(); let _ = read_files_ref .lock() .map(move |mut l| l.push((branch_name, file_name))); let contents = format!( r#" [branches] main = "{}" next = "{}" dev = "{}" "#, branches.main(), branches.next(), branches.dev() ); Ok(contents) }); let branches = repo_config.branches().clone(); let remote_branches = vec![branches.main(), branches.next(), branches.dev()]; load_config_from_repo_open_repository .expect_remote_branches() .return_once(move || Ok(remote_branches)); (load_config_from_repo_open_repository, read_files) } pub fn a_webhook_auth() -> WebhookAuth { WebhookAuth::generate() } pub fn repo_branches() -> RepoBranches { RepoBranches::new( format!("main-{}", a_name()), format!("next-{}", a_name()), format!("dev-{}", a_name()), ) } pub fn a_forge_alias() -> ForgeAlias { ForgeAlias::new(a_name()) } pub fn a_repo_alias() -> RepoAlias { RepoAlias::new(a_name()) } pub fn a_network() -> kxio::network::MockNetwork { kxio::network::MockNetwork::new() } pub fn a_webhook_url( forge_alias: &ForgeAlias, repo_alias: &RepoAlias, ) -> git_next_config::server::WebhookUrl { config::server::Webhook::new(a_name()).url(forge_alias, repo_alias) } pub fn a_name() -> String { use rand::Rng; use std::iter; fn generate(len: usize) -> String { const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; let mut rng = rand::thread_rng(); let one_char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char; iter::repeat_with(one_char).take(len).collect() } generate(5) } pub fn a_webhook_id() -> WebhookId { WebhookId::new(a_name()) } pub fn a_branch_name(prefix: impl Into) -> BranchName { BranchName::new(format!("{}-{}", prefix.into(), a_name())) } pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir { let dir_name = a_name(); let dir = fs.base().join(dir_name); GitDir::new(dir) } pub fn a_forge_config() -> ForgeConfig { ForgeConfig::new( ForgeType::MockForge, a_name(), a_name(), a_name(), Default::default(), // no repos ) } pub fn a_server_repo_config() -> ServerRepoConfig { let main = a_branch_name("main").to_string(); let next = a_branch_name("next").to_string(); let dev = a_branch_name("dev").to_string(); ServerRepoConfig::new( format!("{}/{}", a_name(), a_name()), main.clone(), None, Some(main), Some(next), Some(dev), ) } pub fn a_repo_config() -> RepoConfig { RepoConfig::new(given::repo_branches(), config::RepoConfigSource::Repo) } pub fn a_named_commit(name: impl Into) -> git::Commit { git::Commit::new(a_named_commit_sha(name), a_commit_message()) } pub fn a_commit() -> git::Commit { git::Commit::new(a_commit_sha(), a_commit_message()) } pub fn a_commit_with_message(message: impl Into) -> git::Commit { git::Commit::new(a_commit_sha(), message.into()) } pub fn a_commit_message() -> git::commit::Message { git::commit::Message::new(a_name()) } pub fn a_named_commit_sha(name: impl Into) -> git::commit::Sha { git::commit::Sha::new(format!("{}-{}", name.into(), a_name())) } pub fn a_commit_sha() -> git::commit::Sha { git::commit::Sha::new(a_name()) } pub fn a_filesystem() -> kxio::fs::FileSystem { kxio::fs::temp().unwrap_or_else(|e| panic!("{}", e)) } pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails { let generation = git::Generation::default(); let repo_alias = a_repo_alias(); let server_repo_config = a_server_repo_config(); let forge_alias = a_forge_alias(); let forge_config = a_forge_config(); let gitdir = a_git_dir(fs); RepoDetails::new( generation, &repo_alias, &server_repo_config, &forge_alias, &forge_config, gitdir, ) } pub fn an_open_repository( fs: &kxio::fs::FileSystem, ) -> ( git::repository::open::MockOpenRepositoryLike, git::RepoDetails, ) { let open_repository = MockOpenRepositoryLike::new(); let gitdir = given::a_git_dir(fs); let hostname = given::a_hostname(); let repo_details = given::repo_details(fs) .with_gitdir(gitdir) .with_hostname(hostname); (open_repository, repo_details) } pub fn a_message_token() -> MessageToken { MessageToken::default() } pub fn a_webhook(url: &WebhookUrl) -> Webhook { Webhook::new(url.clone().into()) } pub fn a_forge() -> Box { Box::new(MockForgeLike::new()) } pub fn a_repo_actor( repo_details: git::RepoDetails, repository_factory: Box, forge: Box, net: kxio::network::Network, ) -> (actor::RepoActor, RepoActorLog) { let forge_alias = repo_details.forge.forge_alias(); let repo_alias = &repo_details.repo_alias; let webhook_url = given::a_webhook_url(forge_alias, repo_alias); let webhook = given::a_webhook(&webhook_url); let generation = Generation::default(); let log = Arc::new(Mutex::new(vec![])); let actors_log = log.clone(); ( actor::RepoActor::new( repo_details, forge, webhook, generation, net, repository_factory, std::time::Duration::from_nanos(1), ) .with_log(actors_log), log, ) } pub fn a_hostname() -> config::Hostname { config::Hostname::new(given::a_name()) } pub fn a_registered_webhook() -> RegisteredWebhook { RegisteredWebhook::new(given::a_webhook_id(), given::a_webhook_auth()) }