From 5d02ad6b893f9059044914c115d77cf9d8e589c3 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 8 Jan 2026 11:20:35 +0000 Subject: refactor: replace hardcoded Kind constants with rust-nostr variants - Replace KIND_REPOSITORY_ANNOUNCEMENT with Kind::GitRepoAnnouncement - Replace KIND_REPOSITORY_STATE with Kind::RepoState - Replace KIND_PR with Kind::GitPullRequest - Replace KIND_PR_UPDATE with Kind::GitPullRequestUpdate - Replace KIND_USER_GRASP_LIST with Kind::GitUserGraspList - Replace KIND_PATCH with Kind::GitPatch - Replace KIND_ISSUE with Kind::GitIssue - Replace KIND_COMMENT with Kind::Comment - Replace all Kind::Custom(30617|30618|1617|1618|1619|1621|1111|10317) patterns - Remove all hardcoded KIND_* constants from events.rs - Update all match statements to use Kind enum directly - Update all filter builders to use Kind variants - Update all test helpers and assertions Benefits: - Type safety: compiler prevents wrong kind numbers - Readability: Kind::GitRepoAnnouncement is self-documenting - Maintainability: single source of truth (rust-nostr) - IDE support: full autocompletion and refactoring - Standards: aligns with rust-nostr best practices Files modified: 21 Constants removed: 9 Patterns replaced: 100+ Tests passing: 222/222 --- tests/sync/discovery.rs | 28 ++++++++++++++-------------- tests/sync/historic_sync.rs | 16 +++++++--------- tests/sync/live_sync.rs | 14 +++++--------- tests/sync/metrics.rs | 14 +++++++------- tests/sync/tag_variations.rs | 14 +++++++------- 5 files changed, 40 insertions(+), 46 deletions(-) (limited to 'tests/sync') diff --git a/tests/sync/discovery.rs b/tests/sync/discovery.rs index 3aa6dda..8ed80b5 100644 --- a/tests/sync/discovery.rs +++ b/tests/sync/discovery.rs @@ -14,8 +14,8 @@ use nostr_sdk::prelude::*; use crate::common::{sync_helpers::*, TestRelay}; -/// Kind 1617 - Patch event (NIP-34) -const KIND_PATCH: u16 = 1617; +// NOTE: Using rust-nostr Kind variant: +// - Kind::GitPatch.as_u16() -> Kind::GitPatch (1617) /// Create an event referencing a repository coordinate via 'a' tag. /// @@ -26,7 +26,7 @@ fn create_event_referencing_repo(keys: &Keys, repo_coord: &str, kind: u16, conte vec![repo_coord.to_string()], )]; - EventBuilder::new(Kind::Custom(kind), content) + EventBuilder::new(Kind::from_u16(kind), content) .tags(tags) .sign_with_keys(keys) .expect("Failed to sign event") @@ -82,14 +82,18 @@ async fn test_discovers_layer3_via_layer2() { // 5. Build the repo coordinate for the 'a' tag in the patch let repo_coord = format!( "{}:{}:{}", - KIND_REPOSITORY_STATE, + Kind::GitRepoAnnouncement.as_u16(), keys.public_key().to_hex(), "test-repo-discovery" ); // 6. Create a patch event (Layer 2) that references the announcement - let patch = - create_event_referencing_repo(&keys, &repo_coord, KIND_PATCH, "Test patch proposal"); + let patch = create_event_referencing_repo( + &keys, + &repo_coord, + Kind::GitPatch.as_u16(), + "Test patch proposal", + ); let patch_id = patch.id; println!("Created patch {} (kind {})", patch_id, patch.kind.as_u16()); @@ -134,9 +138,7 @@ async fn test_discovers_layer3_via_layer2() { tokio::time::sleep(Duration::from_secs(3)).await; // 10. Verify patch was synced to relay_b - let filter = Filter::new() - .kind(Kind::Custom(KIND_PATCH)) - .author(keys.public_key()); + let filter = Filter::new().kind(Kind::GitPatch).author(keys.public_key()); let patch_synced = wait_for_event_on_relay(relay_b.url(), filter, Duration::from_secs(5)).await; @@ -250,9 +252,7 @@ async fn test_relay_discovery_via_announcements_with_historic_sync() { tokio::time::sleep(Duration::from_secs(3)).await; // 8. Verify Layer 2 event synced to relay_b - let issue_filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) - .author(keys.public_key()); + let issue_filter = Filter::new().kind(Kind::GitIssue).author(keys.public_key()); let issue_synced = wait_for_event_on_relay(relay_b.url(), issue_filter, Duration::from_secs(5)).await; @@ -389,7 +389,7 @@ async fn test_recursive_relay_discovery_via_announcements_with_historic_sync() { // 8. Verify announcement_x was synced to relay_a (from bootstrap relay_b) let filter_x = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(keys_x.public_key()); let announcement_x_synced = @@ -402,7 +402,7 @@ async fn test_recursive_relay_discovery_via_announcements_with_historic_sync() { // 9. Verify announcement_y was synced to relay_a (from discovered relay_c) let filter_y = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(keys_y.public_key()); let announcement_y_synced = diff --git a/tests/sync/historic_sync.rs b/tests/sync/historic_sync.rs index c388a7f..aec2819 100644 --- a/tests/sync/historic_sync.rs +++ b/tests/sync/historic_sync.rs @@ -29,7 +29,7 @@ async fn test_bootstrap_syncs_existing_layer2_events() { // Verify announcement synced to syncing relay let filter = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(result.maintainer_keys.public_key()); let synced = @@ -64,7 +64,7 @@ async fn test_relay_replays_events_after_restart() { // Verify announcement synced on first run let filter = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(result.maintainer_keys.public_key()); let synced_first = wait_for_event_on_relay( @@ -173,7 +173,7 @@ async fn test_announcement_not_listing_relay_is_not_synced() { // Verify announcement did NOT sync to syncing relay let filter = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(keys.public_key()); let synced = wait_for_event_on_relay(syncing.url(), filter, Duration::from_secs(2)).await; @@ -274,7 +274,7 @@ async fn test_history_sync_without_negentropy() { // Verify announcement synced to syncing relay via HISTORY sync let filter = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(keys.public_key()); let synced = wait_for_event_on_relay(syncing.url(), filter, Duration::from_secs(5)).await; @@ -339,7 +339,7 @@ async fn test_pagination_for_large_historic_sync() { // Create 40 issue events to test pagination (with limit=10, threshold=7) let repo_coord = format!( "{}:{}:{}", - KIND_REPOSITORY_STATE, + Kind::GitRepoAnnouncement.as_u16(), keys.public_key().to_hex(), repo_id ); @@ -416,16 +416,14 @@ async fn test_pagination_for_large_historic_sync() { // Verify announcement synced let announcement_filter = Filter::new() - .kind(Kind::Custom(KIND_REPOSITORY_STATE)) + .kind(Kind::GitRepoAnnouncement) .author(keys.public_key()); let announcement_synced = wait_for_event_on_relay(syncing.url(), announcement_filter, Duration::from_secs(3)).await; // Verify ALL 40 issues synced - let issues_filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) - .author(keys.public_key()); + let issues_filter = Filter::new().kind(Kind::GitIssue).author(keys.public_key()); // Query for all issues let temp_keys = Keys::generate(); diff --git a/tests/sync/live_sync.rs b/tests/sync/live_sync.rs index 7fa08a0..8ee3119 100644 --- a/tests/sync/live_sync.rs +++ b/tests/sync/live_sync.rs @@ -115,7 +115,7 @@ async fn test_live_sync_layer2_events() { // 9. Wait and verify event syncs to relay_b let filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) + .kind(Kind::GitIssue) .author(keys.public_key()) .id(issue_id); @@ -237,7 +237,7 @@ async fn test_live_sync_layer3_events() { // 6. Now wait for issue to sync to relay_b (this triggers Layer 3 filter creation) tokio::time::sleep(Duration::from_secs(2)).await; - let issue_filter = Filter::new().kind(Kind::Custom(KIND_ISSUE)).id(issue_id); + let issue_filter = Filter::new().kind(Kind::GitIssue).id(issue_id); let issue_synced = wait_for_event_on_relay(relay_b.url(), issue_filter, Duration::from_secs(3)).await; println!("Issue synced to relay_b: {}", issue_synced); @@ -247,7 +247,7 @@ async fn test_live_sync_layer3_events() { // 7. Wait and verify comment syncs to relay_b let comment_filter = Filter::new() - .kind(Kind::Custom(KIND_COMMENT)) + .kind(Kind::Comment) .author(keys.public_key()) .id(comment_id); @@ -267,9 +267,7 @@ async fn test_live_sync_layer3_events() { client.connect().await; tokio::time::sleep(Duration::from_millis(500)).await; - let fetch_filter = Filter::new() - .kind(Kind::Custom(KIND_COMMENT)) - .id(comment_id); + let fetch_filter = Filter::new().kind(Kind::Comment).id(comment_id); if let Ok(events) = client .fetch_events(fetch_filter, Duration::from_secs(2)) @@ -418,9 +416,7 @@ async fn test_live_sync_event_ordering() { client.connect().await; tokio::time::sleep(Duration::from_millis(500)).await; - let filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) - .author(keys.public_key()); + let filter = Filter::new().kind(Kind::GitIssue).author(keys.public_key()); match client.fetch_events(filter, Duration::from_secs(3)).await { Ok(events) => { diff --git a/tests/sync/metrics.rs b/tests/sync/metrics.rs index 987b83a..e8c75c7 100644 --- a/tests/sync/metrics.rs +++ b/tests/sync/metrics.rs @@ -17,7 +17,7 @@ use nostr_sdk::prelude::*; use crate::common::{ sync_helpers::{ create_repo_announcement, fetch_metrics, wait_for_sync_connection, MetricsTestHarness, - ParsedMetrics, TestClient, KIND_REPOSITORY_STATE, + ParsedMetrics, TestClient, }, TestRelay, }; @@ -175,8 +175,8 @@ async fn test_metric_values_are_numeric() { // Phase 2: Real Metrics Tests (Using MetricsTestHarness) // ============================================================================ -/// Kind 1617 - Patch event (NIP-34) -const KIND_PATCH: u16 = 1617; +// NOTE: Using rust-nostr Kind variant: +// - Kind::GitPatch.as_u16() -> Kind::GitPatch (1617) /// Create an event referencing a repository coordinate via 'a' tag. /// @@ -187,7 +187,7 @@ fn create_event_referencing_repo(keys: &Keys, repo_coord: &str, kind: u16, conte vec![repo_coord.to_string()], )]; - EventBuilder::new(Kind::Custom(kind), content) + EventBuilder::new(Kind::from_u16(kind), content) .tags(tags) .sign_with_keys(keys) .expect("Failed to sign event") @@ -239,7 +239,7 @@ async fn test_startup_sync_event_count() { // 5. Build the repo coordinate for the 'a' tag in the patches let repo_coord = format!( "{}:{}:{}", - KIND_REPOSITORY_STATE, + Kind::GitRepoAnnouncement.as_u16(), keys.public_key().to_hex(), "test-repo-metrics" ); @@ -250,7 +250,7 @@ async fn test_startup_sync_event_count() { create_event_referencing_repo( &keys, &repo_coord, - KIND_PATCH, + Kind::GitPatch.as_u16(), &format!("Test patch {}", i), ) }) @@ -320,7 +320,7 @@ async fn test_startup_sync_event_count() { // 12. Verify patches actually synced (functional check) let filter = Filter::new() - .kind(Kind::Custom(KIND_PATCH)) + .kind(Kind::Custom(Kind::GitPatch.as_u16())) .author(keys.public_key()); let patches_synced = crate::common::sync_helpers::wait_for_event_on_relay( diff --git a/tests/sync/tag_variations.rs b/tests/sync/tag_variations.rs index 7153104..46b1203 100644 --- a/tests/sync/tag_variations.rs +++ b/tests/sync/tag_variations.rs @@ -110,7 +110,7 @@ async fn test_layer2_sync_with_lowercase_a_tag() { // 5. Wait and verify event syncs to relay_b let filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) + .kind(Kind::GitIssue) .author(keys.public_key()) .id(issue_id); @@ -212,7 +212,7 @@ async fn test_layer2_sync_with_uppercase_a_tag() { // 5. Wait and verify event syncs to relay_b let filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) + .kind(Kind::GitIssue) .author(keys.public_key()) .id(issue_id); @@ -309,7 +309,7 @@ async fn test_layer2_sync_with_q_tag() { // 5. Wait and verify event syncs to relay_b let filter = Filter::new() - .kind(Kind::Custom(KIND_ISSUE)) + .kind(Kind::GitIssue) .author(keys.public_key()) .id(issue_id); @@ -403,7 +403,7 @@ async fn test_layer3_sync_with_lowercase_e_tag() { println!("Layer 2 issue {} sent to relay_a", issue_id); // 5. Wait for issue to sync to relay_b - let issue_filter = Filter::new().kind(Kind::Custom(KIND_ISSUE)).id(issue_id); + let issue_filter = Filter::new().kind(Kind::GitIssue).id(issue_id); let issue_synced = wait_for_event_on_relay(relay_b.url(), issue_filter, Duration::from_secs(5)).await; println!("Issue synced to relay_b: {}", issue_synced); @@ -527,7 +527,7 @@ async fn test_layer3_sync_with_uppercase_e_tag() { println!("Layer 2 issue {} sent to relay_a", issue_id); // 5. Wait for issue to sync to relay_b - let issue_filter = Filter::new().kind(Kind::Custom(KIND_ISSUE)).id(issue_id); + let issue_filter = Filter::new().kind(Kind::GitIssue).id(issue_id); let issue_synced = wait_for_event_on_relay(relay_b.url(), issue_filter, Duration::from_secs(5)).await; println!("Issue synced to relay_b: {}", issue_synced); @@ -567,7 +567,7 @@ async fn test_layer3_sync_with_uppercase_e_tag() { // 7. Wait and verify comment syncs to relay_b let comment_filter = Filter::new() - .kind(Kind::Custom(KIND_COMMENT)) // Kind 1111 + .kind(Kind::Comment) // Kind 1111 .author(keys.public_key()) .id(comment_id); @@ -655,7 +655,7 @@ async fn test_layer3_sync_with_q_tag() { println!("Layer 2 issue {} sent to relay_a", issue_id); // 5. Wait for issue to sync to relay_b - let issue_filter = Filter::new().kind(Kind::Custom(KIND_ISSUE)).id(issue_id); + let issue_filter = Filter::new().kind(Kind::GitIssue).id(issue_id); let issue_synced = wait_for_event_on_relay(relay_b.url(), issue_filter, Duration::from_secs(5)).await; println!("Issue synced to relay_b: {}", issue_synced); -- cgit v1.2.3