use git_next_core::server::ListenUrl; // use super::*; pub fn has_all_valid_remote_defaults( open_repository: &mut MockOpenRepositoryLike, repo_details: &RepoDetails, ) { has_remote_defaults( open_repository, HashMap::from([ (Direction::Push, repo_details.remote_url()), (Direction::Fetch, repo_details.remote_url()), ]), ); } 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); }); } 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_listen_url() -> ListenUrl { ListenUrl::new(a_name()) } 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, StoragePathType::Internal) } 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(), RepoConfigSource::Repo) } pub fn a_named_commit(name: impl Into) -> Commit { Commit::new(a_named_commit_sha(name), a_commit_message()) } pub fn a_commit() -> Commit { Commit::new(a_commit_sha(), a_commit_message()) } pub fn a_commit_with_message(message: impl Into) -> Commit { Commit::new(a_commit_sha(), message.into()) } pub fn a_commit_message() -> crate::git::commit::Message { crate::git::commit::Message::new(a_name()) } pub fn a_named_commit_sha(name: impl Into) -> Sha { Sha::new(format!("{}-{}", name.into(), a_name())) } pub fn a_commit_sha() -> Sha { 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) -> RepoDetails { let generation = 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) -> (MockOpenRepositoryLike, 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_forge() -> Box { Box::new(MockForgeLike::new()) } pub fn a_repo_actor( repo_details: RepoDetails, repository_factory: Box, forge: Box, net: kxio::network::Network, ) -> (RepoActor, RepoActorLog) { let listen_url = given::a_listen_url(); let generation = Generation::default(); let log = RepoActorLog::default(); let actors_log = log.clone(); ( RepoActor::new( repo_details, forge, listen_url, generation, net, repository_factory, std::time::Duration::from_nanos(1), None, ) .with_log(actors_log), log, ) } pub fn a_hostname() -> Hostname { Hostname::new(given::a_name()) } pub fn a_registered_webhook() -> RegisteredWebhook { RegisteredWebhook::new(given::a_webhook_id(), given::a_webhook_auth()) } pub fn a_push() -> Push { Push::new( given::a_branch_name("push"), given::a_name(), given::a_name(), ) }