diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-01-08 11:20:35 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-01-08 11:23:28 +0000 |
| commit | 5d02ad6b893f9059044914c115d77cf9d8e589c3 (patch) | |
| tree | b727f9c44d2f2d4e203dc2344e4c9bd5144a77dd /tests/common/sync_helpers.rs | |
| parent | 075307804bf66bba10f5bc55cb40e2e6a98a65ee (diff) | |
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
Diffstat (limited to 'tests/common/sync_helpers.rs')
| -rw-r--r-- | tests/common/sync_helpers.rs | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/tests/common/sync_helpers.rs b/tests/common/sync_helpers.rs index acf8c87..27422e9 100644 --- a/tests/common/sync_helpers.rs +++ b/tests/common/sync_helpers.rs | |||
| @@ -17,14 +17,10 @@ use nostr_sdk::prelude::*; | |||
| 17 | 17 | ||
| 18 | use super::relay::TestRelay; | 18 | use super::relay::TestRelay; |
| 19 | 19 | ||
| 20 | /// Kind 1618 - Issue (NIP-34 git-related event) | 20 | // NOTE: Using rust-nostr Kind variants: |
| 21 | pub const KIND_ISSUE: u16 = 1621; | 21 | // - Kind::GitIssue.as_u16() -> Kind::GitIssue (1621) |
| 22 | 22 | // - Kind::Comment.as_u16() -> Kind::Comment (1111) | |
| 23 | /// Kind 1111 - NIP-22 Comment | 23 | // - Kind::GitRepoAnnouncement.as_u16() -> Kind::GitRepoAnnouncement (30617) |
| 24 | pub const KIND_COMMENT: u16 = 1111; | ||
| 25 | |||
| 26 | /// Kind 30617 - Repository state/announcement (NIP-34) | ||
| 27 | pub const KIND_REPOSITORY_STATE: u16 = 30617; | ||
| 28 | 24 | ||
| 29 | /// Test client with built-in retry logic for connect and send operations. | 25 | /// Test client with built-in retry logic for connect and send operations. |
| 30 | /// | 26 | /// |
| @@ -225,7 +221,7 @@ fn build_layer2_issue_with_tag( | |||
| 225 | 221 | ||
| 226 | let tags = vec![tag]; | 222 | let tags = vec![tag]; |
| 227 | 223 | ||
| 228 | EventBuilder::new(Kind::Custom(KIND_ISSUE), title) | 224 | EventBuilder::new(Kind::GitIssue, title) |
| 229 | .tags(tags) | 225 | .tags(tags) |
| 230 | .sign_with_keys(keys) | 226 | .sign_with_keys(keys) |
| 231 | .map_err(|e| format!("Failed to sign Layer 2 issue event: {}", e)) | 227 | .map_err(|e| format!("Failed to sign Layer 2 issue event: {}", e)) |
| @@ -240,7 +236,7 @@ fn build_layer2_issue_with_tag( | |||
| 240 | /// * `keys` - Keys for signing the event | 236 | /// * `keys` - Keys for signing the event |
| 241 | /// * `parent_event_id` - Event ID being referenced (e.g., an issue or patch) | 237 | /// * `parent_event_id` - Event ID being referenced (e.g., an issue or patch) |
| 242 | /// * `content` - Comment content | 238 | /// * `content` - Comment content |
| 243 | /// * `kind` - Event kind (Kind::Custom(1) for reply, Kind::Custom(1111) for NIP-22 comment) | 239 | /// * `kind` - Event kind (Kind::TextNote for reply, Kind::Comment for NIP-22 comment) |
| 244 | /// | 240 | /// |
| 245 | /// # Tag Types | 241 | /// # Tag Types |
| 246 | /// - For kind 1111: Uses uppercase 'E' tag (NIP-22 style) | 242 | /// - For kind 1111: Uses uppercase 'E' tag (NIP-22 style) |
| @@ -258,7 +254,7 @@ pub fn build_layer3_comment_event( | |||
| 258 | let kind_num = kind.as_u16(); | 254 | let kind_num = kind.as_u16(); |
| 259 | 255 | ||
| 260 | // Choose tag based on kind (NIP-22 uses E, NIP-10 style uses e) | 256 | // Choose tag based on kind (NIP-22 uses E, NIP-10 style uses e) |
| 261 | let tag = if kind_num == KIND_COMMENT { | 257 | let tag = if kind_num == Kind::Comment.as_u16() { |
| 262 | // NIP-22 comment: uppercase 'E' tag | 258 | // NIP-22 comment: uppercase 'E' tag |
| 263 | Tag::custom(TagKind::custom("E"), vec![parent_event_id.to_hex()]) | 259 | Tag::custom(TagKind::custom("E"), vec![parent_event_id.to_hex()]) |
| 264 | } else { | 260 | } else { |
| @@ -302,7 +298,7 @@ pub fn build_layer3_comment_with_uppercase_e_tag( | |||
| 302 | ) -> Result<Event, String> { | 298 | ) -> Result<Event, String> { |
| 303 | let tag = Tag::custom(TagKind::custom("E"), vec![parent_event_id.to_hex()]); | 299 | let tag = Tag::custom(TagKind::custom("E"), vec![parent_event_id.to_hex()]); |
| 304 | 300 | ||
| 305 | EventBuilder::new(Kind::Custom(KIND_COMMENT), content) | 301 | EventBuilder::new(Kind::Comment, content) |
| 306 | .tags(vec![tag]) | 302 | .tags(vec![tag]) |
| 307 | .sign_with_keys(keys) | 303 | .sign_with_keys(keys) |
| 308 | .map_err(|e| format!("Failed to sign Layer 3 comment event: {}", e)) | 304 | .map_err(|e| format!("Failed to sign Layer 3 comment event: {}", e)) |
| @@ -362,7 +358,7 @@ pub fn create_repo_announcement(keys: &Keys, domains: &[&str], identifier: &str) | |||
| 362 | Tag::custom(TagKind::custom("relays"), relay_urls), | 358 | Tag::custom(TagKind::custom("relays"), relay_urls), |
| 363 | ]; | 359 | ]; |
| 364 | 360 | ||
| 365 | EventBuilder::new(Kind::Custom(KIND_REPOSITORY_STATE), "Repository state") | 361 | EventBuilder::new(Kind::GitRepoAnnouncement, "Repository state") |
| 366 | .tags(tags) | 362 | .tags(tags) |
| 367 | .sign_with_keys(keys) | 363 | .sign_with_keys(keys) |
| 368 | .expect("Failed to sign repo announcement") | 364 | .expect("Failed to sign repo announcement") |
| @@ -503,7 +499,7 @@ fn check_sync_connections_in_metrics(metrics: &str, expected: usize) -> bool { | |||
| 503 | /// # Example | 499 | /// # Example |
| 504 | /// ```ignore | 500 | /// ```ignore |
| 505 | /// let filter = Filter::new() | 501 | /// let filter = Filter::new() |
| 506 | /// .kind(Kind::Custom(1618)) | 502 | /// .kind(Kind::GitPullRequest) |
| 507 | /// .author(keys.public_key()) | 503 | /// .author(keys.public_key()) |
| 508 | /// .id(event.id); | 504 | /// .id(event.id); |
| 509 | /// | 505 | /// |
| @@ -559,7 +555,7 @@ pub async fn wait_for_event_on_relay(relay_url: &str, filter: Filter, timeout: D | |||
| 559 | pub fn repo_coord(keys: &Keys, identifier: &str) -> String { | 555 | pub fn repo_coord(keys: &Keys, identifier: &str) -> String { |
| 560 | format!( | 556 | format!( |
| 561 | "{}:{}:{}", | 557 | "{}:{}:{}", |
| 562 | KIND_REPOSITORY_STATE, | 558 | Kind::GitRepoAnnouncement.as_u16(), |
| 563 | keys.public_key().to_hex(), | 559 | keys.public_key().to_hex(), |
| 564 | identifier | 560 | identifier |
| 565 | ) | 561 | ) |
| @@ -897,7 +893,7 @@ mod tests { | |||
| 897 | build_layer2_issue_event(&keys, &coord, "Test Issue").expect("Should create event"); | 893 | build_layer2_issue_event(&keys, &coord, "Test Issue").expect("Should create event"); |
| 898 | 894 | ||
| 899 | // nostr-sdk 0.43: use field access | 895 | // nostr-sdk 0.43: use field access |
| 900 | assert_eq!(event.kind.as_u16(), KIND_ISSUE); | 896 | assert_eq!(event.kind.as_u16(), Kind::GitIssue.as_u16()); |
| 901 | 897 | ||
| 902 | // Check the tag exists | 898 | // Check the tag exists |
| 903 | let has_a_tag = event.tags.iter().any(|tag| { | 899 | let has_a_tag = event.tags.iter().any(|tag| { |
| @@ -942,15 +938,10 @@ mod tests { | |||
| 942 | let keys = Keys::generate(); | 938 | let keys = Keys::generate(); |
| 943 | let parent_id = EventId::all_zeros(); | 939 | let parent_id = EventId::all_zeros(); |
| 944 | 940 | ||
| 945 | let event = build_layer3_comment_event( | 941 | let event = build_layer3_comment_event(&keys, &parent_id, "Test comment", Kind::Comment) |
| 946 | &keys, | 942 | .expect("Should create event"); |
| 947 | &parent_id, | ||
| 948 | "Test comment", | ||
| 949 | Kind::Custom(KIND_COMMENT), | ||
| 950 | ) | ||
| 951 | .expect("Should create event"); | ||
| 952 | 943 | ||
| 953 | assert_eq!(event.kind.as_u16(), KIND_COMMENT); | 944 | assert_eq!(event.kind.as_u16(), Kind::Comment.as_u16()); |
| 954 | 945 | ||
| 955 | // NIP-22 comment should have uppercase 'E' tag | 946 | // NIP-22 comment should have uppercase 'E' tag |
| 956 | let has_e_tag = event.tags.iter().any(|tag| { | 947 | let has_e_tag = event.tags.iter().any(|tag| { |
| @@ -1003,7 +994,7 @@ mod tests { | |||
| 1003 | let event = build_layer3_comment_with_uppercase_e_tag(&keys, &parent_id, "Comment content") | 994 | let event = build_layer3_comment_with_uppercase_e_tag(&keys, &parent_id, "Comment content") |
| 1004 | .expect("Should create event"); | 995 | .expect("Should create event"); |
| 1005 | 996 | ||
| 1006 | assert_eq!(event.kind.as_u16(), KIND_COMMENT); | 997 | assert_eq!(event.kind.as_u16(), Kind::Comment.as_u16()); |
| 1007 | 998 | ||
| 1008 | let has_upper_e_tag = event.tags.iter().any(|tag| { | 999 | let has_upper_e_tag = event.tags.iter().any(|tag| { |
| 1009 | let slice = tag.as_slice(); | 1000 | let slice = tag.as_slice(); |
| @@ -1123,7 +1114,7 @@ async fn send_to_relay(relay: &TestRelay, event: &Event) -> Result<(), String> { | |||
| 1123 | /// // Assert issue synced to result.syncing_relay | 1114 | /// // Assert issue synced to result.syncing_relay |
| 1124 | /// | 1115 | /// |
| 1125 | /// // Live sync test | 1116 | /// // Live sync test |
| 1126 | /// let comment = build_layer3_comment_event(&keys, &issue.id, "Live Comment", Kind::Custom(1111))?; | 1117 | /// let comment = build_layer3_comment_event(&keys, &issue.id, "Live Comment", Kind::Comment)?; |
| 1127 | /// let result = run_sync_test(&[], &[comment]).await; | 1118 | /// let result = run_sync_test(&[], &[comment]).await; |
| 1128 | /// // Assert comment synced to result.syncing_relay | 1119 | /// // Assert comment synced to result.syncing_relay |
| 1129 | /// ``` | 1120 | /// ``` |