diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-10-01 00:00:00 +0100 |
| commit | 000901c0cbca8464b5a89bcc93c5474f6564bafd (patch) | |
| tree | 0ae11836c173ec6246e8b1eab7dc1e265e125426 /tests | |
| parent | b9a88672b8734448615354e3f46748d2fdc2f647 (diff) | |
feat(prs-create) send to multiple relays
add tests but these currently don't work when run together
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/prs_create.rs | 455 |
1 files changed, 434 insertions, 21 deletions
diff --git a/tests/prs_create.rs b/tests/prs_create.rs index d598e34..0863496 100644 --- a/tests/prs_create.rs +++ b/tests/prs_create.rs | |||
| @@ -121,12 +121,36 @@ mod when_commits_behind_ask_to_proceed { | |||
| 121 | } | 121 | } |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | mod when_no_commits_behind { | 124 | #[test] |
| 125 | #[serial] | ||
| 126 | fn cli_message_creating_patches() -> Result<()> { | ||
| 127 | let test_repo = GitTestRepo::default(); | ||
| 128 | test_repo.populate()?; | ||
| 129 | // create feature branch with 2 commit ahead | ||
| 130 | test_repo.create_branch("feature")?; | ||
| 131 | test_repo.checkout("feature")?; | ||
| 132 | std::fs::write(test_repo.dir.join("t3.md"), "some content")?; | ||
| 133 | test_repo.stage_and_commit("add t3.md")?; | ||
| 134 | std::fs::write(test_repo.dir.join("t4.md"), "some content")?; | ||
| 135 | test_repo.stage_and_commit("add t4.md")?; | ||
| 136 | |||
| 137 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | ||
| 138 | |||
| 139 | p.expect("creating patch for 2 commits from 'head' that can be merged into 'main'")?; | ||
| 140 | p.exit()?; | ||
| 141 | Ok(()) | ||
| 142 | } | ||
| 143 | |||
| 144 | mod sends_pr_and_2_patches_to_3_relays { | ||
| 145 | use futures::join; | ||
| 146 | use test_utils::relay::Relay; | ||
| 147 | |||
| 125 | use super::*; | 148 | use super::*; |
| 126 | 149 | ||
| 127 | #[test] | 150 | static PR_KIND: u64 = 318; |
| 128 | #[serial] | 151 | static PATCH_KIND: u64 = 317; |
| 129 | fn message_for_creating_patches() -> Result<()> { | 152 | |
| 153 | fn prep_git_repo() -> Result<GitTestRepo> { | ||
| 130 | let test_repo = GitTestRepo::default(); | 154 | let test_repo = GitTestRepo::default(); |
| 131 | test_repo.populate()?; | 155 | test_repo.populate()?; |
| 132 | // create feature branch with 2 commit ahead | 156 | // create feature branch with 2 commit ahead |
| @@ -136,28 +160,417 @@ mod when_no_commits_behind { | |||
| 136 | test_repo.stage_and_commit("add t3.md")?; | 160 | test_repo.stage_and_commit("add t3.md")?; |
| 137 | std::fs::write(test_repo.dir.join("t4.md"), "some content")?; | 161 | std::fs::write(test_repo.dir.join("t4.md"), "some content")?; |
| 138 | test_repo.stage_and_commit("add t4.md")?; | 162 | test_repo.stage_and_commit("add t4.md")?; |
| 163 | Ok(test_repo) | ||
| 164 | } | ||
| 139 | 165 | ||
| 140 | let mut p = CliTester::new_from_dir(&test_repo.dir, ["prs", "create"]); | 166 | fn cli_tester_create_pr(git_repo: &GitTestRepo) -> CliTester { |
| 167 | CliTester::new_from_dir( | ||
| 168 | &git_repo.dir, | ||
| 169 | [ | ||
| 170 | "--nsec", | ||
| 171 | TEST_KEY_1_NSEC, | ||
| 172 | "--disable-cli-spinners", | ||
| 173 | "prs", | ||
| 174 | "create", | ||
| 175 | "--title", | ||
| 176 | "example", | ||
| 177 | "--description", | ||
| 178 | "example", | ||
| 179 | ], | ||
| 180 | ) | ||
| 181 | } | ||
| 141 | 182 | ||
| 142 | p.expect("creating patch for 2 commits from 'head' that can be merged into 'main'")?; | 183 | fn expect_msgs_first(p: &mut CliTester) -> Result<()> { |
| 143 | p.exit()?; | 184 | p.expect("creating patch for 2 commits from 'head' that can be merged into 'main'\r\n")?; |
| 185 | p.expect( | ||
| 186 | "logged in as npub175lyhnt6nn00qjw0v3navw9pxgv43txnku0tpxprl4h6mvpr6a5qlphudg\r\n", | ||
| 187 | )?; | ||
| 188 | p.expect("connecting to relays...\r\n")?; | ||
| 189 | p.expect("\r")?; | ||
| 190 | p.expect("posting 1 pull request with 2 commits...\r\n")?; | ||
| 191 | Ok(()) | ||
| 192 | } | ||
| 193 | |||
| 194 | async fn prep_run_create_pr() -> Result<(Relay<'static>, Relay<'static>, Relay<'static>)> { | ||
| 195 | let git_repo = prep_git_repo()?; | ||
| 196 | |||
| 197 | let (mut r51, mut r52, mut r53) = ( | ||
| 198 | Relay::new(8051, None), | ||
| 199 | Relay::new(8052, None), | ||
| 200 | Relay::new(8053, None), | ||
| 201 | ); | ||
| 202 | |||
| 203 | // // check relay had the right number of events | ||
| 204 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 205 | let mut p = cli_tester_create_pr(&git_repo); | ||
| 206 | p.expect_end_eventually()?; | ||
| 207 | Ok(()) | ||
| 208 | }); | ||
| 209 | |||
| 210 | // launch relay | ||
| 211 | let _ = join!( | ||
| 212 | r51.listen_until_close(), | ||
| 213 | r52.listen_until_close(), | ||
| 214 | r53.listen_until_close(), | ||
| 215 | ); | ||
| 216 | cli_tester_handle.join().unwrap()?; | ||
| 217 | Ok((r51, r52, r53)) | ||
| 218 | } | ||
| 219 | |||
| 220 | #[test] | ||
| 221 | #[serial] | ||
| 222 | fn only_1_pr_kind_event_sent_to_each_relay() -> Result<()> { | ||
| 223 | let (r51, r52, r53) = futures::executor::block_on(prep_run_create_pr())?; | ||
| 224 | for relay in [&r51, &r52, &r53] { | ||
| 225 | assert_eq!( | ||
| 226 | relay | ||
| 227 | .events | ||
| 228 | .iter() | ||
| 229 | .filter(|e| e.kind.as_u64().eq(&PR_KIND)) | ||
| 230 | .count(), | ||
| 231 | 1, | ||
| 232 | ); | ||
| 233 | } | ||
| 234 | Ok(()) | ||
| 235 | } | ||
| 236 | |||
| 237 | #[test] | ||
| 238 | #[serial] | ||
| 239 | fn only_2_patch_kind_events_sent_to_each_relay() -> Result<()> { | ||
| 240 | let (r51, r52, r53) = futures::executor::block_on(prep_run_create_pr())?; | ||
| 241 | for relay in [&r51, &r52, &r53] { | ||
| 242 | assert_eq!( | ||
| 243 | relay | ||
| 244 | .events | ||
| 245 | .iter() | ||
| 246 | .filter(|e| e.kind.as_u64().eq(&PATCH_KIND)) | ||
| 247 | .count(), | ||
| 248 | 2, | ||
| 249 | ); | ||
| 250 | } | ||
| 144 | Ok(()) | 251 | Ok(()) |
| 145 | } | 252 | } |
| 146 | } | ||
| 147 | 253 | ||
| 148 | // #[test] | 254 | #[test] |
| 149 | // #[serial] | 255 | #[serial] |
| 150 | // fn succeeds_with_text_logged_in_as_npub() -> Result<()> { | 256 | fn patch_content_contains_patch_in_email_format() -> Result<()> { |
| 151 | // with_fresh_config(|| { | 257 | let (r51, r52, r53) = futures::executor::block_on(prep_run_create_pr())?; |
| 152 | // let mut p = CliTester::new(["login"]); | 258 | for relay in [&r51, &r52, &r53] { |
| 259 | let patch_events: Vec<&nostr::Event> = relay | ||
| 260 | .events | ||
| 261 | .iter() | ||
| 262 | .filter(|e| e.kind.as_u64().eq(&PATCH_KIND)) | ||
| 263 | .collect(); | ||
| 264 | |||
| 265 | assert_eq!( | ||
| 266 | patch_events[0].content, | ||
| 267 | "\ | ||
| 268 | From fe973a840fba2a8ab37dd505c154854a69a6505c Mon Sep 17 00:00:00 2001\n\ | ||
| 269 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ | ||
| 270 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ | ||
| 271 | Subject: [PATCH] add t4.md\n\ | ||
| 272 | \n\ | ||
| 273 | ---\n \ | ||
| 274 | t4.md | 1 +\n \ | ||
| 275 | 1 file changed, 1 insertion(+)\n \ | ||
| 276 | create mode 100644 t4.md\n\ | ||
| 277 | \n\ | ||
| 278 | diff --git a/t4.md b/t4.md\n\ | ||
| 279 | new file mode 100644\n\ | ||
| 280 | index 0000000..f0eec86\n\ | ||
| 281 | --- /dev/null\n\ | ||
| 282 | +++ b/t4.md\n\ | ||
| 283 | @@ -0,0 +1 @@\n\ | ||
| 284 | +some content\n\\ \ | ||
| 285 | No newline at end of file\n\ | ||
| 286 | --\n\ | ||
| 287 | libgit2 1.7.1\n\ | ||
| 288 | \n\ | ||
| 289 | ", | ||
| 290 | ); | ||
| 291 | assert_eq!( | ||
| 292 | patch_events[1].content, | ||
| 293 | "\ | ||
| 294 | From 232efb37ebc67692c9e9ff58b83c0d3d63971a0a Mon Sep 17 00:00:00 2001\n\ | ||
| 295 | From: Joe Bloggs <joe.bloggs@pm.me>\n\ | ||
| 296 | Date: Thu, 1 Jan 1970 00:00:00 +0000\n\ | ||
| 297 | Subject: [PATCH] add t3.md\n\ | ||
| 298 | \n\ | ||
| 299 | ---\n \ | ||
| 300 | t3.md | 1 +\n \ | ||
| 301 | 1 file changed, 1 insertion(+)\n \ | ||
| 302 | create mode 100644 t3.md\n\ | ||
| 303 | \n\ | ||
| 304 | diff --git a/t3.md b/t3.md\n\ | ||
| 305 | new file mode 100644\n\ | ||
| 306 | index 0000000..f0eec86\n\ | ||
| 307 | --- /dev/null\n\ | ||
| 308 | +++ b/t3.md\n\ | ||
| 309 | @@ -0,0 +1 @@\n\ | ||
| 310 | +some content\n\\ \ | ||
| 311 | No newline at end of file\n\ | ||
| 312 | --\n\ | ||
| 313 | libgit2 1.7.1\n\ | ||
| 314 | \n\ | ||
| 315 | ", | ||
| 316 | ); | ||
| 317 | } | ||
| 318 | Ok(()) | ||
| 319 | } | ||
| 320 | |||
| 321 | mod pr_tags { | ||
| 322 | use super::*; | ||
| 323 | #[test] | ||
| 324 | #[serial] | ||
| 325 | fn pr_tags_repo_commit() -> Result<()> { | ||
| 326 | let (r51, r52, r53) = futures::executor::block_on(prep_run_create_pr())?; | ||
| 327 | for relay in [&r51, &r52, &r53] { | ||
| 328 | let pr_event: &nostr::Event = relay | ||
| 329 | .events | ||
| 330 | .iter() | ||
| 331 | .find(|e| e.kind.as_u64().eq(&PR_KIND)) | ||
| 332 | .unwrap(); | ||
| 333 | |||
| 334 | // root commit 't' tag | ||
| 335 | assert!(pr_event.tags.iter().any(|t| t.as_vec()[0].eq("t") | ||
| 336 | && t.as_vec()[1].eq("r-9ee507fc4357d7ee16a5d8901bedcd103f23c17d"))); | ||
| 337 | } | ||
| 338 | Ok(()) | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | mod patch_tags { | ||
| 343 | use super::*; | ||
| 344 | #[test] | ||
| 345 | #[serial] | ||
| 346 | fn patch_tags_correctly_formatted() -> Result<()> { | ||
| 347 | let (r51, r52, r53) = futures::executor::block_on(prep_run_create_pr())?; | ||
| 348 | for relay in [&r51, &r52, &r53] { | ||
| 349 | let patch_events: Vec<&nostr::Event> = relay | ||
| 350 | .events | ||
| 351 | .iter() | ||
| 352 | .filter(|e| e.kind.as_u64().eq(&PATCH_KIND)) | ||
| 353 | .collect(); | ||
| 354 | |||
| 355 | static COMMIT_ID: &str = "fe973a840fba2a8ab37dd505c154854a69a6505c"; | ||
| 356 | let most_recent_patch = patch_events[0]; | ||
| 357 | |||
| 358 | // commit 't' and 'commit' tag | ||
| 359 | assert!( | ||
| 360 | most_recent_patch | ||
| 361 | .tags | ||
| 362 | .iter() | ||
| 363 | .any(|t| t.as_vec()[0].eq("t") && t.as_vec()[1].eq(COMMIT_ID)) | ||
| 364 | ); | ||
| 365 | assert!( | ||
| 366 | most_recent_patch | ||
| 367 | .tags | ||
| 368 | .iter() | ||
| 369 | .any(|t| t.as_vec()[0].eq("commit") && t.as_vec()[1].eq(COMMIT_ID)) | ||
| 370 | ); | ||
| 371 | |||
| 372 | // commit parent 't' and 'parent-commit' tag | ||
| 373 | static COMMIT_PARENT_ID: &str = "232efb37ebc67692c9e9ff58b83c0d3d63971a0a"; | ||
| 374 | assert!( | ||
| 375 | most_recent_patch | ||
| 376 | .tags | ||
| 377 | .iter() | ||
| 378 | .any(|t| t.as_vec()[0].eq("t") && t.as_vec()[1].eq(COMMIT_PARENT_ID)) | ||
| 379 | ); | ||
| 380 | assert!(most_recent_patch.tags.iter().any( | ||
| 381 | |t| t.as_vec()[0].eq("parent-commit") && t.as_vec()[1].eq(COMMIT_PARENT_ID) | ||
| 382 | )); | ||
| 383 | |||
| 384 | // root commit 't' tag | ||
| 385 | assert!(most_recent_patch.tags.iter().any(|t| t.as_vec()[0].eq("t") | ||
| 386 | && t.as_vec()[1].eq("r-9ee507fc4357d7ee16a5d8901bedcd103f23c17d"))); | ||
| 387 | } | ||
| 388 | Ok(()) | ||
| 389 | } | ||
| 390 | |||
| 391 | #[test] | ||
| 392 | #[serial] | ||
| 393 | fn patch_tags_pr_event_as_root() -> Result<()> { | ||
| 394 | let (r51, r52, r53) = futures::executor::block_on(prep_run_create_pr())?; | ||
| 395 | for relay in [&r51, &r52, &r53] { | ||
| 396 | let patch_events: Vec<&nostr::Event> = relay | ||
| 397 | .events | ||
| 398 | .iter() | ||
| 399 | .filter(|e| e.kind.as_u64().eq(&PATCH_KIND)) | ||
| 400 | .collect(); | ||
| 401 | |||
| 402 | let most_recent_patch = patch_events[0]; | ||
| 403 | let pr_event = relay | ||
| 404 | .events | ||
| 405 | .iter() | ||
| 406 | .find(|e| e.kind.as_u64().eq(&PR_KIND)) | ||
| 407 | .unwrap(); | ||
| 408 | |||
| 409 | let root_event_tag = most_recent_patch | ||
| 410 | .tags | ||
| 411 | .iter() | ||
| 412 | .find(|t| { | ||
| 413 | t.as_vec()[0].eq("e") && t.as_vec().len().eq(&4) && t.as_vec()[3].eq("root") | ||
| 414 | }) | ||
| 415 | .unwrap(); | ||
| 416 | |||
| 417 | assert_eq!(root_event_tag.as_vec()[1], pr_event.id.to_string()); | ||
| 418 | } | ||
| 419 | Ok(()) | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | mod cli_ouput { | ||
| 424 | use super::*; | ||
| 425 | |||
| 426 | async fn run_test_async() -> Result<()> { | ||
| 427 | let git_repo = prep_git_repo()?; | ||
| 153 | 428 | ||
| 154 | // p.expect_input(EXPECTED_NSEC_PROMPT)? | 429 | let (mut r51, mut r52, mut r53) = ( |
| 155 | // .succeeds_with(TEST_KEY_1_NSEC)?; | 430 | Relay::new(8051, None), |
| 431 | Relay::new(8052, None), | ||
| 432 | Relay::new(8053, None), | ||
| 433 | ); | ||
| 156 | 434 | ||
| 157 | // p.expect_password(EXPECTED_SET_PASSWORD_PROMPT)? | 435 | // // check relay had the right number of events |
| 158 | // .with_confirmation(EXPECTED_SET_PASSWORD_CONFIRM_PROMPT)? | 436 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { |
| 159 | // .succeeds_with(TEST_PASSWORD)?; | 437 | let mut p = cli_tester_create_pr(&git_repo); |
| 438 | expect_msgs_first(&mut p)?; | ||
| 439 | relay::expect_send_with_progress( | ||
| 440 | &mut p, | ||
| 441 | vec![ | ||
| 442 | (" [my-relay] [repo-relay] ws://localhost:8051", true, ""), | ||
| 443 | (" [my-relay] ws://localhost:8052", true, ""), | ||
| 444 | (" [repo-relay] ws://localhost:8053", true, ""), | ||
| 445 | ], | ||
| 446 | 3, | ||
| 447 | )?; | ||
| 448 | p.expect_end_with_whitespace()?; | ||
| 449 | Ok(()) | ||
| 450 | }); | ||
| 160 | 451 | ||
| 161 | // p.expect_end_with(format!("logged in as {}\r\n", | 452 | // launch relay |
| 162 | // TEST_KEY_1_NPUB).as_str()) }) | 453 | let _ = join!( |
| 163 | // } | 454 | r51.listen_until_close(), |
| 455 | r52.listen_until_close(), | ||
| 456 | r53.listen_until_close(), | ||
| 457 | ); | ||
| 458 | cli_tester_handle.join().unwrap()?; | ||
| 459 | Ok(()) | ||
| 460 | } | ||
| 461 | |||
| 462 | #[test] | ||
| 463 | #[serial] | ||
| 464 | fn check_cli_output() -> Result<()> { | ||
| 465 | futures::executor::block_on(run_test_async())?; | ||
| 466 | Ok(()) | ||
| 467 | } | ||
| 468 | } | ||
| 469 | |||
| 470 | mod first_event_rejected_by_1_relay { | ||
| 471 | use super::*; | ||
| 472 | |||
| 473 | mod only_first_rejected_event_sent_to_relay { | ||
| 474 | use super::*; | ||
| 475 | |||
| 476 | async fn run_test_async() -> Result<()> { | ||
| 477 | let git_repo = prep_git_repo()?; | ||
| 478 | |||
| 479 | let (mut r51, mut r52, mut r53) = ( | ||
| 480 | Relay::new(8051, None), | ||
| 481 | Relay::new( | ||
| 482 | 8052, | ||
| 483 | Some(&|relay, client_id, event| -> Result<()> { | ||
| 484 | relay.respond_ok(client_id, event, Some("Payment Required"))?; | ||
| 485 | Ok(()) | ||
| 486 | }), | ||
| 487 | ), | ||
| 488 | Relay::new(8053, None), | ||
| 489 | ); | ||
| 490 | |||
| 491 | // // check relay had the right number of events | ||
| 492 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 493 | let mut p = cli_tester_create_pr(&git_repo); | ||
| 494 | p.expect_end_eventually()?; | ||
| 495 | Ok(()) | ||
| 496 | }); | ||
| 497 | |||
| 498 | // launch relay | ||
| 499 | let _ = join!( | ||
| 500 | r51.listen_until_close(), | ||
| 501 | r52.listen_until_close(), | ||
| 502 | r53.listen_until_close(), | ||
| 503 | ); | ||
| 504 | cli_tester_handle.join().unwrap()?; | ||
| 505 | |||
| 506 | assert_eq!(r52.events.len(), 1); | ||
| 507 | |||
| 508 | Ok(()) | ||
| 509 | } | ||
| 510 | |||
| 511 | #[test] | ||
| 512 | #[serial] | ||
| 513 | fn only_first_rejected_event_sent_to_relay() -> Result<()> { | ||
| 514 | futures::executor::block_on(run_test_async())?; | ||
| 515 | Ok(()) | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | mod cli_show_rejection_with_comment { | ||
| 520 | use super::*; | ||
| 521 | |||
| 522 | async fn run_test_async() -> Result<(Relay<'static>, Relay<'static>, Relay<'static>)> { | ||
| 523 | let git_repo = prep_git_repo()?; | ||
| 524 | |||
| 525 | let (mut r51, mut r52, mut r53) = ( | ||
| 526 | Relay::new(8051, None), | ||
| 527 | Relay::new( | ||
| 528 | 8052, | ||
| 529 | Some(&|relay, client_id, event| -> Result<()> { | ||
| 530 | relay.respond_ok(client_id, event, Some("Payment Required"))?; | ||
| 531 | Ok(()) | ||
| 532 | }), | ||
| 533 | ), | ||
| 534 | Relay::new(8053, None), | ||
| 535 | ); | ||
| 536 | |||
| 537 | // // check relay had the right number of events | ||
| 538 | let cli_tester_handle = std::thread::spawn(move || -> Result<()> { | ||
| 539 | let mut p = cli_tester_create_pr(&git_repo); | ||
| 540 | expect_msgs_first(&mut p)?; | ||
| 541 | relay::expect_send_with_progress( | ||
| 542 | &mut p, | ||
| 543 | vec![ | ||
| 544 | (" [my-relay] [repo-relay] ws://localhost:8051", true, ""), | ||
| 545 | ( | ||
| 546 | " [my-relay] ws://localhost:8052", | ||
| 547 | false, | ||
| 548 | "error: Payment Required", | ||
| 549 | ), | ||
| 550 | (" [repo-relay] ws://localhost:8053", true, ""), | ||
| 551 | ], | ||
| 552 | 3, | ||
| 553 | )?; | ||
| 554 | p.expect_end_with_whitespace()?; | ||
| 555 | Ok(()) | ||
| 556 | }); | ||
| 557 | |||
| 558 | // launch relay | ||
| 559 | let _ = join!( | ||
| 560 | r51.listen_until_close(), | ||
| 561 | r52.listen_until_close(), | ||
| 562 | r53.listen_until_close(), | ||
| 563 | ); | ||
| 564 | cli_tester_handle.join().unwrap()?; | ||
| 565 | Ok((r51, r52, r53)) | ||
| 566 | } | ||
| 567 | |||
| 568 | #[test] | ||
| 569 | #[serial] | ||
| 570 | fn cli_show_rejection_with_comment() -> Result<()> { | ||
| 571 | futures::executor::block_on(run_test_async())?; | ||
| 572 | Ok(()) | ||
| 573 | } | ||
| 574 | } | ||
| 575 | } | ||
| 576 | } | ||