From f294a249307accc0f173917989ce27c27f0c6792 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 12 Dec 2024 15:19:40 +0000 Subject: 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 --- Cargo.lock | 1 + test_utils/Cargo.toml | 1 + test_utils/src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++------ tests/git_remote_nostr/push.rs | 6 ++--- 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 = [ "once_cell", "rand", "rexpect", + "sha2", "simple-websockets", "strip-ansi-escapes", "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", strip-ansi-escapes = "0.2.0" tokio = { version = "1.40.0", features = ["full"] } tungstenite = "0.20.1" +sha2 = "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}; use dialoguer::theme::{ColorfulTheme, Theme}; use futures::executor::block_on; use git::GitTestRepo; +use git2::{Signature, Time}; use nostr::{self, nips::nip65::RelayMetadata, Kind, Tag}; use nostr_database::NostrEventsDatabase; use nostr_lmdb::NostrLMDB; @@ -1226,21 +1227,64 @@ pub fn create_and_populate_branch( test_repo.checkout("main")?; test_repo.create_branch(branch_name)?; test_repo.checkout(branch_name)?; - std::fs::write( - test_repo.dir.join(format!("{}3.md", prefix)), - "some content", + let file_name = format!("{}3.md", prefix); + std::fs::write(test_repo.dir.join(&file_name), "some content")?; + test_repo.stage_and_commit_custom_signature( + &format!("add {}3.md", prefix), + Some( + &Signature::new( + "Joe Bloggs", + "joe.bloggs@pm.me", + &Time::new(deterministic_timestamp(&file_name), 0), + ) + .unwrap(), + ), + None, )?; - test_repo.stage_and_commit(format!("add {}3.md", prefix).as_str())?; if !only_one_commit { - std::fs::write( - test_repo.dir.join(format!("{}4.md", prefix)), - "some content", + let file_name = format!("{}4.md", prefix); + std::fs::write(test_repo.dir.join(&file_name), "some content")?; + test_repo.stage_and_commit_custom_signature( + &format!("add {}4.md", prefix), + Some( + &Signature::new( + "Joe Bloggs", + "joe.bloggs@pm.me", + &Time::new(deterministic_timestamp(&file_name), 0), + ) + .unwrap(), + ), + None, )?; - test_repo.stage_and_commit(format!("add {}4.md", prefix).as_str())?; } Ok(()) } +use sha2::{Digest, Sha256}; + +fn deterministic_timestamp(input: &str) -> i64 { + // Create a SHA-256 hasher + let mut hasher = Sha256::new(); + + // Hash the input string + hasher.update(input); + + // Get the hash result + let result = hasher.finalize(); + + // Convert the first 8 bytes of the hash to an i64 + let timestamp_bytes = &result[..8]; + let timestamp = i64::from_be_bytes( + timestamp_bytes + .try_into() + .expect("slice with incorrect length"), + ); + + // Normalize the timestamp to a valid Unix timestamp + // You can adjust this to fit your needs, e.g., adding a base timestamp + timestamp.abs() % 1_000_000_000 // Keep it within a reasonable range +} + pub fn cli_tester_create_proposal( test_repo: &GitTestRepo, 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 // HashSet::::from_iter(vec![ // "merge-commit-id".to_string(), // "6bd4f54bdf6a9ef2ec09e88e7a8d05376b0c1ff4".to_string(), - // "eb5d67886abad23c259ebd684c2bba233f9ed3d1".to_string(), + // "f1d302586abad23c259ebd684c2bba233f9ed3d1".to_string(), // ]), HashSet::from_iter( [ @@ -1292,7 +1292,7 @@ async fn push_2_commits_to_existing_proposal() -> Result<()> { assert_eq!( output, - format!(" eb5d678..7de5e41 {branch_name} -> {branch_name}\r\n").as_str(), + format!(" 2d1b467..9d83ff4 {branch_name} -> {branch_name}\r\n").as_str(), ); let new_events = r55 @@ -1447,7 +1447,7 @@ async fn force_push_creates_proposal_revision() -> Result<()> { assert_eq!( output, - format!(" + eb5d678...8a296c8 {branch_name} -> {branch_name} (forced update)\r\n").as_str(), + format!(" + 2d1b467...ead85e0 {branch_name} -> {branch_name} (forced update)\r\n").as_str(), ); let new_events = r55 -- cgit v1.2.3