upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/nip01_compliance.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2025-11-20 22:15:03 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2025-11-20 22:20:28 +0000
commit519fdc66930280cd1772417dca327ed858333d64 (patch)
tree4b20e18ccbc7406106bc72316dc3e26f2b58495f /tests/nip01_compliance.rs
parentca50f5b98f30d0933a510c05db86b608afee73a0 (diff)
refactor: isolate each grasp-audit lib test with minimal boilerplate
- Add isolated_test! macro pattern to nip34_announcements.rs and nip01_compliance.rs - Each test runs with its own fresh relay instance for complete isolation - Make all individual test functions public in grasp-audit library (nip01_smoke.rs, event_acceptance_policy.rs) - Eliminates 122 lines of boilerplate across integration tests - Tests: 15 GRASP-01 event acceptance policy tests + 6 NIP-01 smoke tests - Ensures tests don't interfere with each other, preventing flakiness
Diffstat (limited to 'tests/nip01_compliance.rs')
-rw-r--r--tests/nip01_compliance.rs120
1 files changed, 37 insertions, 83 deletions
diff --git a/tests/nip01_compliance.rs b/tests/nip01_compliance.rs
index 4cb2af4..6fb721a 100644
--- a/tests/nip01_compliance.rs
+++ b/tests/nip01_compliance.rs
@@ -1,13 +1,13 @@
1//! NIP-01 Compliance Integration Tests 1//! NIP-01 Compliance Integration Tests
2//! 2//!
3//! Tests ngit-grasp relay's NIP-01 compliance using grasp-audit library. 3//! Tests ngit-grasp relay's NIP-01 compliance using grasp-audit library.
4//! Avoids code duplication by delegating to grasp-audit's test suite. 4//! Uses isolated test pattern for complete test independence.
5//! 5//!
6//! # Test Strategy 6//! # Test Strategy
7//! 7//!
8//! - Uses TestRelay fixture for ngit-grasp relay lifecycle management 8//! - Each test runs in complete isolation with its own fresh relay instance
9//! - Uses grasp-audit's Nip01SmokeTests for actual test logic 9//! - Uses macro to eliminate boilerplate while maintaining test isolation
10//! - Minimal duplication - single source of truth in grasp-audit 10//! - Calls individual test methods from grasp-audit for minimal duplication
11//! 11//!
12//! # Running Tests 12//! # Running Tests
13//! 13//!
@@ -16,7 +16,7 @@
16//! cargo test --test nip01_compliance 16//! cargo test --test nip01_compliance
17//! 17//!
18//! # Run specific test 18//! # Run specific test
19//! cargo test --test nip01_compliance test_nip01_smoke 19//! cargo test --test nip01_compliance test_websocket_connection
20//! 20//!
21//! # With output 21//! # With output
22//! cargo test --test nip01_compliance -- --nocapture 22//! cargo test --test nip01_compliance -- --nocapture
@@ -27,87 +27,41 @@ mod common;
27use common::TestRelay; 27use common::TestRelay;
28use grasp_audit::*; 28use grasp_audit::*;
29 29
30/// Test NIP-01 smoke tests against ngit-grasp relay 30/// Macro to generate isolated integration tests
31/// 31///
32/// This test runs all NIP-01 smoke tests from grasp-audit against 32/// Each test runs with its own fresh relay instance to ensure complete isolation.
33/// the ngit-grasp relay implementation. 33/// This eliminates flakiness and ensures tests don't interfere with each other.
34/// 34macro_rules! isolated_test {
35/// Tests cover: 35 ($test_name:ident) => {
36/// - WebSocket connection 36 #[tokio::test]
37/// - Event send/receive 37 async fn $test_name() {
38/// - Subscriptions (REQ/CLOSE) 38 let relay = TestRelay::start().await;
39/// - Event validation (signature, ID) 39 let config = AuditConfig::ci();
40#[tokio::test] 40 let client = AuditClient::new(relay.url(), config)
41async fn test_nip01_smoke() { 41 .await
42 // Start test relay 42 .expect("Failed to create audit client");
43 let relay = TestRelay::start().await; 43
44 44 let result = specs::Nip01SmokeTests::$test_name(&client).await;
45 // Create audit client in CI mode (isolated testing) 45
46 let config = AuditConfig::ci(); 46 relay.stop().await;
47 let client = AuditClient::new(relay.url(), config) 47
48 .await 48 assert!(
49 .expect("Failed to create audit client"); 49 result.passed,
50 50 "{} failed: {}",
51 // Run all NIP-01 smoke tests 51 stringify!($test_name),
52 let results = specs::Nip01SmokeTests::run_all(&client).await; 52 result.error.as_deref().unwrap_or("unknown error")
53 53 );
54 // Print detailed report 54 }
55 results.print_report(); 55 };
56
57 // Stop relay
58 relay.stop().await;
59
60 // Assert all tests passed
61 assert!(
62 results.all_passed(),
63 "NIP-01 smoke tests failed: {}/{} passed",
64 results.passed_count(),
65 results.total_count()
66 );
67} 56}
68 57
69/// Test that relay properly validates events 58// Generate isolated tests for all NIP-01 smoke tests
70/// 59isolated_test!(test_websocket_connection);
71/// Critical security test - ensures relay validates: 60isolated_test!(test_send_receive_event);
72/// - Event signatures 61isolated_test!(test_create_subscription);
73/// - Event IDs 62isolated_test!(test_close_subscription);
74/// - Other NIP-01 requirements 63isolated_test!(test_reject_invalid_signature);
75#[tokio::test] 64isolated_test!(test_reject_invalid_event_id);
76async fn test_relay_validates_events() {
77 let relay = TestRelay::start().await;
78 let config = AuditConfig::ci();
79 let client = AuditClient::new(relay.url(), config)
80 .await
81 .expect("Failed to create audit client");
82
83 // Run smoke tests which include validation tests
84 let results = specs::Nip01SmokeTests::run_all(&client).await;
85
86 relay.stop().await;
87
88 // Filter to validation tests
89 let validation_tests: Vec<_> = results
90 .results
91 .iter()
92 .filter(|t| t.name.contains("reject") || t.name.contains("invalid"))
93 .collect();
94
95 // Should have validation tests
96 assert!(
97 !validation_tests.is_empty(),
98 "No validation tests found (these are critical for security)"
99 );
100
101 // All validation tests should pass
102 for test in validation_tests {
103 assert!(
104 test.passed,
105 "Validation test failed: {} - {}\nThis is a security issue!",
106 test.name,
107 test.error.as_deref().unwrap_or("unknown error")
108 );
109 }
110}
111 65
112/// Test relay lifecycle management 66/// Test relay lifecycle management
113/// 67///