upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/common
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-11-21 05:18:15 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-11-21 05:34:56 +0000
commit7a81643367515a9d01eb2d4deb623e9a7c071a12 (patch)
tree88d6f2e69ca051d2c94269f4753e2ef807882438 /tests/common
parent7dda553918705277c7fa5b903c6a40e4b4a0aa8d (diff)
add repository creation
Diffstat (limited to 'tests/common')
-rw-r--r--tests/common/relay.rs37
1 files changed, 35 insertions, 2 deletions
diff --git a/tests/common/relay.rs b/tests/common/relay.rs
index 7185acd..6b512cd 100644
--- a/tests/common/relay.rs
+++ b/tests/common/relay.rs
@@ -2,6 +2,8 @@
2//! 2//!
3//! Provides automatic relay lifecycle management for integration tests. 3//! Provides automatic relay lifecycle management for integration tests.
4 4
5use nostr_sdk::ToBech32;
6use std::path::PathBuf;
5use std::process::{Child, Command, Stdio}; 7use std::process::{Child, Command, Stdio};
6use std::time::Duration; 8use std::time::Duration;
7use tokio::time::sleep; 9use tokio::time::sleep;
@@ -9,11 +11,12 @@ use tokio::time::sleep;
9/// Test relay fixture that manages relay lifecycle 11/// Test relay fixture that manages relay lifecycle
10/// 12///
11/// Automatically starts and stops the ngit-grasp relay for testing. 13/// Automatically starts and stops the ngit-grasp relay for testing.
12/// Uses a random port to avoid conflicts. 14/// Uses a random port to avoid conflicts and cleans up created repositories.
13pub struct TestRelay { 15pub struct TestRelay {
14 process: Child, 16 process: Child,
15 url: String, 17 url: String,
16 port: u16, 18 port: u16,
19 git_data_dir: tempfile::TempDir,
17} 20}
18 21
19impl TestRelay { 22impl TestRelay {
@@ -40,6 +43,10 @@ impl TestRelay {
40 let bind_address = format!("127.0.0.1:{}", port); 43 let bind_address = format!("127.0.0.1:{}", port);
41 let url = format!("ws://127.0.0.1:{}", port); 44 let url = format!("ws://127.0.0.1:{}", port);
42 45
46 // Create temporary directory for git repositories
47 let git_data_dir = tempfile::tempdir()
48 .expect("Failed to create temporary git data directory");
49
43 // Use the built binary directly (faster than cargo run) 50 // Use the built binary directly (faster than cargo run)
44 let binary_path = std::env::current_exe() 51 let binary_path = std::env::current_exe()
45 .expect("Failed to get current exe") 52 .expect("Failed to get current exe")
@@ -49,17 +56,29 @@ impl TestRelay {
49 .expect("Failed to get grandparent dir") 56 .expect("Failed to get grandparent dir")
50 .join("ngit-grasp"); 57 .join("ngit-grasp");
51 58
59 // Generate a test owner npub (using a random keypair)
60 let test_keys = nostr_sdk::Keys::generate();
61 let test_npub = test_keys.public_key().to_bech32()
62 .expect("Failed to generate test npub");
63
52 // Start the relay process 64 // Start the relay process
53 let process = Command::new(&binary_path) 65 let process = Command::new(&binary_path)
54 .env("NGIT_BIND_ADDRESS", &bind_address) 66 .env("NGIT_BIND_ADDRESS", &bind_address)
55 .env("NGIT_DOMAIN", &bind_address) // Set domain to match bind address 67 .env("NGIT_DOMAIN", &bind_address) // Set domain to match bind address
68 .env("NGIT_GIT_DATA_PATH", git_data_dir.path())
69 .env("NGIT_OWNER_NPUB", &test_npub)
56 .env("RUST_LOG", "warn") // Less logging during tests 70 .env("RUST_LOG", "warn") // Less logging during tests
57 .stdout(Stdio::null()) 71 .stdout(Stdio::null())
58 .stderr(Stdio::null()) 72 .stderr(Stdio::null())
59 .spawn() 73 .spawn()
60 .expect("Failed to start relay process"); 74 .expect("Failed to start relay process");
61 75
62 let relay = Self { process, url, port }; 76 let relay = Self {
77 process,
78 url,
79 port,
80 git_data_dir,
81 };
63 82
64 // Wait for relay to be ready 83 // Wait for relay to be ready
65 relay.wait_for_ready().await; 84 relay.wait_for_ready().await;
@@ -82,6 +101,20 @@ impl TestRelay {
82 format!("127.0.0.1:{}", self.port) 101 format!("127.0.0.1:{}", self.port)
83 } 102 }
84 103
104 /// Get the git data directory path
105 pub fn git_data_dir(&self) -> &std::path::Path {
106 self.git_data_dir.path()
107 }
108
109 /// Get the expected repository path for a given npub and repo identifier
110 ///
111 /// Repositories are stored at: <git_data_dir>/<npub>/<identifier>.git
112 pub fn repo_path(&self, npub: &str, identifier: &str) -> PathBuf {
113 self.git_data_dir.path()
114 .join(npub)
115 .join(format!("{}.git", identifier))
116 }
117
85 /// Wait for the relay to be ready to accept connections 118 /// Wait for the relay to be ready to accept connections
86 async fn wait_for_ready(&self) { 119 async fn wait_for_ready(&self) {
87 let max_attempts = 50; // 5 seconds total 120 let max_attempts = 50; // 5 seconds total