Store tests share their store and merge into tables
Browse files at this commit281e0ee9ed7c26b8b097e1aa9d3876e049feaee3Parent: 0fef3ae- test
- store tests share their store and merge into tables
4 changed files +1136 -909
- crates/forge-core/src/store/commit_status.rs +76 -44
- crates/forge-core/src/store/pulls.rs +517 -412
- crates/forge-core/src/store/reactions.rs +57 -42
- crates/forge-core/src/store/reviews.rs +486 -411
crates/forge-core/src/store/commit_status.rsmodified+76 -44
| @@ -378,9 +378,21 @@ mod tests { | |||
| 378 | (dir, store, repo.id, owner.id) | 378 | (dir, store, repo.id, owner.id) |
| 379 | } | 379 | } |
| 380 | 380 | ||
| 381 | + // One store and owner serve every scenario. Each scenario that writes | ||
| 382 | + // statuses gets its own repo so the scenarios cannot see each other. | ||
| 383 | + async fn new_repo(store: &Store, owner_id: &str, name: &str) -> String { | ||
| 384 | + store | ||
| 385 | + .create_repo(owner_id, name, "", Visibility::Public, "main") | ||
| 386 | + .await | ||
| 387 | + .expect("repo") | ||
| 388 | + .id | ||
| 389 | + } | ||
| 390 | + | ||
| 381 | #[tokio::test] | 391 | #[tokio::test] |
| 382 | - async fn upsert_keeps_one_row_per_context_and_combines() { | 392 | + async fn status_scenarios_share_one_store() { |
| 383 | let (_dir, store, repo_id, user_id) = fixture().await; | 393 | let (_dir, store, repo_id, user_id) = fixture().await; |
| 394 | + | ||
| 395 | + // Upsert: one row per context, the latest report wins, states combine. | ||
| 384 | let set = |context: &'static str, state: StatusState| { | 396 | let set = |context: &'static str, state: StatusState| { |
| 385 | let store = store.clone(); | 397 | let store = store.clone(); |
| 386 | let repo_id = repo_id.clone(); | 398 | let repo_id = repo_id.clone(); |
| @@ -396,36 +408,42 @@ mod tests { | |||
| 396 | set("b", StatusState::Success).await; | 408 | set("b", StatusState::Success).await; |
| 397 | assert_eq!( | 409 | assert_eq!( |
| 398 | store.combined_state(&repo_id, SHA_A).await.unwrap(), | 410 | store.combined_state(&repo_id, SHA_A).await.unwrap(), |
| 399 | - Some(StatusState::Pending) | 411 | + Some(StatusState::Pending), |
| 412 | + "upsert: pending wins over success" | ||
| 400 | ); | 413 | ); |
| 401 | 414 | ||
| 402 | set("a", StatusState::Failure).await; | 415 | set("a", StatusState::Failure).await; |
| 403 | assert_eq!( | 416 | assert_eq!( |
| 404 | store.combined_state(&repo_id, SHA_A).await.unwrap(), | 417 | store.combined_state(&repo_id, SHA_A).await.unwrap(), |
| 405 | - Some(StatusState::Failure) | 418 | + Some(StatusState::Failure), |
| 419 | + "upsert: failure wins over success" | ||
| 406 | ); | 420 | ); |
| 407 | 421 | ||
| 408 | // The latest report for "a" replaces the failure. | 422 | // The latest report for "a" replaces the failure. |
| 409 | let replaced = set("a", StatusState::Success).await; | 423 | let replaced = set("a", StatusState::Success).await; |
| 410 | - assert_eq!(replaced.state, StatusState::Success); | 424 | + assert_eq!(replaced.state, StatusState::Success, "upsert: stored state"); |
| 411 | assert_eq!( | 425 | assert_eq!( |
| 412 | store.combined_state(&repo_id, SHA_A).await.unwrap(), | 426 | store.combined_state(&repo_id, SHA_A).await.unwrap(), |
| 413 | - Some(StatusState::Success) | 427 | + Some(StatusState::Success), |
| 428 | + "upsert: latest report replaces the failure" | ||
| 414 | ); | 429 | ); |
| 415 | let rows = store.statuses_for(&repo_id, SHA_A).await.unwrap(); | 430 | let rows = store.statuses_for(&repo_id, SHA_A).await.unwrap(); |
| 416 | assert_eq!( | 431 | assert_eq!( |
| 417 | rows.iter().map(|r| r.context.as_str()).collect::<Vec<_>>(), | 432 | rows.iter().map(|r| r.context.as_str()).collect::<Vec<_>>(), |
| 418 | - ["a", "b"] | 433 | + ["a", "b"], |
| 434 | + "upsert: one row per context" | ||
| 435 | + ); | ||
| 436 | + assert_eq!( | ||
| 437 | + store.combined_state(&repo_id, SHA_B).await.unwrap(), | ||
| 438 | + None, | ||
| 439 | + "upsert: unreported commit has no state" | ||
| 419 | ); | 440 | ); |
| 420 | - assert_eq!(store.combined_state(&repo_id, SHA_B).await.unwrap(), None); | ||
| 421 | - } | ||
| 422 | 441 | ||
| 423 | - #[tokio::test] | 442 | + // Many-lookup skips commits without statuses. |
| 424 | - async fn many_lookup_skips_commits_without_statuses() { | 443 | + let many_repo = new_repo(&store, &user_id, "many").await; |
| 425 | - let (_dir, store, repo_id, user_id) = fixture().await; | ||
| 426 | store | 444 | store |
| 427 | .set_status( | 445 | .set_status( |
| 428 | - &repo_id, | 446 | + &many_repo, |
| 429 | SHA_A, | 447 | SHA_A, |
| 430 | "ci", | 448 | "ci", |
| 431 | StatusState::Error, | 449 | StatusState::Error, |
| @@ -436,26 +454,29 @@ mod tests { | |||
| 436 | .await | 454 | .await |
| 437 | .expect("set"); | 455 | .expect("set"); |
| 438 | let found = store | 456 | let found = store |
| 439 | - .statuses_for_many(&repo_id, &[SHA_A, SHA_B]) | 457 | + .statuses_for_many(&many_repo, &[SHA_A, SHA_B]) |
| 440 | .await | 458 | .await |
| 441 | .unwrap(); | 459 | .unwrap(); |
| 442 | - assert_eq!(found.len(), 1); | 460 | + assert_eq!(found.len(), 1, "many: only the reported commit"); |
| 443 | - assert_eq!(found.get(SHA_A), Some(&StatusState::Failure)); | 461 | + assert_eq!( |
| 462 | + found.get(SHA_A), | ||
| 463 | + Some(&StatusState::Failure), | ||
| 464 | + "many: error maps to failure" | ||
| 465 | + ); | ||
| 444 | assert!( | 466 | assert!( |
| 445 | store | 467 | store |
| 446 | - .statuses_for_many(&repo_id, &[]) | 468 | + .statuses_for_many(&many_repo, &[]) |
| 447 | .await | 469 | .await |
| 448 | .unwrap() | 470 | .unwrap() |
| 449 | - .is_empty() | 471 | + .is_empty(), |
| 472 | + "many: empty input gives nothing" | ||
| 450 | ); | 473 | ); |
| 451 | - } | ||
| 452 | 474 | ||
| 453 | - #[tokio::test] | 475 | + // Invalid input is refused and stores nothing. |
| 454 | - async fn invalid_input_is_refused_and_stores_nothing() { | 476 | + let invalid_repo = new_repo(&store, &user_id, "invalid").await; |
| 455 | - let (_dir, store, repo_id, user_id) = fixture().await; | ||
| 456 | let bad_sha = store | 477 | let bad_sha = store |
| 457 | .set_status( | 478 | .set_status( |
| 458 | - &repo_id, | 479 | + &invalid_repo, |
| 459 | "ABC", | 480 | "ABC", |
| 460 | "ci", | 481 | "ci", |
| 461 | StatusState::Success, | 482 | StatusState::Success, |
| @@ -464,10 +485,13 @@ mod tests { | |||
| 464 | &user_id, | 485 | &user_id, |
| 465 | ) | 486 | ) |
| 466 | .await; | 487 | .await; |
| 467 | - assert!(matches!(bad_sha, Err(Error::Invalid(_)))); | 488 | + assert!( |
| 489 | + matches!(bad_sha, Err(Error::Invalid(_))), | ||
| 490 | + "invalid: short sha" | ||
| 491 | + ); | ||
| 468 | let bad_url = store | 492 | let bad_url = store |
| 469 | .set_status( | 493 | .set_status( |
| 470 | - &repo_id, | 494 | + &invalid_repo, |
| 471 | SHA_A, | 495 | SHA_A, |
| 472 | "ci", | 496 | "ci", |
| 473 | StatusState::Success, | 497 | StatusState::Success, |
| @@ -476,22 +500,24 @@ mod tests { | |||
| 476 | &user_id, | 500 | &user_id, |
| 477 | ) | 501 | ) |
| 478 | .await; | 502 | .await; |
| 479 | - assert!(matches!(bad_url, Err(Error::Invalid(_)))); | 503 | + assert!( |
| 504 | + matches!(bad_url, Err(Error::Invalid(_))), | ||
| 505 | + "invalid: ftp url" | ||
| 506 | + ); | ||
| 480 | assert!( | 507 | assert!( |
| 481 | store | 508 | store |
| 482 | - .statuses_for(&repo_id, SHA_A) | 509 | + .statuses_for(&invalid_repo, SHA_A) |
| 483 | .await | 510 | .await |
| 484 | .unwrap() | 511 | .unwrap() |
| 485 | - .is_empty() | 512 | + .is_empty(), |
| 513 | + "invalid: nothing stored" | ||
| 486 | ); | 514 | ); |
| 487 | - } | ||
| 488 | 515 | ||
| 489 | - #[tokio::test] | 516 | + // Deleting the repo removes its statuses. |
| 490 | - async fn deleting_the_repo_removes_its_statuses() { | 517 | + let doomed_repo = new_repo(&store, &user_id, "doomed").await; |
| 491 | - let (_dir, store, repo_id, user_id) = fixture().await; | ||
| 492 | store | 518 | store |
| 493 | .set_status( | 519 | .set_status( |
| 494 | - &repo_id, | 520 | + &doomed_repo, |
| 495 | SHA_A, | 521 | SHA_A, |
| 496 | "ci", | 522 | "ci", |
| 497 | StatusState::Success, | 523 | StatusState::Success, |
| @@ -501,26 +527,25 @@ mod tests { | |||
| 501 | ) | 527 | ) |
| 502 | .await | 528 | .await |
| 503 | .expect("set"); | 529 | .expect("set"); |
| 504 | - store.delete_repo(&repo_id).await.expect("delete"); | 530 | + store.delete_repo(&doomed_repo).await.expect("delete"); |
| 505 | assert!( | 531 | assert!( |
| 506 | store | 532 | store |
| 507 | - .statuses_for(&repo_id, SHA_A) | 533 | + .statuses_for(&doomed_repo, SHA_A) |
| 508 | .await | 534 | .await |
| 509 | .unwrap() | 535 | .unwrap() |
| 510 | - .is_empty() | 536 | + .is_empty(), |
| 537 | + "delete repo: statuses gone" | ||
| 511 | ); | 538 | ); |
| 512 | - } | ||
| 513 | 539 | ||
| 514 | - #[tokio::test] | 540 | + // Deleting a user reassigns their statuses to the ghost user. |
| 515 | - async fn deleting_a_user_reassigns_their_statuses_to_ghost() { | 541 | + let reassigned_repo = new_repo(&store, &user_id, "reassigned").await; |
| 516 | - let (_dir, store, repo_id, _owner_id) = fixture().await; | ||
| 517 | let reporter = store | 542 | let reporter = store |
| 518 | .create_user("ci-bot", None, "not-a-real-hash", false) | 543 | .create_user("ci-bot", None, "not-a-real-hash", false) |
| 519 | .await | 544 | .await |
| 520 | .expect("user"); | 545 | .expect("user"); |
| 521 | store | 546 | store |
| 522 | .set_status( | 547 | .set_status( |
| 523 | - &repo_id, | 548 | + &reassigned_repo, |
| 524 | SHA_A, | 549 | SHA_A, |
| 525 | "ci", | 550 | "ci", |
| 526 | StatusState::Failure, | 551 | StatusState::Failure, |
| @@ -535,9 +560,16 @@ mod tests { | |||
| 535 | .delete_user(&reporter.id, &ghost.id) | 560 | .delete_user(&reporter.id, &ghost.id) |
| 536 | .await | 561 | .await |
| 537 | .expect("delete user"); | 562 | .expect("delete user"); |
| 538 | - let rows = store.statuses_for(&repo_id, SHA_A).await.unwrap(); | 563 | + let rows = store.statuses_for(&reassigned_repo, SHA_A).await.unwrap(); |
| 539 | - assert_eq!(rows.len(), 1); | 564 | + assert_eq!(rows.len(), 1, "delete user: status kept"); |
| 540 | - assert_eq!(rows[0].creator_id, ghost.id); | 565 | + assert_eq!( |
| 541 | - assert_eq!(rows[0].state, StatusState::Failure); | 566 | + rows[0].creator_id, ghost.id, |
| 567 | + "delete user: reassigned to ghost" | ||
| 568 | + ); | ||
| 569 | + assert_eq!( | ||
| 570 | + rows[0].state, | ||
| 571 | + StatusState::Failure, | ||
| 572 | + "delete user: state kept" | ||
| 573 | + ); | ||
| 542 | } | 574 | } |
| 543 | } | 575 | } |
crates/forge-core/src/store/pulls.rsmodified+517 -412
| @@ -647,6 +647,14 @@ mod tests { | |||
| 647 | } | 647 | } |
| 648 | } | 648 | } |
| 649 | 649 | ||
| 650 | + /// A fresh repo owned by Tahinli, so each scenario has its own numbering and counts. | ||
| 651 | + async fn new_repo(fx: &Fixture, name: &str) -> Repo { | ||
| 652 | + fx.store | ||
| 653 | + .create_repo(&fx.tahinli.id, name, "", Visibility::Public, "main") | ||
| 654 | + .await | ||
| 655 | + .unwrap() | ||
| 656 | + } | ||
| 657 | + | ||
| 650 | fn all_states() -> IssueFilter { | 658 | fn all_states() -> IssueFilter { |
| 651 | IssueFilter { | 659 | IssueFilter { |
| 652 | state: None, | 660 | state: None, |
| @@ -655,16 +663,17 @@ mod tests { | |||
| 655 | } | 663 | } |
| 656 | } | 664 | } |
| 657 | 665 | ||
| 658 | - /// A pull from `head_repo` (its own repo, for same-repo pulls) into `main` of `fx.repo`. | 666 | + /// A pull from `head_repo` into `main` of `repo`. |
| 659 | async fn open_pull( | 667 | async fn open_pull( |
| 660 | fx: &Fixture, | 668 | fx: &Fixture, |
| 669 | + repo: &Repo, | ||
| 661 | head_repo: &str, | 670 | head_repo: &str, |
| 662 | head_branch: &str, | 671 | head_branch: &str, |
| 663 | title: &str, | 672 | title: &str, |
| 664 | ) -> PullRequest { | 673 | ) -> PullRequest { |
| 665 | fx.store | 674 | fx.store |
| 666 | .create_pull( | 675 | .create_pull( |
| 667 | - &fx.repo.id, | 676 | + &repo.id, |
| 668 | &fx.tahinli.id, | 677 | &fx.tahinli.id, |
| 669 | title, | 678 | title, |
| 670 | "", | 679 | "", |
| @@ -678,93 +687,111 @@ mod tests { | |||
| 678 | .unwrap() | 687 | .unwrap() |
| 679 | } | 688 | } |
| 680 | 689 | ||
| 690 | + // Numbering, counts, head and mergeability updates, merges, drafts, | ||
| 691 | + // auto merge and events. Each scenario has its own repo. | ||
| 681 | #[tokio::test] | 692 | #[tokio::test] |
| 682 | - async fn issues_and_pulls_share_one_numbering() { | 693 | + async fn pull_scenarios_share_one_store() { |
| 683 | let fx = fixture().await; | 694 | let fx = fixture().await; |
| 695 | + | ||
| 696 | + // Issues and pulls share one numbering; each list shows only its kind. | ||
| 697 | + let repo = new_repo(&fx, "numbering").await; | ||
| 684 | let issue1 = fx | 698 | let issue1 = fx |
| 685 | .store | 699 | .store |
| 686 | - .create_issue(&fx.repo.id, &fx.tahinli.id, "first", "") | 700 | + .create_issue(&repo.id, &fx.tahinli.id, "first", "") |
| 687 | .await | 701 | .await |
| 688 | .unwrap(); | 702 | .unwrap(); |
| 689 | - let pull2 = open_pull(&fx, &fx.repo.id, "feature", "second").await; | 703 | + let pull2 = open_pull(&fx, &repo, &repo.id, "feature", "second").await; |
| 690 | let issue3 = fx | 704 | let issue3 = fx |
| 691 | .store | 705 | .store |
| 692 | - .create_issue(&fx.repo.id, &fx.tahinli.id, "third", "") | 706 | + .create_issue(&repo.id, &fx.tahinli.id, "third", "") |
| 693 | .await | 707 | .await |
| 694 | .unwrap(); | 708 | .unwrap(); |
| 695 | assert_eq!( | 709 | assert_eq!( |
| 696 | (issue1.number, pull2.issue.number, issue3.number), | 710 | (issue1.number, pull2.issue.number, issue3.number), |
| 697 | - (1, 2, 3) | 711 | + (1, 2, 3), |
| 712 | + "numbering: shared sequence" | ||
| 713 | + ); | ||
| 714 | + assert_eq!( | ||
| 715 | + pull2.mergeable, | ||
| 716 | + Mergeable::Unknown, | ||
| 717 | + "numbering: new pull unknown" | ||
| 718 | + ); | ||
| 719 | + assert_eq!( | ||
| 720 | + pull2.head_repo_id.as_deref(), | ||
| 721 | + Some(repo.id.as_str()), | ||
| 722 | + "numbering: head repo" | ||
| 698 | ); | 723 | ); |
| 699 | - assert_eq!(pull2.mergeable, Mergeable::Unknown); | ||
| 700 | - assert_eq!(pull2.head_repo_id.as_deref(), Some(fx.repo.id.as_str())); | ||
| 701 | 724 | ||
| 702 | - // Kind filtering: issues never list pulls, and pulls never list issues. | 725 | + let (issues, _) = fx.store.list_issues(&repo.id, &all_states()).await.unwrap(); |
| 703 | - let (issues, _) = fx | ||
| 704 | - .store | ||
| 705 | - .list_issues(&fx.repo.id, &all_states()) | ||
| 706 | - .await | ||
| 707 | - .unwrap(); | ||
| 708 | let numbers: Vec<i64> = issues.iter().map(|i| i.issue.number).collect(); | 726 | let numbers: Vec<i64> = issues.iter().map(|i| i.issue.number).collect(); |
| 709 | - assert_eq!(numbers, vec![1, 3]); | 727 | + assert_eq!(numbers, vec![1, 3], "numbering: issues never list pulls"); |
| 710 | - let (pulls, total) = fx | 728 | + let (pulls, total) = fx.store.list_pulls(&repo.id, &all_states()).await.unwrap(); |
| 711 | - .store | 729 | + assert_eq!(total, 1, "numbering: pulls never list issues"); |
| 712 | - .list_pulls(&fx.repo.id, &all_states()) | 730 | + assert_eq!(pulls[0].issue.number, 2, "numbering: only the pull"); |
| 713 | - .await | ||
| 714 | - .unwrap(); | ||
| 715 | - assert_eq!(total, 1); | ||
| 716 | - assert_eq!(pulls[0].issue.number, 2); | ||
| 717 | assert!( | 731 | assert!( |
| 718 | fx.store | 732 | fx.store |
| 719 | - .issue_by_number(&fx.repo.id, 2) | 733 | + .issue_by_number(&repo.id, 2) |
| 720 | .await | 734 | .await |
| 721 | .unwrap() | 735 | .unwrap() |
| 722 | - .is_none() | 736 | + .is_none(), |
| 737 | + "numbering: pull number is not an issue" | ||
| 723 | ); | 738 | ); |
| 724 | assert!( | 739 | assert!( |
| 725 | fx.store | 740 | fx.store |
| 726 | - .pull_by_number(&fx.repo.id, 1) | 741 | + .pull_by_number(&repo.id, 1) |
| 727 | .await | 742 | .await |
| 728 | .unwrap() | 743 | .unwrap() |
| 729 | - .is_none() | 744 | + .is_none(), |
| 745 | + "numbering: issue number is not a pull" | ||
| 730 | ); | 746 | ); |
| 731 | assert_eq!( | 747 | assert_eq!( |
| 732 | - fx.store.pull_by_number(&fx.repo.id, 2).await.unwrap(), | 748 | + fx.store.pull_by_number(&repo.id, 2).await.unwrap(), |
| 733 | - Some(pull2) | 749 | + Some(pull2), |
| 750 | + "numbering: pull reads back" | ||
| 734 | ); | 751 | ); |
| 735 | - } | ||
| 736 | 752 | ||
| 737 | - #[tokio::test] | 753 | + // Counts: a merged pull counts as closed; issue counts are untouched. |
| 738 | - async fn counts_and_lists_filter_by_kind() { | 754 | + let repo = new_repo(&fx, "counts").await; |
| 739 | - let fx = fixture().await; | ||
| 740 | fx.store | 755 | fx.store |
| 741 | - .create_issue(&fx.repo.id, &fx.tahinli.id, "issue", "") | 756 | + .create_issue(&repo.id, &fx.tahinli.id, "issue", "") |
| 742 | .await | 757 | .await |
| 743 | .unwrap(); | 758 | .unwrap(); |
| 744 | - let pull = open_pull(&fx, &fx.repo.id, "one", "pull one").await; | 759 | + let pull = open_pull(&fx, &repo, &repo.id, "one", "pull one").await; |
| 745 | - open_pull(&fx, &fx.repo.id, "two", "pull two").await; | 760 | + open_pull(&fx, &repo, &repo.id, "two", "pull two").await; |
| 746 | - assert_eq!(fx.store.issue_counts(&fx.repo.id).await.unwrap(), (1, 0)); | 761 | + assert_eq!( |
| 747 | - assert_eq!(fx.store.pull_counts(&fx.repo.id).await.unwrap(), (2, 0)); | 762 | + fx.store.issue_counts(&repo.id).await.unwrap(), |
| 763 | + (1, 0), | ||
| 764 | + "counts: before merge issues" | ||
| 765 | + ); | ||
| 766 | + assert_eq!( | ||
| 767 | + fx.store.pull_counts(&repo.id).await.unwrap(), | ||
| 768 | + (2, 0), | ||
| 769 | + "counts: before merge pulls" | ||
| 770 | + ); | ||
| 748 | 771 | ||
| 749 | let id = pull.issue.id.clone(); | 772 | let id = pull.issue.id.clone(); |
| 750 | fx.store | 773 | fx.store |
| 751 | .mark_merged(&id, &fx.bob.id, &"c".repeat(40)) | 774 | .mark_merged(&id, &fx.bob.id, &"c".repeat(40)) |
| 752 | .await | 775 | .await |
| 753 | .unwrap(); | 776 | .unwrap(); |
| 754 | - // A merged pull counts as closed, and issue counts are untouched. | 777 | + assert_eq!( |
| 755 | - assert_eq!(fx.store.pull_counts(&fx.repo.id).await.unwrap(), (1, 1)); | 778 | + fx.store.pull_counts(&repo.id).await.unwrap(), |
| 756 | - assert_eq!(fx.store.issue_counts(&fx.repo.id).await.unwrap(), (1, 0)); | 779 | + (1, 1), |
| 757 | - | 780 | + "counts: merged pull closed" |
| 781 | + ); | ||
| 782 | + assert_eq!( | ||
| 783 | + fx.store.issue_counts(&repo.id).await.unwrap(), | ||
| 784 | + (1, 0), | ||
| 785 | + "counts: issues untouched" | ||
| 786 | + ); | ||
| 758 | let open_only = IssueFilter::default(); | 787 | let open_only = IssueFilter::default(); |
| 759 | - let (open, total) = fx.store.list_pulls(&fx.repo.id, &open_only).await.unwrap(); | 788 | + let (open, total) = fx.store.list_pulls(&repo.id, &open_only).await.unwrap(); |
| 760 | - assert_eq!(total, 1); | 789 | + assert_eq!(total, 1, "counts: open filter total"); |
| 761 | - assert_eq!(open[0].issue.title, "pull two"); | 790 | + assert_eq!(open[0].issue.title, "pull two", "counts: open filter row"); |
| 762 | - } | ||
| 763 | 791 | ||
| 764 | - #[tokio::test] | 792 | + // A head push resets mergeability and keeps the base. |
| 765 | - async fn update_pull_head_resets_mergeable() { | 793 | + let repo = new_repo(&fx, "head").await; |
| 766 | - let fx = fixture().await; | 794 | + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; |
| 767 | - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; | ||
| 768 | let id = pull.issue.id.clone(); | 795 | let id = pull.issue.id.clone(); |
| 769 | fx.store | 796 | fx.store |
| 770 | .set_mergeability( | 797 | .set_mergeability( |
| @@ -781,21 +808,18 @@ mod tests { | |||
| 781 | fx.store.update_pull_head(&id, &new_head).await.unwrap(); | 808 | fx.store.update_pull_head(&id, &new_head).await.unwrap(); |
| 782 | let after = fx | 809 | let after = fx |
| 783 | .store | 810 | .store |
| 784 | - .pull_by_number(&fx.repo.id, pull.issue.number) | 811 | + .pull_by_number(&repo.id, pull.issue.number) |
| 785 | .await | 812 | .await |
| 786 | .unwrap() | 813 | .unwrap() |
| 787 | .unwrap(); | 814 | .unwrap(); |
| 788 | - assert_eq!(after.head_oid, new_head); | 815 | + assert_eq!(after.head_oid, new_head, "head: new head"); |
| 789 | - assert_eq!(after.mergeable, Mergeable::Unknown); | 816 | + assert_eq!(after.mergeable, Mergeable::Unknown, "head: mergeable reset"); |
| 790 | - assert!(after.conflict_files.is_empty()); | 817 | + assert!(after.conflict_files.is_empty(), "head: conflicts cleared"); |
| 791 | - // The base was not touched by a head push. | 818 | + assert_eq!(after.base_oid, "b".repeat(40), "head: base untouched"); |
| 792 | - assert_eq!(after.base_oid, "b".repeat(40)); | 819 | + |
| 793 | - } | 820 | + // Mergeability round-trips conflict files; Merged is set only by mark_merged. |
| 794 | - | 821 | + let repo = new_repo(&fx, "mergeability").await; |
| 795 | - #[tokio::test] | 822 | + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; |
| 796 | - async fn set_mergeability_round_trips_conflict_files() { | ||
| 797 | - let fx = fixture().await; | ||
| 798 | - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; | ||
| 799 | let id = pull.issue.id.clone(); | 823 | let id = pull.issue.id.clone(); |
| 800 | let files = vec!["src/lib.rs".to_string(), "dir/with space.md".to_string()]; | 824 | let files = vec!["src/lib.rs".to_string(), "dir/with space.md".to_string()]; |
| 801 | let base = "f".repeat(40); | 825 | let base = "f".repeat(40); |
| @@ -811,14 +835,18 @@ mod tests { | |||
| 811 | .unwrap(); | 835 | .unwrap(); |
| 812 | let got = fx | 836 | let got = fx |
| 813 | .store | 837 | .store |
| 814 | - .pull_by_number(&fx.repo.id, pull.issue.number) | 838 | + .pull_by_number(&repo.id, pull.issue.number) |
| 815 | .await | 839 | .await |
| 816 | .unwrap() | 840 | .unwrap() |
| 817 | .unwrap(); | 841 | .unwrap(); |
| 818 | - assert_eq!(got.mergeable, Mergeable::Conflict); | 842 | + assert_eq!(got.mergeable, Mergeable::Conflict, "mergeability: conflict"); |
| 819 | - assert_eq!(got.conflict_files, files); | 843 | + assert_eq!(got.conflict_files, files, "mergeability: files round trip"); |
| 820 | - assert_eq!(got.base_oid, base); | 844 | + assert_eq!(got.base_oid, base, "mergeability: base stored"); |
| 821 | - assert_eq!(got.merge_base, Some("1".repeat(40))); | 845 | + assert_eq!( |
| 846 | + got.merge_base, | ||
| 847 | + Some("1".repeat(40)), | ||
| 848 | + "mergeability: merge base" | ||
| 849 | + ); | ||
| 822 | 850 | ||
| 823 | fx.store | 851 | fx.store |
| 824 | .set_mergeability(&id, &base, None, Mergeable::Clean, &[]) | 852 | .set_mergeability(&id, &base, None, Mergeable::Clean, &[]) |
| @@ -826,48 +854,53 @@ mod tests { | |||
| 826 | .unwrap(); | 854 | .unwrap(); |
| 827 | let got = fx | 855 | let got = fx |
| 828 | .store | 856 | .store |
| 829 | - .pull_by_number(&fx.repo.id, pull.issue.number) | 857 | + .pull_by_number(&repo.id, pull.issue.number) |
| 830 | .await | 858 | .await |
| 831 | .unwrap() | 859 | .unwrap() |
| 832 | .unwrap(); | 860 | .unwrap(); |
| 833 | - assert_eq!(got.mergeable, Mergeable::Clean); | 861 | + assert_eq!(got.mergeable, Mergeable::Clean, "mergeability: clean"); |
| 834 | - assert!(got.conflict_files.is_empty()); | 862 | + assert!(got.conflict_files.is_empty(), "mergeability: files cleared"); |
| 835 | - assert_eq!(got.merge_base, None); | 863 | + assert_eq!(got.merge_base, None, "mergeability: merge base cleared"); |
| 836 | 864 | ||
| 837 | - // Merged is set only through mark_merged, and a missing pull is NotFound. | 865 | + assert!( |
| 838 | - assert!(matches!( | 866 | + matches!( |
| 839 | - fx.store | 867 | + fx.store |
| 840 | - .set_mergeability(&id, &base, None, Mergeable::Merged, &[]) | 868 | + .set_mergeability(&id, &base, None, Mergeable::Merged, &[]) |
| 841 | - .await, | 869 | + .await, |
| 842 | - Err(Error::Invalid(_)) | 870 | + Err(Error::Invalid(_)) |
| 843 | - )); | 871 | + ), |
| 844 | - assert!(matches!( | 872 | + "mergeability: merged refused" |
| 845 | - fx.store | 873 | + ); |
| 846 | - .set_mergeability("no-such-issue", &base, None, Mergeable::Clean, &[]) | 874 | + assert!( |
| 847 | - .await, | 875 | + matches!( |
| 848 | - Err(Error::NotFound) | 876 | + fx.store |
| 849 | - )); | 877 | + .set_mergeability("no-such-issue", &base, None, Mergeable::Clean, &[]) |
| 850 | - } | 878 | + .await, |
| 879 | + Err(Error::NotFound) | ||
| 880 | + ), | ||
| 881 | + "mergeability: missing pull" | ||
| 882 | + ); | ||
| 851 | 883 | ||
| 852 | - #[tokio::test] | 884 | + // Merging closes the pull, records an event, and is final. |
| 853 | - async fn mark_merged_closes_records_event_and_is_final() { | 885 | + let repo = new_repo(&fx, "merge").await; |
| 854 | - let fx = fixture().await; | ||
| 855 | let issue = fx | 886 | let issue = fx |
| 856 | .store | 887 | .store |
| 857 | - .create_issue(&fx.repo.id, &fx.tahinli.id, "plain issue", "") | 888 | + .create_issue(&repo.id, &fx.tahinli.id, "plain issue", "") |
| 858 | .await | 889 | .await |
| 859 | .unwrap(); | 890 | .unwrap(); |
| 860 | - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; | 891 | + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; |
| 861 | let id = pull.issue.id.clone(); | 892 | let id = pull.issue.id.clone(); |
| 862 | let merge_commit = "c".repeat(40); | 893 | let merge_commit = "c".repeat(40); |
| 863 | 894 | ||
| 864 | - // An issue id is not a pull request. | 895 | + assert!( |
| 865 | - assert!(matches!( | 896 | + matches!( |
| 866 | - fx.store | 897 | + fx.store |
| 867 | - .mark_merged(&issue.id, &fx.bob.id, &merge_commit) | 898 | + .mark_merged(&issue.id, &fx.bob.id, &merge_commit) |
| 868 | - .await, | 899 | + .await, |
| 869 | - Err(Error::NotFound) | 900 | + Err(Error::NotFound) |
| 870 | - )); | 901 | + ), |
| 902 | + "merge: an issue id is not a pull" | ||
| 903 | + ); | ||
| 871 | 904 | ||
| 872 | fx.store | 905 | fx.store |
| 873 | .mark_merged(&id, &fx.bob.id, &merge_commit) | 906 | .mark_merged(&id, &fx.bob.id, &merge_commit) |
| @@ -875,99 +908,120 @@ mod tests { | |||
| 875 | .unwrap(); | 908 | .unwrap(); |
| 876 | let got = fx | 909 | let got = fx |
| 877 | .store | 910 | .store |
| 878 | - .pull_by_number(&fx.repo.id, pull.issue.number) | 911 | + .pull_by_number(&repo.id, pull.issue.number) |
| 879 | .await | 912 | .await |
| 880 | .unwrap() | 913 | .unwrap() |
| 881 | .unwrap(); | 914 | .unwrap(); |
| 882 | - assert_eq!(got.issue.state, IssueState::Closed); | 915 | + assert_eq!(got.issue.state, IssueState::Closed, "merge: closed"); |
| 883 | assert_eq!( | 916 | assert_eq!( |
| 884 | got.issue.state_reason, | 917 | got.issue.state_reason, |
| 885 | - Some(crate::issues::StateReason::Completed) | 918 | + Some(crate::issues::StateReason::Completed), |
| 919 | + "merge: completed" | ||
| 920 | + ); | ||
| 921 | + assert!(got.issue.closed_at.is_some(), "merge: closed time"); | ||
| 922 | + assert_eq!(got.mergeable, Mergeable::Merged, "merge: merged state"); | ||
| 923 | + assert!(got.merged_at.is_some(), "merge: merged time"); | ||
| 924 | + assert_eq!( | ||
| 925 | + got.merged_by.as_deref(), | ||
| 926 | + Some(fx.bob.id.as_str()), | ||
| 927 | + "merge: merged by" | ||
| 928 | + ); | ||
| 929 | + assert_eq!( | ||
| 930 | + got.merge_commit.as_deref(), | ||
| 931 | + Some(merge_commit.as_str()), | ||
| 932 | + "merge: merge commit" | ||
| 886 | ); | 933 | ); |
| 887 | - assert!(got.issue.closed_at.is_some()); | ||
| 888 | - assert_eq!(got.mergeable, Mergeable::Merged); | ||
| 889 | - assert!(got.merged_at.is_some()); | ||
| 890 | - assert_eq!(got.merged_by.as_deref(), Some(fx.bob.id.as_str())); | ||
| 891 | - assert_eq!(got.merge_commit.as_deref(), Some(merge_commit.as_str())); | ||
| 892 | 934 | ||
| 893 | let events = fx.store.list_events(&id).await.unwrap(); | 935 | let events = fx.store.list_events(&id).await.unwrap(); |
| 894 | let merged: Vec<_> = events.iter().filter(|e| e.kind == "merged").collect(); | 936 | let merged: Vec<_> = events.iter().filter(|e| e.kind == "merged").collect(); |
| 895 | - assert_eq!(merged.len(), 1); | 937 | + assert_eq!(merged.len(), 1, "merge: one event"); |
| 896 | - assert_eq!(merged[0].data["commit"], merge_commit.as_str()); | 938 | + assert_eq!( |
| 897 | - assert_eq!(merged[0].actor_id.as_deref(), Some(fx.bob.id.as_str())); | 939 | + merged[0].data["commit"], |
| 898 | - | 940 | + merge_commit.as_str(), |
| 899 | - // A second merge is refused. A late mergeability recompute is a no-op on a merged pull. | 941 | + "merge: event commit" |
| 900 | - assert!(matches!( | 942 | + ); |
| 901 | - fx.store.mark_merged(&id, &fx.bob.id, &merge_commit).await, | 943 | + assert_eq!( |
| 902 | - Err(Error::Conflict(_)) | 944 | + merged[0].actor_id.as_deref(), |
| 903 | - )); | 945 | + Some(fx.bob.id.as_str()), |
| 946 | + "merge: event actor" | ||
| 947 | + ); | ||
| 948 | + | ||
| 949 | + // A second merge is refused, and a late mergeability recompute is a no-op. | ||
| 950 | + assert!( | ||
| 951 | + matches!( | ||
| 952 | + fx.store.mark_merged(&id, &fx.bob.id, &merge_commit).await, | ||
| 953 | + Err(Error::Conflict(_)) | ||
| 954 | + ), | ||
| 955 | + "merge: second merge refused" | ||
| 956 | + ); | ||
| 904 | fx.store | 957 | fx.store |
| 905 | .set_mergeability(&id, &"b".repeat(40), None, Mergeable::Clean, &[]) | 958 | .set_mergeability(&id, &"b".repeat(40), None, Mergeable::Clean, &[]) |
| 906 | .await | 959 | .await |
| 907 | .unwrap(); | 960 | .unwrap(); |
| 908 | let still = fx | 961 | let still = fx |
| 909 | .store | 962 | .store |
| 910 | - .pull_by_number(&fx.repo.id, pull.issue.number) | 963 | + .pull_by_number(&repo.id, pull.issue.number) |
| 911 | .await | 964 | .await |
| 912 | .unwrap() | 965 | .unwrap() |
| 913 | .unwrap(); | 966 | .unwrap(); |
| 914 | - assert_eq!(still.mergeable, Mergeable::Merged); | 967 | + assert_eq!(still.mergeable, Mergeable::Merged, "merge: stays merged"); |
| 915 | - } | ||
| 916 | 968 | ||
| 917 | - #[tokio::test] | 969 | + // A second open pull for the same comparison is refused, and takes no number. |
| 918 | - async fn create_pull_refuses_a_second_open_pull_for_the_same_comparison() { | 970 | + let repo = new_repo(&fx, "refuse").await; |
| 919 | - let fx = fixture().await; | 971 | + let first = open_pull(&fx, &repo, &repo.id, "feature", "first").await; |
| 920 | - let first = open_pull(&fx, &fx.repo.id, "feature", "first").await; | ||
| 921 | let second = fx | 972 | let second = fx |
| 922 | .store | 973 | .store |
| 923 | .create_pull( | 974 | .create_pull( |
| 924 | - &fx.repo.id, | 975 | + &repo.id, |
| 925 | &fx.bob.id, | 976 | &fx.bob.id, |
| 926 | "second", | 977 | "second", |
| 927 | "", | 978 | "", |
| 928 | "main", | 979 | "main", |
| 929 | - &fx.repo.id, | 980 | + &repo.id, |
| 930 | "feature", | 981 | "feature", |
| 931 | &"c".repeat(40), | 982 | &"c".repeat(40), |
| 932 | &"b".repeat(40), | 983 | &"b".repeat(40), |
| 933 | ) | 984 | ) |
| 934 | .await; | 985 | .await; |
| 935 | match second { | 986 | match second { |
| 936 | - Err(Error::Conflict(msg)) => assert!(msg.contains(&format!("#{}", first.issue.number))), | 987 | + Err(Error::Conflict(msg)) => assert!( |
| 937 | - other => panic!("expected conflict, got {other:?}"), | 988 | + msg.contains(&format!("#{}", first.issue.number)), |
| 989 | + "refuse: conflict names the open pull" | ||
| 990 | + ), | ||
| 991 | + other => panic!("refuse: expected conflict, got {other:?}"), | ||
| 938 | } | 992 | } |
| 939 | - // The refused attempt took no number and wrote no row. | 993 | + let next = open_pull(&fx, &repo, &repo.id, "other", "third").await; |
| 940 | - let next = open_pull(&fx, &fx.repo.id, "other", "third").await; | 994 | + assert_eq!( |
| 941 | - assert_eq!(next.issue.number, first.issue.number + 1); | 995 | + next.issue.number, |
| 942 | - } | 996 | + first.issue.number + 1, |
| 997 | + "refuse: no number taken" | ||
| 998 | + ); | ||
| 943 | 999 | ||
| 944 | - #[tokio::test] | 1000 | + // find_open_pull matches base and head, and a merged pull stops matching. |
| 945 | - async fn find_open_pull_matches_base_and_head() { | 1001 | + let repo = new_repo(&fx, "find").await; |
| 946 | - let fx = fixture().await; | 1002 | + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; |
| 947 | - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; | ||
| 948 | let found = fx | 1003 | let found = fx |
| 949 | .store | 1004 | .store |
| 950 | - .find_open_pull(&fx.repo.id, "main", &fx.repo.id, "feature") | 1005 | + .find_open_pull(&repo.id, "main", &repo.id, "feature") |
| 951 | .await | 1006 | .await |
| 952 | .unwrap(); | 1007 | .unwrap(); |
| 953 | - assert_eq!(found, Some(pull.issue.number)); | 1008 | + assert_eq!(found, Some(pull.issue.number), "find: matches"); |
| 954 | - | ||
| 955 | assert_eq!( | 1009 | assert_eq!( |
| 956 | fx.store | 1010 | fx.store |
| 957 | - .find_open_pull(&fx.repo.id, "main", &fx.repo.id, "other") | 1011 | + .find_open_pull(&repo.id, "main", &repo.id, "other") |
| 958 | .await | 1012 | .await |
| 959 | .unwrap(), | 1013 | .unwrap(), |
| 960 | - None | 1014 | + None, |
| 1015 | + "find: other head" | ||
| 961 | ); | 1016 | ); |
| 962 | assert_eq!( | 1017 | assert_eq!( |
| 963 | fx.store | 1018 | fx.store |
| 964 | - .find_open_pull(&fx.repo.id, "dev", &fx.repo.id, "feature") | 1019 | + .find_open_pull(&repo.id, "dev", &repo.id, "feature") |
| 965 | .await | 1020 | .await |
| 966 | .unwrap(), | 1021 | .unwrap(), |
| 967 | - None | 1022 | + None, |
| 1023 | + "find: other base" | ||
| 968 | ); | 1024 | ); |
| 969 | - | ||
| 970 | - // Once it is merged it no longer blocks a new pull for the same branches. | ||
| 971 | let id = pull.issue.id.clone(); | 1025 | let id = pull.issue.id.clone(); |
| 972 | fx.store | 1026 | fx.store |
| 973 | .mark_merged(&id, &fx.tahinli.id, &"c".repeat(40)) | 1027 | .mark_merged(&id, &fx.tahinli.id, &"c".repeat(40)) |
| @@ -975,145 +1029,375 @@ mod tests { | |||
| 975 | .unwrap(); | 1029 | .unwrap(); |
| 976 | assert_eq!( | 1030 | assert_eq!( |
| 977 | fx.store | 1031 | fx.store |
| 978 | - .find_open_pull(&fx.repo.id, "main", &fx.repo.id, "feature") | 1032 | + .find_open_pull(&repo.id, "main", &repo.id, "feature") |
| 979 | .await | 1033 | .await |
| 980 | .unwrap(), | 1034 | .unwrap(), |
| 981 | - None | 1035 | + None, |
| 1036 | + "find: merged pull no longer blocks" | ||
| 982 | ); | 1037 | ); |
| 983 | - } | ||
| 984 | 1038 | ||
| 985 | - #[tokio::test] | 1039 | + // Draft flag: round trip, events, list filter and draft numbers. |
| 986 | - async fn open_pulls_for_head_and_base_skip_closed() { | 1040 | + let repo = new_repo(&fx, "draft").await; |
| 987 | - let fx = fixture().await; | 1041 | + let pr = open_pull(&fx, &repo, &repo.id, "feature", "wip").await; |
| 988 | - let fork = fx | 1042 | + let _ready = open_pull(&fx, &repo, &repo.id, "other", "ready").await; |
| 1043 | + assert!(!pr.draft, "draft: starts ready"); | ||
| 1044 | + | ||
| 1045 | + assert!( | ||
| 1046 | + fx.store | ||
| 1047 | + .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) | ||
| 1048 | + .await | ||
| 1049 | + .unwrap(), | ||
| 1050 | + "draft: set changes the flag" | ||
| 1051 | + ); | ||
| 1052 | + // Setting the same value again changes nothing and records no event. | ||
| 1053 | + assert!( | ||
| 1054 | + !fx.store | ||
| 1055 | + .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) | ||
| 1056 | + .await | ||
| 1057 | + .unwrap(), | ||
| 1058 | + "draft: same value changes nothing" | ||
| 1059 | + ); | ||
| 1060 | + let got = fx | ||
| 989 | .store | 1061 | .store |
| 990 | - .create_fork(&fx.repo, &fx.bob.id, "proj-fork") | 1062 | + .pull_by_number(&repo.id, pr.issue.number) |
| 991 | .await | 1063 | .await |
| 1064 | + .unwrap() | ||
| 992 | .unwrap(); | 1065 | .unwrap(); |
| 993 | - let from_fork = open_pull(&fx, &fork.id, "topic", "from fork").await; | 1066 | + assert!(got.draft, "draft: read back"); |
| 994 | - let same_repo = open_pull(&fx, &fx.repo.id, "topic", "same repo").await; | ||
| 995 | 1067 | ||
| 996 | - let heads = fx | 1068 | + let only = |draft: Option<bool>| IssueFilter { |
| 1069 | + state: None, | ||
| 1070 | + draft, | ||
| 1071 | + ..IssueFilter::default() | ||
| 1072 | + }; | ||
| 1073 | + let (items, total) = fx | ||
| 997 | .store | 1074 | .store |
| 998 | - .open_pulls_for_head(&fork.id, "topic") | 1075 | + .list_pulls(&repo.id, &only(Some(true))) |
| 999 | .await | 1076 | .await |
| 1000 | .unwrap(); | 1077 | .unwrap(); |
| 1001 | assert_eq!( | 1078 | assert_eq!( |
| 1002 | - heads, | 1079 | + (total, items[0].issue.number), |
| 1003 | - vec![( | 1080 | + (1, pr.issue.number), |
| 1004 | - fx.repo.id.clone(), | 1081 | + "draft: filter drafts" |
| 1005 | - from_fork.issue.number, | 1082 | + ); |
| 1006 | - from_fork.issue.id.clone() | 1083 | + let (_, total) = fx |
| 1007 | - )] | 1084 | + .store |
| 1085 | + .list_pulls(&repo.id, &only(Some(false))) | ||
| 1086 | + .await | ||
| 1087 | + .unwrap(); | ||
| 1088 | + assert_eq!(total, 1, "draft: filter ready"); | ||
| 1089 | + let (_, total) = fx.store.list_pulls(&repo.id, &only(None)).await.unwrap(); | ||
| 1090 | + assert_eq!(total, 2, "draft: no filter"); | ||
| 1091 | + let numbers = [pr.issue.number, pr.issue.number + 1]; | ||
| 1092 | + let drafts = fx.store.draft_numbers(&repo.id, &numbers).await.unwrap(); | ||
| 1093 | + assert_eq!(drafts.len(), 1, "draft: numbers filtered"); | ||
| 1094 | + assert!( | ||
| 1095 | + drafts.contains(&pr.issue.number), | ||
| 1096 | + "draft: draft number listed" | ||
| 1008 | ); | 1097 | ); |
| 1098 | + | ||
| 1009 | assert!( | 1099 | assert!( |
| 1010 | fx.store | 1100 | fx.store |
| 1011 | - .open_pulls_for_head(&fork.id, "missing") | 1101 | + .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), false) |
| 1102 | + .await | ||
| 1103 | + .unwrap(), | ||
| 1104 | + "draft: cleared" | ||
| 1105 | + ); | ||
| 1106 | + let kinds: Vec<String> = fx | ||
| 1107 | + .store | ||
| 1108 | + .list_events(&pr.issue.id) | ||
| 1109 | + .await | ||
| 1110 | + .unwrap() | ||
| 1111 | + .into_iter() | ||
| 1112 | + .map(|e| e.kind) | ||
| 1113 | + .collect(); | ||
| 1114 | + assert_eq!( | ||
| 1115 | + kinds, | ||
| 1116 | + vec!["converted_to_draft", "ready_for_review"], | ||
| 1117 | + "draft: events" | ||
| 1118 | + ); | ||
| 1119 | + | ||
| 1120 | + // Auto merge is cleared by a new head, a draft, or a merge. | ||
| 1121 | + let repo = new_repo(&fx, "auto").await; | ||
| 1122 | + let pr = open_pull(&fx, &repo, &repo.id, "feature", "auto").await; | ||
| 1123 | + let head = "a".repeat(40); | ||
| 1124 | + let load = || async { | ||
| 1125 | + fx.store | ||
| 1126 | + .pull_by_number(&repo.id, pr.issue.number) | ||
| 1012 | .await | 1127 | .await |
| 1013 | .unwrap() | 1128 | .unwrap() |
| 1014 | - .is_empty() | 1129 | + .unwrap() |
| 1130 | + }; | ||
| 1131 | + assert!(load().await.auto_merge.is_none(), "auto: starts unset"); | ||
| 1132 | + assert!( | ||
| 1133 | + !fx.store | ||
| 1134 | + .disable_auto_merge(&pr.issue.id, None, "manual") | ||
| 1135 | + .await | ||
| 1136 | + .unwrap(), | ||
| 1137 | + "auto: nothing to disable" | ||
| 1015 | ); | 1138 | ); |
| 1016 | 1139 | ||
| 1017 | - let bases = fx | 1140 | + fx.store |
| 1018 | - .store | 1141 | + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Rebase) |
| 1019 | - .open_pulls_for_base(&fx.repo.id, "main") | ||
| 1020 | .await | 1142 | .await |
| 1021 | .unwrap(); | 1143 | .unwrap(); |
| 1144 | + let auto = load().await.auto_merge.expect("enabled"); | ||
| 1022 | assert_eq!( | 1145 | assert_eq!( |
| 1023 | - bases, | 1146 | + (auto.style, auto.user_id.as_str()), |
| 1024 | - vec![ | 1147 | + (MergeStyle::Rebase, fx.bob.id.as_str()), |
| 1025 | - (from_fork.issue.number, from_fork.issue.id.clone()), | 1148 | + "auto: style and user stored" |
| 1026 | - (same_repo.issue.number, same_repo.issue.id.clone()), | 1149 | + ); |
| 1027 | - ] | 1150 | + let found = fx |
| 1151 | + .store | ||
| 1152 | + .auto_merge_pulls_for_head(&repo.id, &head) | ||
| 1153 | + .await | ||
| 1154 | + .unwrap(); | ||
| 1155 | + assert_eq!(found.len(), 1, "auto: found for its head"); | ||
| 1156 | + assert!( | ||
| 1157 | + fx.store | ||
| 1158 | + .auto_merge_pulls_for_head(&repo.id, &"d".repeat(40)) | ||
| 1159 | + .await | ||
| 1160 | + .unwrap() | ||
| 1161 | + .is_empty(), | ||
| 1162 | + "auto: not found for another head" | ||
| 1028 | ); | 1163 | ); |
| 1029 | 1164 | ||
| 1030 | fx.store | 1165 | fx.store |
| 1031 | - .mark_merged(&same_repo.issue.id, &fx.tahinli.id, &"c".repeat(40)) | 1166 | + .update_pull_head(&pr.issue.id, &"b".repeat(40)) |
| 1032 | .await | 1167 | .await |
| 1033 | .unwrap(); | 1168 | .unwrap(); |
| 1034 | - let bases = fx | 1169 | + assert!( |
| 1035 | - .store | 1170 | + load().await.auto_merge.is_none(), |
| 1036 | - .open_pulls_for_base(&fx.repo.id, "main") | 1171 | + "auto: new head clears it" |
| 1172 | + ); | ||
| 1173 | + | ||
| 1174 | + fx.store | ||
| 1175 | + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) | ||
| 1176 | + .await | ||
| 1177 | + .unwrap(); | ||
| 1178 | + fx.store | ||
| 1179 | + .set_pull_draft(&pr.issue.id, None, true) | ||
| 1180 | + .await | ||
| 1181 | + .unwrap(); | ||
| 1182 | + assert!(load().await.auto_merge.is_none(), "auto: draft clears it"); | ||
| 1183 | + | ||
| 1184 | + fx.store | ||
| 1185 | + .set_pull_draft(&pr.issue.id, None, false) | ||
| 1186 | + .await | ||
| 1187 | + .unwrap(); | ||
| 1188 | + fx.store | ||
| 1189 | + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) | ||
| 1190 | + .await | ||
| 1191 | + .unwrap(); | ||
| 1192 | + assert!( | ||
| 1193 | + fx.store | ||
| 1194 | + .disable_auto_merge(&pr.issue.id, Some(&fx.bob.id), "refused") | ||
| 1195 | + .await | ||
| 1196 | + .unwrap(), | ||
| 1197 | + "auto: disabled" | ||
| 1198 | + ); | ||
| 1199 | + let events = fx.store.list_events(&pr.issue.id).await.unwrap(); | ||
| 1200 | + let last = events.last().unwrap(); | ||
| 1201 | + assert_eq!(last.kind, "auto_merge_disabled", "auto: disable event"); | ||
| 1202 | + assert_eq!(last.data["reason"], "refused", "auto: disable reason"); | ||
| 1203 | + | ||
| 1204 | + fx.store | ||
| 1205 | + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) | ||
| 1037 | .await | 1206 | .await |
| 1038 | .unwrap(); | 1207 | .unwrap(); |
| 1039 | - assert_eq!(bases.len(), 1); | 1208 | + fx.store |
| 1040 | - assert_eq!(bases[0].0, from_fork.issue.number); | 1209 | + .mark_merged(&pr.issue.id, &fx.bob.id, &"c".repeat(40)) |
| 1210 | + .await | ||
| 1211 | + .unwrap(); | ||
| 1212 | + assert!(load().await.auto_merge.is_none(), "auto: merge clears it"); | ||
| 1213 | + assert!( | ||
| 1214 | + fx.store | ||
| 1215 | + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) | ||
| 1216 | + .await | ||
| 1217 | + .is_err(), | ||
| 1218 | + "auto: merged pull refuses auto merge" | ||
| 1219 | + ); | ||
| 1220 | + | ||
| 1221 | + // Pull event kinds are accepted on pulls. | ||
| 1222 | + let repo = new_repo(&fx, "events").await; | ||
| 1223 | + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; | ||
| 1224 | + for kind in ["head_updated", "base_changed", "head_deleted"] { | ||
| 1225 | + fx.store | ||
| 1226 | + .add_event(&pull.issue.id, None, kind, serde_json::Value::Null) | ||
| 1227 | + .await | ||
| 1228 | + .unwrap(); | ||
| 1229 | + } | ||
| 1230 | + let kinds: Vec<String> = fx | ||
| 1231 | + .store | ||
| 1232 | + .list_events(&pull.issue.id) | ||
| 1233 | + .await | ||
| 1234 | + .unwrap() | ||
| 1235 | + .into_iter() | ||
| 1236 | + .map(|e| e.kind) | ||
| 1237 | + .collect(); | ||
| 1238 | + assert_eq!( | ||
| 1239 | + kinds, | ||
| 1240 | + vec!["head_updated", "base_changed", "head_deleted"], | ||
| 1241 | + "events: pull kinds accepted" | ||
| 1242 | + ); | ||
| 1041 | } | 1243 | } |
| 1042 | 1244 | ||
| 1245 | + // Forks, fork pulls and repo deletion. Forks are created under names that | ||
| 1246 | + // are unique for Bob, and the deletions run last. | ||
| 1043 | #[tokio::test] | 1247 | #[tokio::test] |
| 1044 | - async fn create_fork_copies_settings_and_lists_forks() { | 1248 | + async fn fork_scenarios_share_one_store() { |
| 1045 | let fx = fixture().await; | 1249 | let fx = fixture().await; |
| 1250 | + | ||
| 1251 | + // A fork copies the source settings and lists under its source. | ||
| 1046 | let fork = fx | 1252 | let fork = fx |
| 1047 | .store | 1253 | .store |
| 1048 | .create_fork(&fx.repo, &fx.bob.id, "proj-1") | 1254 | .create_fork(&fx.repo, &fx.bob.id, "proj-1") |
| 1049 | .await | 1255 | .await |
| 1050 | .unwrap(); | 1256 | .unwrap(); |
| 1051 | - assert_eq!(fork.owner_id, fx.bob.id); | 1257 | + assert_eq!(fork.owner_id, fx.bob.id, "fork: owner"); |
| 1052 | - assert_eq!(fork.owner_name, "Bob"); | 1258 | + assert_eq!(fork.owner_name, "Bob", "fork: owner name"); |
| 1053 | - assert_eq!(fork.name, "proj-1"); | 1259 | + assert_eq!(fork.name, "proj-1", "fork: name"); |
| 1054 | - assert_eq!(fork.description, "desc"); | 1260 | + assert_eq!(fork.description, "desc", "fork: description copied"); |
| 1055 | - assert_eq!(fork.default_branch, "main"); | 1261 | + assert_eq!(fork.default_branch, "main", "fork: branch copied"); |
| 1056 | - assert_eq!(fork.visibility, Visibility::Public); | 1262 | + assert_eq!( |
| 1057 | - assert_eq!(fork.fork_of.as_deref(), Some(fx.repo.id.as_str())); | 1263 | + fork.visibility, |
| 1058 | - assert_eq!(fx.repo.fork_of, None); | 1264 | + Visibility::Public, |
| 1265 | + "fork: visibility copied" | ||
| 1266 | + ); | ||
| 1267 | + assert_eq!( | ||
| 1268 | + fork.fork_of.as_deref(), | ||
| 1269 | + Some(fx.repo.id.as_str()), | ||
| 1270 | + "fork: points at source" | ||
| 1271 | + ); | ||
| 1272 | + assert_eq!(fx.repo.fork_of, None, "fork: source is not a fork"); | ||
| 1059 | 1273 | ||
| 1060 | // fork_of survives a read back through the shared repo queries. | 1274 | // fork_of survives a read back through the shared repo queries. |
| 1061 | let read = fx.store.repo_by_id(&fork.id).await.unwrap().unwrap(); | 1275 | let read = fx.store.repo_by_id(&fork.id).await.unwrap().unwrap(); |
| 1062 | - assert_eq!(read.fork_of, Some(fx.repo.id.clone())); | 1276 | + assert_eq!(read.fork_of, Some(fx.repo.id.clone()), "fork: read by id"); |
| 1063 | let read = fx | 1277 | let read = fx |
| 1064 | .store | 1278 | .store |
| 1065 | .repo_by_owner_and_name("Bob", "proj-1") | 1279 | .repo_by_owner_and_name("Bob", "proj-1") |
| 1066 | .await | 1280 | .await |
| 1067 | .unwrap() | 1281 | .unwrap() |
| 1068 | .unwrap(); | 1282 | .unwrap(); |
| 1069 | - assert_eq!(read.fork_of.as_deref(), Some(fx.repo.id.as_str())); | 1283 | + assert_eq!( |
| 1284 | + read.fork_of.as_deref(), | ||
| 1285 | + Some(fx.repo.id.as_str()), | ||
| 1286 | + "fork: read by name" | ||
| 1287 | + ); | ||
| 1070 | 1288 | ||
| 1071 | assert_eq!( | 1289 | assert_eq!( |
| 1072 | fx.store.list_forks(&fx.repo.id).await.unwrap(), | 1290 | fx.store.list_forks(&fx.repo.id).await.unwrap(), |
| 1073 | - vec![fork.clone()] | 1291 | + vec![fork.clone()], |
| 1292 | + "fork: listed under source" | ||
| 1293 | + ); | ||
| 1294 | + assert!( | ||
| 1295 | + fx.store.list_forks(&fork.id).await.unwrap().is_empty(), | ||
| 1296 | + "fork: fork has no forks" | ||
| 1074 | ); | 1297 | ); |
| 1075 | - assert!(fx.store.list_forks(&fork.id).await.unwrap().is_empty()); | ||
| 1076 | 1298 | ||
| 1077 | - // A name clash with an existing repo of the same owner is a conflict. | 1299 | + assert!( |
| 1078 | - assert!(matches!( | 1300 | + matches!( |
| 1079 | - fx.store.create_fork(&fx.repo, &fx.bob.id, "proj-1").await, | 1301 | + fx.store.create_fork(&fx.repo, &fx.bob.id, "proj-1").await, |
| 1080 | - Err(Error::Conflict(_)) | 1302 | + Err(Error::Conflict(_)) |
| 1081 | - )); | 1303 | + ), |
| 1082 | - } | 1304 | + "fork: name clash is a conflict" |
| 1305 | + ); | ||
| 1083 | 1306 | ||
| 1084 | - #[tokio::test] | 1307 | + // A fork of a private repo is private and keeps its branch name. |
| 1085 | - async fn fork_of_a_private_repo_is_private() { | ||
| 1086 | - let fx = fixture().await; | ||
| 1087 | let private = fx | 1308 | let private = fx |
| 1088 | .store | 1309 | .store |
| 1089 | .create_repo(&fx.tahinli.id, "secret", "", Visibility::Private, "trunk") | 1310 | .create_repo(&fx.tahinli.id, "secret", "", Visibility::Private, "trunk") |
| 1090 | .await | 1311 | .await |
| 1091 | .unwrap(); | 1312 | .unwrap(); |
| 1092 | - let fork = fx | 1313 | + let secret_fork = fx |
| 1093 | .store | 1314 | .store |
| 1094 | .create_fork(&private, &fx.bob.id, "secret-1") | 1315 | .create_fork(&private, &fx.bob.id, "secret-1") |
| 1095 | .await | 1316 | .await |
| 1096 | .unwrap(); | 1317 | .unwrap(); |
| 1097 | - assert_eq!(fork.visibility, Visibility::Private); | 1318 | + assert_eq!( |
| 1098 | - assert_eq!(fork.default_branch, "trunk"); | 1319 | + secret_fork.visibility, |
| 1099 | - } | 1320 | + Visibility::Private, |
| 1321 | + "private fork: private" | ||
| 1322 | + ); | ||
| 1323 | + assert_eq!(secret_fork.default_branch, "trunk", "private fork: branch"); | ||
| 1100 | 1324 | ||
| 1101 | - #[tokio::test] | 1325 | + // Open pulls for a head or base skip closed ones; forks feed the head side. |
| 1102 | - async fn delete_repo_detaches_forks_and_heads() { | 1326 | + let source = new_repo(&fx, "fork-src").await; |
| 1103 | - let fx = fixture().await; | ||
| 1104 | - let source = fx.repo.clone(); | ||
| 1105 | let fork = fx | 1327 | let fork = fx |
| 1106 | .store | 1328 | .store |
| 1107 | - .create_fork(&source, &fx.bob.id, "proj-1") | 1329 | + .create_fork(&source, &fx.bob.id, "fork-src-fork") |
| 1330 | + .await | ||
| 1331 | + .unwrap(); | ||
| 1332 | + let from_fork = open_pull(&fx, &source, &fork.id, "topic", "from fork").await; | ||
| 1333 | + let same_repo = open_pull(&fx, &source, &source.id, "topic", "same repo").await; | ||
| 1334 | + | ||
| 1335 | + let heads = fx | ||
| 1336 | + .store | ||
| 1337 | + .open_pulls_for_head(&fork.id, "topic") | ||
| 1338 | + .await | ||
| 1339 | + .unwrap(); | ||
| 1340 | + assert_eq!( | ||
| 1341 | + heads, | ||
| 1342 | + vec![( | ||
| 1343 | + source.id.clone(), | ||
| 1344 | + from_fork.issue.number, | ||
| 1345 | + from_fork.issue.id.clone() | ||
| 1346 | + )], | ||
| 1347 | + "heads: fork pull only" | ||
| 1348 | + ); | ||
| 1349 | + assert!( | ||
| 1350 | + fx.store | ||
| 1351 | + .open_pulls_for_head(&fork.id, "missing") | ||
| 1352 | + .await | ||
| 1353 | + .unwrap() | ||
| 1354 | + .is_empty(), | ||
| 1355 | + "heads: unknown branch" | ||
| 1356 | + ); | ||
| 1357 | + | ||
| 1358 | + let bases = fx | ||
| 1359 | + .store | ||
| 1360 | + .open_pulls_for_base(&source.id, "main") | ||
| 1361 | + .await | ||
| 1362 | + .unwrap(); | ||
| 1363 | + assert_eq!( | ||
| 1364 | + bases, | ||
| 1365 | + vec![ | ||
| 1366 | + (from_fork.issue.number, from_fork.issue.id.clone()), | ||
| 1367 | + (same_repo.issue.number, same_repo.issue.id.clone()), | ||
| 1368 | + ], | ||
| 1369 | + "bases: both open pulls" | ||
| 1370 | + ); | ||
| 1371 | + | ||
| 1372 | + fx.store | ||
| 1373 | + .mark_merged(&same_repo.issue.id, &fx.tahinli.id, &"c".repeat(40)) | ||
| 1374 | + .await | ||
| 1375 | + .unwrap(); | ||
| 1376 | + let bases = fx | ||
| 1377 | + .store | ||
| 1378 | + .open_pulls_for_base(&source.id, "main") | ||
| 1379 | + .await | ||
| 1380 | + .unwrap(); | ||
| 1381 | + assert_eq!(bases.len(), 1, "bases: merged pull skipped"); | ||
| 1382 | + assert_eq!( | ||
| 1383 | + bases[0].0, from_fork.issue.number, | ||
| 1384 | + "bases: fork pull remains" | ||
| 1385 | + ); | ||
| 1386 | + | ||
| 1387 | + // Deleting a fork detaches its incoming pulls; deleting a source removes | ||
| 1388 | + // its pulls and detaches its surviving forks. | ||
| 1389 | + let source = new_repo(&fx, "detach-src").await; | ||
| 1390 | + let fork = fx | ||
| 1391 | + .store | ||
| 1392 | + .create_fork(&source, &fx.bob.id, "detach-1") | ||
| 1108 | .await | 1393 | .await |
| 1109 | .unwrap(); | 1394 | .unwrap(); |
| 1110 | let other_fork = fx | 1395 | let other_fork = fx |
| 1111 | .store | 1396 | .store |
| 1112 | - .create_fork(&source, &fx.bob.id, "proj-2") | 1397 | + .create_fork(&source, &fx.bob.id, "detach-2") |
| 1113 | .await | 1398 | .await |
| 1114 | .unwrap(); | 1399 | .unwrap(); |
| 1115 | - // A pull from the fork into the source, and one in the other direction. | 1400 | + let incoming = open_pull(&fx, &source, &fork.id, "topic", "incoming").await; |
| 1116 | - let incoming = open_pull(&fx, &fork.id, "topic", "incoming").await; | ||
| 1117 | let outgoing = fx | 1401 | let outgoing = fx |
| 1118 | .store | 1402 | .store |
| 1119 | .create_pull( | 1403 | .create_pull( |
| @@ -1130,7 +1414,6 @@ mod tests { | |||
| 1130 | .await | 1414 | .await |
| 1131 | .unwrap(); | 1415 | .unwrap(); |
| 1132 | 1416 | ||
| 1133 | - // Deleting the fork: the pull into the source keeps its row with no head repo. | ||
| 1134 | fx.store.delete_repo(&fork.id).await.unwrap(); | 1417 | fx.store.delete_repo(&fork.id).await.unwrap(); |
| 1135 | let kept = fx | 1418 | let kept = fx |
| 1136 | .store | 1419 | .store |
| @@ -1138,41 +1421,51 @@ mod tests { | |||
| 1138 | .await | 1421 | .await |
| 1139 | .unwrap() | 1422 | .unwrap() |
| 1140 | .unwrap(); | 1423 | .unwrap(); |
| 1141 | - assert_eq!(kept.head_repo_id, None); | 1424 | + assert_eq!(kept.head_repo_id, None, "detach fork: head repo cleared"); |
| 1142 | - assert_eq!(kept.head_branch, "topic"); | 1425 | + assert_eq!(kept.head_branch, "topic", "detach fork: branch kept"); |
| 1143 | - assert_eq!(kept.head_oid, "a".repeat(40)); | 1426 | + assert_eq!(kept.head_oid, "a".repeat(40), "detach fork: head kept"); |
| 1144 | - assert!(fx.store.repo_by_id(&fork.id).await.unwrap().is_none()); | 1427 | + assert!( |
| 1428 | + fx.store.repo_by_id(&fork.id).await.unwrap().is_none(), | ||
| 1429 | + "detach fork: fork gone" | ||
| 1430 | + ); | ||
| 1145 | assert_eq!( | 1431 | assert_eq!( |
| 1146 | fx.store.list_forks(&source.id).await.unwrap(), | 1432 | fx.store.list_forks(&source.id).await.unwrap(), |
| 1147 | - vec![other_fork.clone()] | 1433 | + vec![other_fork.clone()], |
| 1434 | + "detach fork: other fork listed" | ||
| 1148 | ); | 1435 | ); |
| 1149 | 1436 | ||
| 1150 | - // Deleting the source: its pulls go, and the surviving fork is detached. | ||
| 1151 | fx.store.delete_repo(&source.id).await.unwrap(); | 1437 | fx.store.delete_repo(&source.id).await.unwrap(); |
| 1152 | - assert!(fx.store.repo_by_id(&source.id).await.unwrap().is_none()); | 1438 | + assert!( |
| 1439 | + fx.store.repo_by_id(&source.id).await.unwrap().is_none(), | ||
| 1440 | + "detach source: source gone" | ||
| 1441 | + ); | ||
| 1153 | let detached = fx.store.repo_by_id(&other_fork.id).await.unwrap().unwrap(); | 1442 | let detached = fx.store.repo_by_id(&other_fork.id).await.unwrap().unwrap(); |
| 1154 | - assert_eq!(detached.fork_of, None); | 1443 | + assert_eq!( |
| 1444 | + detached.fork_of, None, | ||
| 1445 | + "detach source: surviving fork detached" | ||
| 1446 | + ); | ||
| 1155 | assert!( | 1447 | assert!( |
| 1156 | fx.store | 1448 | fx.store |
| 1157 | .pull_by_number(&source.id, incoming.issue.number) | 1449 | .pull_by_number(&source.id, incoming.issue.number) |
| 1158 | .await | 1450 | .await |
| 1159 | .unwrap() | 1451 | .unwrap() |
| 1160 | - .is_none() | 1452 | + .is_none(), |
| 1453 | + "detach source: its pulls gone" | ||
| 1161 | ); | 1454 | ); |
| 1162 | - // The pull from the deleted fork went with it. | ||
| 1163 | assert!( | 1455 | assert!( |
| 1164 | fx.store | 1456 | fx.store |
| 1165 | .pull_by_number(&fork.id, outgoing.issue.number) | 1457 | .pull_by_number(&fork.id, outgoing.issue.number) |
| 1166 | .await | 1458 | .await |
| 1167 | .unwrap() | 1459 | .unwrap() |
| 1168 | - .is_none() | 1460 | + .is_none(), |
| 1461 | + "detach source: pull from deleted fork gone" | ||
| 1169 | ); | 1462 | ); |
| 1170 | } | 1463 | } |
| 1171 | 1464 | ||
| 1172 | #[tokio::test] | 1465 | #[tokio::test] |
| 1173 | async fn delete_repo_removes_its_pull_rows() { | 1466 | async fn delete_repo_removes_its_pull_rows() { |
| 1174 | let fx = fixture().await; | 1467 | let fx = fixture().await; |
| 1175 | - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; | 1468 | + let pull = open_pull(&fx, &fx.repo, &fx.repo.id, "feature", "t").await; |
| 1176 | fx.store | 1469 | fx.store |
| 1177 | .add_event( | 1470 | .add_event( |
| 1178 | &pull.issue.id, | 1471 | &pull.issue.id, |
| @@ -1190,194 +1483,6 @@ mod tests { | |||
| 1190 | .await | 1483 | .await |
| 1191 | .unwrap() | 1484 | .unwrap() |
| 1192 | .unwrap(); | 1485 | .unwrap(); |
| 1193 | - assert_eq!(left, 0); | 1486 | + assert_eq!(left, 0, "delete repo: no pull rows left"); |
| 1194 | - } | ||
| 1195 | - | ||
| 1196 | - #[tokio::test] | ||
| 1197 | - async fn add_event_accepts_pull_kinds_on_pulls() { | ||
| 1198 | - let fx = fixture().await; | ||
| 1199 | - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; | ||
| 1200 | - for kind in ["head_updated", "base_changed", "head_deleted"] { | ||
| 1201 | - fx.store | ||
| 1202 | - .add_event(&pull.issue.id, None, kind, serde_json::Value::Null) | ||
| 1203 | - .await | ||
| 1204 | - .unwrap(); | ||
| 1205 | - } | ||
| 1206 | - let kinds: Vec<String> = fx | ||
| 1207 | - .store | ||
| 1208 | - .list_events(&pull.issue.id) | ||
| 1209 | - .await | ||
| 1210 | - .unwrap() | ||
| 1211 | - .into_iter() | ||
| 1212 | - .map(|e| e.kind) | ||
| 1213 | - .collect(); | ||
| 1214 | - assert_eq!(kinds, vec!["head_updated", "base_changed", "head_deleted"]); | ||
| 1215 | - } | ||
| 1216 | - | ||
| 1217 | - #[tokio::test] | ||
| 1218 | - async fn draft_flag_round_trips_with_events_and_list_filter() { | ||
| 1219 | - let fx = fixture().await; | ||
| 1220 | - let pr = open_pull(&fx, &fx.repo.id, "feature", "wip").await; | ||
| 1221 | - let _ready = open_pull(&fx, &fx.repo.id, "other", "ready").await; | ||
| 1222 | - assert!(!pr.draft); | ||
| 1223 | - | ||
| 1224 | - assert!( | ||
| 1225 | - fx.store | ||
| 1226 | - .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) | ||
| 1227 | - .await | ||
| 1228 | - .unwrap() | ||
| 1229 | - ); | ||
| 1230 | - // Setting the same value again changes nothing and records no event. | ||
| 1231 | - assert!( | ||
| 1232 | - !fx.store | ||
| 1233 | - .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) | ||
| 1234 | - .await | ||
| 1235 | - .unwrap() | ||
| 1236 | - ); | ||
| 1237 | - let got = fx | ||
| 1238 | - .store | ||
| 1239 | - .pull_by_number(&fx.repo.id, pr.issue.number) | ||
| 1240 | - .await | ||
| 1241 | - .unwrap() | ||
| 1242 | - .unwrap(); | ||
| 1243 | - assert!(got.draft); | ||
| 1244 | - | ||
| 1245 | - let only = |draft: Option<bool>| IssueFilter { | ||
| 1246 | - state: None, | ||
| 1247 | - draft, | ||
| 1248 | - ..IssueFilter::default() | ||
| 1249 | - }; | ||
| 1250 | - let (items, total) = fx | ||
| 1251 | - .store | ||
| 1252 | - .list_pulls(&fx.repo.id, &only(Some(true))) | ||
| 1253 | - .await | ||
| 1254 | - .unwrap(); | ||
| 1255 | - assert_eq!((total, items[0].issue.number), (1, pr.issue.number)); | ||
| 1256 | - let (_, total) = fx | ||
| 1257 | - .store | ||
| 1258 | - .list_pulls(&fx.repo.id, &only(Some(false))) | ||
| 1259 | - .await | ||
| 1260 | - .unwrap(); | ||
| 1261 | - assert_eq!(total, 1); | ||
| 1262 | - let (_, total) = fx.store.list_pulls(&fx.repo.id, &only(None)).await.unwrap(); | ||
| 1263 | - assert_eq!(total, 2); | ||
| 1264 | - let numbers = [pr.issue.number, pr.issue.number + 1]; | ||
| 1265 | - let drafts = fx.store.draft_numbers(&fx.repo.id, &numbers).await.unwrap(); | ||
| 1266 | - assert_eq!(drafts.len(), 1); | ||
| 1267 | - assert!(drafts.contains(&pr.issue.number)); | ||
| 1268 | - | ||
| 1269 | - assert!( | ||
| 1270 | - fx.store | ||
| 1271 | - .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), false) | ||
| 1272 | - .await | ||
| 1273 | - .unwrap() | ||
| 1274 | - ); | ||
| 1275 | - let kinds: Vec<String> = fx | ||
| 1276 | - .store | ||
| 1277 | - .list_events(&pr.issue.id) | ||
| 1278 | - .await | ||
| 1279 | - .unwrap() | ||
| 1280 | - .into_iter() | ||
| 1281 | - .map(|e| e.kind) | ||
| 1282 | - .collect(); | ||
| 1283 | - assert_eq!(kinds, vec!["converted_to_draft", "ready_for_review"]); | ||
| 1284 | - } | ||
| 1285 | - | ||
| 1286 | - #[tokio::test] | ||
| 1287 | - async fn auto_merge_is_stored_and_cleared_by_push_draft_and_merge() { | ||
| 1288 | - let fx = fixture().await; | ||
| 1289 | - let pr = open_pull(&fx, &fx.repo.id, "feature", "auto").await; | ||
| 1290 | - let head = "a".repeat(40); | ||
| 1291 | - let load = || async { | ||
| 1292 | - fx.store | ||
| 1293 | - .pull_by_number(&fx.repo.id, pr.issue.number) | ||
| 1294 | - .await | ||
| 1295 | - .unwrap() | ||
| 1296 | - .unwrap() | ||
| 1297 | - }; | ||
| 1298 | - assert!(load().await.auto_merge.is_none()); | ||
| 1299 | - assert!( | ||
| 1300 | - !fx.store | ||
| 1301 | - .disable_auto_merge(&pr.issue.id, None, "manual") | ||
| 1302 | - .await | ||
| 1303 | - .unwrap() | ||
| 1304 | - ); | ||
| 1305 | - | ||
| 1306 | - fx.store | ||
| 1307 | - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Rebase) | ||
| 1308 | - .await | ||
| 1309 | - .unwrap(); | ||
| 1310 | - let auto = load().await.auto_merge.expect("enabled"); | ||
| 1311 | - assert_eq!( | ||
| 1312 | - (auto.style, auto.user_id.as_str()), | ||
| 1313 | - (MergeStyle::Rebase, fx.bob.id.as_str()) | ||
| 1314 | - ); | ||
| 1315 | - let found = fx | ||
| 1316 | - .store | ||
| 1317 | - .auto_merge_pulls_for_head(&fx.repo.id, &head) | ||
| 1318 | - .await | ||
| 1319 | - .unwrap(); | ||
| 1320 | - assert_eq!(found.len(), 1); | ||
| 1321 | - assert!( | ||
| 1322 | - fx.store | ||
| 1323 | - .auto_merge_pulls_for_head(&fx.repo.id, &"d".repeat(40)) | ||
| 1324 | - .await | ||
| 1325 | - .unwrap() | ||
| 1326 | - .is_empty() | ||
| 1327 | - ); | ||
| 1328 | - | ||
| 1329 | - // A new head clears it. | ||
| 1330 | - fx.store | ||
| 1331 | - .update_pull_head(&pr.issue.id, &"b".repeat(40)) | ||
| 1332 | - .await | ||
| 1333 | - .unwrap(); | ||
| 1334 | - assert!(load().await.auto_merge.is_none()); | ||
| 1335 | - | ||
| 1336 | - // Converting to a draft clears it. | ||
| 1337 | - fx.store | ||
| 1338 | - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) | ||
| 1339 | - .await | ||
| 1340 | - .unwrap(); | ||
| 1341 | - fx.store | ||
| 1342 | - .set_pull_draft(&pr.issue.id, None, true) | ||
| 1343 | - .await | ||
| 1344 | - .unwrap(); | ||
| 1345 | - assert!(load().await.auto_merge.is_none()); | ||
| 1346 | - | ||
| 1347 | - // Disabling records why; merging clears it. | ||
| 1348 | - fx.store | ||
| 1349 | - .set_pull_draft(&pr.issue.id, None, false) | ||
| 1350 | - .await | ||
| 1351 | - .unwrap(); | ||
| 1352 | - fx.store | ||
| 1353 | - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) | ||
| 1354 | - .await | ||
| 1355 | - .unwrap(); | ||
| 1356 | - assert!( | ||
| 1357 | - fx.store | ||
| 1358 | - .disable_auto_merge(&pr.issue.id, Some(&fx.bob.id), "refused") | ||
| 1359 | - .await | ||
| 1360 | - .unwrap() | ||
| 1361 | - ); | ||
| 1362 | - let events = fx.store.list_events(&pr.issue.id).await.unwrap(); | ||
| 1363 | - let last = events.last().unwrap(); | ||
| 1364 | - assert_eq!(last.kind, "auto_merge_disabled"); | ||
| 1365 | - assert_eq!(last.data["reason"], "refused"); | ||
| 1366 | - | ||
| 1367 | - fx.store | ||
| 1368 | - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) | ||
| 1369 | - .await | ||
| 1370 | - .unwrap(); | ||
| 1371 | - fx.store | ||
| 1372 | - .mark_merged(&pr.issue.id, &fx.bob.id, &"c".repeat(40)) | ||
| 1373 | - .await | ||
| 1374 | - .unwrap(); | ||
| 1375 | - assert!(load().await.auto_merge.is_none()); | ||
| 1376 | - assert!( | ||
| 1377 | - fx.store | ||
| 1378 | - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) | ||
| 1379 | - .await | ||
| 1380 | - .is_err() | ||
| 1381 | - ); | ||
| 1382 | } | 1487 | } |
| 1383 | } | 1488 | } |
crates/forge-core/src/store/reactions.rsmodified+57 -42
| @@ -186,9 +186,13 @@ mod tests { | |||
| 186 | } | 186 | } |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | + // One store serves every scenario. Each scenario opens its own issue, so | ||
| 190 | + // the reactions never overlap. | ||
| 189 | #[tokio::test] | 191 | #[tokio::test] |
| 190 | - async fn toggle_adds_then_removes_and_rejects_missing_target() { | 192 | + async fn reaction_scenarios_share_one_store() { |
| 191 | let fx = fixture().await; | 193 | let fx = fixture().await; |
| 194 | + | ||
| 195 | + // Toggle adds then removes, and a missing target is refused. | ||
| 192 | let issue = fx | 196 | let issue = fx |
| 193 | .store | 197 | .store |
| 194 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") | 198 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") |
| @@ -204,7 +208,7 @@ mod tests { | |||
| 204 | ) | 208 | ) |
| 205 | .await | 209 | .await |
| 206 | .unwrap(); | 210 | .unwrap(); |
| 207 | - assert!(on); | 211 | + assert!(on, "toggle: first reaction is added"); |
| 208 | let off = fx | 212 | let off = fx |
| 209 | .store | 213 | .store |
| 210 | .toggle_reaction( | 214 | .toggle_reaction( |
| @@ -215,29 +219,29 @@ mod tests { | |||
| 215 | ) | 219 | ) |
| 216 | .await | 220 | .await |
| 217 | .unwrap(); | 221 | .unwrap(); |
| 218 | - assert!(!off); | 222 | + assert!(!off, "toggle: second reaction removes it"); |
| 219 | let map = fx | 223 | let map = fx |
| 220 | .store | 224 | .store |
| 221 | .reactions_for(ReactionTarget::Issue, std::slice::from_ref(&issue.id), None) | 225 | .reactions_for(ReactionTarget::Issue, std::slice::from_ref(&issue.id), None) |
| 222 | .await | 226 | .await |
| 223 | .unwrap(); | 227 | .unwrap(); |
| 224 | - assert!(map.is_empty()); | 228 | + assert!(map.is_empty(), "toggle: nothing left after removal"); |
| 225 | - assert!(matches!( | 229 | + assert!( |
| 226 | - fx.store | 230 | + matches!( |
| 227 | - .toggle_reaction( | 231 | + fx.store |
| 228 | - ReactionTarget::Comment, | 232 | + .toggle_reaction( |
| 229 | - "missing", | 233 | + ReactionTarget::Comment, |
| 230 | - &fx.bob.id, | 234 | + "missing", |
| 231 | - Reaction::Eyes | 235 | + &fx.bob.id, |
| 232 | - ) | 236 | + Reaction::Eyes |
| 233 | - .await, | 237 | + ) |
| 234 | - Err(Error::NotFound) | 238 | + .await, |
| 235 | - )); | 239 | + Err(Error::NotFound) |
| 236 | - } | 240 | + ), |
| 241 | + "toggle: missing target is NotFound" | ||
| 242 | + ); | ||
| 237 | 243 | ||
| 238 | - #[tokio::test] | 244 | + // Aggregates count users and flag the viewer's own reactions. |
| 239 | - async fn aggregate_counts_users_and_viewer_flag() { | ||
| 240 | - let fx = fixture().await; | ||
| 241 | let issue = fx | 245 | let issue = fx |
| 242 | .store | 246 | .store |
| 243 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") | 247 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") |
| @@ -286,34 +290,48 @@ mod tests { | |||
| 286 | .await | 290 | .await |
| 287 | .unwrap(); | 291 | .unwrap(); |
| 288 | let summaries = &map[&issue.id]; | 292 | let summaries = &map[&issue.id]; |
| 289 | - assert_eq!(summaries.len(), 2); | 293 | + assert_eq!(summaries.len(), 2, "aggregate: two reaction kinds"); |
| 290 | - assert_eq!(summaries[0].reaction, Reaction::PlusOne); | 294 | + assert_eq!(summaries[0].reaction, Reaction::PlusOne, "aggregate: order"); |
| 291 | - assert_eq!(summaries[0].count, 2); | 295 | + assert_eq!(summaries[0].count, 2, "aggregate: plus one count"); |
| 292 | - assert!(summaries[0].reacted_by_viewer); | 296 | + assert!(summaries[0].reacted_by_viewer, "aggregate: viewer flag"); |
| 293 | - assert_eq!(summaries[0].users, vec!["Tahinli", "Bob"]); | 297 | + assert_eq!( |
| 294 | - assert_eq!(summaries[1].reaction, Reaction::Rocket); | 298 | + summaries[0].users, |
| 295 | - assert_eq!(summaries[1].count, 1); | 299 | + vec!["Tahinli", "Bob"], |
| 296 | - assert!(!map.contains_key(&comment.id)); | 300 | + "aggregate: users" |
| 301 | + ); | ||
| 302 | + assert_eq!(summaries[1].reaction, Reaction::Rocket, "aggregate: rocket"); | ||
| 303 | + assert_eq!(summaries[1].count, 1, "aggregate: rocket count"); | ||
| 304 | + assert!( | ||
| 305 | + !map.contains_key(&comment.id), | ||
| 306 | + "aggregate: comment kept apart" | ||
| 307 | + ); | ||
| 297 | 308 | ||
| 298 | let anon = fx | 309 | let anon = fx |
| 299 | .store | 310 | .store |
| 300 | .reactions_for(ReactionTarget::Issue, std::slice::from_ref(&issue.id), None) | 311 | .reactions_for(ReactionTarget::Issue, std::slice::from_ref(&issue.id), None) |
| 301 | .await | 312 | .await |
| 302 | .unwrap(); | 313 | .unwrap(); |
| 303 | - assert!(anon[&issue.id].iter().all(|s| !s.reacted_by_viewer)); | 314 | + assert!( |
| 315 | + anon[&issue.id].iter().all(|s| !s.reacted_by_viewer), | ||
| 316 | + "aggregate: no viewer, no flags" | ||
| 317 | + ); | ||
| 304 | 318 | ||
| 305 | let on_comment = fx | 319 | let on_comment = fx |
| 306 | .store | 320 | .store |
| 307 | .reactions_for(ReactionTarget::Comment, &ids, Some(&fx.tahinli.id)) | 321 | .reactions_for(ReactionTarget::Comment, &ids, Some(&fx.tahinli.id)) |
| 308 | .await | 322 | .await |
| 309 | .unwrap(); | 323 | .unwrap(); |
| 310 | - assert_eq!(on_comment[&comment.id][0].reaction, Reaction::Eyes); | 324 | + assert_eq!( |
| 311 | - assert!(on_comment[&comment.id][0].reacted_by_viewer); | 325 | + on_comment[&comment.id][0].reaction, |
| 312 | - } | 326 | + Reaction::Eyes, |
| 327 | + "comment reaction" | ||
| 328 | + ); | ||
| 329 | + assert!( | ||
| 330 | + on_comment[&comment.id][0].reacted_by_viewer, | ||
| 331 | + "comment viewer flag" | ||
| 332 | + ); | ||
| 313 | 333 | ||
| 314 | - #[tokio::test] | 334 | + // Summaries keep the first ten names and the full count. |
| 315 | - async fn summaries_keep_first_ten_names_and_full_count() { | ||
| 316 | - let fx = fixture().await; | ||
| 317 | let issue = fx | 335 | let issue = fx |
| 318 | .store | 336 | .store |
| 319 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") | 337 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") |
| @@ -336,14 +354,11 @@ mod tests { | |||
| 336 | .await | 354 | .await |
| 337 | .unwrap(); | 355 | .unwrap(); |
| 338 | let summary = &map[&issue.id][0]; | 356 | let summary = &map[&issue.id][0]; |
| 339 | - assert_eq!(summary.count, 12); | 357 | + assert_eq!(summary.count, 12, "summary: full count"); |
| 340 | - assert_eq!(summary.users.len(), SUMMARY_USERS); | 358 | + assert_eq!(summary.users.len(), SUMMARY_USERS, "summary: capped names"); |
| 341 | - assert_eq!(summary.users[0], "user0"); | 359 | + assert_eq!(summary.users[0], "user0", "summary: first name"); |
| 342 | - } | ||
| 343 | 360 | ||
| 344 | - #[tokio::test] | 361 | + // Deleting a comment removes its reactions. |
| 345 | - async fn deleting_a_comment_removes_its_reactions() { | ||
| 346 | - let fx = fixture().await; | ||
| 347 | let issue = fx | 362 | let issue = fx |
| 348 | .store | 363 | .store |
| 349 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") | 364 | .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") |
| @@ -373,6 +388,6 @@ mod tests { | |||
| 373 | ) | 388 | ) |
| 374 | .await | 389 | .await |
| 375 | .unwrap(); | 390 | .unwrap(); |
| 376 | - assert!(map.is_empty()); | 391 | + assert!(map.is_empty(), "delete comment: reactions gone"); |
| 377 | } | 392 | } |
| 378 | } | 393 | } |
crates/forge-core/src/store/reviews.rsmodified+486 -411
| @@ -866,11 +866,10 @@ mod tests { | |||
| 866 | bob: User, | 866 | bob: User, |
| 867 | carol: User, | 867 | carol: User, |
| 868 | repo_id: String, | 868 | repo_id: String, |
| 869 | - issue: String, | ||
| 870 | _dir: tempfile::TempDir, | 869 | _dir: tempfile::TempDir, |
| 871 | } | 870 | } |
| 872 | 871 | ||
| 873 | - /// Tahinli owns the repo and opened the pull request; Bob and Carol review it. | 872 | + /// Tahinli owns the repo; Bob and Carol review its pull requests. |
| 874 | async fn fixture() -> Fixture { | 873 | async fn fixture() -> Fixture { |
| 875 | let dir = tempfile::tempdir().unwrap(); | 874 | let dir = tempfile::tempdir().unwrap(); |
| 876 | let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | 875 | let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); |
| @@ -887,36 +886,49 @@ mod tests { | |||
| 887 | .create_repo(&tahinli.id, "proj", "desc", Visibility::Public, "main") | 886 | .create_repo(&tahinli.id, "proj", "desc", Visibility::Public, "main") |
| 888 | .await | 887 | .await |
| 889 | .unwrap(); | 888 | .unwrap(); |
| 890 | - let pull = store | 889 | + Fixture { |
| 890 | + store, | ||
| 891 | + tahinli, | ||
| 892 | + bob, | ||
| 893 | + carol, | ||
| 894 | + repo_id: repo.id.clone(), | ||
| 895 | + _dir: dir, | ||
| 896 | + } | ||
| 897 | + } | ||
| 898 | + | ||
| 899 | + /// Opens a pull request from `feature-{topic}` and returns its issue id. | ||
| 900 | + /// Each scenario gets its own pull, so reviews never leak between them. | ||
| 901 | + async fn new_pull(fx: &Fixture, topic: &str) -> String { | ||
| 902 | + let head = format!("feature-{topic}"); | ||
| 903 | + let pull = fx | ||
| 904 | + .store | ||
| 891 | .create_pull( | 905 | .create_pull( |
| 892 | - &repo.id, | 906 | + &fx.repo_id, |
| 893 | - &tahinli.id, | 907 | + &fx.tahinli.id, |
| 894 | "change", | 908 | "change", |
| 895 | "", | 909 | "", |
| 896 | "main", | 910 | "main", |
| 897 | - &repo.id, | 911 | + &fx.repo_id, |
| 898 | - "feature", | 912 | + &head, |
| 899 | &oid('a'), | 913 | &oid('a'), |
| 900 | &oid('b'), | 914 | &oid('b'), |
| 901 | ) | 915 | ) |
| 902 | .await | 916 | .await |
| 903 | .unwrap(); | 917 | .unwrap(); |
| 904 | - Fixture { | 918 | + pull.issue.id.clone() |
| 905 | - store, | ||
| 906 | - tahinli, | ||
| 907 | - bob, | ||
| 908 | - carol, | ||
| 909 | - repo_id: repo.id.clone(), | ||
| 910 | - issue: pull.issue.id.clone(), | ||
| 911 | - _dir: dir, | ||
| 912 | - } | ||
| 913 | } | 919 | } |
| 914 | 920 | ||
| 915 | /// A root comment on `src/lib.rs` line 3 at head `a`. | 921 | /// A root comment on `src/lib.rs` line 3 at head `a`. |
| 916 | - async fn comment(fx: &Fixture, author: &str, pending: bool, body: &str) -> ReviewComment { | 922 | + async fn comment( |
| 923 | + fx: &Fixture, | ||
| 924 | + issue: &str, | ||
| 925 | + author: &str, | ||
| 926 | + pending: bool, | ||
| 927 | + body: &str, | ||
| 928 | + ) -> ReviewComment { | ||
| 917 | fx.store | 929 | fx.store |
| 918 | .add_review_comment( | 930 | .add_review_comment( |
| 919 | - &fx.issue, | 931 | + issue, |
| 920 | author, | 932 | author, |
| 921 | pending, | 933 | pending, |
| 922 | "src/lib.rs", | 934 | "src/lib.rs", |
| @@ -930,9 +942,15 @@ mod tests { | |||
| 930 | .unwrap() | 942 | .unwrap() |
| 931 | } | 943 | } |
| 932 | 944 | ||
| 933 | - async fn reply(fx: &Fixture, author: &str, root: &str, body: &str) -> ReviewComment { | 945 | + async fn reply( |
| 946 | + fx: &Fixture, | ||
| 947 | + issue: &str, | ||
| 948 | + author: &str, | ||
| 949 | + root: &str, | ||
| 950 | + body: &str, | ||
| 951 | + ) -> ReviewComment { | ||
| 934 | fx.store | 952 | fx.store |
| 935 | - .reply_review_comment(&fx.issue, author, root, body) | 953 | + .reply_review_comment(issue, author, root, body) |
| 936 | .await | 954 | .await |
| 937 | .unwrap() | 955 | .unwrap() |
| 938 | } | 956 | } |
| @@ -945,224 +963,139 @@ mod tests { | |||
| 945 | .unwrap() | 963 | .unwrap() |
| 946 | } | 964 | } |
| 947 | 965 | ||
| 966 | + // Comment, reply, edit, delete, resolve, outdated and relocate. Each | ||
| 967 | + // scenario has its own pull request. | ||
| 948 | #[tokio::test] | 968 | #[tokio::test] |
| 949 | - async fn pending_comment_is_visible_only_to_its_author() { | 969 | + async fn comment_scenarios_share_one_store() { |
| 950 | let fx = fixture().await; | 970 | let fx = fixture().await; |
| 951 | - let draft = comment(&fx, &fx.bob.id, true, "draft").await; | 971 | + |
| 952 | - assert!(draft.pending); | 972 | + // A pending comment is visible only to its author and joins one draft. |
| 953 | - assert_eq!(draft.author_name, "Bob"); | 973 | + let issue = new_pull(&fx, "pending").await; |
| 974 | + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; | ||
| 975 | + assert!(draft.pending, "pending: draft is pending"); | ||
| 976 | + assert_eq!(draft.author_name, "Bob", "pending: author name"); | ||
| 954 | 977 | ||
| 955 | let own = fx | 978 | let own = fx |
| 956 | .store | 979 | .store |
| 957 | - .review_threads(&fx.issue, Some(&fx.bob.id)) | 980 | + .review_threads(&issue, Some(&fx.bob.id)) |
| 958 | .await | 981 | .await |
| 959 | .unwrap(); | 982 | .unwrap(); |
| 960 | - assert_eq!(own.len(), 1); | 983 | + assert_eq!(own.len(), 1, "pending: author sees the draft"); |
| 961 | - assert_eq!(own[0].root.id, draft.id); | 984 | + assert_eq!(own[0].root.id, draft.id, "pending: author sees its root"); |
| 962 | - assert!(own[0].root.pending); | 985 | + assert!(own[0].root.pending, "pending: root still pending"); |
| 963 | 986 | ||
| 964 | for viewer in [Some(fx.carol.id.as_str()), None] { | 987 | for viewer in [Some(fx.carol.id.as_str()), None] { |
| 965 | - let threads = fx.store.review_threads(&fx.issue, viewer).await.unwrap(); | 988 | + let threads = fx.store.review_threads(&issue, viewer).await.unwrap(); |
| 966 | - assert!(threads.is_empty()); | 989 | + assert!(threads.is_empty(), "pending: hidden from others"); |
| 967 | } | 990 | } |
| 968 | - assert!(fx.store.list_reviews(&fx.issue).await.unwrap().is_empty()); | 991 | + assert!( |
| 992 | + fx.store.list_reviews(&issue).await.unwrap().is_empty(), | ||
| 993 | + "pending: no submitted review yet" | ||
| 994 | + ); | ||
| 969 | 995 | ||
| 970 | let pending = fx | 996 | let pending = fx |
| 971 | .store | 997 | .store |
| 972 | - .pending_review(&fx.issue, &fx.bob.id) | 998 | + .pending_review(&issue, &fx.bob.id) |
| 973 | .await | 999 | .await |
| 974 | .unwrap() | 1000 | .unwrap() |
| 975 | .expect("pending review"); | 1001 | .expect("pending review"); |
| 976 | - assert_eq!(pending.state, ReviewState::Pending); | 1002 | + assert_eq!(pending.state, ReviewState::Pending, "pending: state"); |
| 977 | - assert_eq!(pending.id, draft.review_id); | 1003 | + assert_eq!(pending.id, draft.review_id, "pending: draft review"); |
| 978 | 1004 | ||
| 979 | - // A second pending comment joins the same draft review. | 1005 | + let second = comment(&fx, &issue, &fx.bob.id, true, "second").await; |
| 980 | - let second = comment(&fx, &fx.bob.id, true, "second").await; | 1006 | + assert_eq!( |
| 981 | - assert_eq!(second.review_id, draft.review_id); | 1007 | + second.review_id, draft.review_id, |
| 982 | - } | 1008 | + "pending: same draft review" |
| 983 | - | ||
| 984 | - #[tokio::test] | ||
| 985 | - async fn single_comment_creates_submitted_commented_review() { | ||
| 986 | - let fx = fixture().await; | ||
| 987 | - let c = comment(&fx, &fx.bob.id, false, "nit").await; | ||
| 988 | - assert!(!c.pending); | ||
| 989 | - | ||
| 990 | - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); | ||
| 991 | - assert_eq!(reviews.len(), 1); | ||
| 992 | - assert_eq!(reviews[0].id, c.review_id); | ||
| 993 | - assert_eq!(reviews[0].state, ReviewState::Commented); | ||
| 994 | - assert_eq!(reviews[0].body, ""); | ||
| 995 | - assert!(reviews[0].submitted_at.is_some()); | ||
| 996 | - assert!( | ||
| 997 | - fx.store | ||
| 998 | - .pending_review(&fx.issue, &fx.bob.id) | ||
| 999 | - .await | ||
| 1000 | - .unwrap() | ||
| 1001 | - .is_none() | ||
| 1002 | ); | 1009 | ); |
| 1003 | 1010 | ||
| 1004 | - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); | 1011 | + // A single submitted comment creates its own commented review. |
| 1005 | - assert_eq!(threads.len(), 1); | 1012 | + let issue = new_pull(&fx, "single").await; |
| 1006 | - assert!(threads[0].replies.is_empty()); | 1013 | + let c = comment(&fx, &issue, &fx.bob.id, false, "nit").await; |
| 1007 | - } | 1014 | + assert!(!c.pending, "single: submitted"); |
| 1008 | 1015 | ||
| 1009 | - #[tokio::test] | 1016 | + let reviews = fx.store.list_reviews(&issue).await.unwrap(); |
| 1010 | - async fn submit_pending_as_approved() { | 1017 | + assert_eq!(reviews.len(), 1, "single: one review"); |
| 1011 | - let fx = fixture().await; | 1018 | + assert_eq!( |
| 1012 | - let draft = comment(&fx, &fx.bob.id, true, "draft").await; | 1019 | + reviews[0].id, c.review_id, |
| 1013 | - let review = fx | 1020 | + "single: review holds the comment" |
| 1014 | - .store | 1021 | + ); |
| 1015 | - .submit_review( | 1022 | + assert_eq!( |
| 1016 | - &fx.issue, | 1023 | + reviews[0].state, |
| 1017 | - &fx.bob.id, | 1024 | + ReviewState::Commented, |
| 1018 | - ReviewState::Approved, | 1025 | + "single: commented" |
| 1019 | - "lgtm", | 1026 | + ); |
| 1020 | - &oid('c'), | 1027 | + assert_eq!(reviews[0].body, "", "single: empty body"); |
| 1021 | - ) | 1028 | + assert!(reviews[0].submitted_at.is_some(), "single: submitted time"); |
| 1022 | - .await | ||
| 1023 | - .unwrap(); | ||
| 1024 | - assert_eq!(review.id, draft.review_id); | ||
| 1025 | - assert_eq!(review.state, ReviewState::Approved); | ||
| 1026 | - assert_eq!(review.body, "lgtm"); | ||
| 1027 | - assert_eq!(review.commit_oid, oid('c')); | ||
| 1028 | - assert_eq!(review.author_name, "Bob"); | ||
| 1029 | - assert!(review.submitted_at.is_some()); | ||
| 1030 | - | ||
| 1031 | - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); | ||
| 1032 | - assert_eq!(reviews.len(), 1); | ||
| 1033 | assert!( | 1029 | assert!( |
| 1034 | fx.store | 1030 | fx.store |
| 1035 | - .pending_review(&fx.issue, &fx.bob.id) | 1031 | + .pending_review(&issue, &fx.bob.id) |
| 1036 | .await | 1032 | .await |
| 1037 | .unwrap() | 1033 | .unwrap() |
| 1038 | - .is_none() | 1034 | + .is_none(), |
| 1035 | + "single: no draft left" | ||
| 1039 | ); | 1036 | ); |
| 1040 | - let threads = fx | 1037 | + let threads = fx.store.review_threads(&issue, None).await.unwrap(); |
| 1041 | - .store | 1038 | + assert_eq!(threads.len(), 1, "single: one thread"); |
| 1042 | - .review_threads(&fx.issue, Some(&fx.bob.id)) | 1039 | + assert!(threads[0].replies.is_empty(), "single: no replies"); |
| 1043 | - .await | 1040 | + |
| 1044 | - .unwrap(); | 1041 | + // A reply copies the root position and needs a submitted root. |
| 1045 | - assert!(!threads[0].root.pending); | 1042 | + let issue = new_pull(&fx, "reply").await; |
| 1046 | - | 1043 | + let root = comment(&fx, &issue, &fx.bob.id, false, "question").await; |
| 1047 | - let summary = fx.store.review_summary(&fx.issue).await.unwrap(); | 1044 | + let answer = reply(&fx, &issue, &fx.carol.id, &root.id, "answer").await; |
| 1048 | - assert_eq!(summary.approved_by, vec!["Bob".to_string()]); | 1045 | + assert_eq!(answer.path, root.path, "reply: path copied"); |
| 1049 | - assert!(summary.changes_requested_by.is_empty()); | 1046 | + assert_eq!(answer.side, Side::New, "reply: side copied"); |
| 1050 | - } | 1047 | + assert_eq!(answer.line, 3, "reply: line copied"); |
| 1051 | - | 1048 | + assert_eq!(answer.commit_oid, oid('a'), "reply: commit copied"); |
| 1052 | - #[tokio::test] | 1049 | + assert_eq!( |
| 1053 | - async fn pull_request_author_cannot_approve_or_request_changes() { | 1050 | + answer.reply_to.as_deref(), |
| 1054 | - let fx = fixture().await; | 1051 | + Some(root.id.as_str()), |
| 1055 | - for state in [ReviewState::Approved, ReviewState::ChangesRequested] { | 1052 | + "reply: points at root" |
| 1056 | - let err = fx | 1053 | + ); |
| 1057 | - .store | 1054 | + assert!(!answer.pending, "reply: submitted"); |
| 1058 | - .submit_review(&fx.issue, &fx.tahinli.id, state, "", &oid('a')) | ||
| 1059 | - .await | ||
| 1060 | - .unwrap_err(); | ||
| 1061 | - assert!(matches!(err, Error::Forbidden(_)), "{err:?}"); | ||
| 1062 | - } | ||
| 1063 | - // Refused reviews store nothing. | ||
| 1064 | - assert!(fx.store.list_reviews(&fx.issue).await.unwrap().is_empty()); | ||
| 1065 | - | ||
| 1066 | - // The author may still comment. | ||
| 1067 | - let review = fx | ||
| 1068 | - .store | ||
| 1069 | - .submit_review( | ||
| 1070 | - &fx.issue, | ||
| 1071 | - &fx.tahinli.id, | ||
| 1072 | - ReviewState::Commented, | ||
| 1073 | - "note", | ||
| 1074 | - &oid('a'), | ||
| 1075 | - ) | ||
| 1076 | - .await | ||
| 1077 | - .unwrap(); | ||
| 1078 | - assert_eq!(review.state, ReviewState::Commented); | ||
| 1079 | - } | ||
| 1080 | - | ||
| 1081 | - #[tokio::test] | ||
| 1082 | - async fn empty_commented_review_is_invalid() { | ||
| 1083 | - let fx = fixture().await; | ||
| 1084 | - let err = fx | ||
| 1085 | - .store | ||
| 1086 | - .submit_review( | ||
| 1087 | - &fx.issue, | ||
| 1088 | - &fx.bob.id, | ||
| 1089 | - ReviewState::Commented, | ||
| 1090 | - " ", | ||
| 1091 | - &oid('a'), | ||
| 1092 | - ) | ||
| 1093 | - .await | ||
| 1094 | - .unwrap_err(); | ||
| 1095 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | ||
| 1096 | - | ||
| 1097 | - // An approval needs no body. | ||
| 1098 | - fx.store | ||
| 1099 | - .submit_review( | ||
| 1100 | - &fx.issue, | ||
| 1101 | - &fx.carol.id, | ||
| 1102 | - ReviewState::Approved, | ||
| 1103 | - "", | ||
| 1104 | - &oid('a'), | ||
| 1105 | - ) | ||
| 1106 | - .await | ||
| 1107 | - .unwrap(); | ||
| 1108 | - | ||
| 1109 | - // A pending comment makes an empty comment review valid. | ||
| 1110 | - comment(&fx, &fx.bob.id, true, "draft").await; | ||
| 1111 | - fx.store | ||
| 1112 | - .submit_review(&fx.issue, &fx.bob.id, ReviewState::Commented, "", &oid('a')) | ||
| 1113 | - .await | ||
| 1114 | - .unwrap(); | ||
| 1115 | - assert_eq!(fx.store.list_reviews(&fx.issue).await.unwrap().len(), 2); | ||
| 1116 | - } | ||
| 1117 | - | ||
| 1118 | - #[tokio::test] | ||
| 1119 | - async fn reply_copies_position_and_requires_submitted_root() { | ||
| 1120 | - let fx = fixture().await; | ||
| 1121 | - let root = comment(&fx, &fx.bob.id, false, "question").await; | ||
| 1122 | - let answer = reply(&fx, &fx.carol.id, &root.id, "answer").await; | ||
| 1123 | - assert_eq!(answer.path, root.path); | ||
| 1124 | - assert_eq!(answer.side, Side::New); | ||
| 1125 | - assert_eq!(answer.line, 3); | ||
| 1126 | - assert_eq!(answer.commit_oid, oid('a')); | ||
| 1127 | - assert_eq!(answer.reply_to.as_deref(), Some(root.id.as_str())); | ||
| 1128 | - assert!(!answer.pending); | ||
| 1129 | 1055 | ||
| 1130 | // Each reply is submitted through its own commented review. | 1056 | // Each reply is submitted through its own commented review. |
| 1131 | - assert_eq!(fx.store.list_reviews(&fx.issue).await.unwrap().len(), 2); | 1057 | + assert_eq!( |
| 1132 | - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); | 1058 | + fx.store.list_reviews(&issue).await.unwrap().len(), |
| 1133 | - assert_eq!(threads.len(), 1); | 1059 | + 2, |
| 1134 | - assert_eq!(threads[0].replies.len(), 1); | 1060 | + "reply: root review and reply review" |
| 1061 | + ); | ||
| 1062 | + let threads = fx.store.review_threads(&issue, None).await.unwrap(); | ||
| 1063 | + assert_eq!(threads.len(), 1, "reply: one thread"); | ||
| 1064 | + assert_eq!(threads[0].replies.len(), 1, "reply: one reply in thread"); | ||
| 1135 | 1065 | ||
| 1136 | - // Not a reply to a reply, not a pending root, not a missing id. | ||
| 1137 | for target in [answer.id.as_str(), "missing"] { | 1066 | for target in [answer.id.as_str(), "missing"] { |
| 1138 | let err = fx | 1067 | let err = fx |
| 1139 | .store | 1068 | .store |
| 1140 | - .reply_review_comment(&fx.issue, &fx.carol.id, target, "x") | 1069 | + .reply_review_comment(&issue, &fx.carol.id, target, "x") |
| 1141 | .await | 1070 | .await |
| 1142 | .unwrap_err(); | 1071 | .unwrap_err(); |
| 1143 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | 1072 | + assert!( |
| 1073 | + matches!(err, Error::NotFound), | ||
| 1074 | + "reply: refused {target}: {err:?}" | ||
| 1075 | + ); | ||
| 1144 | } | 1076 | } |
| 1145 | - let draft = comment(&fx, &fx.bob.id, true, "draft").await; | 1077 | + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; |
| 1146 | for author in [&fx.bob.id, &fx.carol.id] { | 1078 | for author in [&fx.bob.id, &fx.carol.id] { |
| 1147 | let err = fx | 1079 | let err = fx |
| 1148 | .store | 1080 | .store |
| 1149 | - .reply_review_comment(&fx.issue, author, &draft.id, "x") | 1081 | + .reply_review_comment(&issue, author, &draft.id, "x") |
| 1150 | .await | 1082 | .await |
| 1151 | .unwrap_err(); | 1083 | .unwrap_err(); |
| 1152 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | 1084 | + assert!( |
| 1085 | + matches!(err, Error::NotFound), | ||
| 1086 | + "reply: no reply to draft: {err:?}" | ||
| 1087 | + ); | ||
| 1153 | } | 1088 | } |
| 1154 | - } | ||
| 1155 | 1089 | ||
| 1156 | - #[tokio::test] | 1090 | + // Edits record the previous body. |
| 1157 | - async fn edit_records_history() { | 1091 | + let issue = new_pull(&fx, "edit").await; |
| 1158 | - let fx = fixture().await; | 1092 | + let c = comment(&fx, &issue, &fx.bob.id, false, "old text").await; |
| 1159 | - let c = comment(&fx, &fx.bob.id, false, "old text").await; | ||
| 1160 | fx.store | 1093 | fx.store |
| 1161 | .edit_review_comment(&c.id, "new text") | 1094 | .edit_review_comment(&c.id, "new text") |
| 1162 | .await | 1095 | .await |
| 1163 | .unwrap(); | 1096 | .unwrap(); |
| 1164 | let edited = fx.store.review_comment(&c.id).await.unwrap().unwrap(); | 1097 | let edited = fx.store.review_comment(&c.id).await.unwrap().unwrap(); |
| 1165 | - assert_eq!(edited.body, "new text"); | 1098 | + assert_eq!(edited.body, "new text", "edit: new body"); |
| 1166 | 1099 | ||
| 1167 | let history: Vec<(String, String, String)> = { | 1100 | let history: Vec<(String, String, String)> = { |
| 1168 | let conn = fx.store.inner.conn.lock().await; | 1101 | let conn = fx.store.inner.conn.lock().await; |
| @@ -1181,56 +1114,79 @@ mod tests { | |||
| 1181 | "review_comment".to_string(), | 1114 | "review_comment".to_string(), |
| 1182 | "old text".to_string(), | 1115 | "old text".to_string(), |
| 1183 | fx.bob.id.clone() | 1116 | fx.bob.id.clone() |
| 1184 | - )] | 1117 | + )], |
| 1118 | + "edit: history keeps old body and editor" | ||
| 1185 | ); | 1119 | ); |
| 1186 | 1120 | ||
| 1187 | let err = fx.store.edit_review_comment(&c.id, " ").await.unwrap_err(); | 1121 | let err = fx.store.edit_review_comment(&c.id, " ").await.unwrap_err(); |
| 1188 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | 1122 | + assert!( |
| 1123 | + matches!(err, Error::Invalid(_)), | ||
| 1124 | + "edit: blank refused: {err:?}" | ||
| 1125 | + ); | ||
| 1189 | let err = fx | 1126 | let err = fx |
| 1190 | .store | 1127 | .store |
| 1191 | .edit_review_comment("missing", "x") | 1128 | .edit_review_comment("missing", "x") |
| 1192 | .await | 1129 | .await |
| 1193 | .unwrap_err(); | 1130 | .unwrap_err(); |
| 1194 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | 1131 | + assert!( |
| 1195 | - } | 1132 | + matches!(err, Error::NotFound), |
| 1133 | + "edit: missing refused: {err:?}" | ||
| 1134 | + ); | ||
| 1196 | 1135 | ||
| 1197 | - #[tokio::test] | 1136 | + // Deleting a root removes its replies; deleting a reply keeps the root. |
| 1198 | - async fn delete_root_removes_replies() { | 1137 | + let issue = new_pull(&fx, "delete").await; |
| 1199 | - let fx = fixture().await; | 1138 | + let root = comment(&fx, &issue, &fx.bob.id, false, "root").await; |
| 1200 | - let root = comment(&fx, &fx.bob.id, false, "root").await; | 1139 | + let r1 = reply(&fx, &issue, &fx.carol.id, &root.id, "one").await; |
| 1201 | - let r1 = reply(&fx, &fx.carol.id, &root.id, "one").await; | 1140 | + let r2 = reply(&fx, &issue, &fx.bob.id, &root.id, "two").await; |
| 1202 | - let r2 = reply(&fx, &fx.bob.id, &root.id, "two").await; | 1141 | + let other = comment(&fx, &issue, &fx.carol.id, false, "other").await; |
| 1203 | - let other = comment(&fx, &fx.carol.id, false, "other").await; | ||
| 1204 | 1142 | ||
| 1205 | fx.store.delete_review_comment(&root.id).await.unwrap(); | 1143 | fx.store.delete_review_comment(&root.id).await.unwrap(); |
| 1206 | - assert!(fx.store.review_comment(&root.id).await.unwrap().is_none()); | 1144 | + assert!( |
| 1207 | - assert!(fx.store.review_comment(&r1.id).await.unwrap().is_none()); | 1145 | + fx.store.review_comment(&root.id).await.unwrap().is_none(), |
| 1208 | - assert!(fx.store.review_comment(&r2.id).await.unwrap().is_none()); | 1146 | + "delete root: root gone" |
| 1209 | - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); | 1147 | + ); |
| 1210 | - assert_eq!(threads.len(), 1); | 1148 | + assert!( |
| 1211 | - assert_eq!(threads[0].root.id, other.id); | 1149 | + fx.store.review_comment(&r1.id).await.unwrap().is_none(), |
| 1150 | + "delete root: first reply gone" | ||
| 1151 | + ); | ||
| 1152 | + assert!( | ||
| 1153 | + fx.store.review_comment(&r2.id).await.unwrap().is_none(), | ||
| 1154 | + "delete root: second reply gone" | ||
| 1155 | + ); | ||
| 1156 | + let threads = fx.store.review_threads(&issue, None).await.unwrap(); | ||
| 1157 | + assert_eq!(threads.len(), 1, "delete root: other thread remains"); | ||
| 1158 | + assert_eq!(threads[0].root.id, other.id, "delete root: other root kept"); | ||
| 1212 | // The emptied reviews of the root and its replies are gone too. | 1159 | // The emptied reviews of the root and its replies are gone too. |
| 1213 | - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); | 1160 | + let reviews = fx.store.list_reviews(&issue).await.unwrap(); |
| 1214 | - assert_eq!(reviews.len(), 1); | 1161 | + assert_eq!(reviews.len(), 1, "delete root: emptied reviews removed"); |
| 1215 | - assert_eq!(reviews[0].id, other.review_id); | 1162 | + assert_eq!( |
| 1163 | + reviews[0].id, other.review_id, | ||
| 1164 | + "delete root: other review kept" | ||
| 1165 | + ); | ||
| 1216 | 1166 | ||
| 1217 | - // Deleting a reply keeps its root. | 1167 | + let root2 = comment(&fx, &issue, &fx.bob.id, false, "root two").await; |
| 1218 | - let root2 = comment(&fx, &fx.bob.id, false, "root two").await; | 1168 | + let r3 = reply(&fx, &issue, &fx.carol.id, &root2.id, "three").await; |
| 1219 | - let r3 = reply(&fx, &fx.carol.id, &root2.id, "three").await; | ||
| 1220 | fx.store.delete_review_comment(&r3.id).await.unwrap(); | 1169 | fx.store.delete_review_comment(&r3.id).await.unwrap(); |
| 1221 | - assert!(fx.store.review_comment(&root2.id).await.unwrap().is_some()); | 1170 | + assert!( |
| 1222 | - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); | 1171 | + fx.store.review_comment(&root2.id).await.unwrap().is_some(), |
| 1172 | + "delete reply: root kept" | ||
| 1173 | + ); | ||
| 1174 | + let threads = fx.store.review_threads(&issue, None).await.unwrap(); | ||
| 1223 | let root2_thread = threads.iter().find(|t| t.root.id == root2.id).unwrap(); | 1175 | let root2_thread = threads.iter().find(|t| t.root.id == root2.id).unwrap(); |
| 1224 | - assert!(root2_thread.replies.is_empty()); | 1176 | + assert!( |
| 1177 | + root2_thread.replies.is_empty(), | ||
| 1178 | + "delete reply: no replies left" | ||
| 1179 | + ); | ||
| 1225 | 1180 | ||
| 1226 | let err = fx.store.delete_review_comment("missing").await.unwrap_err(); | 1181 | let err = fx.store.delete_review_comment("missing").await.unwrap_err(); |
| 1227 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | 1182 | + assert!( |
| 1228 | - } | 1183 | + matches!(err, Error::NotFound), |
| 1184 | + "delete: missing refused: {err:?}" | ||
| 1185 | + ); | ||
| 1229 | 1186 | ||
| 1230 | - #[tokio::test] | 1187 | + // Resolving records events, and only submitted roots can be resolved. |
| 1231 | - async fn resolve_and_unresolve_record_events() { | 1188 | + let issue = new_pull(&fx, "resolve").await; |
| 1232 | - let fx = fixture().await; | 1189 | + let root = comment(&fx, &issue, &fx.bob.id, false, "fix this").await; |
| 1233 | - let root = comment(&fx, &fx.bob.id, false, "fix this").await; | ||
| 1234 | 1190 | ||
| 1235 | fx.store | 1191 | fx.store |
| 1236 | .set_thread_resolved(&root.id, &fx.carol.id, true) | 1192 | .set_thread_resolved(&root.id, &fx.carol.id, true) |
| @@ -1242,7 +1198,8 @@ mod tests { | |||
| 1242 | .await | 1198 | .await |
| 1243 | .unwrap() | 1199 | .unwrap() |
| 1244 | .unwrap() | 1200 | .unwrap() |
| 1245 | - .resolved | 1201 | + .resolved, |
| 1202 | + "resolve: resolved" | ||
| 1246 | ); | 1203 | ); |
| 1247 | // Already resolved: no second event. | 1204 | // Already resolved: no second event. |
| 1248 | fx.store | 1205 | fx.store |
| @@ -1259,69 +1216,128 @@ mod tests { | |||
| 1259 | .await | 1216 | .await |
| 1260 | .unwrap() | 1217 | .unwrap() |
| 1261 | .unwrap() | 1218 | .unwrap() |
| 1262 | - .resolved | 1219 | + .resolved, |
| 1220 | + "resolve: unresolved again" | ||
| 1263 | ); | 1221 | ); |
| 1264 | 1222 | ||
| 1265 | let events: Vec<_> = fx | 1223 | let events: Vec<_> = fx |
| 1266 | .store | 1224 | .store |
| 1267 | - .list_events(&fx.issue) | 1225 | + .list_events(&issue) |
| 1268 | .await | 1226 | .await |
| 1269 | .unwrap() | 1227 | .unwrap() |
| 1270 | .into_iter() | 1228 | .into_iter() |
| 1271 | .filter(|e| e.kind.starts_with("thread_")) | 1229 | .filter(|e| e.kind.starts_with("thread_")) |
| 1272 | .collect(); | 1230 | .collect(); |
| 1273 | let kinds: Vec<&str> = events.iter().map(|e| e.kind.as_str()).collect(); | 1231 | let kinds: Vec<&str> = events.iter().map(|e| e.kind.as_str()).collect(); |
| 1274 | - assert_eq!(kinds, vec!["thread_resolved", "thread_unresolved"]); | 1232 | + assert_eq!( |
| 1233 | + kinds, | ||
| 1234 | + vec!["thread_resolved", "thread_unresolved"], | ||
| 1235 | + "resolve: events" | ||
| 1236 | + ); | ||
| 1275 | assert_eq!( | 1237 | assert_eq!( |
| 1276 | events[0].data, | 1238 | events[0].data, |
| 1277 | - serde_json::json!({"comment": root.id, "path": "src/lib.rs"}) | 1239 | + serde_json::json!({"comment": root.id, "path": "src/lib.rs"}), |
| 1240 | + "resolve: event data" | ||
| 1241 | + ); | ||
| 1242 | + assert_eq!( | ||
| 1243 | + events[0].actor_id.as_deref(), | ||
| 1244 | + Some(fx.carol.id.as_str()), | ||
| 1245 | + "resolve: resolver" | ||
| 1246 | + ); | ||
| 1247 | + assert_eq!( | ||
| 1248 | + events[1].actor_id.as_deref(), | ||
| 1249 | + Some(fx.bob.id.as_str()), | ||
| 1250 | + "resolve: unresolver" | ||
| 1278 | ); | 1251 | ); |
| 1279 | - assert_eq!(events[0].actor_id.as_deref(), Some(fx.carol.id.as_str())); | ||
| 1280 | - assert_eq!(events[1].actor_id.as_deref(), Some(fx.bob.id.as_str())); | ||
| 1281 | 1252 | ||
| 1282 | - // Only submitted roots can be resolved. | 1253 | + let r = reply(&fx, &issue, &fx.carol.id, &root.id, "ok").await; |
| 1283 | - let r = reply(&fx, &fx.carol.id, &root.id, "ok").await; | 1254 | + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; |
| 1284 | - let draft = comment(&fx, &fx.bob.id, true, "draft").await; | ||
| 1285 | for id in [r.id.as_str(), draft.id.as_str(), "missing"] { | 1255 | for id in [r.id.as_str(), draft.id.as_str(), "missing"] { |
| 1286 | let err = fx | 1256 | let err = fx |
| 1287 | .store | 1257 | .store |
| 1288 | .set_thread_resolved(id, &fx.carol.id, true) | 1258 | .set_thread_resolved(id, &fx.carol.id, true) |
| 1289 | .await | 1259 | .await |
| 1290 | .unwrap_err(); | 1260 | .unwrap_err(); |
| 1291 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | 1261 | + assert!( |
| 1262 | + matches!(err, Error::NotFound), | ||
| 1263 | + "resolve: refused {id}: {err:?}" | ||
| 1264 | + ); | ||
| 1292 | } | 1265 | } |
| 1293 | - } | ||
| 1294 | 1266 | ||
| 1295 | - #[tokio::test] | 1267 | + // Outdated marks only submitted roots. |
| 1296 | - async fn mark_outdated_only_touches_submitted_roots() { | 1268 | + let issue = new_pull(&fx, "outdated").await; |
| 1297 | - let fx = fixture().await; | 1269 | + let root = comment(&fx, &issue, &fx.bob.id, false, "a").await; |
| 1298 | - let root = comment(&fx, &fx.bob.id, false, "a").await; | 1270 | + let r = reply(&fx, &issue, &fx.carol.id, &root.id, "b").await; |
| 1299 | - let r = reply(&fx, &fx.carol.id, &root.id, "b").await; | 1271 | + let draft = comment(&fx, &issue, &fx.bob.id, true, "c").await; |
| 1300 | - let draft = comment(&fx, &fx.bob.id, true, "c").await; | ||
| 1301 | 1272 | ||
| 1302 | fx.store | 1273 | fx.store |
| 1303 | - .mark_outdated( | 1274 | + .mark_outdated(&issue, &[root.id.clone(), r.id.clone(), draft.id.clone()]) |
| 1304 | - &fx.issue, | ||
| 1305 | - &[root.id.clone(), r.id.clone(), draft.id.clone()], | ||
| 1306 | - ) | ||
| 1307 | .await | 1275 | .await |
| 1308 | .unwrap(); | 1276 | .unwrap(); |
| 1309 | 1277 | ||
| 1310 | - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); | 1278 | + let threads = fx.store.review_threads(&issue, None).await.unwrap(); |
| 1311 | - assert_eq!(threads.len(), 1); | 1279 | + assert_eq!(threads.len(), 1, "outdated: one submitted thread"); |
| 1312 | - assert!(threads[0].root.outdated); | 1280 | + assert!(threads[0].root.outdated, "outdated: root marked"); |
| 1313 | - assert!(!threads[0].replies[0].outdated); | 1281 | + assert!(!threads[0].replies[0].outdated, "outdated: reply untouched"); |
| 1314 | let own = fx | 1282 | let own = fx |
| 1315 | .store | 1283 | .store |
| 1316 | - .review_threads(&fx.issue, Some(&fx.bob.id)) | 1284 | + .review_threads(&issue, Some(&fx.bob.id)) |
| 1317 | .await | 1285 | .await |
| 1318 | .unwrap(); | 1286 | .unwrap(); |
| 1319 | let draft_thread = own.iter().find(|t| t.root.id == draft.id).unwrap(); | 1287 | let draft_thread = own.iter().find(|t| t.root.id == draft.id).unwrap(); |
| 1320 | - assert!(!draft_thread.root.outdated); | 1288 | + assert!(!draft_thread.root.outdated, "outdated: draft untouched"); |
| 1289 | + | ||
| 1290 | + // Relocation moves the root and its replies, keeping the hunk. | ||
| 1291 | + let issue = new_pull(&fx, "relocate").await; | ||
| 1292 | + let root = comment(&fx, &issue, &fx.bob.id, false, "root").await; | ||
| 1293 | + let answer = reply(&fx, &issue, &fx.tahinli.id, &root.id, "answer").await; | ||
| 1294 | + | ||
| 1295 | + fx.store | ||
| 1296 | + .relocate_review_comment(&root.id, 7, &oid('c')) | ||
| 1297 | + .await | ||
| 1298 | + .unwrap(); | ||
| 1299 | + | ||
| 1300 | + let moved = fx.store.review_comment(&root.id).await.unwrap().unwrap(); | ||
| 1301 | + assert_eq!(moved.line, 7, "relocate: root line"); | ||
| 1302 | + assert_eq!(moved.commit_oid, oid('c'), "relocate: root commit"); | ||
| 1303 | + assert_eq!(moved.diff_hunk, "+let x = 1;", "relocate: hunk kept"); | ||
| 1304 | + let reply_after = fx.store.review_comment(&answer.id).await.unwrap().unwrap(); | ||
| 1305 | + assert_eq!(reply_after.line, 7, "relocate: reply line"); | ||
| 1306 | + assert_eq!(reply_after.commit_oid, oid('c'), "relocate: reply commit"); | ||
| 1307 | + | ||
| 1308 | + let err = fx | ||
| 1309 | + .store | ||
| 1310 | + .relocate_review_comment(&answer.id, 2, &oid('c')) | ||
| 1311 | + .await | ||
| 1312 | + .unwrap_err(); | ||
| 1313 | + assert!( | ||
| 1314 | + matches!(err, Error::Invalid(_)), | ||
| 1315 | + "relocate: reply refused: {err:?}" | ||
| 1316 | + ); | ||
| 1317 | + let err = fx | ||
| 1318 | + .store | ||
| 1319 | + .relocate_review_comment("missing", 2, &oid('c')) | ||
| 1320 | + .await | ||
| 1321 | + .unwrap_err(); | ||
| 1322 | + assert!( | ||
| 1323 | + matches!(err, Error::NotFound), | ||
| 1324 | + "relocate: missing refused: {err:?}" | ||
| 1325 | + ); | ||
| 1326 | + let err = fx | ||
| 1327 | + .store | ||
| 1328 | + .relocate_review_comment(&root.id, 0, &oid('c')) | ||
| 1329 | + .await | ||
| 1330 | + .unwrap_err(); | ||
| 1331 | + assert!( | ||
| 1332 | + matches!(err, Error::Invalid(_)), | ||
| 1333 | + "relocate: line 0 refused: {err:?}" | ||
| 1334 | + ); | ||
| 1321 | } | 1335 | } |
| 1322 | 1336 | ||
| 1337 | + // Submitting, approving, dismissing and discarding reviews. Each scenario | ||
| 1338 | + // has its own pull request. | ||
| 1323 | #[tokio::test] | 1339 | #[tokio::test] |
| 1324 | - async fn review_summary_uses_latest_state_and_ignores_dismissed() { | 1340 | + async fn review_scenarios_share_one_store() { |
| 1325 | let fx = fixture().await; | 1341 | let fx = fixture().await; |
| 1326 | let dave = fx | 1342 | let dave = fx |
| 1327 | .store | 1343 | .store |
| @@ -1329,14 +1345,123 @@ mod tests { | |||
| 1329 | .await | 1345 | .await |
| 1330 | .unwrap(); | 1346 | .unwrap(); |
| 1331 | 1347 | ||
| 1332 | - // Bob approves, then requests changes: the later state counts. | 1348 | + // A pending draft becomes an approved review. |
| 1349 | + let issue = new_pull(&fx, "approve").await; | ||
| 1350 | + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; | ||
| 1351 | + let review = fx | ||
| 1352 | + .store | ||
| 1353 | + .submit_review(&issue, &fx.bob.id, ReviewState::Approved, "lgtm", &oid('c')) | ||
| 1354 | + .await | ||
| 1355 | + .unwrap(); | ||
| 1356 | + assert_eq!(review.id, draft.review_id, "approve: draft becomes review"); | ||
| 1357 | + assert_eq!(review.state, ReviewState::Approved, "approve: state"); | ||
| 1358 | + assert_eq!(review.body, "lgtm", "approve: body"); | ||
| 1359 | + assert_eq!(review.commit_oid, oid('c'), "approve: commit"); | ||
| 1360 | + assert_eq!(review.author_name, "Bob", "approve: author"); | ||
| 1361 | + assert!(review.submitted_at.is_some(), "approve: submitted time"); | ||
| 1362 | + | ||
| 1363 | + assert_eq!( | ||
| 1364 | + fx.store.list_reviews(&issue).await.unwrap().len(), | ||
| 1365 | + 1, | ||
| 1366 | + "approve: one review" | ||
| 1367 | + ); | ||
| 1368 | + assert!( | ||
| 1369 | + fx.store | ||
| 1370 | + .pending_review(&issue, &fx.bob.id) | ||
| 1371 | + .await | ||
| 1372 | + .unwrap() | ||
| 1373 | + .is_none(), | ||
| 1374 | + "approve: no draft left" | ||
| 1375 | + ); | ||
| 1376 | + let threads = fx | ||
| 1377 | + .store | ||
| 1378 | + .review_threads(&issue, Some(&fx.bob.id)) | ||
| 1379 | + .await | ||
| 1380 | + .unwrap(); | ||
| 1381 | + assert!(!threads[0].root.pending, "approve: comment submitted"); | ||
| 1382 | + | ||
| 1383 | + let summary = fx.store.review_summary(&issue).await.unwrap(); | ||
| 1384 | + assert_eq!( | ||
| 1385 | + summary.approved_by, | ||
| 1386 | + vec!["Bob".to_string()], | ||
| 1387 | + "approve: approver" | ||
| 1388 | + ); | ||
| 1389 | + assert!( | ||
| 1390 | + summary.changes_requested_by.is_empty(), | ||
| 1391 | + "approve: no requests" | ||
| 1392 | + ); | ||
| 1393 | + | ||
| 1394 | + // The pull request author may comment but not approve or request changes. | ||
| 1395 | + let issue = new_pull(&fx, "author").await; | ||
| 1396 | + for state in [ReviewState::Approved, ReviewState::ChangesRequested] { | ||
| 1397 | + let err = fx | ||
| 1398 | + .store | ||
| 1399 | + .submit_review(&issue, &fx.tahinli.id, state, "", &oid('a')) | ||
| 1400 | + .await | ||
| 1401 | + .unwrap_err(); | ||
| 1402 | + assert!( | ||
| 1403 | + matches!(err, Error::Forbidden(_)), | ||
| 1404 | + "author: refused {state:?}: {err:?}" | ||
| 1405 | + ); | ||
| 1406 | + } | ||
| 1407 | + assert!( | ||
| 1408 | + fx.store.list_reviews(&issue).await.unwrap().is_empty(), | ||
| 1409 | + "author: refused reviews store nothing" | ||
| 1410 | + ); | ||
| 1411 | + let review = fx | ||
| 1412 | + .store | ||
| 1413 | + .submit_review( | ||
| 1414 | + &issue, | ||
| 1415 | + &fx.tahinli.id, | ||
| 1416 | + ReviewState::Commented, | ||
| 1417 | + "note", | ||
| 1418 | + &oid('a'), | ||
| 1419 | + ) | ||
| 1420 | + .await | ||
| 1421 | + .unwrap(); | ||
| 1422 | + assert_eq!( | ||
| 1423 | + review.state, | ||
| 1424 | + ReviewState::Commented, | ||
| 1425 | + "author: comment allowed" | ||
| 1426 | + ); | ||
| 1427 | + | ||
| 1428 | + // A commented review needs text unless it carries a pending comment. | ||
| 1429 | + let issue = new_pull(&fx, "empty").await; | ||
| 1430 | + let err = fx | ||
| 1431 | + .store | ||
| 1432 | + .submit_review(&issue, &fx.bob.id, ReviewState::Commented, " ", &oid('a')) | ||
| 1433 | + .await | ||
| 1434 | + .unwrap_err(); | ||
| 1435 | + assert!( | ||
| 1436 | + matches!(err, Error::Invalid(_)), | ||
| 1437 | + "empty: blank refused: {err:?}" | ||
| 1438 | + ); | ||
| 1439 | + | ||
| 1440 | + fx.store | ||
| 1441 | + .submit_review(&issue, &fx.carol.id, ReviewState::Approved, "", &oid('a')) | ||
| 1442 | + .await | ||
| 1443 | + .unwrap(); | ||
| 1444 | + | ||
| 1445 | + comment(&fx, &issue, &fx.bob.id, true, "draft").await; | ||
| 1446 | + fx.store | ||
| 1447 | + .submit_review(&issue, &fx.bob.id, ReviewState::Commented, "", &oid('a')) | ||
| 1448 | + .await | ||
| 1449 | + .unwrap(); | ||
| 1450 | + assert_eq!( | ||
| 1451 | + fx.store.list_reviews(&issue).await.unwrap().len(), | ||
| 1452 | + 2, | ||
| 1453 | + "empty: approval without body and comment review with draft" | ||
| 1454 | + ); | ||
| 1455 | + | ||
| 1456 | + // The latest state of each reviewer counts; dismissed reviews do not. | ||
| 1457 | + let issue = new_pull(&fx, "summary").await; | ||
| 1333 | fx.store | 1458 | fx.store |
| 1334 | - .submit_review(&fx.issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) | 1459 | + .submit_review(&issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) |
| 1335 | .await | 1460 | .await |
| 1336 | .unwrap(); | 1461 | .unwrap(); |
| 1337 | fx.store | 1462 | fx.store |
| 1338 | .submit_review( | 1463 | .submit_review( |
| 1339 | - &fx.issue, | 1464 | + &issue, |
| 1340 | &fx.bob.id, | 1465 | &fx.bob.id, |
| 1341 | ReviewState::ChangesRequested, | 1466 | ReviewState::ChangesRequested, |
| 1342 | "fix", | 1467 | "fix", |
| @@ -1344,20 +1469,13 @@ mod tests { | |||
| 1344 | ) | 1469 | ) |
| 1345 | .await | 1470 | .await |
| 1346 | .unwrap(); | 1471 | .unwrap(); |
| 1347 | - // Carol approves, then only comments: the approval stands. | ||
| 1348 | fx.store | 1472 | fx.store |
| 1349 | - .submit_review( | 1473 | + .submit_review(&issue, &fx.carol.id, ReviewState::Approved, "", &oid('a')) |
| 1350 | - &fx.issue, | ||
| 1351 | - &fx.carol.id, | ||
| 1352 | - ReviewState::Approved, | ||
| 1353 | - "", | ||
| 1354 | - &oid('a'), | ||
| 1355 | - ) | ||
| 1356 | .await | 1474 | .await |
| 1357 | .unwrap(); | 1475 | .unwrap(); |
| 1358 | fx.store | 1476 | fx.store |
| 1359 | .submit_review( | 1477 | .submit_review( |
| 1360 | - &fx.issue, | 1478 | + &issue, |
| 1361 | &fx.carol.id, | 1479 | &fx.carol.id, |
| 1362 | ReviewState::Commented, | 1480 | ReviewState::Commented, |
| 1363 | "chat", | 1481 | "chat", |
| @@ -1365,11 +1483,10 @@ mod tests { | |||
| 1365 | ) | 1483 | ) |
| 1366 | .await | 1484 | .await |
| 1367 | .unwrap(); | 1485 | .unwrap(); |
| 1368 | - // Dave requests changes, which is then dismissed. | ||
| 1369 | let dave_review = fx | 1486 | let dave_review = fx |
| 1370 | .store | 1487 | .store |
| 1371 | .submit_review( | 1488 | .submit_review( |
| 1372 | - &fx.issue, | 1489 | + &issue, |
| 1373 | &dave.id, | 1490 | &dave.id, |
| 1374 | ReviewState::ChangesRequested, | 1491 | ReviewState::ChangesRequested, |
| 1375 | "no", | 1492 | "no", |
| @@ -1377,42 +1494,50 @@ mod tests { | |||
| 1377 | ) | 1494 | ) |
| 1378 | .await | 1495 | .await |
| 1379 | .unwrap(); | 1496 | .unwrap(); |
| 1380 | - let before = fx.store.review_summary(&fx.issue).await.unwrap(); | 1497 | + let before = fx.store.review_summary(&issue).await.unwrap(); |
| 1381 | - assert_eq!(before.approved_by, vec!["Carol".to_string()]); | 1498 | + assert_eq!( |
| 1499 | + before.approved_by, | ||
| 1500 | + vec!["Carol".to_string()], | ||
| 1501 | + "summary: approvals" | ||
| 1502 | + ); | ||
| 1382 | assert_eq!( | 1503 | assert_eq!( |
| 1383 | before.changes_requested_by, | 1504 | before.changes_requested_by, |
| 1384 | - vec!["Bob".to_string(), "Dave".to_string()] | 1505 | + vec!["Bob".to_string(), "Dave".to_string()], |
| 1506 | + "summary: requests before dismiss" | ||
| 1385 | ); | 1507 | ); |
| 1386 | 1508 | ||
| 1387 | fx.store | 1509 | fx.store |
| 1388 | .dismiss_review(&dave_review.id, &fx.tahinli.id, "wrong branch") | 1510 | .dismiss_review(&dave_review.id, &fx.tahinli.id, "wrong branch") |
| 1389 | .await | 1511 | .await |
| 1390 | .unwrap(); | 1512 | .unwrap(); |
| 1391 | - let summary = fx.store.review_summary(&fx.issue).await.unwrap(); | 1513 | + let summary = fx.store.review_summary(&issue).await.unwrap(); |
| 1392 | - assert_eq!(summary.approved_by, vec!["Carol".to_string()]); | 1514 | + assert_eq!( |
| 1393 | - assert_eq!(summary.changes_requested_by, vec!["Bob".to_string()]); | 1515 | + summary.approved_by, |
| 1394 | - } | 1516 | + vec!["Carol".to_string()], |
| 1517 | + "summary: after dismiss" | ||
| 1518 | + ); | ||
| 1519 | + assert_eq!( | ||
| 1520 | + summary.changes_requested_by, | ||
| 1521 | + vec!["Bob".to_string()], | ||
| 1522 | + "summary: dismissed request gone" | ||
| 1523 | + ); | ||
| 1395 | 1524 | ||
| 1396 | - #[tokio::test] | 1525 | + // Only approvals and change requests can be dismissed, and only once. |
| 1397 | - async fn dismiss_only_approved_or_changes_requested() { | 1526 | + let issue = new_pull(&fx, "dismiss").await; |
| 1398 | - let fx = fixture().await; | 1527 | + let note = comment(&fx, &issue, &fx.bob.id, false, "note").await; |
| 1399 | - let note = comment(&fx, &fx.bob.id, false, "note").await; | ||
| 1400 | let err = fx | 1528 | let err = fx |
| 1401 | .store | 1529 | .store |
| 1402 | .dismiss_review(¬e.review_id, &fx.tahinli.id, "x") | 1530 | .dismiss_review(¬e.review_id, &fx.tahinli.id, "x") |
| 1403 | .await | 1531 | .await |
| 1404 | .unwrap_err(); | 1532 | .unwrap_err(); |
| 1405 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | 1533 | + assert!( |
| 1534 | + matches!(err, Error::Invalid(_)), | ||
| 1535 | + "dismiss: comment refused: {err:?}" | ||
| 1536 | + ); | ||
| 1406 | 1537 | ||
| 1407 | let approved = fx | 1538 | let approved = fx |
| 1408 | .store | 1539 | .store |
| 1409 | - .submit_review( | 1540 | + .submit_review(&issue, &fx.carol.id, ReviewState::Approved, "", &oid('a')) |
| 1410 | - &fx.issue, | ||
| 1411 | - &fx.carol.id, | ||
| 1412 | - ReviewState::Approved, | ||
| 1413 | - "", | ||
| 1414 | - &oid('a'), | ||
| 1415 | - ) | ||
| 1416 | .await | 1541 | .await |
| 1417 | .unwrap(); | 1542 | .unwrap(); |
| 1418 | let err = fx | 1543 | let err = fx |
| @@ -1420,84 +1545,126 @@ mod tests { | |||
| 1420 | .dismiss_review(&approved.id, &fx.tahinli.id, " ") | 1545 | .dismiss_review(&approved.id, &fx.tahinli.id, " ") |
| 1421 | .await | 1546 | .await |
| 1422 | .unwrap_err(); | 1547 | .unwrap_err(); |
| 1423 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | 1548 | + assert!( |
| 1549 | + matches!(err, Error::Invalid(_)), | ||
| 1550 | + "dismiss: blank reason refused: {err:?}" | ||
| 1551 | + ); | ||
| 1424 | let err = fx | 1552 | let err = fx |
| 1425 | .store | 1553 | .store |
| 1426 | .dismiss_review("missing", &fx.tahinli.id, "x") | 1554 | .dismiss_review("missing", &fx.tahinli.id, "x") |
| 1427 | .await | 1555 | .await |
| 1428 | .unwrap_err(); | 1556 | .unwrap_err(); |
| 1429 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | 1557 | + assert!( |
| 1558 | + matches!(err, Error::NotFound), | ||
| 1559 | + "dismiss: missing refused: {err:?}" | ||
| 1560 | + ); | ||
| 1430 | 1561 | ||
| 1431 | fx.store | 1562 | fx.store |
| 1432 | .dismiss_review(&approved.id, &fx.tahinli.id, "stale") | 1563 | .dismiss_review(&approved.id, &fx.tahinli.id, "stale") |
| 1433 | .await | 1564 | .await |
| 1434 | .unwrap(); | 1565 | .unwrap(); |
| 1435 | - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); | 1566 | + let reviews = fx.store.list_reviews(&issue).await.unwrap(); |
| 1436 | let dismissed = reviews.iter().find(|r| r.id == approved.id).unwrap(); | 1567 | let dismissed = reviews.iter().find(|r| r.id == approved.id).unwrap(); |
| 1437 | - assert_eq!(dismissed.state, ReviewState::Dismissed); | 1568 | + assert_eq!(dismissed.state, ReviewState::Dismissed, "dismiss: state"); |
| 1438 | - assert_eq!(dismissed.dismissed_reason.as_deref(), Some("stale")); | 1569 | + assert_eq!( |
| 1570 | + dismissed.dismissed_reason.as_deref(), | ||
| 1571 | + Some("stale"), | ||
| 1572 | + "dismiss: reason" | ||
| 1573 | + ); | ||
| 1574 | + assert_eq!( | ||
| 1575 | + dismissed.dismissed_from, | ||
| 1576 | + Some(ReviewState::Approved), | ||
| 1577 | + "dismiss: remembers approval" | ||
| 1578 | + ); | ||
| 1439 | 1579 | ||
| 1440 | let events: Vec<_> = fx | 1580 | let events: Vec<_> = fx |
| 1441 | .store | 1581 | .store |
| 1442 | - .list_events(&fx.issue) | 1582 | + .list_events(&issue) |
| 1443 | .await | 1583 | .await |
| 1444 | .unwrap() | 1584 | .unwrap() |
| 1445 | .into_iter() | 1585 | .into_iter() |
| 1446 | .filter(|e| e.kind == "review_dismissed") | 1586 | .filter(|e| e.kind == "review_dismissed") |
| 1447 | .collect(); | 1587 | .collect(); |
| 1448 | - assert_eq!(events.len(), 1); | 1588 | + assert_eq!(events.len(), 1, "dismiss: one event"); |
| 1449 | assert_eq!( | 1589 | assert_eq!( |
| 1450 | events[0].data, | 1590 | events[0].data, |
| 1451 | - serde_json::json!({"review": approved.id, "reason": "stale"}) | 1591 | + serde_json::json!({"review": approved.id, "reason": "stale"}), |
| 1592 | + "dismiss: event data" | ||
| 1593 | + ); | ||
| 1594 | + assert_eq!( | ||
| 1595 | + events[0].actor_id.as_deref(), | ||
| 1596 | + Some(fx.tahinli.id.as_str()), | ||
| 1597 | + "dismiss: actor" | ||
| 1452 | ); | 1598 | ); |
| 1453 | - assert_eq!(events[0].actor_id.as_deref(), Some(fx.tahinli.id.as_str())); | ||
| 1454 | 1599 | ||
| 1455 | - // Dismissing twice is refused. | ||
| 1456 | let err = fx | 1600 | let err = fx |
| 1457 | .store | 1601 | .store |
| 1458 | .dismiss_review(&approved.id, &fx.tahinli.id, "again") | 1602 | .dismiss_review(&approved.id, &fx.tahinli.id, "again") |
| 1459 | .await | 1603 | .await |
| 1460 | .unwrap_err(); | 1604 | .unwrap_err(); |
| 1461 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | 1605 | + assert!( |
| 1462 | - } | 1606 | + matches!(err, Error::Invalid(_)), |
| 1607 | + "dismiss: twice refused: {err:?}" | ||
| 1608 | + ); | ||
| 1463 | 1609 | ||
| 1464 | - #[tokio::test] | 1610 | + // A review written before the previous-state column has none. |
| 1465 | - async fn discard_pending_removes_its_comments() { | 1611 | + { |
| 1466 | - let fx = fixture().await; | 1612 | + let conn = fx.store.inner.conn.lock().await; |
| 1467 | - let submitted = comment(&fx, &fx.bob.id, false, "done").await; | 1613 | + conn.execute( |
| 1468 | - let d1 = comment(&fx, &fx.bob.id, true, "d1").await; | 1614 | + "UPDATE pull_reviews SET dismissed_from = NULL WHERE id = ?1", |
| 1469 | - let d2 = comment(&fx, &fx.bob.id, true, "d2").await; | 1615 | + (approved.id.clone(),), |
| 1616 | + ) | ||
| 1617 | + .await | ||
| 1618 | + .unwrap(); | ||
| 1619 | + } | ||
| 1620 | + let listed = fx.store.list_reviews(&issue).await.unwrap(); | ||
| 1621 | + let legacy = listed.iter().find(|r| r.id == approved.id).unwrap(); | ||
| 1622 | + assert_eq!(legacy.dismissed_from, None, "legacy: no previous state"); | ||
| 1623 | + | ||
| 1624 | + // Discarding a draft keeps submitted comments. | ||
| 1625 | + let issue = new_pull(&fx, "discard").await; | ||
| 1626 | + let submitted = comment(&fx, &issue, &fx.bob.id, false, "done").await; | ||
| 1627 | + let d1 = comment(&fx, &issue, &fx.bob.id, true, "d1").await; | ||
| 1628 | + let d2 = comment(&fx, &issue, &fx.bob.id, true, "d2").await; | ||
| 1470 | 1629 | ||
| 1471 | fx.store | 1630 | fx.store |
| 1472 | - .discard_pending_review(&fx.issue, &fx.bob.id) | 1631 | + .discard_pending_review(&issue, &fx.bob.id) |
| 1473 | .await | 1632 | .await |
| 1474 | .unwrap(); | 1633 | .unwrap(); |
| 1475 | assert!( | 1634 | assert!( |
| 1476 | fx.store | 1635 | fx.store |
| 1477 | - .pending_review(&fx.issue, &fx.bob.id) | 1636 | + .pending_review(&issue, &fx.bob.id) |
| 1478 | .await | 1637 | .await |
| 1479 | .unwrap() | 1638 | .unwrap() |
| 1480 | - .is_none() | 1639 | + .is_none(), |
| 1640 | + "discard: no draft left" | ||
| 1641 | + ); | ||
| 1642 | + assert!( | ||
| 1643 | + fx.store.review_comment(&d1.id).await.unwrap().is_none(), | ||
| 1644 | + "discard: first draft comment gone" | ||
| 1645 | + ); | ||
| 1646 | + assert!( | ||
| 1647 | + fx.store.review_comment(&d2.id).await.unwrap().is_none(), | ||
| 1648 | + "discard: second draft comment gone" | ||
| 1481 | ); | 1649 | ); |
| 1482 | - assert!(fx.store.review_comment(&d1.id).await.unwrap().is_none()); | ||
| 1483 | - assert!(fx.store.review_comment(&d2.id).await.unwrap().is_none()); | ||
| 1484 | assert!( | 1650 | assert!( |
| 1485 | fx.store | 1651 | fx.store |
| 1486 | .review_comment(&submitted.id) | 1652 | .review_comment(&submitted.id) |
| 1487 | .await | 1653 | .await |
| 1488 | .unwrap() | 1654 | .unwrap() |
| 1489 | - .is_some() | 1655 | + .is_some(), |
| 1656 | + "discard: submitted comment kept" | ||
| 1490 | ); | 1657 | ); |
| 1491 | let own = fx | 1658 | let own = fx |
| 1492 | .store | 1659 | .store |
| 1493 | - .review_threads(&fx.issue, Some(&fx.bob.id)) | 1660 | + .review_threads(&issue, Some(&fx.bob.id)) |
| 1494 | .await | 1661 | .await |
| 1495 | .unwrap(); | 1662 | .unwrap(); |
| 1496 | - assert_eq!(own.len(), 1); | 1663 | + assert_eq!(own.len(), 1, "discard: only the submitted thread"); |
| 1497 | 1664 | ||
| 1498 | // Nothing pending left: discarding again is fine. | 1665 | // Nothing pending left: discarding again is fine. |
| 1499 | fx.store | 1666 | fx.store |
| 1500 | - .discard_pending_review(&fx.issue, &fx.bob.id) | 1667 | + .discard_pending_review(&issue, &fx.bob.id) |
| 1501 | .await | 1668 | .await |
| 1502 | .unwrap(); | 1669 | .unwrap(); |
| 1503 | } | 1670 | } |
| @@ -1505,12 +1672,13 @@ mod tests { | |||
| 1505 | #[tokio::test] | 1672 | #[tokio::test] |
| 1506 | async fn delete_repo_cleans_up_reviews() { | 1673 | async fn delete_repo_cleans_up_reviews() { |
| 1507 | let fx = fixture().await; | 1674 | let fx = fixture().await; |
| 1508 | - let root = comment(&fx, &fx.bob.id, false, "root").await; | 1675 | + let issue = new_pull(&fx, "main").await; |
| 1509 | - reply(&fx, &fx.carol.id, &root.id, "reply").await; | 1676 | + let root = comment(&fx, &issue, &fx.bob.id, false, "root").await; |
| 1510 | - comment(&fx, &fx.carol.id, true, "draft").await; | 1677 | + reply(&fx, &issue, &fx.carol.id, &root.id, "reply").await; |
| 1678 | + comment(&fx, &issue, &fx.carol.id, true, "draft").await; | ||
| 1511 | fx.store | 1679 | fx.store |
| 1512 | .submit_review( | 1680 | .submit_review( |
| 1513 | - &fx.issue, | 1681 | + &issue, |
| 1514 | &fx.tahinli.id, | 1682 | &fx.tahinli.id, |
| 1515 | ReviewState::Commented, | 1683 | ReviewState::Commented, |
| 1516 | "hi", | 1684 | "hi", |
| @@ -1519,7 +1687,7 @@ mod tests { | |||
| 1519 | .await | 1687 | .await |
| 1520 | .unwrap(); | 1688 | .unwrap(); |
| 1521 | fx.store | 1689 | fx.store |
| 1522 | - .submit_review(&fx.issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) | 1690 | + .submit_review(&issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) |
| 1523 | .await | 1691 | .await |
| 1524 | .unwrap(); | 1692 | .unwrap(); |
| 1525 | fx.store | 1693 | fx.store |
| @@ -1557,97 +1725,4 @@ mod tests { | |||
| 1557 | } | 1725 | } |
| 1558 | assert_eq!(ReviewState::ChangesRequested.as_str(), "changes_requested"); | 1726 | assert_eq!(ReviewState::ChangesRequested.as_str(), "changes_requested"); |
| 1559 | } | 1727 | } |
| 1560 | - | ||
| 1561 | - #[tokio::test] | ||
| 1562 | - async fn dismissed_review_remembers_its_previous_state() { | ||
| 1563 | - let fx = fixture().await; | ||
| 1564 | - let approved = fx | ||
| 1565 | - .store | ||
| 1566 | - .submit_review( | ||
| 1567 | - &fx.issue, | ||
| 1568 | - &fx.carol.id, | ||
| 1569 | - ReviewState::Approved, | ||
| 1570 | - "", | ||
| 1571 | - &oid('a'), | ||
| 1572 | - ) | ||
| 1573 | - .await | ||
| 1574 | - .unwrap(); | ||
| 1575 | - assert_eq!(approved.dismissed_from, None); | ||
| 1576 | - fx.store | ||
| 1577 | - .dismiss_review(&approved.id, &fx.tahinli.id, "stale") | ||
| 1578 | - .await | ||
| 1579 | - .unwrap(); | ||
| 1580 | - let listed = fx.store.list_reviews(&fx.issue).await.unwrap(); | ||
| 1581 | - let dismissed = listed.iter().find(|r| r.id == approved.id).unwrap(); | ||
| 1582 | - assert_eq!(dismissed.state, ReviewState::Dismissed); | ||
| 1583 | - assert_eq!(dismissed.dismissed_from, Some(ReviewState::Approved)); | ||
| 1584 | - assert_eq!(dismissed.dismissed_reason.as_deref(), Some("stale")); | ||
| 1585 | - | ||
| 1586 | - // A row written before the column existed has no previous state. | ||
| 1587 | - { | ||
| 1588 | - let conn = fx.store.inner.conn.lock().await; | ||
| 1589 | - conn.execute( | ||
| 1590 | - "UPDATE pull_reviews SET dismissed_from = NULL WHERE id = ?1", | ||
| 1591 | - (approved.id.clone(),), | ||
| 1592 | - ) | ||
| 1593 | - .await | ||
| 1594 | - .unwrap(); | ||
| 1595 | - } | ||
| 1596 | - let listed = fx.store.list_reviews(&fx.issue).await.unwrap(); | ||
| 1597 | - let legacy = listed.iter().find(|r| r.id == approved.id).unwrap(); | ||
| 1598 | - assert_eq!(legacy.dismissed_from, None); | ||
| 1599 | - } | ||
| 1600 | - | ||
| 1601 | - #[tokio::test] | ||
| 1602 | - async fn relocate_moves_root_and_replies_keeping_hunk() { | ||
| 1603 | - let fx = fixture().await; | ||
| 1604 | - let root = fx | ||
| 1605 | - .store | ||
| 1606 | - .add_review_comment( | ||
| 1607 | - &fx.issue, | ||
| 1608 | - &fx.bob.id, | ||
| 1609 | - false, | ||
| 1610 | - "src/lib.rs", | ||
| 1611 | - Side::New, | ||
| 1612 | - 3, | ||
| 1613 | - &oid('a'), | ||
| 1614 | - "+let x = 1;", | ||
| 1615 | - "root", | ||
| 1616 | - ) | ||
| 1617 | - .await | ||
| 1618 | - .unwrap(); | ||
| 1619 | - let answer = reply(&fx, &fx.tahinli.id, &root.id, "answer").await; | ||
| 1620 | - | ||
| 1621 | - fx.store | ||
| 1622 | - .relocate_review_comment(&root.id, 7, &oid('c')) | ||
| 1623 | - .await | ||
| 1624 | - .unwrap(); | ||
| 1625 | - | ||
| 1626 | - let moved = fx.store.review_comment(&root.id).await.unwrap().unwrap(); | ||
| 1627 | - assert_eq!(moved.line, 7); | ||
| 1628 | - assert_eq!(moved.commit_oid, oid('c')); | ||
| 1629 | - assert_eq!(moved.diff_hunk, "+let x = 1;"); | ||
| 1630 | - let reply_after = fx.store.review_comment(&answer.id).await.unwrap().unwrap(); | ||
| 1631 | - assert_eq!(reply_after.line, 7); | ||
| 1632 | - assert_eq!(reply_after.commit_oid, oid('c')); | ||
| 1633 | - | ||
| 1634 | - let err = fx | ||
| 1635 | - .store | ||
| 1636 | - .relocate_review_comment(&answer.id, 2, &oid('c')) | ||
| 1637 | - .await | ||
| 1638 | - .unwrap_err(); | ||
| 1639 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | ||
| 1640 | - let err = fx | ||
| 1641 | - .store | ||
| 1642 | - .relocate_review_comment("missing", 2, &oid('c')) | ||
| 1643 | - .await | ||
| 1644 | - .unwrap_err(); | ||
| 1645 | - assert!(matches!(err, Error::NotFound), "{err:?}"); | ||
| 1646 | - let err = fx | ||
| 1647 | - .store | ||
| 1648 | - .relocate_review_comment(&root.id, 0, &oid('c')) | ||
| 1649 | - .await | ||
| 1650 | - .unwrap_err(); | ||
| 1651 | - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); | ||
| 1652 | - } | ||
| 1653 | } | 1728 | } |