diff --git a/crates/forge-core/src/store/commit_status.rs b/crates/forge-core/src/store/commit_status.rs index ba2a733..71c5f08 100644 --- a/crates/forge-core/src/store/commit_status.rs +++ b/crates/forge-core/src/store/commit_status.rs @@ -378,9 +378,21 @@ mod tests { (dir, store, repo.id, owner.id) } + // One store and owner serve every scenario. Each scenario that writes + // statuses gets its own repo so the scenarios cannot see each other. + async fn new_repo(store: &Store, owner_id: &str, name: &str) -> String { + store + .create_repo(owner_id, name, "", Visibility::Public, "main") + .await + .expect("repo") + .id + } + #[tokio::test] - async fn upsert_keeps_one_row_per_context_and_combines() { + async fn status_scenarios_share_one_store() { let (_dir, store, repo_id, user_id) = fixture().await; + + // Upsert: one row per context, the latest report wins, states combine. let set = |context: &'static str, state: StatusState| { let store = store.clone(); let repo_id = repo_id.clone(); @@ -396,36 +408,42 @@ mod tests { set("b", StatusState::Success).await; assert_eq!( store.combined_state(&repo_id, SHA_A).await.unwrap(), - Some(StatusState::Pending) + Some(StatusState::Pending), + "upsert: pending wins over success" ); set("a", StatusState::Failure).await; assert_eq!( store.combined_state(&repo_id, SHA_A).await.unwrap(), - Some(StatusState::Failure) + Some(StatusState::Failure), + "upsert: failure wins over success" ); // The latest report for "a" replaces the failure. let replaced = set("a", StatusState::Success).await; - assert_eq!(replaced.state, StatusState::Success); + assert_eq!(replaced.state, StatusState::Success, "upsert: stored state"); assert_eq!( store.combined_state(&repo_id, SHA_A).await.unwrap(), - Some(StatusState::Success) + Some(StatusState::Success), + "upsert: latest report replaces the failure" ); let rows = store.statuses_for(&repo_id, SHA_A).await.unwrap(); assert_eq!( rows.iter().map(|r| r.context.as_str()).collect::>(), - ["a", "b"] + ["a", "b"], + "upsert: one row per context" + ); + assert_eq!( + store.combined_state(&repo_id, SHA_B).await.unwrap(), + None, + "upsert: unreported commit has no state" ); - assert_eq!(store.combined_state(&repo_id, SHA_B).await.unwrap(), None); - } - #[tokio::test] - async fn many_lookup_skips_commits_without_statuses() { - let (_dir, store, repo_id, user_id) = fixture().await; + // Many-lookup skips commits without statuses. + let many_repo = new_repo(&store, &user_id, "many").await; store .set_status( - &repo_id, + &many_repo, SHA_A, "ci", StatusState::Error, @@ -436,26 +454,29 @@ mod tests { .await .expect("set"); let found = store - .statuses_for_many(&repo_id, &[SHA_A, SHA_B]) + .statuses_for_many(&many_repo, &[SHA_A, SHA_B]) .await .unwrap(); - assert_eq!(found.len(), 1); - assert_eq!(found.get(SHA_A), Some(&StatusState::Failure)); + assert_eq!(found.len(), 1, "many: only the reported commit"); + assert_eq!( + found.get(SHA_A), + Some(&StatusState::Failure), + "many: error maps to failure" + ); assert!( store - .statuses_for_many(&repo_id, &[]) + .statuses_for_many(&many_repo, &[]) .await .unwrap() - .is_empty() + .is_empty(), + "many: empty input gives nothing" ); - } - #[tokio::test] - async fn invalid_input_is_refused_and_stores_nothing() { - let (_dir, store, repo_id, user_id) = fixture().await; + // Invalid input is refused and stores nothing. + let invalid_repo = new_repo(&store, &user_id, "invalid").await; let bad_sha = store .set_status( - &repo_id, + &invalid_repo, "ABC", "ci", StatusState::Success, @@ -464,10 +485,13 @@ mod tests { &user_id, ) .await; - assert!(matches!(bad_sha, Err(Error::Invalid(_)))); + assert!( + matches!(bad_sha, Err(Error::Invalid(_))), + "invalid: short sha" + ); let bad_url = store .set_status( - &repo_id, + &invalid_repo, SHA_A, "ci", StatusState::Success, @@ -476,22 +500,24 @@ mod tests { &user_id, ) .await; - assert!(matches!(bad_url, Err(Error::Invalid(_)))); + assert!( + matches!(bad_url, Err(Error::Invalid(_))), + "invalid: ftp url" + ); assert!( store - .statuses_for(&repo_id, SHA_A) + .statuses_for(&invalid_repo, SHA_A) .await .unwrap() - .is_empty() + .is_empty(), + "invalid: nothing stored" ); - } - #[tokio::test] - async fn deleting_the_repo_removes_its_statuses() { - let (_dir, store, repo_id, user_id) = fixture().await; + // Deleting the repo removes its statuses. + let doomed_repo = new_repo(&store, &user_id, "doomed").await; store .set_status( - &repo_id, + &doomed_repo, SHA_A, "ci", StatusState::Success, @@ -501,26 +527,25 @@ mod tests { ) .await .expect("set"); - store.delete_repo(&repo_id).await.expect("delete"); + store.delete_repo(&doomed_repo).await.expect("delete"); assert!( store - .statuses_for(&repo_id, SHA_A) + .statuses_for(&doomed_repo, SHA_A) .await .unwrap() - .is_empty() + .is_empty(), + "delete repo: statuses gone" ); - } - #[tokio::test] - async fn deleting_a_user_reassigns_their_statuses_to_ghost() { - let (_dir, store, repo_id, _owner_id) = fixture().await; + // Deleting a user reassigns their statuses to the ghost user. + let reassigned_repo = new_repo(&store, &user_id, "reassigned").await; let reporter = store .create_user("ci-bot", None, "not-a-real-hash", false) .await .expect("user"); store .set_status( - &repo_id, + &reassigned_repo, SHA_A, "ci", StatusState::Failure, @@ -535,9 +560,16 @@ mod tests { .delete_user(&reporter.id, &ghost.id) .await .expect("delete user"); - let rows = store.statuses_for(&repo_id, SHA_A).await.unwrap(); - assert_eq!(rows.len(), 1); - assert_eq!(rows[0].creator_id, ghost.id); - assert_eq!(rows[0].state, StatusState::Failure); + let rows = store.statuses_for(&reassigned_repo, SHA_A).await.unwrap(); + assert_eq!(rows.len(), 1, "delete user: status kept"); + assert_eq!( + rows[0].creator_id, ghost.id, + "delete user: reassigned to ghost" + ); + assert_eq!( + rows[0].state, + StatusState::Failure, + "delete user: state kept" + ); } } diff --git a/crates/forge-core/src/store/pulls.rs b/crates/forge-core/src/store/pulls.rs index a3ab25a..185705d 100644 --- a/crates/forge-core/src/store/pulls.rs +++ b/crates/forge-core/src/store/pulls.rs @@ -647,6 +647,14 @@ mod tests { } } + /// A fresh repo owned by Tahinli, so each scenario has its own numbering and counts. + async fn new_repo(fx: &Fixture, name: &str) -> Repo { + fx.store + .create_repo(&fx.tahinli.id, name, "", Visibility::Public, "main") + .await + .unwrap() + } + fn all_states() -> IssueFilter { IssueFilter { state: None, @@ -655,16 +663,17 @@ mod tests { } } - /// A pull from `head_repo` (its own repo, for same-repo pulls) into `main` of `fx.repo`. + /// A pull from `head_repo` into `main` of `repo`. async fn open_pull( fx: &Fixture, + repo: &Repo, head_repo: &str, head_branch: &str, title: &str, ) -> PullRequest { fx.store .create_pull( - &fx.repo.id, + &repo.id, &fx.tahinli.id, title, "", @@ -678,93 +687,111 @@ mod tests { .unwrap() } + // Numbering, counts, head and mergeability updates, merges, drafts, + // auto merge and events. Each scenario has its own repo. #[tokio::test] - async fn issues_and_pulls_share_one_numbering() { + async fn pull_scenarios_share_one_store() { let fx = fixture().await; + + // Issues and pulls share one numbering; each list shows only its kind. + let repo = new_repo(&fx, "numbering").await; let issue1 = fx .store - .create_issue(&fx.repo.id, &fx.tahinli.id, "first", "") + .create_issue(&repo.id, &fx.tahinli.id, "first", "") .await .unwrap(); - let pull2 = open_pull(&fx, &fx.repo.id, "feature", "second").await; + let pull2 = open_pull(&fx, &repo, &repo.id, "feature", "second").await; let issue3 = fx .store - .create_issue(&fx.repo.id, &fx.tahinli.id, "third", "") + .create_issue(&repo.id, &fx.tahinli.id, "third", "") .await .unwrap(); assert_eq!( (issue1.number, pull2.issue.number, issue3.number), - (1, 2, 3) + (1, 2, 3), + "numbering: shared sequence" + ); + assert_eq!( + pull2.mergeable, + Mergeable::Unknown, + "numbering: new pull unknown" + ); + assert_eq!( + pull2.head_repo_id.as_deref(), + Some(repo.id.as_str()), + "numbering: head repo" ); - assert_eq!(pull2.mergeable, Mergeable::Unknown); - assert_eq!(pull2.head_repo_id.as_deref(), Some(fx.repo.id.as_str())); - // Kind filtering: issues never list pulls, and pulls never list issues. - let (issues, _) = fx - .store - .list_issues(&fx.repo.id, &all_states()) - .await - .unwrap(); + let (issues, _) = fx.store.list_issues(&repo.id, &all_states()).await.unwrap(); let numbers: Vec = issues.iter().map(|i| i.issue.number).collect(); - assert_eq!(numbers, vec![1, 3]); - let (pulls, total) = fx - .store - .list_pulls(&fx.repo.id, &all_states()) - .await - .unwrap(); - assert_eq!(total, 1); - assert_eq!(pulls[0].issue.number, 2); + assert_eq!(numbers, vec![1, 3], "numbering: issues never list pulls"); + let (pulls, total) = fx.store.list_pulls(&repo.id, &all_states()).await.unwrap(); + assert_eq!(total, 1, "numbering: pulls never list issues"); + assert_eq!(pulls[0].issue.number, 2, "numbering: only the pull"); assert!( fx.store - .issue_by_number(&fx.repo.id, 2) + .issue_by_number(&repo.id, 2) .await .unwrap() - .is_none() + .is_none(), + "numbering: pull number is not an issue" ); assert!( fx.store - .pull_by_number(&fx.repo.id, 1) + .pull_by_number(&repo.id, 1) .await .unwrap() - .is_none() + .is_none(), + "numbering: issue number is not a pull" ); assert_eq!( - fx.store.pull_by_number(&fx.repo.id, 2).await.unwrap(), - Some(pull2) + fx.store.pull_by_number(&repo.id, 2).await.unwrap(), + Some(pull2), + "numbering: pull reads back" ); - } - #[tokio::test] - async fn counts_and_lists_filter_by_kind() { - let fx = fixture().await; + // Counts: a merged pull counts as closed; issue counts are untouched. + let repo = new_repo(&fx, "counts").await; fx.store - .create_issue(&fx.repo.id, &fx.tahinli.id, "issue", "") + .create_issue(&repo.id, &fx.tahinli.id, "issue", "") .await .unwrap(); - let pull = open_pull(&fx, &fx.repo.id, "one", "pull one").await; - open_pull(&fx, &fx.repo.id, "two", "pull two").await; - assert_eq!(fx.store.issue_counts(&fx.repo.id).await.unwrap(), (1, 0)); - assert_eq!(fx.store.pull_counts(&fx.repo.id).await.unwrap(), (2, 0)); + let pull = open_pull(&fx, &repo, &repo.id, "one", "pull one").await; + open_pull(&fx, &repo, &repo.id, "two", "pull two").await; + assert_eq!( + fx.store.issue_counts(&repo.id).await.unwrap(), + (1, 0), + "counts: before merge issues" + ); + assert_eq!( + fx.store.pull_counts(&repo.id).await.unwrap(), + (2, 0), + "counts: before merge pulls" + ); let id = pull.issue.id.clone(); fx.store .mark_merged(&id, &fx.bob.id, &"c".repeat(40)) .await .unwrap(); - // A merged pull counts as closed, and issue counts are untouched. - assert_eq!(fx.store.pull_counts(&fx.repo.id).await.unwrap(), (1, 1)); - assert_eq!(fx.store.issue_counts(&fx.repo.id).await.unwrap(), (1, 0)); - + assert_eq!( + fx.store.pull_counts(&repo.id).await.unwrap(), + (1, 1), + "counts: merged pull closed" + ); + assert_eq!( + fx.store.issue_counts(&repo.id).await.unwrap(), + (1, 0), + "counts: issues untouched" + ); let open_only = IssueFilter::default(); - let (open, total) = fx.store.list_pulls(&fx.repo.id, &open_only).await.unwrap(); - assert_eq!(total, 1); - assert_eq!(open[0].issue.title, "pull two"); - } + let (open, total) = fx.store.list_pulls(&repo.id, &open_only).await.unwrap(); + assert_eq!(total, 1, "counts: open filter total"); + assert_eq!(open[0].issue.title, "pull two", "counts: open filter row"); - #[tokio::test] - async fn update_pull_head_resets_mergeable() { - let fx = fixture().await; - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; + // A head push resets mergeability and keeps the base. + let repo = new_repo(&fx, "head").await; + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; let id = pull.issue.id.clone(); fx.store .set_mergeability( @@ -781,21 +808,18 @@ mod tests { fx.store.update_pull_head(&id, &new_head).await.unwrap(); let after = fx .store - .pull_by_number(&fx.repo.id, pull.issue.number) + .pull_by_number(&repo.id, pull.issue.number) .await .unwrap() .unwrap(); - assert_eq!(after.head_oid, new_head); - assert_eq!(after.mergeable, Mergeable::Unknown); - assert!(after.conflict_files.is_empty()); - // The base was not touched by a head push. - assert_eq!(after.base_oid, "b".repeat(40)); - } - - #[tokio::test] - async fn set_mergeability_round_trips_conflict_files() { - let fx = fixture().await; - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; + assert_eq!(after.head_oid, new_head, "head: new head"); + assert_eq!(after.mergeable, Mergeable::Unknown, "head: mergeable reset"); + assert!(after.conflict_files.is_empty(), "head: conflicts cleared"); + assert_eq!(after.base_oid, "b".repeat(40), "head: base untouched"); + + // Mergeability round-trips conflict files; Merged is set only by mark_merged. + let repo = new_repo(&fx, "mergeability").await; + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; let id = pull.issue.id.clone(); let files = vec!["src/lib.rs".to_string(), "dir/with space.md".to_string()]; let base = "f".repeat(40); @@ -811,14 +835,18 @@ mod tests { .unwrap(); let got = fx .store - .pull_by_number(&fx.repo.id, pull.issue.number) + .pull_by_number(&repo.id, pull.issue.number) .await .unwrap() .unwrap(); - assert_eq!(got.mergeable, Mergeable::Conflict); - assert_eq!(got.conflict_files, files); - assert_eq!(got.base_oid, base); - assert_eq!(got.merge_base, Some("1".repeat(40))); + assert_eq!(got.mergeable, Mergeable::Conflict, "mergeability: conflict"); + assert_eq!(got.conflict_files, files, "mergeability: files round trip"); + assert_eq!(got.base_oid, base, "mergeability: base stored"); + assert_eq!( + got.merge_base, + Some("1".repeat(40)), + "mergeability: merge base" + ); fx.store .set_mergeability(&id, &base, None, Mergeable::Clean, &[]) @@ -826,48 +854,53 @@ mod tests { .unwrap(); let got = fx .store - .pull_by_number(&fx.repo.id, pull.issue.number) + .pull_by_number(&repo.id, pull.issue.number) .await .unwrap() .unwrap(); - assert_eq!(got.mergeable, Mergeable::Clean); - assert!(got.conflict_files.is_empty()); - assert_eq!(got.merge_base, None); + assert_eq!(got.mergeable, Mergeable::Clean, "mergeability: clean"); + assert!(got.conflict_files.is_empty(), "mergeability: files cleared"); + assert_eq!(got.merge_base, None, "mergeability: merge base cleared"); - // Merged is set only through mark_merged, and a missing pull is NotFound. - assert!(matches!( - fx.store - .set_mergeability(&id, &base, None, Mergeable::Merged, &[]) - .await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store - .set_mergeability("no-such-issue", &base, None, Mergeable::Clean, &[]) - .await, - Err(Error::NotFound) - )); - } + assert!( + matches!( + fx.store + .set_mergeability(&id, &base, None, Mergeable::Merged, &[]) + .await, + Err(Error::Invalid(_)) + ), + "mergeability: merged refused" + ); + assert!( + matches!( + fx.store + .set_mergeability("no-such-issue", &base, None, Mergeable::Clean, &[]) + .await, + Err(Error::NotFound) + ), + "mergeability: missing pull" + ); - #[tokio::test] - async fn mark_merged_closes_records_event_and_is_final() { - let fx = fixture().await; + // Merging closes the pull, records an event, and is final. + let repo = new_repo(&fx, "merge").await; let issue = fx .store - .create_issue(&fx.repo.id, &fx.tahinli.id, "plain issue", "") + .create_issue(&repo.id, &fx.tahinli.id, "plain issue", "") .await .unwrap(); - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; let id = pull.issue.id.clone(); let merge_commit = "c".repeat(40); - // An issue id is not a pull request. - assert!(matches!( - fx.store - .mark_merged(&issue.id, &fx.bob.id, &merge_commit) - .await, - Err(Error::NotFound) - )); + assert!( + matches!( + fx.store + .mark_merged(&issue.id, &fx.bob.id, &merge_commit) + .await, + Err(Error::NotFound) + ), + "merge: an issue id is not a pull" + ); fx.store .mark_merged(&id, &fx.bob.id, &merge_commit) @@ -875,99 +908,120 @@ mod tests { .unwrap(); let got = fx .store - .pull_by_number(&fx.repo.id, pull.issue.number) + .pull_by_number(&repo.id, pull.issue.number) .await .unwrap() .unwrap(); - assert_eq!(got.issue.state, IssueState::Closed); + assert_eq!(got.issue.state, IssueState::Closed, "merge: closed"); assert_eq!( got.issue.state_reason, - Some(crate::issues::StateReason::Completed) + Some(crate::issues::StateReason::Completed), + "merge: completed" + ); + assert!(got.issue.closed_at.is_some(), "merge: closed time"); + assert_eq!(got.mergeable, Mergeable::Merged, "merge: merged state"); + assert!(got.merged_at.is_some(), "merge: merged time"); + assert_eq!( + got.merged_by.as_deref(), + Some(fx.bob.id.as_str()), + "merge: merged by" + ); + assert_eq!( + got.merge_commit.as_deref(), + Some(merge_commit.as_str()), + "merge: merge commit" ); - assert!(got.issue.closed_at.is_some()); - assert_eq!(got.mergeable, Mergeable::Merged); - assert!(got.merged_at.is_some()); - assert_eq!(got.merged_by.as_deref(), Some(fx.bob.id.as_str())); - assert_eq!(got.merge_commit.as_deref(), Some(merge_commit.as_str())); let events = fx.store.list_events(&id).await.unwrap(); let merged: Vec<_> = events.iter().filter(|e| e.kind == "merged").collect(); - assert_eq!(merged.len(), 1); - assert_eq!(merged[0].data["commit"], merge_commit.as_str()); - assert_eq!(merged[0].actor_id.as_deref(), Some(fx.bob.id.as_str())); - - // A second merge is refused. A late mergeability recompute is a no-op on a merged pull. - assert!(matches!( - fx.store.mark_merged(&id, &fx.bob.id, &merge_commit).await, - Err(Error::Conflict(_)) - )); + assert_eq!(merged.len(), 1, "merge: one event"); + assert_eq!( + merged[0].data["commit"], + merge_commit.as_str(), + "merge: event commit" + ); + assert_eq!( + merged[0].actor_id.as_deref(), + Some(fx.bob.id.as_str()), + "merge: event actor" + ); + + // A second merge is refused, and a late mergeability recompute is a no-op. + assert!( + matches!( + fx.store.mark_merged(&id, &fx.bob.id, &merge_commit).await, + Err(Error::Conflict(_)) + ), + "merge: second merge refused" + ); fx.store .set_mergeability(&id, &"b".repeat(40), None, Mergeable::Clean, &[]) .await .unwrap(); let still = fx .store - .pull_by_number(&fx.repo.id, pull.issue.number) + .pull_by_number(&repo.id, pull.issue.number) .await .unwrap() .unwrap(); - assert_eq!(still.mergeable, Mergeable::Merged); - } + assert_eq!(still.mergeable, Mergeable::Merged, "merge: stays merged"); - #[tokio::test] - async fn create_pull_refuses_a_second_open_pull_for_the_same_comparison() { - let fx = fixture().await; - let first = open_pull(&fx, &fx.repo.id, "feature", "first").await; + // A second open pull for the same comparison is refused, and takes no number. + let repo = new_repo(&fx, "refuse").await; + let first = open_pull(&fx, &repo, &repo.id, "feature", "first").await; let second = fx .store .create_pull( - &fx.repo.id, + &repo.id, &fx.bob.id, "second", "", "main", - &fx.repo.id, + &repo.id, "feature", &"c".repeat(40), &"b".repeat(40), ) .await; match second { - Err(Error::Conflict(msg)) => assert!(msg.contains(&format!("#{}", first.issue.number))), - other => panic!("expected conflict, got {other:?}"), + Err(Error::Conflict(msg)) => assert!( + msg.contains(&format!("#{}", first.issue.number)), + "refuse: conflict names the open pull" + ), + other => panic!("refuse: expected conflict, got {other:?}"), } - // The refused attempt took no number and wrote no row. - let next = open_pull(&fx, &fx.repo.id, "other", "third").await; - assert_eq!(next.issue.number, first.issue.number + 1); - } + let next = open_pull(&fx, &repo, &repo.id, "other", "third").await; + assert_eq!( + next.issue.number, + first.issue.number + 1, + "refuse: no number taken" + ); - #[tokio::test] - async fn find_open_pull_matches_base_and_head() { - let fx = fixture().await; - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; + // find_open_pull matches base and head, and a merged pull stops matching. + let repo = new_repo(&fx, "find").await; + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; let found = fx .store - .find_open_pull(&fx.repo.id, "main", &fx.repo.id, "feature") + .find_open_pull(&repo.id, "main", &repo.id, "feature") .await .unwrap(); - assert_eq!(found, Some(pull.issue.number)); - + assert_eq!(found, Some(pull.issue.number), "find: matches"); assert_eq!( fx.store - .find_open_pull(&fx.repo.id, "main", &fx.repo.id, "other") + .find_open_pull(&repo.id, "main", &repo.id, "other") .await .unwrap(), - None + None, + "find: other head" ); assert_eq!( fx.store - .find_open_pull(&fx.repo.id, "dev", &fx.repo.id, "feature") + .find_open_pull(&repo.id, "dev", &repo.id, "feature") .await .unwrap(), - None + None, + "find: other base" ); - - // Once it is merged it no longer blocks a new pull for the same branches. let id = pull.issue.id.clone(); fx.store .mark_merged(&id, &fx.tahinli.id, &"c".repeat(40)) @@ -975,145 +1029,375 @@ mod tests { .unwrap(); assert_eq!( fx.store - .find_open_pull(&fx.repo.id, "main", &fx.repo.id, "feature") + .find_open_pull(&repo.id, "main", &repo.id, "feature") .await .unwrap(), - None + None, + "find: merged pull no longer blocks" ); - } - #[tokio::test] - async fn open_pulls_for_head_and_base_skip_closed() { - let fx = fixture().await; - let fork = fx + // Draft flag: round trip, events, list filter and draft numbers. + let repo = new_repo(&fx, "draft").await; + let pr = open_pull(&fx, &repo, &repo.id, "feature", "wip").await; + let _ready = open_pull(&fx, &repo, &repo.id, "other", "ready").await; + assert!(!pr.draft, "draft: starts ready"); + + assert!( + fx.store + .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) + .await + .unwrap(), + "draft: set changes the flag" + ); + // Setting the same value again changes nothing and records no event. + assert!( + !fx.store + .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) + .await + .unwrap(), + "draft: same value changes nothing" + ); + let got = fx .store - .create_fork(&fx.repo, &fx.bob.id, "proj-fork") + .pull_by_number(&repo.id, pr.issue.number) .await + .unwrap() .unwrap(); - let from_fork = open_pull(&fx, &fork.id, "topic", "from fork").await; - let same_repo = open_pull(&fx, &fx.repo.id, "topic", "same repo").await; + assert!(got.draft, "draft: read back"); - let heads = fx + let only = |draft: Option| IssueFilter { + state: None, + draft, + ..IssueFilter::default() + }; + let (items, total) = fx .store - .open_pulls_for_head(&fork.id, "topic") + .list_pulls(&repo.id, &only(Some(true))) .await .unwrap(); assert_eq!( - heads, - vec![( - fx.repo.id.clone(), - from_fork.issue.number, - from_fork.issue.id.clone() - )] + (total, items[0].issue.number), + (1, pr.issue.number), + "draft: filter drafts" + ); + let (_, total) = fx + .store + .list_pulls(&repo.id, &only(Some(false))) + .await + .unwrap(); + assert_eq!(total, 1, "draft: filter ready"); + let (_, total) = fx.store.list_pulls(&repo.id, &only(None)).await.unwrap(); + assert_eq!(total, 2, "draft: no filter"); + let numbers = [pr.issue.number, pr.issue.number + 1]; + let drafts = fx.store.draft_numbers(&repo.id, &numbers).await.unwrap(); + assert_eq!(drafts.len(), 1, "draft: numbers filtered"); + assert!( + drafts.contains(&pr.issue.number), + "draft: draft number listed" ); + assert!( fx.store - .open_pulls_for_head(&fork.id, "missing") + .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), false) + .await + .unwrap(), + "draft: cleared" + ); + let kinds: Vec = fx + .store + .list_events(&pr.issue.id) + .await + .unwrap() + .into_iter() + .map(|e| e.kind) + .collect(); + assert_eq!( + kinds, + vec!["converted_to_draft", "ready_for_review"], + "draft: events" + ); + + // Auto merge is cleared by a new head, a draft, or a merge. + let repo = new_repo(&fx, "auto").await; + let pr = open_pull(&fx, &repo, &repo.id, "feature", "auto").await; + let head = "a".repeat(40); + let load = || async { + fx.store + .pull_by_number(&repo.id, pr.issue.number) .await .unwrap() - .is_empty() + .unwrap() + }; + assert!(load().await.auto_merge.is_none(), "auto: starts unset"); + assert!( + !fx.store + .disable_auto_merge(&pr.issue.id, None, "manual") + .await + .unwrap(), + "auto: nothing to disable" ); - let bases = fx - .store - .open_pulls_for_base(&fx.repo.id, "main") + fx.store + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Rebase) .await .unwrap(); + let auto = load().await.auto_merge.expect("enabled"); assert_eq!( - bases, - vec![ - (from_fork.issue.number, from_fork.issue.id.clone()), - (same_repo.issue.number, same_repo.issue.id.clone()), - ] + (auto.style, auto.user_id.as_str()), + (MergeStyle::Rebase, fx.bob.id.as_str()), + "auto: style and user stored" + ); + let found = fx + .store + .auto_merge_pulls_for_head(&repo.id, &head) + .await + .unwrap(); + assert_eq!(found.len(), 1, "auto: found for its head"); + assert!( + fx.store + .auto_merge_pulls_for_head(&repo.id, &"d".repeat(40)) + .await + .unwrap() + .is_empty(), + "auto: not found for another head" ); fx.store - .mark_merged(&same_repo.issue.id, &fx.tahinli.id, &"c".repeat(40)) + .update_pull_head(&pr.issue.id, &"b".repeat(40)) .await .unwrap(); - let bases = fx - .store - .open_pulls_for_base(&fx.repo.id, "main") + assert!( + load().await.auto_merge.is_none(), + "auto: new head clears it" + ); + + fx.store + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) + .await + .unwrap(); + fx.store + .set_pull_draft(&pr.issue.id, None, true) + .await + .unwrap(); + assert!(load().await.auto_merge.is_none(), "auto: draft clears it"); + + fx.store + .set_pull_draft(&pr.issue.id, None, false) + .await + .unwrap(); + fx.store + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) + .await + .unwrap(); + assert!( + fx.store + .disable_auto_merge(&pr.issue.id, Some(&fx.bob.id), "refused") + .await + .unwrap(), + "auto: disabled" + ); + let events = fx.store.list_events(&pr.issue.id).await.unwrap(); + let last = events.last().unwrap(); + assert_eq!(last.kind, "auto_merge_disabled", "auto: disable event"); + assert_eq!(last.data["reason"], "refused", "auto: disable reason"); + + fx.store + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) .await .unwrap(); - assert_eq!(bases.len(), 1); - assert_eq!(bases[0].0, from_fork.issue.number); + fx.store + .mark_merged(&pr.issue.id, &fx.bob.id, &"c".repeat(40)) + .await + .unwrap(); + assert!(load().await.auto_merge.is_none(), "auto: merge clears it"); + assert!( + fx.store + .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) + .await + .is_err(), + "auto: merged pull refuses auto merge" + ); + + // Pull event kinds are accepted on pulls. + let repo = new_repo(&fx, "events").await; + let pull = open_pull(&fx, &repo, &repo.id, "feature", "t").await; + for kind in ["head_updated", "base_changed", "head_deleted"] { + fx.store + .add_event(&pull.issue.id, None, kind, serde_json::Value::Null) + .await + .unwrap(); + } + let kinds: Vec = fx + .store + .list_events(&pull.issue.id) + .await + .unwrap() + .into_iter() + .map(|e| e.kind) + .collect(); + assert_eq!( + kinds, + vec!["head_updated", "base_changed", "head_deleted"], + "events: pull kinds accepted" + ); } + // Forks, fork pulls and repo deletion. Forks are created under names that + // are unique for Bob, and the deletions run last. #[tokio::test] - async fn create_fork_copies_settings_and_lists_forks() { + async fn fork_scenarios_share_one_store() { let fx = fixture().await; + + // A fork copies the source settings and lists under its source. let fork = fx .store .create_fork(&fx.repo, &fx.bob.id, "proj-1") .await .unwrap(); - assert_eq!(fork.owner_id, fx.bob.id); - assert_eq!(fork.owner_name, "Bob"); - assert_eq!(fork.name, "proj-1"); - assert_eq!(fork.description, "desc"); - assert_eq!(fork.default_branch, "main"); - assert_eq!(fork.visibility, Visibility::Public); - assert_eq!(fork.fork_of.as_deref(), Some(fx.repo.id.as_str())); - assert_eq!(fx.repo.fork_of, None); + assert_eq!(fork.owner_id, fx.bob.id, "fork: owner"); + assert_eq!(fork.owner_name, "Bob", "fork: owner name"); + assert_eq!(fork.name, "proj-1", "fork: name"); + assert_eq!(fork.description, "desc", "fork: description copied"); + assert_eq!(fork.default_branch, "main", "fork: branch copied"); + assert_eq!( + fork.visibility, + Visibility::Public, + "fork: visibility copied" + ); + assert_eq!( + fork.fork_of.as_deref(), + Some(fx.repo.id.as_str()), + "fork: points at source" + ); + assert_eq!(fx.repo.fork_of, None, "fork: source is not a fork"); // fork_of survives a read back through the shared repo queries. let read = fx.store.repo_by_id(&fork.id).await.unwrap().unwrap(); - assert_eq!(read.fork_of, Some(fx.repo.id.clone())); + assert_eq!(read.fork_of, Some(fx.repo.id.clone()), "fork: read by id"); let read = fx .store .repo_by_owner_and_name("Bob", "proj-1") .await .unwrap() .unwrap(); - assert_eq!(read.fork_of.as_deref(), Some(fx.repo.id.as_str())); + assert_eq!( + read.fork_of.as_deref(), + Some(fx.repo.id.as_str()), + "fork: read by name" + ); assert_eq!( fx.store.list_forks(&fx.repo.id).await.unwrap(), - vec![fork.clone()] + vec![fork.clone()], + "fork: listed under source" + ); + assert!( + fx.store.list_forks(&fork.id).await.unwrap().is_empty(), + "fork: fork has no forks" ); - assert!(fx.store.list_forks(&fork.id).await.unwrap().is_empty()); - // A name clash with an existing repo of the same owner is a conflict. - assert!(matches!( - fx.store.create_fork(&fx.repo, &fx.bob.id, "proj-1").await, - Err(Error::Conflict(_)) - )); - } + assert!( + matches!( + fx.store.create_fork(&fx.repo, &fx.bob.id, "proj-1").await, + Err(Error::Conflict(_)) + ), + "fork: name clash is a conflict" + ); - #[tokio::test] - async fn fork_of_a_private_repo_is_private() { - let fx = fixture().await; + // A fork of a private repo is private and keeps its branch name. let private = fx .store .create_repo(&fx.tahinli.id, "secret", "", Visibility::Private, "trunk") .await .unwrap(); - let fork = fx + let secret_fork = fx .store .create_fork(&private, &fx.bob.id, "secret-1") .await .unwrap(); - assert_eq!(fork.visibility, Visibility::Private); - assert_eq!(fork.default_branch, "trunk"); - } + assert_eq!( + secret_fork.visibility, + Visibility::Private, + "private fork: private" + ); + assert_eq!(secret_fork.default_branch, "trunk", "private fork: branch"); - #[tokio::test] - async fn delete_repo_detaches_forks_and_heads() { - let fx = fixture().await; - let source = fx.repo.clone(); + // Open pulls for a head or base skip closed ones; forks feed the head side. + let source = new_repo(&fx, "fork-src").await; let fork = fx .store - .create_fork(&source, &fx.bob.id, "proj-1") + .create_fork(&source, &fx.bob.id, "fork-src-fork") + .await + .unwrap(); + let from_fork = open_pull(&fx, &source, &fork.id, "topic", "from fork").await; + let same_repo = open_pull(&fx, &source, &source.id, "topic", "same repo").await; + + let heads = fx + .store + .open_pulls_for_head(&fork.id, "topic") + .await + .unwrap(); + assert_eq!( + heads, + vec![( + source.id.clone(), + from_fork.issue.number, + from_fork.issue.id.clone() + )], + "heads: fork pull only" + ); + assert!( + fx.store + .open_pulls_for_head(&fork.id, "missing") + .await + .unwrap() + .is_empty(), + "heads: unknown branch" + ); + + let bases = fx + .store + .open_pulls_for_base(&source.id, "main") + .await + .unwrap(); + assert_eq!( + bases, + vec![ + (from_fork.issue.number, from_fork.issue.id.clone()), + (same_repo.issue.number, same_repo.issue.id.clone()), + ], + "bases: both open pulls" + ); + + fx.store + .mark_merged(&same_repo.issue.id, &fx.tahinli.id, &"c".repeat(40)) + .await + .unwrap(); + let bases = fx + .store + .open_pulls_for_base(&source.id, "main") + .await + .unwrap(); + assert_eq!(bases.len(), 1, "bases: merged pull skipped"); + assert_eq!( + bases[0].0, from_fork.issue.number, + "bases: fork pull remains" + ); + + // Deleting a fork detaches its incoming pulls; deleting a source removes + // its pulls and detaches its surviving forks. + let source = new_repo(&fx, "detach-src").await; + let fork = fx + .store + .create_fork(&source, &fx.bob.id, "detach-1") .await .unwrap(); let other_fork = fx .store - .create_fork(&source, &fx.bob.id, "proj-2") + .create_fork(&source, &fx.bob.id, "detach-2") .await .unwrap(); - // A pull from the fork into the source, and one in the other direction. - let incoming = open_pull(&fx, &fork.id, "topic", "incoming").await; + let incoming = open_pull(&fx, &source, &fork.id, "topic", "incoming").await; let outgoing = fx .store .create_pull( @@ -1130,7 +1414,6 @@ mod tests { .await .unwrap(); - // Deleting the fork: the pull into the source keeps its row with no head repo. fx.store.delete_repo(&fork.id).await.unwrap(); let kept = fx .store @@ -1138,41 +1421,51 @@ mod tests { .await .unwrap() .unwrap(); - assert_eq!(kept.head_repo_id, None); - assert_eq!(kept.head_branch, "topic"); - assert_eq!(kept.head_oid, "a".repeat(40)); - assert!(fx.store.repo_by_id(&fork.id).await.unwrap().is_none()); + assert_eq!(kept.head_repo_id, None, "detach fork: head repo cleared"); + assert_eq!(kept.head_branch, "topic", "detach fork: branch kept"); + assert_eq!(kept.head_oid, "a".repeat(40), "detach fork: head kept"); + assert!( + fx.store.repo_by_id(&fork.id).await.unwrap().is_none(), + "detach fork: fork gone" + ); assert_eq!( fx.store.list_forks(&source.id).await.unwrap(), - vec![other_fork.clone()] + vec![other_fork.clone()], + "detach fork: other fork listed" ); - // Deleting the source: its pulls go, and the surviving fork is detached. fx.store.delete_repo(&source.id).await.unwrap(); - assert!(fx.store.repo_by_id(&source.id).await.unwrap().is_none()); + assert!( + fx.store.repo_by_id(&source.id).await.unwrap().is_none(), + "detach source: source gone" + ); let detached = fx.store.repo_by_id(&other_fork.id).await.unwrap().unwrap(); - assert_eq!(detached.fork_of, None); + assert_eq!( + detached.fork_of, None, + "detach source: surviving fork detached" + ); assert!( fx.store .pull_by_number(&source.id, incoming.issue.number) .await .unwrap() - .is_none() + .is_none(), + "detach source: its pulls gone" ); - // The pull from the deleted fork went with it. assert!( fx.store .pull_by_number(&fork.id, outgoing.issue.number) .await .unwrap() - .is_none() + .is_none(), + "detach source: pull from deleted fork gone" ); } #[tokio::test] async fn delete_repo_removes_its_pull_rows() { let fx = fixture().await; - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; + let pull = open_pull(&fx, &fx.repo, &fx.repo.id, "feature", "t").await; fx.store .add_event( &pull.issue.id, @@ -1190,194 +1483,6 @@ mod tests { .await .unwrap() .unwrap(); - assert_eq!(left, 0); - } - - #[tokio::test] - async fn add_event_accepts_pull_kinds_on_pulls() { - let fx = fixture().await; - let pull = open_pull(&fx, &fx.repo.id, "feature", "t").await; - for kind in ["head_updated", "base_changed", "head_deleted"] { - fx.store - .add_event(&pull.issue.id, None, kind, serde_json::Value::Null) - .await - .unwrap(); - } - let kinds: Vec = fx - .store - .list_events(&pull.issue.id) - .await - .unwrap() - .into_iter() - .map(|e| e.kind) - .collect(); - assert_eq!(kinds, vec!["head_updated", "base_changed", "head_deleted"]); - } - - #[tokio::test] - async fn draft_flag_round_trips_with_events_and_list_filter() { - let fx = fixture().await; - let pr = open_pull(&fx, &fx.repo.id, "feature", "wip").await; - let _ready = open_pull(&fx, &fx.repo.id, "other", "ready").await; - assert!(!pr.draft); - - assert!( - fx.store - .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) - .await - .unwrap() - ); - // Setting the same value again changes nothing and records no event. - assert!( - !fx.store - .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), true) - .await - .unwrap() - ); - let got = fx - .store - .pull_by_number(&fx.repo.id, pr.issue.number) - .await - .unwrap() - .unwrap(); - assert!(got.draft); - - let only = |draft: Option| IssueFilter { - state: None, - draft, - ..IssueFilter::default() - }; - let (items, total) = fx - .store - .list_pulls(&fx.repo.id, &only(Some(true))) - .await - .unwrap(); - assert_eq!((total, items[0].issue.number), (1, pr.issue.number)); - let (_, total) = fx - .store - .list_pulls(&fx.repo.id, &only(Some(false))) - .await - .unwrap(); - assert_eq!(total, 1); - let (_, total) = fx.store.list_pulls(&fx.repo.id, &only(None)).await.unwrap(); - assert_eq!(total, 2); - let numbers = [pr.issue.number, pr.issue.number + 1]; - let drafts = fx.store.draft_numbers(&fx.repo.id, &numbers).await.unwrap(); - assert_eq!(drafts.len(), 1); - assert!(drafts.contains(&pr.issue.number)); - - assert!( - fx.store - .set_pull_draft(&pr.issue.id, Some(&fx.tahinli.id), false) - .await - .unwrap() - ); - let kinds: Vec = fx - .store - .list_events(&pr.issue.id) - .await - .unwrap() - .into_iter() - .map(|e| e.kind) - .collect(); - assert_eq!(kinds, vec!["converted_to_draft", "ready_for_review"]); - } - - #[tokio::test] - async fn auto_merge_is_stored_and_cleared_by_push_draft_and_merge() { - let fx = fixture().await; - let pr = open_pull(&fx, &fx.repo.id, "feature", "auto").await; - let head = "a".repeat(40); - let load = || async { - fx.store - .pull_by_number(&fx.repo.id, pr.issue.number) - .await - .unwrap() - .unwrap() - }; - assert!(load().await.auto_merge.is_none()); - assert!( - !fx.store - .disable_auto_merge(&pr.issue.id, None, "manual") - .await - .unwrap() - ); - - fx.store - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Rebase) - .await - .unwrap(); - let auto = load().await.auto_merge.expect("enabled"); - assert_eq!( - (auto.style, auto.user_id.as_str()), - (MergeStyle::Rebase, fx.bob.id.as_str()) - ); - let found = fx - .store - .auto_merge_pulls_for_head(&fx.repo.id, &head) - .await - .unwrap(); - assert_eq!(found.len(), 1); - assert!( - fx.store - .auto_merge_pulls_for_head(&fx.repo.id, &"d".repeat(40)) - .await - .unwrap() - .is_empty() - ); - - // A new head clears it. - fx.store - .update_pull_head(&pr.issue.id, &"b".repeat(40)) - .await - .unwrap(); - assert!(load().await.auto_merge.is_none()); - - // Converting to a draft clears it. - fx.store - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) - .await - .unwrap(); - fx.store - .set_pull_draft(&pr.issue.id, None, true) - .await - .unwrap(); - assert!(load().await.auto_merge.is_none()); - - // Disabling records why; merging clears it. - fx.store - .set_pull_draft(&pr.issue.id, None, false) - .await - .unwrap(); - fx.store - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) - .await - .unwrap(); - assert!( - fx.store - .disable_auto_merge(&pr.issue.id, Some(&fx.bob.id), "refused") - .await - .unwrap() - ); - let events = fx.store.list_events(&pr.issue.id).await.unwrap(); - let last = events.last().unwrap(); - assert_eq!(last.kind, "auto_merge_disabled"); - assert_eq!(last.data["reason"], "refused"); - - fx.store - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Squash) - .await - .unwrap(); - fx.store - .mark_merged(&pr.issue.id, &fx.bob.id, &"c".repeat(40)) - .await - .unwrap(); - assert!(load().await.auto_merge.is_none()); - assert!( - fx.store - .enable_auto_merge(&pr.issue.id, &fx.bob.id, MergeStyle::Merge) - .await - .is_err() - ); + assert_eq!(left, 0, "delete repo: no pull rows left"); } } diff --git a/crates/forge-core/src/store/reactions.rs b/crates/forge-core/src/store/reactions.rs index 9f0cd1c..4109fd9 100644 --- a/crates/forge-core/src/store/reactions.rs +++ b/crates/forge-core/src/store/reactions.rs @@ -186,9 +186,13 @@ mod tests { } } + // One store serves every scenario. Each scenario opens its own issue, so + // the reactions never overlap. #[tokio::test] - async fn toggle_adds_then_removes_and_rejects_missing_target() { + async fn reaction_scenarios_share_one_store() { let fx = fixture().await; + + // Toggle adds then removes, and a missing target is refused. let issue = fx .store .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") @@ -204,7 +208,7 @@ mod tests { ) .await .unwrap(); - assert!(on); + assert!(on, "toggle: first reaction is added"); let off = fx .store .toggle_reaction( @@ -215,29 +219,29 @@ mod tests { ) .await .unwrap(); - assert!(!off); + assert!(!off, "toggle: second reaction removes it"); let map = fx .store .reactions_for(ReactionTarget::Issue, std::slice::from_ref(&issue.id), None) .await .unwrap(); - assert!(map.is_empty()); - assert!(matches!( - fx.store - .toggle_reaction( - ReactionTarget::Comment, - "missing", - &fx.bob.id, - Reaction::Eyes - ) - .await, - Err(Error::NotFound) - )); - } + assert!(map.is_empty(), "toggle: nothing left after removal"); + assert!( + matches!( + fx.store + .toggle_reaction( + ReactionTarget::Comment, + "missing", + &fx.bob.id, + Reaction::Eyes + ) + .await, + Err(Error::NotFound) + ), + "toggle: missing target is NotFound" + ); - #[tokio::test] - async fn aggregate_counts_users_and_viewer_flag() { - let fx = fixture().await; + // Aggregates count users and flag the viewer's own reactions. let issue = fx .store .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") @@ -286,34 +290,48 @@ mod tests { .await .unwrap(); let summaries = &map[&issue.id]; - assert_eq!(summaries.len(), 2); - assert_eq!(summaries[0].reaction, Reaction::PlusOne); - assert_eq!(summaries[0].count, 2); - assert!(summaries[0].reacted_by_viewer); - assert_eq!(summaries[0].users, vec!["Tahinli", "Bob"]); - assert_eq!(summaries[1].reaction, Reaction::Rocket); - assert_eq!(summaries[1].count, 1); - assert!(!map.contains_key(&comment.id)); + assert_eq!(summaries.len(), 2, "aggregate: two reaction kinds"); + assert_eq!(summaries[0].reaction, Reaction::PlusOne, "aggregate: order"); + assert_eq!(summaries[0].count, 2, "aggregate: plus one count"); + assert!(summaries[0].reacted_by_viewer, "aggregate: viewer flag"); + assert_eq!( + summaries[0].users, + vec!["Tahinli", "Bob"], + "aggregate: users" + ); + assert_eq!(summaries[1].reaction, Reaction::Rocket, "aggregate: rocket"); + assert_eq!(summaries[1].count, 1, "aggregate: rocket count"); + assert!( + !map.contains_key(&comment.id), + "aggregate: comment kept apart" + ); let anon = fx .store .reactions_for(ReactionTarget::Issue, std::slice::from_ref(&issue.id), None) .await .unwrap(); - assert!(anon[&issue.id].iter().all(|s| !s.reacted_by_viewer)); + assert!( + anon[&issue.id].iter().all(|s| !s.reacted_by_viewer), + "aggregate: no viewer, no flags" + ); let on_comment = fx .store .reactions_for(ReactionTarget::Comment, &ids, Some(&fx.tahinli.id)) .await .unwrap(); - assert_eq!(on_comment[&comment.id][0].reaction, Reaction::Eyes); - assert!(on_comment[&comment.id][0].reacted_by_viewer); - } + assert_eq!( + on_comment[&comment.id][0].reaction, + Reaction::Eyes, + "comment reaction" + ); + assert!( + on_comment[&comment.id][0].reacted_by_viewer, + "comment viewer flag" + ); - #[tokio::test] - async fn summaries_keep_first_ten_names_and_full_count() { - let fx = fixture().await; + // Summaries keep the first ten names and the full count. let issue = fx .store .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") @@ -336,14 +354,11 @@ mod tests { .await .unwrap(); let summary = &map[&issue.id][0]; - assert_eq!(summary.count, 12); - assert_eq!(summary.users.len(), SUMMARY_USERS); - assert_eq!(summary.users[0], "user0"); - } + assert_eq!(summary.count, 12, "summary: full count"); + assert_eq!(summary.users.len(), SUMMARY_USERS, "summary: capped names"); + assert_eq!(summary.users[0], "user0", "summary: first name"); - #[tokio::test] - async fn deleting_a_comment_removes_its_reactions() { - let fx = fixture().await; + // Deleting a comment removes its reactions. let issue = fx .store .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") @@ -373,6 +388,6 @@ mod tests { ) .await .unwrap(); - assert!(map.is_empty()); + assert!(map.is_empty(), "delete comment: reactions gone"); } } diff --git a/crates/forge-core/src/store/reviews.rs b/crates/forge-core/src/store/reviews.rs index 7887bd6..aacf5b2 100644 --- a/crates/forge-core/src/store/reviews.rs +++ b/crates/forge-core/src/store/reviews.rs @@ -866,11 +866,10 @@ mod tests { bob: User, carol: User, repo_id: String, - issue: String, _dir: tempfile::TempDir, } - /// Tahinli owns the repo and opened the pull request; Bob and Carol review it. + /// Tahinli owns the repo; Bob and Carol review its pull requests. async fn fixture() -> Fixture { let dir = tempfile::tempdir().unwrap(); let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); @@ -887,36 +886,49 @@ mod tests { .create_repo(&tahinli.id, "proj", "desc", Visibility::Public, "main") .await .unwrap(); - let pull = store + Fixture { + store, + tahinli, + bob, + carol, + repo_id: repo.id.clone(), + _dir: dir, + } + } + + /// Opens a pull request from `feature-{topic}` and returns its issue id. + /// Each scenario gets its own pull, so reviews never leak between them. + async fn new_pull(fx: &Fixture, topic: &str) -> String { + let head = format!("feature-{topic}"); + let pull = fx + .store .create_pull( - &repo.id, - &tahinli.id, + &fx.repo_id, + &fx.tahinli.id, "change", "", "main", - &repo.id, - "feature", + &fx.repo_id, + &head, &oid('a'), &oid('b'), ) .await .unwrap(); - Fixture { - store, - tahinli, - bob, - carol, - repo_id: repo.id.clone(), - issue: pull.issue.id.clone(), - _dir: dir, - } + pull.issue.id.clone() } /// A root comment on `src/lib.rs` line 3 at head `a`. - async fn comment(fx: &Fixture, author: &str, pending: bool, body: &str) -> ReviewComment { + async fn comment( + fx: &Fixture, + issue: &str, + author: &str, + pending: bool, + body: &str, + ) -> ReviewComment { fx.store .add_review_comment( - &fx.issue, + issue, author, pending, "src/lib.rs", @@ -930,9 +942,15 @@ mod tests { .unwrap() } - async fn reply(fx: &Fixture, author: &str, root: &str, body: &str) -> ReviewComment { + async fn reply( + fx: &Fixture, + issue: &str, + author: &str, + root: &str, + body: &str, + ) -> ReviewComment { fx.store - .reply_review_comment(&fx.issue, author, root, body) + .reply_review_comment(issue, author, root, body) .await .unwrap() } @@ -945,224 +963,139 @@ mod tests { .unwrap() } + // Comment, reply, edit, delete, resolve, outdated and relocate. Each + // scenario has its own pull request. #[tokio::test] - async fn pending_comment_is_visible_only_to_its_author() { + async fn comment_scenarios_share_one_store() { let fx = fixture().await; - let draft = comment(&fx, &fx.bob.id, true, "draft").await; - assert!(draft.pending); - assert_eq!(draft.author_name, "Bob"); + + // A pending comment is visible only to its author and joins one draft. + let issue = new_pull(&fx, "pending").await; + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; + assert!(draft.pending, "pending: draft is pending"); + assert_eq!(draft.author_name, "Bob", "pending: author name"); let own = fx .store - .review_threads(&fx.issue, Some(&fx.bob.id)) + .review_threads(&issue, Some(&fx.bob.id)) .await .unwrap(); - assert_eq!(own.len(), 1); - assert_eq!(own[0].root.id, draft.id); - assert!(own[0].root.pending); + assert_eq!(own.len(), 1, "pending: author sees the draft"); + assert_eq!(own[0].root.id, draft.id, "pending: author sees its root"); + assert!(own[0].root.pending, "pending: root still pending"); for viewer in [Some(fx.carol.id.as_str()), None] { - let threads = fx.store.review_threads(&fx.issue, viewer).await.unwrap(); - assert!(threads.is_empty()); + let threads = fx.store.review_threads(&issue, viewer).await.unwrap(); + assert!(threads.is_empty(), "pending: hidden from others"); } - assert!(fx.store.list_reviews(&fx.issue).await.unwrap().is_empty()); + assert!( + fx.store.list_reviews(&issue).await.unwrap().is_empty(), + "pending: no submitted review yet" + ); let pending = fx .store - .pending_review(&fx.issue, &fx.bob.id) + .pending_review(&issue, &fx.bob.id) .await .unwrap() .expect("pending review"); - assert_eq!(pending.state, ReviewState::Pending); - assert_eq!(pending.id, draft.review_id); + assert_eq!(pending.state, ReviewState::Pending, "pending: state"); + assert_eq!(pending.id, draft.review_id, "pending: draft review"); - // A second pending comment joins the same draft review. - let second = comment(&fx, &fx.bob.id, true, "second").await; - assert_eq!(second.review_id, draft.review_id); - } - - #[tokio::test] - async fn single_comment_creates_submitted_commented_review() { - let fx = fixture().await; - let c = comment(&fx, &fx.bob.id, false, "nit").await; - assert!(!c.pending); - - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); - assert_eq!(reviews.len(), 1); - assert_eq!(reviews[0].id, c.review_id); - assert_eq!(reviews[0].state, ReviewState::Commented); - assert_eq!(reviews[0].body, ""); - assert!(reviews[0].submitted_at.is_some()); - assert!( - fx.store - .pending_review(&fx.issue, &fx.bob.id) - .await - .unwrap() - .is_none() + let second = comment(&fx, &issue, &fx.bob.id, true, "second").await; + assert_eq!( + second.review_id, draft.review_id, + "pending: same draft review" ); - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); - assert_eq!(threads.len(), 1); - assert!(threads[0].replies.is_empty()); - } + // A single submitted comment creates its own commented review. + let issue = new_pull(&fx, "single").await; + let c = comment(&fx, &issue, &fx.bob.id, false, "nit").await; + assert!(!c.pending, "single: submitted"); - #[tokio::test] - async fn submit_pending_as_approved() { - let fx = fixture().await; - let draft = comment(&fx, &fx.bob.id, true, "draft").await; - let review = fx - .store - .submit_review( - &fx.issue, - &fx.bob.id, - ReviewState::Approved, - "lgtm", - &oid('c'), - ) - .await - .unwrap(); - assert_eq!(review.id, draft.review_id); - assert_eq!(review.state, ReviewState::Approved); - assert_eq!(review.body, "lgtm"); - assert_eq!(review.commit_oid, oid('c')); - assert_eq!(review.author_name, "Bob"); - assert!(review.submitted_at.is_some()); - - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); - assert_eq!(reviews.len(), 1); + let reviews = fx.store.list_reviews(&issue).await.unwrap(); + assert_eq!(reviews.len(), 1, "single: one review"); + assert_eq!( + reviews[0].id, c.review_id, + "single: review holds the comment" + ); + assert_eq!( + reviews[0].state, + ReviewState::Commented, + "single: commented" + ); + assert_eq!(reviews[0].body, "", "single: empty body"); + assert!(reviews[0].submitted_at.is_some(), "single: submitted time"); assert!( fx.store - .pending_review(&fx.issue, &fx.bob.id) + .pending_review(&issue, &fx.bob.id) .await .unwrap() - .is_none() + .is_none(), + "single: no draft left" ); - let threads = fx - .store - .review_threads(&fx.issue, Some(&fx.bob.id)) - .await - .unwrap(); - assert!(!threads[0].root.pending); - - let summary = fx.store.review_summary(&fx.issue).await.unwrap(); - assert_eq!(summary.approved_by, vec!["Bob".to_string()]); - assert!(summary.changes_requested_by.is_empty()); - } - - #[tokio::test] - async fn pull_request_author_cannot_approve_or_request_changes() { - let fx = fixture().await; - for state in [ReviewState::Approved, ReviewState::ChangesRequested] { - let err = fx - .store - .submit_review(&fx.issue, &fx.tahinli.id, state, "", &oid('a')) - .await - .unwrap_err(); - assert!(matches!(err, Error::Forbidden(_)), "{err:?}"); - } - // Refused reviews store nothing. - assert!(fx.store.list_reviews(&fx.issue).await.unwrap().is_empty()); - - // The author may still comment. - let review = fx - .store - .submit_review( - &fx.issue, - &fx.tahinli.id, - ReviewState::Commented, - "note", - &oid('a'), - ) - .await - .unwrap(); - assert_eq!(review.state, ReviewState::Commented); - } - - #[tokio::test] - async fn empty_commented_review_is_invalid() { - let fx = fixture().await; - let err = fx - .store - .submit_review( - &fx.issue, - &fx.bob.id, - ReviewState::Commented, - " ", - &oid('a'), - ) - .await - .unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); - - // An approval needs no body. - fx.store - .submit_review( - &fx.issue, - &fx.carol.id, - ReviewState::Approved, - "", - &oid('a'), - ) - .await - .unwrap(); - - // A pending comment makes an empty comment review valid. - comment(&fx, &fx.bob.id, true, "draft").await; - fx.store - .submit_review(&fx.issue, &fx.bob.id, ReviewState::Commented, "", &oid('a')) - .await - .unwrap(); - assert_eq!(fx.store.list_reviews(&fx.issue).await.unwrap().len(), 2); - } - - #[tokio::test] - async fn reply_copies_position_and_requires_submitted_root() { - let fx = fixture().await; - let root = comment(&fx, &fx.bob.id, false, "question").await; - let answer = reply(&fx, &fx.carol.id, &root.id, "answer").await; - assert_eq!(answer.path, root.path); - assert_eq!(answer.side, Side::New); - assert_eq!(answer.line, 3); - assert_eq!(answer.commit_oid, oid('a')); - assert_eq!(answer.reply_to.as_deref(), Some(root.id.as_str())); - assert!(!answer.pending); + let threads = fx.store.review_threads(&issue, None).await.unwrap(); + assert_eq!(threads.len(), 1, "single: one thread"); + assert!(threads[0].replies.is_empty(), "single: no replies"); + + // A reply copies the root position and needs a submitted root. + let issue = new_pull(&fx, "reply").await; + let root = comment(&fx, &issue, &fx.bob.id, false, "question").await; + let answer = reply(&fx, &issue, &fx.carol.id, &root.id, "answer").await; + assert_eq!(answer.path, root.path, "reply: path copied"); + assert_eq!(answer.side, Side::New, "reply: side copied"); + assert_eq!(answer.line, 3, "reply: line copied"); + assert_eq!(answer.commit_oid, oid('a'), "reply: commit copied"); + assert_eq!( + answer.reply_to.as_deref(), + Some(root.id.as_str()), + "reply: points at root" + ); + assert!(!answer.pending, "reply: submitted"); // Each reply is submitted through its own commented review. - assert_eq!(fx.store.list_reviews(&fx.issue).await.unwrap().len(), 2); - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); - assert_eq!(threads.len(), 1); - assert_eq!(threads[0].replies.len(), 1); + assert_eq!( + fx.store.list_reviews(&issue).await.unwrap().len(), + 2, + "reply: root review and reply review" + ); + let threads = fx.store.review_threads(&issue, None).await.unwrap(); + assert_eq!(threads.len(), 1, "reply: one thread"); + assert_eq!(threads[0].replies.len(), 1, "reply: one reply in thread"); - // Not a reply to a reply, not a pending root, not a missing id. for target in [answer.id.as_str(), "missing"] { let err = fx .store - .reply_review_comment(&fx.issue, &fx.carol.id, target, "x") + .reply_review_comment(&issue, &fx.carol.id, target, "x") .await .unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); + assert!( + matches!(err, Error::NotFound), + "reply: refused {target}: {err:?}" + ); } - let draft = comment(&fx, &fx.bob.id, true, "draft").await; + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; for author in [&fx.bob.id, &fx.carol.id] { let err = fx .store - .reply_review_comment(&fx.issue, author, &draft.id, "x") + .reply_review_comment(&issue, author, &draft.id, "x") .await .unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); + assert!( + matches!(err, Error::NotFound), + "reply: no reply to draft: {err:?}" + ); } - } - #[tokio::test] - async fn edit_records_history() { - let fx = fixture().await; - let c = comment(&fx, &fx.bob.id, false, "old text").await; + // Edits record the previous body. + let issue = new_pull(&fx, "edit").await; + let c = comment(&fx, &issue, &fx.bob.id, false, "old text").await; fx.store .edit_review_comment(&c.id, "new text") .await .unwrap(); let edited = fx.store.review_comment(&c.id).await.unwrap().unwrap(); - assert_eq!(edited.body, "new text"); + assert_eq!(edited.body, "new text", "edit: new body"); let history: Vec<(String, String, String)> = { let conn = fx.store.inner.conn.lock().await; @@ -1181,56 +1114,79 @@ mod tests { "review_comment".to_string(), "old text".to_string(), fx.bob.id.clone() - )] + )], + "edit: history keeps old body and editor" ); let err = fx.store.edit_review_comment(&c.id, " ").await.unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); + assert!( + matches!(err, Error::Invalid(_)), + "edit: blank refused: {err:?}" + ); let err = fx .store .edit_review_comment("missing", "x") .await .unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); - } + assert!( + matches!(err, Error::NotFound), + "edit: missing refused: {err:?}" + ); - #[tokio::test] - async fn delete_root_removes_replies() { - let fx = fixture().await; - let root = comment(&fx, &fx.bob.id, false, "root").await; - let r1 = reply(&fx, &fx.carol.id, &root.id, "one").await; - let r2 = reply(&fx, &fx.bob.id, &root.id, "two").await; - let other = comment(&fx, &fx.carol.id, false, "other").await; + // Deleting a root removes its replies; deleting a reply keeps the root. + let issue = new_pull(&fx, "delete").await; + let root = comment(&fx, &issue, &fx.bob.id, false, "root").await; + let r1 = reply(&fx, &issue, &fx.carol.id, &root.id, "one").await; + let r2 = reply(&fx, &issue, &fx.bob.id, &root.id, "two").await; + let other = comment(&fx, &issue, &fx.carol.id, false, "other").await; fx.store.delete_review_comment(&root.id).await.unwrap(); - assert!(fx.store.review_comment(&root.id).await.unwrap().is_none()); - assert!(fx.store.review_comment(&r1.id).await.unwrap().is_none()); - assert!(fx.store.review_comment(&r2.id).await.unwrap().is_none()); - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); - assert_eq!(threads.len(), 1); - assert_eq!(threads[0].root.id, other.id); + assert!( + fx.store.review_comment(&root.id).await.unwrap().is_none(), + "delete root: root gone" + ); + assert!( + fx.store.review_comment(&r1.id).await.unwrap().is_none(), + "delete root: first reply gone" + ); + assert!( + fx.store.review_comment(&r2.id).await.unwrap().is_none(), + "delete root: second reply gone" + ); + let threads = fx.store.review_threads(&issue, None).await.unwrap(); + assert_eq!(threads.len(), 1, "delete root: other thread remains"); + assert_eq!(threads[0].root.id, other.id, "delete root: other root kept"); // The emptied reviews of the root and its replies are gone too. - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); - assert_eq!(reviews.len(), 1); - assert_eq!(reviews[0].id, other.review_id); + let reviews = fx.store.list_reviews(&issue).await.unwrap(); + assert_eq!(reviews.len(), 1, "delete root: emptied reviews removed"); + assert_eq!( + reviews[0].id, other.review_id, + "delete root: other review kept" + ); - // Deleting a reply keeps its root. - let root2 = comment(&fx, &fx.bob.id, false, "root two").await; - let r3 = reply(&fx, &fx.carol.id, &root2.id, "three").await; + let root2 = comment(&fx, &issue, &fx.bob.id, false, "root two").await; + let r3 = reply(&fx, &issue, &fx.carol.id, &root2.id, "three").await; fx.store.delete_review_comment(&r3.id).await.unwrap(); - assert!(fx.store.review_comment(&root2.id).await.unwrap().is_some()); - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); + assert!( + fx.store.review_comment(&root2.id).await.unwrap().is_some(), + "delete reply: root kept" + ); + let threads = fx.store.review_threads(&issue, None).await.unwrap(); let root2_thread = threads.iter().find(|t| t.root.id == root2.id).unwrap(); - assert!(root2_thread.replies.is_empty()); + assert!( + root2_thread.replies.is_empty(), + "delete reply: no replies left" + ); let err = fx.store.delete_review_comment("missing").await.unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); - } + assert!( + matches!(err, Error::NotFound), + "delete: missing refused: {err:?}" + ); - #[tokio::test] - async fn resolve_and_unresolve_record_events() { - let fx = fixture().await; - let root = comment(&fx, &fx.bob.id, false, "fix this").await; + // Resolving records events, and only submitted roots can be resolved. + let issue = new_pull(&fx, "resolve").await; + let root = comment(&fx, &issue, &fx.bob.id, false, "fix this").await; fx.store .set_thread_resolved(&root.id, &fx.carol.id, true) @@ -1242,7 +1198,8 @@ mod tests { .await .unwrap() .unwrap() - .resolved + .resolved, + "resolve: resolved" ); // Already resolved: no second event. fx.store @@ -1259,69 +1216,128 @@ mod tests { .await .unwrap() .unwrap() - .resolved + .resolved, + "resolve: unresolved again" ); let events: Vec<_> = fx .store - .list_events(&fx.issue) + .list_events(&issue) .await .unwrap() .into_iter() .filter(|e| e.kind.starts_with("thread_")) .collect(); let kinds: Vec<&str> = events.iter().map(|e| e.kind.as_str()).collect(); - assert_eq!(kinds, vec!["thread_resolved", "thread_unresolved"]); + assert_eq!( + kinds, + vec!["thread_resolved", "thread_unresolved"], + "resolve: events" + ); assert_eq!( events[0].data, - serde_json::json!({"comment": root.id, "path": "src/lib.rs"}) + serde_json::json!({"comment": root.id, "path": "src/lib.rs"}), + "resolve: event data" + ); + assert_eq!( + events[0].actor_id.as_deref(), + Some(fx.carol.id.as_str()), + "resolve: resolver" + ); + assert_eq!( + events[1].actor_id.as_deref(), + Some(fx.bob.id.as_str()), + "resolve: unresolver" ); - assert_eq!(events[0].actor_id.as_deref(), Some(fx.carol.id.as_str())); - assert_eq!(events[1].actor_id.as_deref(), Some(fx.bob.id.as_str())); - // Only submitted roots can be resolved. - let r = reply(&fx, &fx.carol.id, &root.id, "ok").await; - let draft = comment(&fx, &fx.bob.id, true, "draft").await; + let r = reply(&fx, &issue, &fx.carol.id, &root.id, "ok").await; + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; for id in [r.id.as_str(), draft.id.as_str(), "missing"] { let err = fx .store .set_thread_resolved(id, &fx.carol.id, true) .await .unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); + assert!( + matches!(err, Error::NotFound), + "resolve: refused {id}: {err:?}" + ); } - } - #[tokio::test] - async fn mark_outdated_only_touches_submitted_roots() { - let fx = fixture().await; - let root = comment(&fx, &fx.bob.id, false, "a").await; - let r = reply(&fx, &fx.carol.id, &root.id, "b").await; - let draft = comment(&fx, &fx.bob.id, true, "c").await; + // Outdated marks only submitted roots. + let issue = new_pull(&fx, "outdated").await; + let root = comment(&fx, &issue, &fx.bob.id, false, "a").await; + let r = reply(&fx, &issue, &fx.carol.id, &root.id, "b").await; + let draft = comment(&fx, &issue, &fx.bob.id, true, "c").await; fx.store - .mark_outdated( - &fx.issue, - &[root.id.clone(), r.id.clone(), draft.id.clone()], - ) + .mark_outdated(&issue, &[root.id.clone(), r.id.clone(), draft.id.clone()]) .await .unwrap(); - let threads = fx.store.review_threads(&fx.issue, None).await.unwrap(); - assert_eq!(threads.len(), 1); - assert!(threads[0].root.outdated); - assert!(!threads[0].replies[0].outdated); + let threads = fx.store.review_threads(&issue, None).await.unwrap(); + assert_eq!(threads.len(), 1, "outdated: one submitted thread"); + assert!(threads[0].root.outdated, "outdated: root marked"); + assert!(!threads[0].replies[0].outdated, "outdated: reply untouched"); let own = fx .store - .review_threads(&fx.issue, Some(&fx.bob.id)) + .review_threads(&issue, Some(&fx.bob.id)) .await .unwrap(); let draft_thread = own.iter().find(|t| t.root.id == draft.id).unwrap(); - assert!(!draft_thread.root.outdated); + assert!(!draft_thread.root.outdated, "outdated: draft untouched"); + + // Relocation moves the root and its replies, keeping the hunk. + let issue = new_pull(&fx, "relocate").await; + let root = comment(&fx, &issue, &fx.bob.id, false, "root").await; + let answer = reply(&fx, &issue, &fx.tahinli.id, &root.id, "answer").await; + + fx.store + .relocate_review_comment(&root.id, 7, &oid('c')) + .await + .unwrap(); + + let moved = fx.store.review_comment(&root.id).await.unwrap().unwrap(); + assert_eq!(moved.line, 7, "relocate: root line"); + assert_eq!(moved.commit_oid, oid('c'), "relocate: root commit"); + assert_eq!(moved.diff_hunk, "+let x = 1;", "relocate: hunk kept"); + let reply_after = fx.store.review_comment(&answer.id).await.unwrap().unwrap(); + assert_eq!(reply_after.line, 7, "relocate: reply line"); + assert_eq!(reply_after.commit_oid, oid('c'), "relocate: reply commit"); + + let err = fx + .store + .relocate_review_comment(&answer.id, 2, &oid('c')) + .await + .unwrap_err(); + assert!( + matches!(err, Error::Invalid(_)), + "relocate: reply refused: {err:?}" + ); + let err = fx + .store + .relocate_review_comment("missing", 2, &oid('c')) + .await + .unwrap_err(); + assert!( + matches!(err, Error::NotFound), + "relocate: missing refused: {err:?}" + ); + let err = fx + .store + .relocate_review_comment(&root.id, 0, &oid('c')) + .await + .unwrap_err(); + assert!( + matches!(err, Error::Invalid(_)), + "relocate: line 0 refused: {err:?}" + ); } + // Submitting, approving, dismissing and discarding reviews. Each scenario + // has its own pull request. #[tokio::test] - async fn review_summary_uses_latest_state_and_ignores_dismissed() { + async fn review_scenarios_share_one_store() { let fx = fixture().await; let dave = fx .store @@ -1329,14 +1345,123 @@ mod tests { .await .unwrap(); - // Bob approves, then requests changes: the later state counts. + // A pending draft becomes an approved review. + let issue = new_pull(&fx, "approve").await; + let draft = comment(&fx, &issue, &fx.bob.id, true, "draft").await; + let review = fx + .store + .submit_review(&issue, &fx.bob.id, ReviewState::Approved, "lgtm", &oid('c')) + .await + .unwrap(); + assert_eq!(review.id, draft.review_id, "approve: draft becomes review"); + assert_eq!(review.state, ReviewState::Approved, "approve: state"); + assert_eq!(review.body, "lgtm", "approve: body"); + assert_eq!(review.commit_oid, oid('c'), "approve: commit"); + assert_eq!(review.author_name, "Bob", "approve: author"); + assert!(review.submitted_at.is_some(), "approve: submitted time"); + + assert_eq!( + fx.store.list_reviews(&issue).await.unwrap().len(), + 1, + "approve: one review" + ); + assert!( + fx.store + .pending_review(&issue, &fx.bob.id) + .await + .unwrap() + .is_none(), + "approve: no draft left" + ); + let threads = fx + .store + .review_threads(&issue, Some(&fx.bob.id)) + .await + .unwrap(); + assert!(!threads[0].root.pending, "approve: comment submitted"); + + let summary = fx.store.review_summary(&issue).await.unwrap(); + assert_eq!( + summary.approved_by, + vec!["Bob".to_string()], + "approve: approver" + ); + assert!( + summary.changes_requested_by.is_empty(), + "approve: no requests" + ); + + // The pull request author may comment but not approve or request changes. + let issue = new_pull(&fx, "author").await; + for state in [ReviewState::Approved, ReviewState::ChangesRequested] { + let err = fx + .store + .submit_review(&issue, &fx.tahinli.id, state, "", &oid('a')) + .await + .unwrap_err(); + assert!( + matches!(err, Error::Forbidden(_)), + "author: refused {state:?}: {err:?}" + ); + } + assert!( + fx.store.list_reviews(&issue).await.unwrap().is_empty(), + "author: refused reviews store nothing" + ); + let review = fx + .store + .submit_review( + &issue, + &fx.tahinli.id, + ReviewState::Commented, + "note", + &oid('a'), + ) + .await + .unwrap(); + assert_eq!( + review.state, + ReviewState::Commented, + "author: comment allowed" + ); + + // A commented review needs text unless it carries a pending comment. + let issue = new_pull(&fx, "empty").await; + let err = fx + .store + .submit_review(&issue, &fx.bob.id, ReviewState::Commented, " ", &oid('a')) + .await + .unwrap_err(); + assert!( + matches!(err, Error::Invalid(_)), + "empty: blank refused: {err:?}" + ); + + fx.store + .submit_review(&issue, &fx.carol.id, ReviewState::Approved, "", &oid('a')) + .await + .unwrap(); + + comment(&fx, &issue, &fx.bob.id, true, "draft").await; + fx.store + .submit_review(&issue, &fx.bob.id, ReviewState::Commented, "", &oid('a')) + .await + .unwrap(); + assert_eq!( + fx.store.list_reviews(&issue).await.unwrap().len(), + 2, + "empty: approval without body and comment review with draft" + ); + + // The latest state of each reviewer counts; dismissed reviews do not. + let issue = new_pull(&fx, "summary").await; fx.store - .submit_review(&fx.issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) + .submit_review(&issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) .await .unwrap(); fx.store .submit_review( - &fx.issue, + &issue, &fx.bob.id, ReviewState::ChangesRequested, "fix", @@ -1344,20 +1469,13 @@ mod tests { ) .await .unwrap(); - // Carol approves, then only comments: the approval stands. fx.store - .submit_review( - &fx.issue, - &fx.carol.id, - ReviewState::Approved, - "", - &oid('a'), - ) + .submit_review(&issue, &fx.carol.id, ReviewState::Approved, "", &oid('a')) .await .unwrap(); fx.store .submit_review( - &fx.issue, + &issue, &fx.carol.id, ReviewState::Commented, "chat", @@ -1365,11 +1483,10 @@ mod tests { ) .await .unwrap(); - // Dave requests changes, which is then dismissed. let dave_review = fx .store .submit_review( - &fx.issue, + &issue, &dave.id, ReviewState::ChangesRequested, "no", @@ -1377,42 +1494,50 @@ mod tests { ) .await .unwrap(); - let before = fx.store.review_summary(&fx.issue).await.unwrap(); - assert_eq!(before.approved_by, vec!["Carol".to_string()]); + let before = fx.store.review_summary(&issue).await.unwrap(); + assert_eq!( + before.approved_by, + vec!["Carol".to_string()], + "summary: approvals" + ); assert_eq!( before.changes_requested_by, - vec!["Bob".to_string(), "Dave".to_string()] + vec!["Bob".to_string(), "Dave".to_string()], + "summary: requests before dismiss" ); fx.store .dismiss_review(&dave_review.id, &fx.tahinli.id, "wrong branch") .await .unwrap(); - let summary = fx.store.review_summary(&fx.issue).await.unwrap(); - assert_eq!(summary.approved_by, vec!["Carol".to_string()]); - assert_eq!(summary.changes_requested_by, vec!["Bob".to_string()]); - } + let summary = fx.store.review_summary(&issue).await.unwrap(); + assert_eq!( + summary.approved_by, + vec!["Carol".to_string()], + "summary: after dismiss" + ); + assert_eq!( + summary.changes_requested_by, + vec!["Bob".to_string()], + "summary: dismissed request gone" + ); - #[tokio::test] - async fn dismiss_only_approved_or_changes_requested() { - let fx = fixture().await; - let note = comment(&fx, &fx.bob.id, false, "note").await; + // Only approvals and change requests can be dismissed, and only once. + let issue = new_pull(&fx, "dismiss").await; + let note = comment(&fx, &issue, &fx.bob.id, false, "note").await; let err = fx .store .dismiss_review(¬e.review_id, &fx.tahinli.id, "x") .await .unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); + assert!( + matches!(err, Error::Invalid(_)), + "dismiss: comment refused: {err:?}" + ); let approved = fx .store - .submit_review( - &fx.issue, - &fx.carol.id, - ReviewState::Approved, - "", - &oid('a'), - ) + .submit_review(&issue, &fx.carol.id, ReviewState::Approved, "", &oid('a')) .await .unwrap(); let err = fx @@ -1420,84 +1545,126 @@ mod tests { .dismiss_review(&approved.id, &fx.tahinli.id, " ") .await .unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); + assert!( + matches!(err, Error::Invalid(_)), + "dismiss: blank reason refused: {err:?}" + ); let err = fx .store .dismiss_review("missing", &fx.tahinli.id, "x") .await .unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); + assert!( + matches!(err, Error::NotFound), + "dismiss: missing refused: {err:?}" + ); fx.store .dismiss_review(&approved.id, &fx.tahinli.id, "stale") .await .unwrap(); - let reviews = fx.store.list_reviews(&fx.issue).await.unwrap(); + let reviews = fx.store.list_reviews(&issue).await.unwrap(); let dismissed = reviews.iter().find(|r| r.id == approved.id).unwrap(); - assert_eq!(dismissed.state, ReviewState::Dismissed); - assert_eq!(dismissed.dismissed_reason.as_deref(), Some("stale")); + assert_eq!(dismissed.state, ReviewState::Dismissed, "dismiss: state"); + assert_eq!( + dismissed.dismissed_reason.as_deref(), + Some("stale"), + "dismiss: reason" + ); + assert_eq!( + dismissed.dismissed_from, + Some(ReviewState::Approved), + "dismiss: remembers approval" + ); let events: Vec<_> = fx .store - .list_events(&fx.issue) + .list_events(&issue) .await .unwrap() .into_iter() .filter(|e| e.kind == "review_dismissed") .collect(); - assert_eq!(events.len(), 1); + assert_eq!(events.len(), 1, "dismiss: one event"); assert_eq!( events[0].data, - serde_json::json!({"review": approved.id, "reason": "stale"}) + serde_json::json!({"review": approved.id, "reason": "stale"}), + "dismiss: event data" + ); + assert_eq!( + events[0].actor_id.as_deref(), + Some(fx.tahinli.id.as_str()), + "dismiss: actor" ); - assert_eq!(events[0].actor_id.as_deref(), Some(fx.tahinli.id.as_str())); - // Dismissing twice is refused. let err = fx .store .dismiss_review(&approved.id, &fx.tahinli.id, "again") .await .unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); - } + assert!( + matches!(err, Error::Invalid(_)), + "dismiss: twice refused: {err:?}" + ); - #[tokio::test] - async fn discard_pending_removes_its_comments() { - let fx = fixture().await; - let submitted = comment(&fx, &fx.bob.id, false, "done").await; - let d1 = comment(&fx, &fx.bob.id, true, "d1").await; - let d2 = comment(&fx, &fx.bob.id, true, "d2").await; + // A review written before the previous-state column has none. + { + let conn = fx.store.inner.conn.lock().await; + conn.execute( + "UPDATE pull_reviews SET dismissed_from = NULL WHERE id = ?1", + (approved.id.clone(),), + ) + .await + .unwrap(); + } + let listed = fx.store.list_reviews(&issue).await.unwrap(); + let legacy = listed.iter().find(|r| r.id == approved.id).unwrap(); + assert_eq!(legacy.dismissed_from, None, "legacy: no previous state"); + + // Discarding a draft keeps submitted comments. + let issue = new_pull(&fx, "discard").await; + let submitted = comment(&fx, &issue, &fx.bob.id, false, "done").await; + let d1 = comment(&fx, &issue, &fx.bob.id, true, "d1").await; + let d2 = comment(&fx, &issue, &fx.bob.id, true, "d2").await; fx.store - .discard_pending_review(&fx.issue, &fx.bob.id) + .discard_pending_review(&issue, &fx.bob.id) .await .unwrap(); assert!( fx.store - .pending_review(&fx.issue, &fx.bob.id) + .pending_review(&issue, &fx.bob.id) .await .unwrap() - .is_none() + .is_none(), + "discard: no draft left" + ); + assert!( + fx.store.review_comment(&d1.id).await.unwrap().is_none(), + "discard: first draft comment gone" + ); + assert!( + fx.store.review_comment(&d2.id).await.unwrap().is_none(), + "discard: second draft comment gone" ); - assert!(fx.store.review_comment(&d1.id).await.unwrap().is_none()); - assert!(fx.store.review_comment(&d2.id).await.unwrap().is_none()); assert!( fx.store .review_comment(&submitted.id) .await .unwrap() - .is_some() + .is_some(), + "discard: submitted comment kept" ); let own = fx .store - .review_threads(&fx.issue, Some(&fx.bob.id)) + .review_threads(&issue, Some(&fx.bob.id)) .await .unwrap(); - assert_eq!(own.len(), 1); + assert_eq!(own.len(), 1, "discard: only the submitted thread"); // Nothing pending left: discarding again is fine. fx.store - .discard_pending_review(&fx.issue, &fx.bob.id) + .discard_pending_review(&issue, &fx.bob.id) .await .unwrap(); } @@ -1505,12 +1672,13 @@ mod tests { #[tokio::test] async fn delete_repo_cleans_up_reviews() { let fx = fixture().await; - let root = comment(&fx, &fx.bob.id, false, "root").await; - reply(&fx, &fx.carol.id, &root.id, "reply").await; - comment(&fx, &fx.carol.id, true, "draft").await; + let issue = new_pull(&fx, "main").await; + let root = comment(&fx, &issue, &fx.bob.id, false, "root").await; + reply(&fx, &issue, &fx.carol.id, &root.id, "reply").await; + comment(&fx, &issue, &fx.carol.id, true, "draft").await; fx.store .submit_review( - &fx.issue, + &issue, &fx.tahinli.id, ReviewState::Commented, "hi", @@ -1519,7 +1687,7 @@ mod tests { .await .unwrap(); fx.store - .submit_review(&fx.issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) + .submit_review(&issue, &fx.bob.id, ReviewState::Approved, "", &oid('a')) .await .unwrap(); fx.store @@ -1557,97 +1725,4 @@ mod tests { } assert_eq!(ReviewState::ChangesRequested.as_str(), "changes_requested"); } - - #[tokio::test] - async fn dismissed_review_remembers_its_previous_state() { - let fx = fixture().await; - let approved = fx - .store - .submit_review( - &fx.issue, - &fx.carol.id, - ReviewState::Approved, - "", - &oid('a'), - ) - .await - .unwrap(); - assert_eq!(approved.dismissed_from, None); - fx.store - .dismiss_review(&approved.id, &fx.tahinli.id, "stale") - .await - .unwrap(); - let listed = fx.store.list_reviews(&fx.issue).await.unwrap(); - let dismissed = listed.iter().find(|r| r.id == approved.id).unwrap(); - assert_eq!(dismissed.state, ReviewState::Dismissed); - assert_eq!(dismissed.dismissed_from, Some(ReviewState::Approved)); - assert_eq!(dismissed.dismissed_reason.as_deref(), Some("stale")); - - // A row written before the column existed has no previous state. - { - let conn = fx.store.inner.conn.lock().await; - conn.execute( - "UPDATE pull_reviews SET dismissed_from = NULL WHERE id = ?1", - (approved.id.clone(),), - ) - .await - .unwrap(); - } - let listed = fx.store.list_reviews(&fx.issue).await.unwrap(); - let legacy = listed.iter().find(|r| r.id == approved.id).unwrap(); - assert_eq!(legacy.dismissed_from, None); - } - - #[tokio::test] - async fn relocate_moves_root_and_replies_keeping_hunk() { - let fx = fixture().await; - let root = fx - .store - .add_review_comment( - &fx.issue, - &fx.bob.id, - false, - "src/lib.rs", - Side::New, - 3, - &oid('a'), - "+let x = 1;", - "root", - ) - .await - .unwrap(); - let answer = reply(&fx, &fx.tahinli.id, &root.id, "answer").await; - - fx.store - .relocate_review_comment(&root.id, 7, &oid('c')) - .await - .unwrap(); - - let moved = fx.store.review_comment(&root.id).await.unwrap().unwrap(); - assert_eq!(moved.line, 7); - assert_eq!(moved.commit_oid, oid('c')); - assert_eq!(moved.diff_hunk, "+let x = 1;"); - let reply_after = fx.store.review_comment(&answer.id).await.unwrap().unwrap(); - assert_eq!(reply_after.line, 7); - assert_eq!(reply_after.commit_oid, oid('c')); - - let err = fx - .store - .relocate_review_comment(&answer.id, 2, &oid('c')) - .await - .unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); - let err = fx - .store - .relocate_review_comment("missing", 2, &oid('c')) - .await - .unwrap_err(); - assert!(matches!(err, Error::NotFound), "{err:?}"); - let err = fx - .store - .relocate_review_comment(&root.id, 0, &oid('c')) - .await - .unwrap_err(); - assert!(matches!(err, Error::Invalid(_)), "{err:?}"); - } }