diff options
Diffstat (limited to 'tests/proactive_sync_dynamic.rs')
| -rw-r--r-- | tests/proactive_sync_dynamic.rs | 287 |
1 files changed, 100 insertions, 187 deletions
diff --git a/tests/proactive_sync_dynamic.rs b/tests/proactive_sync_dynamic.rs index 8a3cb88..2d3232f 100644 --- a/tests/proactive_sync_dynamic.rs +++ b/tests/proactive_sync_dynamic.rs | |||
| @@ -117,69 +117,6 @@ fn create_test_reply_event(keys: &Keys, event_id: &str) -> Event { | |||
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | // ============================================================================ | 119 | // ============================================================================ |
| 120 | // Kind Detection Tests | ||
| 121 | // ============================================================================ | ||
| 122 | |||
| 123 | /// Test that announcement kinds are correctly identified | ||
| 124 | #[test] | ||
| 125 | fn test_is_announcement_kind_30617() { | ||
| 126 | assert!(SubscriptionManager::is_announcement_kind(30617)); | ||
| 127 | } | ||
| 128 | |||
| 129 | /// Test that maintainer list kind is correctly identified | ||
| 130 | #[test] | ||
| 131 | fn test_is_announcement_kind_30618() { | ||
| 132 | assert!(SubscriptionManager::is_announcement_kind(30618)); | ||
| 133 | } | ||
| 134 | |||
| 135 | /// Test that non-announcement kinds are not identified as announcements | ||
| 136 | #[test] | ||
| 137 | fn test_is_announcement_kind_negative() { | ||
| 138 | assert!(!SubscriptionManager::is_announcement_kind(1)); // Text note | ||
| 139 | assert!(!SubscriptionManager::is_announcement_kind(1617)); // PR | ||
| 140 | assert!(!SubscriptionManager::is_announcement_kind(1621)); // Issue | ||
| 141 | assert!(!SubscriptionManager::is_announcement_kind(0)); // Unknown | ||
| 142 | } | ||
| 143 | |||
| 144 | /// Test that PR/Issue kinds are correctly identified | ||
| 145 | #[test] | ||
| 146 | fn test_is_pr_issue_kind_1617() { | ||
| 147 | assert!(SubscriptionManager::is_pr_issue_kind(1617)); // Patch proposal | ||
| 148 | } | ||
| 149 | |||
| 150 | /// Test that PR kind 1618 is correctly identified | ||
| 151 | #[test] | ||
| 152 | fn test_is_pr_issue_kind_1618() { | ||
| 153 | assert!(SubscriptionManager::is_pr_issue_kind(1618)); // PR | ||
| 154 | } | ||
| 155 | |||
| 156 | /// Test that PR update kind is correctly identified | ||
| 157 | #[test] | ||
| 158 | fn test_is_pr_issue_kind_1619() { | ||
| 159 | assert!(SubscriptionManager::is_pr_issue_kind(1619)); // PR Update | ||
| 160 | } | ||
| 161 | |||
| 162 | /// Test that Issue kind is correctly identified | ||
| 163 | #[test] | ||
| 164 | fn test_is_pr_issue_kind_1621() { | ||
| 165 | assert!(SubscriptionManager::is_pr_issue_kind(1621)); // Issue | ||
| 166 | } | ||
| 167 | |||
| 168 | /// Test that Reply kind is correctly identified | ||
| 169 | #[test] | ||
| 170 | fn test_is_pr_issue_kind_1622() { | ||
| 171 | assert!(SubscriptionManager::is_pr_issue_kind(1622)); // Reply | ||
| 172 | } | ||
| 173 | |||
| 174 | /// Test that non-PR/Issue kinds are not identified | ||
| 175 | #[test] | ||
| 176 | fn test_is_pr_issue_kind_negative() { | ||
| 177 | assert!(!SubscriptionManager::is_pr_issue_kind(30617)); // Announcement | ||
| 178 | assert!(!SubscriptionManager::is_pr_issue_kind(1)); // Text note | ||
| 179 | assert!(!SubscriptionManager::is_pr_issue_kind(0)); // Unknown | ||
| 180 | } | ||
| 181 | |||
| 182 | // ============================================================================ | ||
| 183 | // Filter Count Tests | 120 | // Filter Count Tests |
| 184 | // ============================================================================ | 121 | // ============================================================================ |
| 185 | 122 | ||
| @@ -188,19 +125,19 @@ fn test_is_pr_issue_kind_negative() { | |||
| 188 | fn test_initial_filter_count() { | 125 | fn test_initial_filter_count() { |
| 189 | // Create a minimal SubscriptionManager-like state for testing | 126 | // Create a minimal SubscriptionManager-like state for testing |
| 190 | // We test the logic without needing a full FilterService | 127 | // We test the logic without needing a full FilterService |
| 191 | 128 | ||
| 192 | // Initial state: 0 announcements, 0 events, not consolidated | 129 | // Initial state: 0 announcements, 0 events, not consolidated |
| 193 | // Filter count should be: 1 (Layer 1) + 0 + 0 = 1 | 130 | // Filter count should be: 1 (Layer 1) + 0 + 0 = 1 |
| 194 | let announcement_count = 0; | 131 | let announcement_count = 0; |
| 195 | let event_count = 0; | 132 | let event_count = 0; |
| 196 | let is_consolidated = false; | 133 | let is_consolidated = false; |
| 197 | 134 | ||
| 198 | let filter_count = if is_consolidated { | 135 | let filter_count = if is_consolidated { |
| 199 | 1 | 136 | 1 |
| 200 | } else { | 137 | } else { |
| 201 | 1 + announcement_count + event_count | 138 | 1 + announcement_count + event_count |
| 202 | }; | 139 | }; |
| 203 | 140 | ||
| 204 | assert_eq!(filter_count, 1); | 141 | assert_eq!(filter_count, 1); |
| 205 | } | 142 | } |
| 206 | 143 | ||
| @@ -210,13 +147,13 @@ fn test_filter_count_with_announcements() { | |||
| 210 | let announcement_count = 5; | 147 | let announcement_count = 5; |
| 211 | let event_count = 0; | 148 | let event_count = 0; |
| 212 | let is_consolidated = false; | 149 | let is_consolidated = false; |
| 213 | 150 | ||
| 214 | let filter_count = if is_consolidated { | 151 | let filter_count = if is_consolidated { |
| 215 | 1 | 152 | 1 |
| 216 | } else { | 153 | } else { |
| 217 | 1 + announcement_count + event_count | 154 | 1 + announcement_count + event_count |
| 218 | }; | 155 | }; |
| 219 | 156 | ||
| 220 | // 1 (Layer 1) + 5 (announcements) = 6 | 157 | // 1 (Layer 1) + 5 (announcements) = 6 |
| 221 | assert_eq!(filter_count, 6); | 158 | assert_eq!(filter_count, 6); |
| 222 | } | 159 | } |
| @@ -227,13 +164,13 @@ fn test_filter_count_with_events() { | |||
| 227 | let announcement_count = 0; | 164 | let announcement_count = 0; |
| 228 | let event_count = 10; | 165 | let event_count = 10; |
| 229 | let is_consolidated = false; | 166 | let is_consolidated = false; |
| 230 | 167 | ||
| 231 | let filter_count = if is_consolidated { | 168 | let filter_count = if is_consolidated { |
| 232 | 1 | 169 | 1 |
| 233 | } else { | 170 | } else { |
| 234 | 1 + announcement_count + event_count | 171 | 1 + announcement_count + event_count |
| 235 | }; | 172 | }; |
| 236 | 173 | ||
| 237 | // 1 (Layer 1) + 10 (events) = 11 | 174 | // 1 (Layer 1) + 10 (events) = 11 |
| 238 | assert_eq!(filter_count, 11); | 175 | assert_eq!(filter_count, 11); |
| 239 | } | 176 | } |
| @@ -244,13 +181,13 @@ fn test_filter_count_mixed() { | |||
| 244 | let announcement_count = 50; | 181 | let announcement_count = 50; |
| 245 | let event_count = 30; | 182 | let event_count = 30; |
| 246 | let is_consolidated = false; | 183 | let is_consolidated = false; |
| 247 | 184 | ||
| 248 | let filter_count = if is_consolidated { | 185 | let filter_count = if is_consolidated { |
| 249 | 1 | 186 | 1 |
| 250 | } else { | 187 | } else { |
| 251 | 1 + announcement_count + event_count | 188 | 1 + announcement_count + event_count |
| 252 | }; | 189 | }; |
| 253 | 190 | ||
| 254 | // 1 + 50 + 30 = 81 | 191 | // 1 + 50 + 30 = 81 |
| 255 | assert_eq!(filter_count, 81); | 192 | assert_eq!(filter_count, 81); |
| 256 | } | 193 | } |
| @@ -261,13 +198,13 @@ fn test_filter_count_consolidated() { | |||
| 261 | let announcement_count = 100; // These would be cleared on consolidation | 198 | let announcement_count = 100; // These would be cleared on consolidation |
| 262 | let event_count = 100; | 199 | let event_count = 100; |
| 263 | let is_consolidated = true; | 200 | let is_consolidated = true; |
| 264 | 201 | ||
| 265 | let filter_count = if is_consolidated { | 202 | let filter_count = if is_consolidated { |
| 266 | 1 | 203 | 1 |
| 267 | } else { | 204 | } else { |
| 268 | 1 + announcement_count + event_count | 205 | 1 + announcement_count + event_count |
| 269 | }; | 206 | }; |
| 270 | 207 | ||
| 271 | assert_eq!(filter_count, 1); | 208 | assert_eq!(filter_count, 1); |
| 272 | } | 209 | } |
| 273 | 210 | ||
| @@ -280,9 +217,9 @@ fn test_filter_count_consolidated() { | |||
| 280 | fn test_should_consolidate_below_threshold() { | 217 | fn test_should_consolidate_below_threshold() { |
| 281 | let filter_count = 100; | 218 | let filter_count = 100; |
| 282 | let is_consolidated = false; | 219 | let is_consolidated = false; |
| 283 | 220 | ||
| 284 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; | 221 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; |
| 285 | 222 | ||
| 286 | assert!(!should_consolidate); | 223 | assert!(!should_consolidate); |
| 287 | } | 224 | } |
| 288 | 225 | ||
| @@ -291,9 +228,9 @@ fn test_should_consolidate_below_threshold() { | |||
| 291 | fn test_should_consolidate_at_threshold() { | 228 | fn test_should_consolidate_at_threshold() { |
| 292 | let filter_count = 151; // > 150 | 229 | let filter_count = 151; // > 150 |
| 293 | let is_consolidated = false; | 230 | let is_consolidated = false; |
| 294 | 231 | ||
| 295 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; | 232 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; |
| 296 | 233 | ||
| 297 | assert!(should_consolidate); | 234 | assert!(should_consolidate); |
| 298 | } | 235 | } |
| 299 | 236 | ||
| @@ -302,9 +239,9 @@ fn test_should_consolidate_at_threshold() { | |||
| 302 | fn test_should_consolidate_above_threshold() { | 239 | fn test_should_consolidate_above_threshold() { |
| 303 | let filter_count = 200; | 240 | let filter_count = 200; |
| 304 | let is_consolidated = false; | 241 | let is_consolidated = false; |
| 305 | 242 | ||
| 306 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; | 243 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; |
| 307 | 244 | ||
| 308 | assert!(should_consolidate); | 245 | assert!(should_consolidate); |
| 309 | } | 246 | } |
| 310 | 247 | ||
| @@ -313,9 +250,9 @@ fn test_should_consolidate_above_threshold() { | |||
| 313 | fn test_should_consolidate_already_consolidated() { | 250 | fn test_should_consolidate_already_consolidated() { |
| 314 | let filter_count = 200; // Would trigger, but already consolidated | 251 | let filter_count = 200; // Would trigger, but already consolidated |
| 315 | let is_consolidated = true; | 252 | let is_consolidated = true; |
| 316 | 253 | ||
| 317 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; | 254 | let should_consolidate = !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD; |
| 318 | 255 | ||
| 319 | assert!(!should_consolidate); | 256 | assert!(!should_consolidate); |
| 320 | } | 257 | } |
| 321 | 258 | ||
| @@ -323,11 +260,11 @@ fn test_should_consolidate_already_consolidated() { | |||
| 323 | #[test] | 260 | #[test] |
| 324 | fn test_consolidation_threshold_boundary() { | 261 | fn test_consolidation_threshold_boundary() { |
| 325 | let is_consolidated = false; | 262 | let is_consolidated = false; |
| 326 | 263 | ||
| 327 | // 150 should NOT trigger (> 150, not >= 150) | 264 | // 150 should NOT trigger (> 150, not >= 150) |
| 328 | let should_consolidate_at_150 = !is_consolidated && 150 > CONSOLIDATION_THRESHOLD; | 265 | let should_consolidate_at_150 = !is_consolidated && 150 > CONSOLIDATION_THRESHOLD; |
| 329 | assert!(!should_consolidate_at_150); | 266 | assert!(!should_consolidate_at_150); |
| 330 | 267 | ||
| 331 | // 151 should trigger | 268 | // 151 should trigger |
| 332 | let should_consolidate_at_151 = !is_consolidated && 151 > CONSOLIDATION_THRESHOLD; | 269 | let should_consolidate_at_151 = !is_consolidated && 151 > CONSOLIDATION_THRESHOLD; |
| 333 | assert!(should_consolidate_at_151); | 270 | assert!(should_consolidate_at_151); |
| @@ -341,14 +278,14 @@ fn test_consolidation_threshold_boundary() { | |||
| 341 | #[test] | 278 | #[test] |
| 342 | fn test_duplicate_announcement_prevention() { | 279 | fn test_duplicate_announcement_prevention() { |
| 343 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); | 280 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); |
| 344 | 281 | ||
| 345 | let event_id = "abc123".to_string(); | 282 | let event_id = "abc123".to_string(); |
| 346 | 283 | ||
| 347 | // First add should succeed | 284 | // First add should succeed |
| 348 | let is_new = !subscribed_announcements.contains(&event_id); | 285 | let is_new = !subscribed_announcements.contains(&event_id); |
| 349 | assert!(is_new); | 286 | assert!(is_new); |
| 350 | subscribed_announcements.insert(event_id.clone()); | 287 | subscribed_announcements.insert(event_id.clone()); |
| 351 | 288 | ||
| 352 | // Second add should fail (duplicate) | 289 | // Second add should fail (duplicate) |
| 353 | let is_new_again = !subscribed_announcements.contains(&event_id); | 290 | let is_new_again = !subscribed_announcements.contains(&event_id); |
| 354 | assert!(!is_new_again); | 291 | assert!(!is_new_again); |
| @@ -358,14 +295,14 @@ fn test_duplicate_announcement_prevention() { | |||
| 358 | #[test] | 295 | #[test] |
| 359 | fn test_duplicate_event_prevention() { | 296 | fn test_duplicate_event_prevention() { |
| 360 | let mut subscribed_events: HashSet<String> = HashSet::new(); | 297 | let mut subscribed_events: HashSet<String> = HashSet::new(); |
| 361 | 298 | ||
| 362 | let event_id = "def456".to_string(); | 299 | let event_id = "def456".to_string(); |
| 363 | 300 | ||
| 364 | // First add should succeed | 301 | // First add should succeed |
| 365 | let is_new = !subscribed_events.contains(&event_id); | 302 | let is_new = !subscribed_events.contains(&event_id); |
| 366 | assert!(is_new); | 303 | assert!(is_new); |
| 367 | subscribed_events.insert(event_id.clone()); | 304 | subscribed_events.insert(event_id.clone()); |
| 368 | 305 | ||
| 369 | // Second add should fail (duplicate) | 306 | // Second add should fail (duplicate) |
| 370 | let is_new_again = !subscribed_events.contains(&event_id); | 307 | let is_new_again = !subscribed_events.contains(&event_id); |
| 371 | assert!(!is_new_again); | 308 | assert!(!is_new_again); |
| @@ -375,14 +312,14 @@ fn test_duplicate_event_prevention() { | |||
| 375 | #[test] | 312 | #[test] |
| 376 | fn test_multiple_unique_items_tracked() { | 313 | fn test_multiple_unique_items_tracked() { |
| 377 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); | 314 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); |
| 378 | 315 | ||
| 379 | // Add multiple unique announcements | 316 | // Add multiple unique announcements |
| 380 | for i in 0..10 { | 317 | for i in 0..10 { |
| 381 | let id = format!("announcement_{}", i); | 318 | let id = format!("announcement_{}", i); |
| 382 | assert!(!subscribed_announcements.contains(&id)); | 319 | assert!(!subscribed_announcements.contains(&id)); |
| 383 | subscribed_announcements.insert(id); | 320 | subscribed_announcements.insert(id); |
| 384 | } | 321 | } |
| 385 | 322 | ||
| 386 | assert_eq!(subscribed_announcements.len(), 10); | 323 | assert_eq!(subscribed_announcements.len(), 10); |
| 387 | } | 324 | } |
| 388 | 325 | ||
| @@ -395,12 +332,12 @@ fn test_multiple_unique_items_tracked() { | |||
| 395 | fn test_announcement_has_d_tag() { | 332 | fn test_announcement_has_d_tag() { |
| 396 | let keys = Keys::generate(); | 333 | let keys = Keys::generate(); |
| 397 | let event = create_test_announcement(&keys, "my-repo"); | 334 | let event = create_test_announcement(&keys, "my-repo"); |
| 398 | 335 | ||
| 399 | let has_d_tag = event.tags.iter().any(|tag| { | 336 | let has_d_tag = event.tags.iter().any(|tag| { |
| 400 | let tag_vec = tag.clone().to_vec(); | 337 | let tag_vec = tag.clone().to_vec(); |
| 401 | tag_vec.len() >= 2 && tag_vec[0] == "d" | 338 | tag_vec.len() >= 2 && tag_vec[0] == "d" |
| 402 | }); | 339 | }); |
| 403 | 340 | ||
| 404 | assert!(has_d_tag); | 341 | assert!(has_d_tag); |
| 405 | } | 342 | } |
| 406 | 343 | ||
| @@ -409,7 +346,7 @@ fn test_announcement_has_d_tag() { | |||
| 409 | fn test_announcement_correct_kind() { | 346 | fn test_announcement_correct_kind() { |
| 410 | let keys = Keys::generate(); | 347 | let keys = Keys::generate(); |
| 411 | let event = create_test_announcement(&keys, "my-repo"); | 348 | let event = create_test_announcement(&keys, "my-repo"); |
| 412 | 349 | ||
| 413 | assert_eq!(event.kind.as_u16(), KIND_REPOSITORY_ANNOUNCEMENT); | 350 | assert_eq!(event.kind.as_u16(), KIND_REPOSITORY_ANNOUNCEMENT); |
| 414 | } | 351 | } |
| 415 | 352 | ||
| @@ -418,7 +355,7 @@ fn test_announcement_correct_kind() { | |||
| 418 | fn test_maintainer_list_correct_kind() { | 355 | fn test_maintainer_list_correct_kind() { |
| 419 | let keys = Keys::generate(); | 356 | let keys = Keys::generate(); |
| 420 | let event = create_test_maintainer_list(&keys, "maintainers"); | 357 | let event = create_test_maintainer_list(&keys, "maintainers"); |
| 421 | 358 | ||
| 422 | assert_eq!(event.kind.as_u16(), KIND_MAINTAINER_LIST); | 359 | assert_eq!(event.kind.as_u16(), KIND_MAINTAINER_LIST); |
| 423 | } | 360 | } |
| 424 | 361 | ||
| @@ -428,12 +365,12 @@ fn test_pr_event_has_a_tag() { | |||
| 428 | let keys = Keys::generate(); | 365 | let keys = Keys::generate(); |
| 429 | let coord = "30617:pubkey123:my-repo"; | 366 | let coord = "30617:pubkey123:my-repo"; |
| 430 | let event = create_test_pr_event(&keys, coord); | 367 | let event = create_test_pr_event(&keys, coord); |
| 431 | 368 | ||
| 432 | let has_a_tag = event.tags.iter().any(|tag| { | 369 | let has_a_tag = event.tags.iter().any(|tag| { |
| 433 | let tag_vec = tag.clone().to_vec(); | 370 | let tag_vec = tag.clone().to_vec(); |
| 434 | tag_vec.len() >= 2 && tag_vec[0] == "a" | 371 | tag_vec.len() >= 2 && tag_vec[0] == "a" |
| 435 | }); | 372 | }); |
| 436 | 373 | ||
| 437 | assert!(has_a_tag); | 374 | assert!(has_a_tag); |
| 438 | } | 375 | } |
| 439 | 376 | ||
| @@ -443,12 +380,12 @@ fn test_issue_event_has_a_tag() { | |||
| 443 | let keys = Keys::generate(); | 380 | let keys = Keys::generate(); |
| 444 | let coord = "30617:pubkey123:my-repo"; | 381 | let coord = "30617:pubkey123:my-repo"; |
| 445 | let event = create_test_issue_event(&keys, coord); | 382 | let event = create_test_issue_event(&keys, coord); |
| 446 | 383 | ||
| 447 | let has_a_tag = event.tags.iter().any(|tag| { | 384 | let has_a_tag = event.tags.iter().any(|tag| { |
| 448 | let tag_vec = tag.clone().to_vec(); | 385 | let tag_vec = tag.clone().to_vec(); |
| 449 | tag_vec.len() >= 2 && tag_vec[0] == "a" | 386 | tag_vec.len() >= 2 && tag_vec[0] == "a" |
| 450 | }); | 387 | }); |
| 451 | 388 | ||
| 452 | assert!(has_a_tag); | 389 | assert!(has_a_tag); |
| 453 | } | 390 | } |
| 454 | 391 | ||
| @@ -458,12 +395,12 @@ fn test_reply_event_has_e_tag() { | |||
| 458 | let keys = Keys::generate(); | 395 | let keys = Keys::generate(); |
| 459 | let event_id = "abc123def456"; | 396 | let event_id = "abc123def456"; |
| 460 | let event = create_test_reply_event(&keys, event_id); | 397 | let event = create_test_reply_event(&keys, event_id); |
| 461 | 398 | ||
| 462 | let has_e_tag = event.tags.iter().any(|tag| { | 399 | let has_e_tag = event.tags.iter().any(|tag| { |
| 463 | let tag_vec = tag.clone().to_vec(); | 400 | let tag_vec = tag.clone().to_vec(); |
| 464 | tag_vec.len() >= 2 && tag_vec[0] == "e" | 401 | tag_vec.len() >= 2 && tag_vec[0] == "e" |
| 465 | }); | 402 | }); |
| 466 | 403 | ||
| 467 | assert!(has_e_tag); | 404 | assert!(has_e_tag); |
| 468 | } | 405 | } |
| 469 | 406 | ||
| @@ -477,50 +414,55 @@ fn test_subscription_lifecycle() { | |||
| 477 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); | 414 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); |
| 478 | let mut subscribed_events: HashSet<String> = HashSet::new(); | 415 | let mut subscribed_events: HashSet<String> = HashSet::new(); |
| 479 | let mut is_consolidated = false; | 416 | let mut is_consolidated = false; |
| 480 | 417 | ||
| 481 | // Initial state | 418 | // Initial state |
| 482 | let initial_count = 1 + subscribed_announcements.len() + subscribed_events.len(); | 419 | let initial_count = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 483 | assert_eq!(initial_count, 1); | 420 | assert_eq!(initial_count, 1); |
| 484 | 421 | ||
| 485 | // Add some announcements | 422 | // Add some announcements |
| 486 | for i in 0..50 { | 423 | for i in 0..50 { |
| 487 | subscribed_announcements.insert(format!("ann_{}", i)); | 424 | subscribed_announcements.insert(format!("ann_{}", i)); |
| 488 | } | 425 | } |
| 489 | 426 | ||
| 490 | let after_announcements = 1 + subscribed_announcements.len() + subscribed_events.len(); | 427 | let after_announcements = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 491 | assert_eq!(after_announcements, 51); | 428 | assert_eq!(after_announcements, 51); |
| 492 | 429 | ||
| 493 | // Add some events | 430 | // Add some events |
| 494 | for i in 0..50 { | 431 | for i in 0..50 { |
| 495 | subscribed_events.insert(format!("evt_{}", i)); | 432 | subscribed_events.insert(format!("evt_{}", i)); |
| 496 | } | 433 | } |
| 497 | 434 | ||
| 498 | let after_events = 1 + subscribed_announcements.len() + subscribed_events.len(); | 435 | let after_events = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 499 | assert_eq!(after_events, 101); | 436 | assert_eq!(after_events, 101); |
| 500 | 437 | ||
| 501 | // Add more to exceed threshold | 438 | // Add more to exceed threshold |
| 502 | for i in 50..100 { | 439 | for i in 50..100 { |
| 503 | subscribed_announcements.insert(format!("ann_{}", i)); | 440 | subscribed_announcements.insert(format!("ann_{}", i)); |
| 504 | } | 441 | } |
| 505 | 442 | ||
| 506 | let before_consolidation = 1 + subscribed_announcements.len() + subscribed_events.len(); | 443 | let before_consolidation = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 507 | assert_eq!(before_consolidation, 151); | 444 | assert_eq!(before_consolidation, 151); |
| 508 | 445 | ||
| 509 | // Should trigger consolidation | 446 | // Should trigger consolidation |
| 510 | let should_consolidate = !is_consolidated && before_consolidation > CONSOLIDATION_THRESHOLD; | 447 | let should_consolidate = !is_consolidated && before_consolidation > CONSOLIDATION_THRESHOLD; |
| 511 | assert!(should_consolidate); | 448 | assert!(should_consolidate); |
| 512 | 449 | ||
| 513 | // Consolidate | 450 | // Consolidate |
| 514 | subscribed_announcements.clear(); | 451 | subscribed_announcements.clear(); |
| 515 | subscribed_events.clear(); | 452 | subscribed_events.clear(); |
| 516 | is_consolidated = true; | 453 | is_consolidated = true; |
| 517 | 454 | ||
| 518 | // After consolidation | 455 | // After consolidation |
| 519 | let after_consolidation = if is_consolidated { 1 } else { 1 + subscribed_announcements.len() + subscribed_events.len() }; | 456 | let after_consolidation = if is_consolidated { |
| 457 | 1 | ||
| 458 | } else { | ||
| 459 | 1 + subscribed_announcements.len() + subscribed_events.len() | ||
| 460 | }; | ||
| 520 | assert_eq!(after_consolidation, 1); | 461 | assert_eq!(after_consolidation, 1); |
| 521 | 462 | ||
| 522 | // Should not trigger consolidation again | 463 | // Should not trigger consolidation again |
| 523 | let should_consolidate_again = !is_consolidated && after_consolidation > CONSOLIDATION_THRESHOLD; | 464 | let should_consolidate_again = |
| 465 | !is_consolidated && after_consolidation > CONSOLIDATION_THRESHOLD; | ||
| 524 | assert!(!should_consolidate_again); | 466 | assert!(!should_consolidate_again); |
| 525 | } | 467 | } |
| 526 | 468 | ||
| @@ -528,11 +470,11 @@ fn test_subscription_lifecycle() { | |||
| 528 | #[test] | 470 | #[test] |
| 529 | fn test_consolidated_blocks_additions() { | 471 | fn test_consolidated_blocks_additions() { |
| 530 | let is_consolidated = true; | 472 | let is_consolidated = true; |
| 531 | 473 | ||
| 532 | // When consolidated, add_announcement should return None (simulated) | 474 | // When consolidated, add_announcement should return None (simulated) |
| 533 | // The logic is: if is_consolidated, return None | 475 | // The logic is: if is_consolidated, return None |
| 534 | let should_add = !is_consolidated; | 476 | let should_add = !is_consolidated; |
| 535 | 477 | ||
| 536 | assert!(!should_add); | 478 | assert!(!should_add); |
| 537 | } | 479 | } |
| 538 | 480 | ||
| @@ -542,12 +484,12 @@ fn test_non_consolidated_allows_additions() { | |||
| 542 | let is_consolidated = false; | 484 | let is_consolidated = false; |
| 543 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); | 485 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); |
| 544 | let event_id = "new_announcement"; | 486 | let event_id = "new_announcement"; |
| 545 | 487 | ||
| 546 | // When not consolidated and event not in set, should add | 488 | // When not consolidated and event not in set, should add |
| 547 | let should_add = !is_consolidated && !subscribed_announcements.contains(event_id); | 489 | let should_add = !is_consolidated && !subscribed_announcements.contains(event_id); |
| 548 | 490 | ||
| 549 | assert!(should_add); | 491 | assert!(should_add); |
| 550 | 492 | ||
| 551 | subscribed_announcements.insert(event_id.to_string()); | 493 | subscribed_announcements.insert(event_id.to_string()); |
| 552 | assert!(subscribed_announcements.contains(event_id)); | 494 | assert!(subscribed_announcements.contains(event_id)); |
| 553 | } | 495 | } |
| @@ -562,7 +504,7 @@ fn test_announcement_coordinate_format() { | |||
| 562 | let keys = Keys::generate(); | 504 | let keys = Keys::generate(); |
| 563 | let identifier = "my-repo"; | 505 | let identifier = "my-repo"; |
| 564 | let event = create_test_announcement(&keys, identifier); | 506 | let event = create_test_announcement(&keys, identifier); |
| 565 | 507 | ||
| 566 | // Extract d tag | 508 | // Extract d tag |
| 567 | let d_tag = event.tags.iter().find_map(|tag| { | 509 | let d_tag = event.tags.iter().find_map(|tag| { |
| 568 | let tag_vec = tag.clone().to_vec(); | 510 | let tag_vec = tag.clone().to_vec(); |
| @@ -572,13 +514,18 @@ fn test_announcement_coordinate_format() { | |||
| 572 | None | 514 | None |
| 573 | } | 515 | } |
| 574 | }); | 516 | }); |
| 575 | 517 | ||
| 576 | assert!(d_tag.is_some()); | 518 | assert!(d_tag.is_some()); |
| 577 | assert_eq!(d_tag.unwrap(), identifier); | 519 | assert_eq!(d_tag.unwrap(), identifier); |
| 578 | 520 | ||
| 579 | // Build coordinate: kind:pubkey:identifier | 521 | // Build coordinate: kind:pubkey:identifier |
| 580 | let coord = format!("{}:{}:{}", KIND_REPOSITORY_ANNOUNCEMENT, event.pubkey.to_hex(), identifier); | 522 | let coord = format!( |
| 581 | 523 | "{}:{}:{}", | |
| 524 | KIND_REPOSITORY_ANNOUNCEMENT, | ||
| 525 | event.pubkey.to_hex(), | ||
| 526 | identifier | ||
| 527 | ); | ||
| 528 | |||
| 582 | // Verify format | 529 | // Verify format |
| 583 | let parts: Vec<&str> = coord.split(':').collect(); | 530 | let parts: Vec<&str> = coord.split(':').collect(); |
| 584 | assert_eq!(parts.len(), 3); | 531 | assert_eq!(parts.len(), 3); |
| @@ -590,16 +537,21 @@ fn test_announcement_coordinate_format() { | |||
| 590 | #[test] | 537 | #[test] |
| 591 | fn test_multiple_announcement_coordinates_unique() { | 538 | fn test_multiple_announcement_coordinates_unique() { |
| 592 | let keys = Keys::generate(); | 539 | let keys = Keys::generate(); |
| 593 | 540 | ||
| 594 | let identifiers = vec!["repo1", "repo2", "repo3"]; | 541 | let identifiers = vec!["repo1", "repo2", "repo3"]; |
| 595 | let mut coords: HashSet<String> = HashSet::new(); | 542 | let mut coords: HashSet<String> = HashSet::new(); |
| 596 | 543 | ||
| 597 | for id in identifiers { | 544 | for id in identifiers { |
| 598 | let event = create_test_announcement(&keys, id); | 545 | let event = create_test_announcement(&keys, id); |
| 599 | let coord = format!("{}:{}:{}", KIND_REPOSITORY_ANNOUNCEMENT, event.pubkey.to_hex(), id); | 546 | let coord = format!( |
| 547 | "{}:{}:{}", | ||
| 548 | KIND_REPOSITORY_ANNOUNCEMENT, | ||
| 549 | event.pubkey.to_hex(), | ||
| 550 | id | ||
| 551 | ); | ||
| 600 | coords.insert(coord); | 552 | coords.insert(coord); |
| 601 | } | 553 | } |
| 602 | 554 | ||
| 603 | assert_eq!(coords.len(), 3); | 555 | assert_eq!(coords.len(), 3); |
| 604 | } | 556 | } |
| 605 | 557 | ||
| @@ -614,30 +566,34 @@ fn test_workflow_announcement_then_pr() { | |||
| 614 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); | 566 | let mut subscribed_announcements: HashSet<String> = HashSet::new(); |
| 615 | let mut subscribed_events: HashSet<String> = HashSet::new(); | 567 | let mut subscribed_events: HashSet<String> = HashSet::new(); |
| 616 | let is_consolidated = false; | 568 | let is_consolidated = false; |
| 617 | 569 | ||
| 618 | // Step 1: Receive announcement | 570 | // Step 1: Receive announcement |
| 619 | let announcement = create_test_announcement(&keys, "my-repo"); | 571 | let announcement = create_test_announcement(&keys, "my-repo"); |
| 620 | let ann_id = announcement.id.to_hex(); | 572 | let ann_id = announcement.id.to_hex(); |
| 621 | 573 | ||
| 622 | // Should add to tracking (simulating add_announcement) | 574 | // Should add to tracking (simulating add_announcement) |
| 623 | let should_add_ann = !is_consolidated && !subscribed_announcements.contains(&ann_id); | 575 | let should_add_ann = !is_consolidated && !subscribed_announcements.contains(&ann_id); |
| 624 | assert!(should_add_ann); | 576 | assert!(should_add_ann); |
| 625 | subscribed_announcements.insert(ann_id.clone()); | 577 | subscribed_announcements.insert(ann_id.clone()); |
| 626 | 578 | ||
| 627 | // Filter count should increase | 579 | // Filter count should increase |
| 628 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); | 580 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 629 | assert_eq!(filter_count, 2); | 581 | assert_eq!(filter_count, 2); |
| 630 | 582 | ||
| 631 | // Step 2: Receive PR for that repo | 583 | // Step 2: Receive PR for that repo |
| 632 | let coord = format!("{}:{}:my-repo", KIND_REPOSITORY_ANNOUNCEMENT, keys.public_key().to_hex()); | 584 | let coord = format!( |
| 585 | "{}:{}:my-repo", | ||
| 586 | KIND_REPOSITORY_ANNOUNCEMENT, | ||
| 587 | keys.public_key().to_hex() | ||
| 588 | ); | ||
| 633 | let pr = create_test_pr_event(&keys, &coord); | 589 | let pr = create_test_pr_event(&keys, &coord); |
| 634 | let pr_id = pr.id.to_hex(); | 590 | let pr_id = pr.id.to_hex(); |
| 635 | 591 | ||
| 636 | // Should add to tracking (simulating add_event) | 592 | // Should add to tracking (simulating add_event) |
| 637 | let should_add_pr = !is_consolidated && !subscribed_events.contains(&pr_id); | 593 | let should_add_pr = !is_consolidated && !subscribed_events.contains(&pr_id); |
| 638 | assert!(should_add_pr); | 594 | assert!(should_add_pr); |
| 639 | subscribed_events.insert(pr_id.clone()); | 595 | subscribed_events.insert(pr_id.clone()); |
| 640 | 596 | ||
| 641 | // Filter count should increase again | 597 | // Filter count should increase again |
| 642 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); | 598 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 643 | assert_eq!(filter_count, 3); | 599 | assert_eq!(filter_count, 3); |
| @@ -651,16 +607,16 @@ fn test_stress_many_items_triggers_consolidation() { | |||
| 651 | let mut subscribed_events: HashSet<String> = HashSet::new(); | 607 | let mut subscribed_events: HashSet<String> = HashSet::new(); |
| 652 | let mut is_consolidated = false; | 608 | let mut is_consolidated = false; |
| 653 | let mut consolidation_triggered = false; | 609 | let mut consolidation_triggered = false; |
| 654 | 610 | ||
| 655 | // Add 100 announcements | 611 | // Add 100 announcements |
| 656 | for i in 0..100 { | 612 | for i in 0..100 { |
| 657 | let event = create_test_announcement(&keys, &format!("repo-{}", i)); | 613 | let event = create_test_announcement(&keys, &format!("repo-{}", i)); |
| 658 | let event_id = event.id.to_hex(); | 614 | let event_id = event.id.to_hex(); |
| 659 | 615 | ||
| 660 | if !is_consolidated && !subscribed_announcements.contains(&event_id) { | 616 | if !is_consolidated && !subscribed_announcements.contains(&event_id) { |
| 661 | subscribed_announcements.insert(event_id); | 617 | subscribed_announcements.insert(event_id); |
| 662 | } | 618 | } |
| 663 | 619 | ||
| 664 | // Check consolidation after each add | 620 | // Check consolidation after each add |
| 665 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); | 621 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 666 | if !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD { | 622 | if !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD { |
| @@ -671,18 +627,18 @@ fn test_stress_many_items_triggers_consolidation() { | |||
| 671 | break; | 627 | break; |
| 672 | } | 628 | } |
| 673 | } | 629 | } |
| 674 | 630 | ||
| 675 | // If we didn't consolidate yet, add events | 631 | // If we didn't consolidate yet, add events |
| 676 | if !consolidation_triggered { | 632 | if !consolidation_triggered { |
| 677 | for i in 0..100 { | 633 | for i in 0..100 { |
| 678 | let coord = format!("30617:pubkey:repo-{}", i); | 634 | let coord = format!("30617:pubkey:repo-{}", i); |
| 679 | let event = create_test_pr_event(&keys, &coord); | 635 | let event = create_test_pr_event(&keys, &coord); |
| 680 | let event_id = event.id.to_hex(); | 636 | let event_id = event.id.to_hex(); |
| 681 | 637 | ||
| 682 | if !is_consolidated && !subscribed_events.contains(&event_id) { | 638 | if !is_consolidated && !subscribed_events.contains(&event_id) { |
| 683 | subscribed_events.insert(event_id); | 639 | subscribed_events.insert(event_id); |
| 684 | } | 640 | } |
| 685 | 641 | ||
| 686 | // Check consolidation after each add | 642 | // Check consolidation after each add |
| 687 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); | 643 | let filter_count = 1 + subscribed_announcements.len() + subscribed_events.len(); |
| 688 | if !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD { | 644 | if !is_consolidated && filter_count > CONSOLIDATION_THRESHOLD { |
| @@ -694,55 +650,12 @@ fn test_stress_many_items_triggers_consolidation() { | |||
| 694 | } | 650 | } |
| 695 | } | 651 | } |
| 696 | } | 652 | } |
| 697 | 653 | ||
| 698 | // Consolidation should have been triggered | 654 | // Consolidation should have been triggered |
| 699 | assert!(consolidation_triggered); | 655 | assert!(consolidation_triggered); |
| 700 | assert!(is_consolidated); | 656 | assert!(is_consolidated); |
| 701 | 657 | ||
| 702 | // After consolidation, counts should be reset | 658 | // After consolidation, counts should be reset |
| 703 | assert_eq!(subscribed_announcements.len(), 0); | 659 | assert_eq!(subscribed_announcements.len(), 0); |
| 704 | assert_eq!(subscribed_events.len(), 0); | 660 | assert_eq!(subscribed_events.len(), 0); |
| 705 | } | 661 | } |
| 706 | |||
| 707 | /// Test that all PR/Issue kinds are handled consistently | ||
| 708 | #[test] | ||
| 709 | fn test_all_pr_issue_kinds_handled() { | ||
| 710 | let keys = Keys::generate(); | ||
| 711 | let coord = "30617:pubkey:repo"; | ||
| 712 | |||
| 713 | // All these kinds should be identified as PR/Issue | ||
| 714 | let pr_kinds = vec![1617, 1618, 1619, 1621, 1622]; | ||
| 715 | |||
| 716 | for kind in pr_kinds { | ||
| 717 | assert!( | ||
| 718 | SubscriptionManager::is_pr_issue_kind(kind), | ||
| 719 | "Kind {} should be identified as PR/Issue", | ||
| 720 | kind | ||
| 721 | ); | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | /// Test that announcement and PR/Issue kinds are mutually exclusive | ||
| 726 | #[test] | ||
| 727 | fn test_kind_categories_mutually_exclusive() { | ||
| 728 | let announcement_kinds = vec![30617, 30618]; | ||
| 729 | let pr_issue_kinds = vec![1617, 1618, 1619, 1621, 1622]; | ||
| 730 | |||
| 731 | // No announcement kind should be a PR/Issue kind | ||
| 732 | for kind in &announcement_kinds { | ||
| 733 | assert!( | ||
| 734 | !SubscriptionManager::is_pr_issue_kind(*kind), | ||
| 735 | "Announcement kind {} should not be PR/Issue", | ||
| 736 | kind | ||
| 737 | ); | ||
| 738 | } | ||
| 739 | |||
| 740 | // No PR/Issue kind should be an announcement kind | ||
| 741 | for kind in &pr_issue_kinds { | ||
| 742 | assert!( | ||
| 743 | !SubscriptionManager::is_announcement_kind(*kind), | ||
| 744 | "PR/Issue kind {} should not be announcement", | ||
| 745 | kind | ||
| 746 | ); | ||
| 747 | } | ||
| 748 | } \ No newline at end of file | ||