upleb.uk

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

summaryrefslogtreecommitdiff
path: root/test_utils/src
diff options
context:
space:
mode:
Diffstat (limited to 'test_utils/src')
-rw-r--r--test_utils/src/lib.rs116
1 files changed, 108 insertions, 8 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs
index e6ce9c6..087f849 100644
--- a/test_utils/src/lib.rs
+++ b/test_utils/src/lib.rs
@@ -2,6 +2,7 @@ use std::{
2 ffi::OsStr, 2 ffi::OsStr,
3 path::{Path, PathBuf}, 3 path::{Path, PathBuf},
4 str::FromStr, 4 str::FromStr,
5 time::Duration,
5}; 6};
6 7
7use anyhow::{bail, ensure, Context, Result}; 8use anyhow::{bail, ensure, Context, Result};
@@ -10,11 +11,12 @@ use futures::executor::block_on;
10use git::GitTestRepo; 11use git::GitTestRepo;
11use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag}; 12use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag};
12use nostr_database::{NostrDatabase, Order}; 13use nostr_database::{NostrDatabase, Order};
13use nostr_sdk::{serde_json, NostrSigner, TagStandard}; 14use nostr_sdk::{serde_json, Client, NostrSigner, TagStandard};
14use nostr_sqlite::SQLiteDatabase; 15use nostr_sqlite::SQLiteDatabase;
15use once_cell::sync::Lazy; 16use once_cell::sync::Lazy;
16use rexpect::session::{Options, PtySession}; 17use rexpect::session::{Options, PtySession};
17use strip_ansi_escapes::strip_str; 18use strip_ansi_escapes::strip_str;
19use tokio::runtime::Handle;
18 20
19pub mod git; 21pub mod git;
20pub mod relay; 22pub mod relay;
@@ -997,6 +999,7 @@ pub fn get_proposal_branch_name(
997pub static FEATURE_BRANCH_NAME_1: &str = "feature-example-t"; 999pub static FEATURE_BRANCH_NAME_1: &str = "feature-example-t";
998pub static FEATURE_BRANCH_NAME_2: &str = "feature-example-f"; 1000pub static FEATURE_BRANCH_NAME_2: &str = "feature-example-f";
999pub static FEATURE_BRANCH_NAME_3: &str = "feature-example-c"; 1001pub static FEATURE_BRANCH_NAME_3: &str = "feature-example-c";
1002pub static FEATURE_BRANCH_NAME_4: &str = "feature-example-d";
1000 1003
1001pub static PROPOSAL_TITLE_1: &str = "proposal a"; 1004pub static PROPOSAL_TITLE_1: &str = "proposal a";
1002pub static PROPOSAL_TITLE_2: &str = "proposal b"; 1005pub static PROPOSAL_TITLE_2: &str = "proposal b";
@@ -1119,23 +1122,27 @@ pub fn cli_tester_create_proposal(
1119} 1122}
1120 1123
1121/// returns (originating_repo, test_repo) 1124/// returns (originating_repo, test_repo)
1122pub fn create_proposals_and_repo_with_first_proposal_pulled_and_checkedout() 1125pub fn create_proposals_and_repo_with_proposal_pulled_and_checkedout(
1123-> Result<(GitTestRepo, GitTestRepo)> { 1126 proposal_number: u16,
1127) -> Result<(GitTestRepo, GitTestRepo)> {
1124 Ok(( 1128 Ok((
1125 cli_tester_create_proposals()?, 1129 cli_tester_create_proposals()?,
1126 create_repo_with_first_proposal_branch_pulled_and_checkedout()?, 1130 create_repo_with_proposal_branch_pulled_and_checkedout(proposal_number)?,
1127 )) 1131 ))
1128} 1132}
1129 1133
1130pub fn create_repo_with_first_proposal_branch_pulled_and_checkedout() -> Result<GitTestRepo> { 1134pub fn create_repo_with_proposal_branch_pulled_and_checkedout(
1135 proposal_number: u16,
1136) -> Result<GitTestRepo> {
1131 let test_repo = GitTestRepo::default(); 1137 let test_repo = GitTestRepo::default();
1132 test_repo.populate()?; 1138 test_repo.populate()?;
1133 use_ngit_list_to_download_and_checkout_first_proposal_branch(&test_repo)?; 1139 use_ngit_list_to_download_and_checkout_proposal_branch(&test_repo, proposal_number)?;
1134 Ok(test_repo) 1140 Ok(test_repo)
1135} 1141}
1136 1142
1137pub fn use_ngit_list_to_download_and_checkout_first_proposal_branch( 1143pub fn use_ngit_list_to_download_and_checkout_proposal_branch(
1138 test_repo: &GitTestRepo, 1144 test_repo: &GitTestRepo,
1145 proposal_number: u16,
1139) -> Result<()> { 1146) -> Result<()> {
1140 let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]); 1147 let mut p = CliTester::new_from_dir(&test_repo.dir, ["list"]);
1141 p.expect("fetching updates...\r\n")?; 1148 p.expect("fetching updates...\r\n")?;
@@ -1148,7 +1155,17 @@ pub fn use_ngit_list_to_download_and_checkout_first_proposal_branch(
1148 format!("\"{PROPOSAL_TITLE_1}\""), 1155 format!("\"{PROPOSAL_TITLE_1}\""),
1149 ], 1156 ],
1150 )?; 1157 )?;
1151 c.succeeds_with(2, true, None)?; 1158 c.succeeds_with(
1159 if proposal_number == 3 {
1160 0
1161 } else if proposal_number == 2 {
1162 1
1163 } else {
1164 2
1165 },
1166 true,
1167 None,
1168 )?;
1152 let mut c = p.expect_choice( 1169 let mut c = p.expect_choice(
1153 "", 1170 "",
1154 vec![ 1171 vec![
@@ -1162,3 +1179,86 @@ pub fn use_ngit_list_to_download_and_checkout_first_proposal_branch(
1162 p.expect_end_eventually()?; 1179 p.expect_end_eventually()?;
1163 Ok(()) 1180 Ok(())
1164} 1181}
1182
1183pub fn remove_latest_commit_so_proposal_branch_is_behind_and_checkout_main(
1184 test_repo: &GitTestRepo,
1185) -> Result<String> {
1186 let branch_name = test_repo.get_checked_out_branch_name()?;
1187 test_repo.checkout("main")?;
1188 test_repo.git_repo.branch(
1189 &branch_name,
1190 &test_repo
1191 .git_repo
1192 .find_commit(test_repo.get_tip_of_local_branch(&branch_name)?)?
1193 .parent(0)?,
1194 true,
1195 )?;
1196 Ok(branch_name)
1197}
1198
1199pub fn ammend_last_commit_and_checkout_main(test_repo: &GitTestRepo) -> Result<String> {
1200 let branch_name =
1201 remove_latest_commit_so_proposal_branch_is_behind_and_checkout_main(test_repo)?;
1202 // add another commit (so we have an ammened local branch)
1203 test_repo.checkout(&branch_name)?;
1204 std::fs::write(test_repo.dir.join("ammended-commit.md"), "some content")?;
1205 test_repo.stage_and_commit("add ammended-commit.md")?;
1206 test_repo.checkout("main")?;
1207 Ok(branch_name)
1208}
1209
1210pub fn create_proposals_with_first_rebased_and_repo_with_latest_main_and_unrebased_proposal()
1211-> Result<(GitTestRepo, GitTestRepo)> {
1212 let (_, test_repo) = create_proposals_and_repo_with_proposal_pulled_and_checkedout(1)?;
1213
1214 // get proposal id of first
1215 let client = Client::default();
1216 Handle::current().block_on(client.add_relay("ws://localhost:8055"))?;
1217 Handle::current().block_on(client.connect_relay("ws://localhost:8055"))?;
1218 let proposals = Handle::current().block_on(client.get_events_of(
1219 vec![
1220 nostr::Filter::default()
1221 .kind(nostr::Kind::Custom(PATCH_KIND))
1222 .custom_tag(
1223 nostr::SingleLetterTag::lowercase(nostr::Alphabet::T),
1224 vec!["root"],
1225 ),
1226 ],
1227 Some(Duration::from_millis(500)),
1228 ))?;
1229 Handle::current().block_on(client.disconnect())?;
1230
1231 let proposal_1_id = proposals
1232 .iter()
1233 .find(|e| {
1234 e.tags
1235 .iter()
1236 .any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1))
1237 })
1238 .unwrap()
1239 .id;
1240 // recreate proposal 1 on top of a another commit (like a rebase on top
1241 // of one extra commit)
1242 let second_originating_repo = GitTestRepo::default();
1243 second_originating_repo.populate()?;
1244 std::fs::write(
1245 second_originating_repo.dir.join("amazing.md"),
1246 "some content",
1247 )?;
1248 second_originating_repo.stage_and_commit("commit for rebasing on top of")?;
1249 cli_tester_create_proposal(
1250 &second_originating_repo,
1251 FEATURE_BRANCH_NAME_1,
1252 "a",
1253 Some((PROPOSAL_TITLE_1, "proposal a description")),
1254 Some(proposal_1_id.to_string()),
1255 )?;
1256
1257 // pretend we have pulled the updated main branch
1258 let branch_name = test_repo.get_checked_out_branch_name()?;
1259 test_repo.checkout("main")?;
1260 std::fs::write(test_repo.dir.join("amazing.md"), "some content")?;
1261 test_repo.stage_and_commit("commit for rebasing on top of")?;
1262 test_repo.checkout(&branch_name)?;
1263 Ok((second_originating_repo, test_repo))
1264}