upleb.uk

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

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-12-12 15:19:40 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-12-13 12:37:20 +0000
commitf294a249307accc0f173917989ce27c27f0c6792 (patch)
tree04df321a504ff1108f6c06e69a703577051d7dde
parentaeb164a6890a433828739bde23d3477b78e532a4 (diff)
test: deterministic commit timestamps in test PRs
now that the remote helper uses the commit author timestamp to determine if commit was applied from a proposal, the timestamps in the test proposal can no longer be identical. this makes them deterministically different based on the file name
-rw-r--r--Cargo.lock1
-rw-r--r--test_utils/Cargo.toml1
-rw-r--r--test_utils/src/lib.rs60
-rw-r--r--tests/git_remote_nostr/push.rs6
4 files changed, 57 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 819a249..51a7472 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3314,6 +3314,7 @@ dependencies = [
3314 "once_cell", 3314 "once_cell",
3315 "rand", 3315 "rand",
3316 "rexpect", 3316 "rexpect",
3317 "sha2",
3317 "simple-websockets", 3318 "simple-websockets",
3318 "strip-ansi-escapes", 3319 "strip-ansi-escapes",
3319 "tokio", 3320 "tokio",
diff --git a/test_utils/Cargo.toml b/test_utils/Cargo.toml
index 672dcdb..b354ff4 100644
--- a/test_utils/Cargo.toml
+++ b/test_utils/Cargo.toml
@@ -21,3 +21,4 @@ simple-websockets = { git = "https://github.com/DanConwayDev/simple-websockets",
21strip-ansi-escapes = "0.2.0" 21strip-ansi-escapes = "0.2.0"
22tokio = { version = "1.40.0", features = ["full"] } 22tokio = { version = "1.40.0", features = ["full"] }
23tungstenite = "0.20.1" 23tungstenite = "0.20.1"
24sha2 = "0.10" \ No newline at end of file
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs
index 0c5de73..3ac171b 100644
--- a/test_utils/src/lib.rs
+++ b/test_utils/src/lib.rs
@@ -10,6 +10,7 @@ use anyhow::{bail, ensure, Context, Result};
10use dialoguer::theme::{ColorfulTheme, Theme}; 10use dialoguer::theme::{ColorfulTheme, Theme};
11use futures::executor::block_on; 11use futures::executor::block_on;
12use git::GitTestRepo; 12use git::GitTestRepo;
13use git2::{Signature, Time};
13use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag}; 14use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag};
14use nostr_database::NostrEventsDatabase; 15use nostr_database::NostrEventsDatabase;
15use nostr_lmdb::NostrLMDB; 16use nostr_lmdb::NostrLMDB;
@@ -1226,21 +1227,64 @@ pub fn create_and_populate_branch(
1226 test_repo.checkout("main")?; 1227 test_repo.checkout("main")?;
1227 test_repo.create_branch(branch_name)?; 1228 test_repo.create_branch(branch_name)?;
1228 test_repo.checkout(branch_name)?; 1229 test_repo.checkout(branch_name)?;
1229 std::fs::write( 1230 let file_name = format!("{}3.md", prefix);
1230 test_repo.dir.join(format!("{}3.md", prefix)), 1231 std::fs::write(test_repo.dir.join(&file_name), "some content")?;
1231 "some content", 1232 test_repo.stage_and_commit_custom_signature(
1233 &format!("add {}3.md", prefix),
1234 Some(
1235 &Signature::new(
1236 "Joe Bloggs",
1237 "joe.bloggs@pm.me",
1238 &Time::new(deterministic_timestamp(&file_name), 0),
1239 )
1240 .unwrap(),
1241 ),
1242 None,
1232 )?; 1243 )?;
1233 test_repo.stage_and_commit(format!("add {}3.md", prefix).as_str())?;
1234 if !only_one_commit { 1244 if !only_one_commit {
1235 std::fs::write( 1245 let file_name = format!("{}4.md", prefix);
1236 test_repo.dir.join(format!("{}4.md", prefix)), 1246 std::fs::write(test_repo.dir.join(&file_name), "some content")?;
1237 "some content", 1247 test_repo.stage_and_commit_custom_signature(
1248 &format!("add {}4.md", prefix),
1249 Some(
1250 &Signature::new(
1251 "Joe Bloggs",
1252 "joe.bloggs@pm.me",
1253 &Time::new(deterministic_timestamp(&file_name), 0),
1254 )
1255 .unwrap(),
1256 ),
1257 None,
1238 )?; 1258 )?;
1239 test_repo.stage_and_commit(format!("add {}4.md", prefix).as_str())?;
1240 } 1259 }
1241 Ok(()) 1260 Ok(())
1242} 1261}
1243 1262
1263use sha2::{Digest, Sha256};
1264
1265fn deterministic_timestamp(input: &str) -> i64 {
1266 // Create a SHA-256 hasher
1267 let mut hasher = Sha256::new();
1268
1269 // Hash the input string
1270 hasher.update(input);
1271
1272 // Get the hash result
1273 let result = hasher.finalize();
1274
1275 // Convert the first 8 bytes of the hash to an i64
1276 let timestamp_bytes = &result[..8];
1277 let timestamp = i64::from_be_bytes(
1278 timestamp_bytes
1279 .try_into()
1280 .expect("slice with incorrect length"),
1281 );
1282
1283 // Normalize the timestamp to a valid Unix timestamp
1284 // You can adjust this to fit your needs, e.g., adding a base timestamp
1285 timestamp.abs() % 1_000_000_000 // Keep it within a reasonable range
1286}
1287
1244pub fn cli_tester_create_proposal( 1288pub fn cli_tester_create_proposal(
1245 test_repo: &GitTestRepo, 1289 test_repo: &GitTestRepo,
1246 branch_name: &str, 1290 branch_name: &str,
diff --git a/tests/git_remote_nostr/push.rs b/tests/git_remote_nostr/push.rs
index e5d1b13..a8ea0ac 100644
--- a/tests/git_remote_nostr/push.rs
+++ b/tests/git_remote_nostr/push.rs
@@ -1179,7 +1179,7 @@ async fn proposal_fast_forward_merge_commits_pushed_to_main_leads_to_status_even
1179 // HashSet::<String>::from_iter(vec![ 1179 // HashSet::<String>::from_iter(vec![
1180 // "merge-commit-id".to_string(), 1180 // "merge-commit-id".to_string(),
1181 // "6bd4f54bdf6a9ef2ec09e88e7a8d05376b0c1ff4".to_string(), 1181 // "6bd4f54bdf6a9ef2ec09e88e7a8d05376b0c1ff4".to_string(),
1182 // "eb5d67886abad23c259ebd684c2bba233f9ed3d1".to_string(), 1182 // "f1d302586abad23c259ebd684c2bba233f9ed3d1".to_string(),
1183 // ]), 1183 // ]),
1184 HashSet::from_iter( 1184 HashSet::from_iter(
1185 [ 1185 [
@@ -1292,7 +1292,7 @@ async fn push_2_commits_to_existing_proposal() -> Result<()> {
1292 1292
1293 assert_eq!( 1293 assert_eq!(
1294 output, 1294 output,
1295 format!(" eb5d678..7de5e41 {branch_name} -> {branch_name}\r\n").as_str(), 1295 format!(" 2d1b467..9d83ff4 {branch_name} -> {branch_name}\r\n").as_str(),
1296 ); 1296 );
1297 1297
1298 let new_events = r55 1298 let new_events = r55
@@ -1447,7 +1447,7 @@ async fn force_push_creates_proposal_revision() -> Result<()> {
1447 1447
1448 assert_eq!( 1448 assert_eq!(
1449 output, 1449 output,
1450 format!(" + eb5d678...8a296c8 {branch_name} -> {branch_name} (forced update)\r\n").as_str(), 1450 format!(" + 2d1b467...ead85e0 {branch_name} -> {branch_name} (forced update)\r\n").as_str(),
1451 ); 1451 );
1452 1452
1453 let new_events = r55 1453 let new_events = r55