upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/test_utils
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils')
-rw-r--r--test_utils/Cargo.toml3
-rw-r--r--test_utils/src/lib.rs60
2 files changed, 62 insertions, 1 deletions
diff --git a/test_utils/Cargo.toml b/test_utils/Cargo.toml
index b7010c9..2e4c012 100644
--- a/test_utils/Cargo.toml
+++ b/test_utils/Cargo.toml
@@ -8,9 +8,12 @@ anyhow = "1.0.75"
8assert_cmd = "2.0.12" 8assert_cmd = "2.0.12"
9dialoguer = "0.10.4" 9dialoguer = "0.10.4"
10directories = "5.0.1" 10directories = "5.0.1"
11futures = "0.3.28"
11git2 = "0.18.1" 12git2 = "0.18.1"
12nostr = "0.33.0" 13nostr = "0.33.0"
14nostr-database = "0.33.0"
13nostr-sdk = "0.33.0" 15nostr-sdk = "0.33.0"
16nostr-sqlite = "0.33.0"
14once_cell = "1.18.0" 17once_cell = "1.18.0"
15rand = "0.8" 18rand = "0.8"
16rexpect = { git = "https://github.com/rust-cli/rexpect.git", rev = "9eb61dd" } 19rexpect = { git = "https://github.com/rust-cli/rexpect.git", rev = "9eb61dd" }
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs
index a2ed5f2..de7aee5 100644
--- a/test_utils/src/lib.rs
+++ b/test_utils/src/lib.rs
@@ -1,9 +1,17 @@
1use std::{ffi::OsStr, path::PathBuf, str::FromStr}; 1use std::{
2 ffi::OsStr,
3 path::{Path, PathBuf},
4 str::FromStr,
5};
2 6
3use anyhow::{bail, ensure, Context, Result}; 7use anyhow::{bail, ensure, Context, Result};
4use dialoguer::theme::{ColorfulTheme, Theme}; 8use dialoguer::theme::{ColorfulTheme, Theme};
9use futures::executor::block_on;
10use git::GitTestRepo;
5use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag}; 11use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag};
12use nostr_database::{NostrDatabase, Order};
6use nostr_sdk::{serde_json, NostrSigner, TagStandard}; 13use nostr_sdk::{serde_json, NostrSigner, TagStandard};
14use nostr_sqlite::SQLiteDatabase;
7use once_cell::sync::Lazy; 15use once_cell::sync::Lazy;
8use rexpect::session::{Options, PtySession}; 16use rexpect::session::{Options, PtySession};
9use strip_ansi_escapes::strip_str; 17use strip_ansi_escapes::strip_str;
@@ -935,3 +943,53 @@ where
935 }, 943 },
936 ) 944 )
937} 945}
946
947/** copied from client.rs */
948async fn get_local_cache_database(git_repo_path: &Path) -> Result<SQLiteDatabase> {
949 SQLiteDatabase::open(git_repo_path.join(".git/nostr-cache.sqlite"))
950 .await
951 .context("cannot open or create nostr cache database at .git/nostr-cache.sqlite")
952}
953
954/** copied from client.rs */
955pub async fn get_events_from_cache(
956 git_repo_path: &Path,
957 filters: Vec<nostr::Filter>,
958) -> Result<Vec<nostr::Event>> {
959 get_local_cache_database(git_repo_path)
960 .await?
961 .query(filters.clone(), Order::Asc)
962 .await
963 .context(
964 "cannot execute query on opened git repo nostr cache database .git/nostr-cache.sqlite",
965 )
966}
967
968pub fn get_proposal_branch_name(
969 test_repo: &GitTestRepo,
970 branch_name_in_event: &str,
971) -> Result<String> {
972 let events = block_on(get_events_from_cache(
973 &test_repo.dir,
974 vec![
975 nostr::Filter::default()
976 .kind(nostr_sdk::Kind::GitPatch)
977 .hashtag("root"),
978 ],
979 ))?;
980 for event in events {
981 if event.iter_tags().any(|t| {
982 !t.as_vec()[1].eq("revision-root")
983 && event.iter_tags().any(|t| {
984 t.as_vec()[0].eq("branch-name") && t.as_vec()[1].eq(branch_name_in_event)
985 })
986 }) {
987 return Ok(format!(
988 "prs/{}({})",
989 branch_name_in_event,
990 &event.id.to_hex().as_str()[..8],
991 ));
992 }
993 }
994 bail!("cannot find proposal root with branch-name tag matching title")
995}