From 965436ce11332cea3c543910867bcaf227eaafa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 05:51:42 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20store=20tests=20share=20the?= =?UTF-8?q?ir=20store=20and=20merge=20into=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scenarios that built the same fixture now run one after another on one store, each with its own repo, issue or users so their assertions stay exact. Every assert carries its scenario label. Test counts per file (before -> after): - issues.rs: 16 -> 3 - labels.rs: 14 -> 3 - discussions.rs: 10 -> 3 - notifications.rs: 9 -> 3 - pinned.rs: 4 -> 2 - attachments.rs: 1 -> 1 (unchanged) Total: 54 -> 15. Full-store checks (whole-table row counts after a repo delete) keep their own store. --- crates/forge-core/src/store/discussions.rs | 467 +++++---- crates/forge-core/src/store/issues.rs | 939 +++++++++---------- crates/forge-core/src/store/labels.rs | 813 ++++++++-------- crates/forge-core/src/store/notifications.rs | 466 +++++---- crates/forge-core/src/store/pinned.rs | 127 ++- 5 files changed, 1480 insertions(+), 1332 deletions(-) diff --git a/crates/forge-core/src/store/discussions.rs b/crates/forge-core/src/store/discussions.rs index 13ea0dc..925ca3b 100644 --- a/crates/forge-core/src/store/discussions.rs +++ b/crates/forge-core/src/store/discussions.rs @@ -759,106 +759,107 @@ mod tests { } } - #[tokio::test] - async fn discussions_are_off_by_default_and_toggle() { - let fx = fixture().await; - assert!(!fx.store.discussions_enabled(&fx.repo_id).await.unwrap()); - fx.store - .set_discussions_enabled(&fx.repo_id, true) + async fn fresh_repo(store: &Store, owner: &str, name: &str) -> String { + store + .create_repo(owner, name, "", Visibility::Public, "main") .await - .unwrap(); - assert!(fx.store.discussions_enabled(&fx.repo_id).await.unwrap()); - assert!(matches!( - fx.store.set_discussions_enabled("missing", true).await, - Err(Error::NotFound) - )); + .unwrap() + .id } + // Each scenario gets its own repo (numbers and lists stay isolated) and, + // where global per-user queries are asserted, its own users. #[tokio::test] - async fn numbers_share_the_issue_counter() { + async fn repo_level_rules_hold_in_each_scenario() { let fx = fixture().await; + + // Scenario 1: discussions are off by default and toggle per repo. + let repo = fresh_repo(&fx.store, &fx.tahinli.id, "toggle").await; + assert!( + !fx.store.discussions_enabled(&repo).await.unwrap(), + "toggle: off by default" + ); + fx.store.set_discussions_enabled(&repo, true).await.unwrap(); + assert!( + fx.store.discussions_enabled(&repo).await.unwrap(), + "toggle: on after enabling" + ); + assert!( + matches!( + fx.store.set_discussions_enabled("missing", true).await, + Err(Error::NotFound) + ), + "toggle: unknown repo" + ); + + // Scenario 2: discussion numbers share the issue counter. + let repo = fresh_repo(&fx.store, &fx.tahinli.id, "numbers").await; let issue = fx .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "first", "") + .create_issue(&repo, &fx.tahinli.id, "first", "") .await .unwrap(); let d1 = fx .store - .create_discussion( - &fx.repo_id, - &fx.tahinli.id, - DiscussionCategory::General, - "a", - "", - ) + .create_discussion(&repo, &fx.tahinli.id, DiscussionCategory::General, "a", "") .await .unwrap(); let issue2 = fx .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "second", "") + .create_issue(&repo, &fx.tahinli.id, "second", "") .await .unwrap(); - assert_eq!(issue.number, 1); - assert_eq!(d1.number, 2); - assert_eq!(issue2.number, 3); - let found = fx.store.discussion_by_number(&fx.repo_id, 2).await.unwrap(); - assert_eq!(found.map(|d| d.title), Some("a".to_string())); - } + assert_eq!(issue.number, 1, "numbers: first issue"); + assert_eq!(d1.number, 2, "numbers: discussion takes the next slot"); + assert_eq!(issue2.number, 3, "numbers: second issue"); + let found = fx.store.discussion_by_number(&repo, 2).await.unwrap(); + assert_eq!( + found.map(|d| d.title), + Some("a".to_string()), + "numbers: lookup by number" + ); - #[tokio::test] - async fn create_rejects_blank_title_and_lists_by_category() { - let fx = fixture().await; - assert!(matches!( - fx.store - .create_discussion( - &fx.repo_id, - &fx.tahinli.id, - DiscussionCategory::Ideas, - " ", - "" - ) - .await, - Err(Error::Invalid(_)) - )); + // Scenario 3: blank titles are refused and listings filter by category. + let repo = fresh_repo(&fx.store, &fx.tahinli.id, "categories").await; + assert!( + matches!( + fx.store + .create_discussion(&repo, &fx.tahinli.id, DiscussionCategory::Ideas, " ", "") + .await, + Err(Error::Invalid(_)) + ), + "categories: blank title refused" + ); fx.store - .create_discussion( - &fx.repo_id, - &fx.tahinli.id, - DiscussionCategory::Ideas, - "idea", - "", - ) + .create_discussion(&repo, &fx.tahinli.id, DiscussionCategory::Ideas, "idea", "") .await .unwrap(); fx.store - .create_discussion( - &fx.repo_id, - &fx.bob.id, - DiscussionCategory::Qa, - "question", - "", - ) + .create_discussion(&repo, &fx.bob.id, DiscussionCategory::Qa, "question", "") .await .unwrap(); let (all, total) = fx .store - .list_discussions(&fx.repo_id, &filter(None)) + .list_discussions(&repo, &filter(None)) .await .unwrap(); - assert_eq!((all.len(), total), (2, 2)); + assert_eq!((all.len(), total), (2, 2), "categories: unfiltered list"); let (qa, total) = fx .store - .list_discussions(&fx.repo_id, &filter(Some(DiscussionCategory::Qa))) + .list_discussions(&repo, &filter(Some(DiscussionCategory::Qa))) .await .unwrap(); - assert_eq!(total, 1); - assert_eq!(qa[0].discussion.title, "question"); - assert_eq!(qa[0].author, "Bob"); + assert_eq!(total, 1, "categories: qa total"); + assert_eq!(qa[0].discussion.title, "question", "categories: qa title"); + assert_eq!(qa[0].author, "Bob", "categories: qa author"); } + // Scenarios on one shared repo; every discussion is its own thread. #[tokio::test] - async fn comments_nest_one_level_and_count() { + async fn thread_rules_hold_in_each_scenario() { let fx = fixture().await; + + // Scenario 1: comments nest one level deep and are counted. let d = fx .store .create_discussion( @@ -880,31 +881,42 @@ mod tests { .add_discussion_comment(&d.id, &fx.tahinli.id, Some(&top.id), "hi back", false) .await .unwrap(); - assert_eq!(reply.parent_id.as_deref(), Some(top.id.as_str())); + assert_eq!( + reply.parent_id.as_deref(), + Some(top.id.as_str()), + "nesting: reply points at its parent" + ); let nested = fx .store .add_discussion_comment(&d.id, &fx.bob.id, Some(&reply.id), "too deep", false) .await; - assert!(matches!(nested, Err(Error::Invalid(_)))); - assert!(matches!( - fx.store - .add_discussion_comment(&d.id, &fx.bob.id, None, " ", false) - .await, - Err(Error::Invalid(_)) - )); + assert!( + matches!(nested, Err(Error::Invalid(_))), + "nesting: too deep" + ); + assert!( + matches!( + fx.store + .add_discussion_comment(&d.id, &fx.bob.id, None, " ", false) + .await, + Err(Error::Invalid(_)) + ), + "nesting: blank body" + ); let stored = fx .store .discussion_by_number(&fx.repo_id, d.number) .await .unwrap(); - assert_eq!(stored.map(|d| d.comment_count), Some(2)); + assert_eq!( + stored.map(|d| d.comment_count), + Some(2), + "nesting: comment count" + ); let all = fx.store.list_discussion_comments(&d.id).await.unwrap(); - assert_eq!(all.len(), 2); - } + assert_eq!(all.len(), 2, "nesting: listed comments"); - #[tokio::test] - async fn answer_only_in_qa_and_only_a_live_comment_of_it() { - let fx = fixture().await; + // Scenario 2: an answer is only accepted in Q&A and only from a live comment of it. let qa = fx .store .create_discussion(&fx.repo_id, &fx.tahinli.id, DiscussionCategory::Qa, "q", "") @@ -931,16 +943,22 @@ mod tests { .add_discussion_comment(&general.id, &fx.bob.id, None, "x", false) .await .unwrap(); - assert!(matches!( - fx.store - .set_discussion_answer(&general.id, Some(&g.id)) - .await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store.set_discussion_answer(&qa.id, Some(&g.id)).await, - Err(Error::Invalid(_)) - )); + assert!( + matches!( + fx.store + .set_discussion_answer(&general.id, Some(&g.id)) + .await, + Err(Error::Invalid(_)) + ), + "answer: refused outside Q&A" + ); + assert!( + matches!( + fx.store.set_discussion_answer(&qa.id, Some(&g.id)).await, + Err(Error::Invalid(_)) + ), + "answer: refused for a comment of another discussion" + ); fx.store .set_discussion_answer(&qa.id, Some(&c.id)) .await @@ -950,19 +968,24 @@ mod tests { .discussion_by_number(&fx.repo_id, qa.number) .await .unwrap(); - assert_eq!(stored.and_then(|d| d.answer_comment_id), Some(c.id.clone())); + assert_eq!( + stored.and_then(|d| d.answer_comment_id), + Some(c.id.clone()), + "answer: accepted" + ); fx.store.delete_discussion_comment(&c.id).await.unwrap(); let stored = fx .store .discussion_by_number(&fx.repo_id, qa.number) .await .unwrap(); - assert_eq!(stored.and_then(|d| d.answer_comment_id), None); - } + assert_eq!( + stored.and_then(|d| d.answer_comment_id), + None, + "answer: cleared when the comment is deleted" + ); - #[tokio::test] - async fn lock_reports_changes_and_delete_comment_keeps_replies() { - let fx = fixture().await; + // Scenario 3: lock reports changes and a deleted comment keeps its replies. let d = fx .store .create_discussion( @@ -983,12 +1006,21 @@ mod tests { .add_discussion_comment(&d.id, &fx.tahinli.id, Some(&top.id), "reply stays", false) .await .unwrap(); - assert!(fx.store.set_discussion_locked(&d.id, true).await.unwrap()); - assert!(!fx.store.set_discussion_locked(&d.id, true).await.unwrap()); - assert!(matches!( - fx.store.set_discussion_locked("missing", true).await, - Err(Error::NotFound) - )); + assert!( + fx.store.set_discussion_locked(&d.id, true).await.unwrap(), + "lock: first change reported" + ); + assert!( + !fx.store.set_discussion_locked(&d.id, true).await.unwrap(), + "lock: repeat is no change" + ); + assert!( + matches!( + fx.store.set_discussion_locked("missing", true).await, + Err(Error::NotFound) + ), + "lock: unknown discussion" + ); fx.store.delete_discussion_comment(&top.id).await.unwrap(); let gone = fx .store @@ -996,16 +1028,16 @@ mod tests { .await .unwrap() .unwrap(); - assert!(gone.deleted); - assert!(gone.body.is_empty()); + assert!(gone.deleted, "lock: parent tombstoned"); + assert!(gone.body.is_empty(), "lock: parent body cleared"); let all = fx.store.list_discussion_comments(&d.id).await.unwrap(); - assert_eq!(all.len(), 2); - assert!(all.iter().any(|c| c.body == "reply stays" && !c.deleted)); - } + assert_eq!(all.len(), 2, "lock: tombstone and reply both listed"); + assert!( + all.iter().any(|c| c.body == "reply stays" && !c.deleted), + "lock: reply survives" + ); - #[tokio::test] - async fn edit_and_delete_discussion_remove_its_rows() { - let fx = fixture().await; + // Scenario 4: edit and delete remove or update the discussion and its rows. let d = fx .store .create_discussion( @@ -1026,7 +1058,11 @@ mod tests { .discussion_by_number(&fx.repo_id, d.number) .await .unwrap(); - assert_eq!(edited.map(|d| d.title), Some("new".to_string())); + assert_eq!( + edited.map(|d| d.title), + Some("new".to_string()), + "edit: title updated" + ); fx.store .add_discussion_comment(&d.id, &fx.bob.id, None, "c", false) .await @@ -1037,71 +1073,26 @@ mod tests { .discussion_by_number(&fx.repo_id, d.number) .await .unwrap() - .is_none() + .is_none(), + "delete: discussion gone" ); assert!( fx.store .list_discussion_comments(&d.id) .await .unwrap() - .is_empty() + .is_empty(), + "delete: comments gone" ); - assert!(matches!( - fx.store.delete_discussion(&d.id).await, - Err(Error::NotFound) - )); - } - - #[tokio::test] - async fn mentions_go_unread_and_refresh_on_repeat() { - let fx = fixture().await; - let d = fx - .store - .create_discussion( - &fx.repo_id, - &fx.tahinli.id, - DiscussionCategory::General, - "t", - "", - ) - .await - .unwrap(); - fx.store - .record_discussion_mentions( - &fx.repo_id, - &d.id, - &fx.tahinli.id, - std::slice::from_ref(&fx.bob.id), - ) - .await - .unwrap(); - fx.store - .record_discussion_mentions( - &fx.repo_id, - &d.id, - &fx.tahinli.id, - std::slice::from_ref(&fx.bob.id), - ) - .await - .unwrap(); - let rows = fx - .store - .unread_discussion_mentions(&fx.bob.id) - .await - .unwrap(); - assert_eq!(rows, vec![(d.id.clone(), fx.repo_id.clone())]); assert!( - fx.store - .unread_discussion_mentions(&fx.tahinli.id) - .await - .unwrap() - .is_empty() + matches!( + fx.store.delete_discussion(&d.id).await, + Err(Error::NotFound) + ), + "delete: second delete is not found" ); - } - #[tokio::test] - async fn locked_discussion_takes_comments_only_from_triage() { - let fx = fixture().await; + // Scenario 5: a locked discussion takes comments only from triage. let d = fx .store .create_discussion( @@ -1118,14 +1109,17 @@ mod tests { .store .add_discussion_comment(&d.id, &fx.bob.id, None, "hi", false) .await; - assert!(matches!(refused, Err(Error::Forbidden(_)))); + assert!( + matches!(refused, Err(Error::Forbidden(_))), + "locked: plain comment refused" + ); let stored = fx .store .discussion_by_number(&fx.repo_id, d.number) .await .unwrap() .unwrap(); - assert_eq!(stored.comment_count, 0); + assert_eq!(stored.comment_count, 0, "locked: nothing was stored"); fx.store .add_discussion_comment(&d.id, &fx.bob.id, None, "maintainer note", true) .await @@ -1137,90 +1131,159 @@ mod tests { .unwrap(); } + // Each scenario uses fresh users so the global per-user inbox queries stay exact. #[tokio::test] - async fn replies_and_mentions_reach_the_inbox() { + async fn inbox_rules_hold_in_each_scenario() { let fx = fixture().await; + + // Scenario 1: repeated mentions are stored once and show as unread. + let ana = fx + .store + .create_user("Ana", None, "hash", false) + .await + .unwrap(); + let ben = fx + .store + .create_user("Ben", None, "hash", false) + .await + .unwrap(); + let repo = fresh_repo(&fx.store, &ana.id, "mentions").await; let d = fx .store - .create_discussion(&fx.repo_id, &fx.tahinli.id, DiscussionCategory::Qa, "Q", "") + .create_discussion(&repo, &ana.id, DiscussionCategory::General, "t", "") .await .unwrap(); - // Bob's comment on Tahinli's discussion gives Tahinli a reply row. + for _ in 0..2 { + fx.store + .record_discussion_mentions(&repo, &d.id, &ana.id, std::slice::from_ref(&ben.id)) + .await + .unwrap(); + } + let rows = fx.store.unread_discussion_mentions(&ben.id).await.unwrap(); + assert_eq!( + rows, + vec![(d.id.clone(), repo.clone())], + "mentions: one row after repeats" + ); + assert!( + fx.store + .unread_discussion_mentions(&ana.id) + .await + .unwrap() + .is_empty(), + "mentions: author is not mentioned" + ); + + // Scenario 2: replies, mentions and mark-read reach the owner's inbox. + let cora = fx + .store + .create_user("Cora", None, "hash", false) + .await + .unwrap(); + let dev = fx + .store + .create_user("Dev", None, "hash", false) + .await + .unwrap(); + let repo = fresh_repo(&fx.store, &cora.id, "replies").await; + let d = fx + .store + .create_discussion(&repo, &cora.id, DiscussionCategory::Qa, "Q", "") + .await + .unwrap(); + // Dev's comment on Cora's discussion gives Cora a reply row. fx.store - .add_discussion_comment(&d.id, &fx.bob.id, None, "answer", false) + .add_discussion_comment(&d.id, &dev.id, None, "answer", false) .await .unwrap(); - // Tahinli's own comment gives her nothing. + // Cora's own comment gives her nothing. fx.store - .add_discussion_comment(&d.id, &fx.tahinli.id, None, "thanks", false) + .add_discussion_comment(&d.id, &cora.id, None, "thanks", false) .await .unwrap(); let rows = fx .store - .list_discussion_notifications(&fx.tahinli.id, true, None, 50) + .list_discussion_notifications(&cora.id, true, None, 50) .await .unwrap(); - assert_eq!(rows.len(), 1); - assert_eq!(rows[0].reason, DiscussionReason::Reply); - assert_eq!(rows[0].title, "Q"); - assert_eq!(rows[0].number, d.number); - assert_eq!(rows[0].repo_owner, "Tahinli"); - assert_eq!(rows[0].last_actor_name.as_deref(), Some("Bob")); + assert_eq!(rows.len(), 1, "replies: one row"); + assert_eq!(rows[0].reason, DiscussionReason::Reply, "replies: reason"); + assert_eq!(rows[0].title, "Q", "replies: title"); + assert_eq!(rows[0].number, d.number, "replies: number"); + assert_eq!(rows[0].repo_owner, "Cora", "replies: owner"); + assert_eq!( + rows[0].last_actor_name.as_deref(), + Some("Dev"), + "replies: last actor" + ); // A mention upgrades the row, and a later reply does not downgrade it. fx.store - .record_discussion_mentions( - &fx.repo_id, - &d.id, - &fx.bob.id, - std::slice::from_ref(&fx.tahinli.id), - ) + .record_discussion_mentions(&repo, &d.id, &dev.id, std::slice::from_ref(&cora.id)) .await .unwrap(); fx.store - .add_discussion_comment(&d.id, &fx.bob.id, None, "again", false) + .add_discussion_comment(&d.id, &dev.id, None, "again", false) .await .unwrap(); let rows = fx .store - .list_discussion_notifications(&fx.tahinli.id, true, None, 50) + .list_discussion_notifications(&cora.id, true, None, 50) .await .unwrap(); - assert_eq!(rows.len(), 1); - assert_eq!(rows[0].reason, DiscussionReason::Mention); - assert_eq!(fx.store.unread_count(&fx.tahinli.id).await.unwrap(), 1); + assert_eq!(rows.len(), 1, "upgrade: still one row"); + assert_eq!( + rows[0].reason, + DiscussionReason::Mention, + "upgrade: mention wins" + ); + assert_eq!( + fx.store.unread_count(&cora.id).await.unwrap(), + 1, + "upgrade: unread count" + ); - // Another user's mark-read changes nothing. + // Another user's mark-read changes nothing; the owner's mark-all clears it. fx.store - .mark_discussion_notification_read(&fx.bob.id, &d.id) + .mark_discussion_notification_read(&dev.id, &d.id) .await .unwrap(); - assert_eq!(fx.store.unread_count(&fx.tahinli.id).await.unwrap(), 1); + assert_eq!( + fx.store.unread_count(&cora.id).await.unwrap(), + 1, + "mark-read: other user is ignored" + ); fx.store - .mark_all_discussion_notifications_read(&fx.tahinli.id, Some(&fx.repo_id)) + .mark_all_discussion_notifications_read(&cora.id, Some(&repo)) .await .unwrap(); - assert_eq!(fx.store.unread_count(&fx.tahinli.id).await.unwrap(), 0); + assert_eq!( + fx.store.unread_count(&cora.id).await.unwrap(), + 0, + "mark-read: cleared" + ); assert!( fx.store - .list_discussion_notifications(&fx.tahinli.id, true, None, 50) + .list_discussion_notifications(&cora.id, true, None, 50) .await .unwrap() - .is_empty() + .is_empty(), + "mark-read: no unread rows" ); let all = fx .store - .list_discussion_notifications(&fx.tahinli.id, false, None, 50) + .list_discussion_notifications(&cora.id, false, None, 50) .await .unwrap(); - assert_eq!(all.len(), 1); - assert!(!all[0].unread); + assert_eq!(all.len(), 1, "mark-read: read rows still listed"); + assert!(!all[0].unread, "mark-read: row is read"); assert!( fx.store - .discussion_notification(&fx.bob.id, &d.id) + .discussion_notification(&dev.id, &d.id) .await .unwrap() - .is_none() + .is_none(), + "mark-read: no row for the commenter" ); } } diff --git a/crates/forge-core/src/store/issues.rs b/crates/forge-core/src/store/issues.rs index e2ebaf8..0ab90b7 100644 --- a/crates/forge-core/src/store/issues.rs +++ b/crates/forge-core/src/store/issues.rs @@ -935,7 +935,6 @@ mod tests { store: Store, tahinli: User, bob: User, - repo_id: String, _dir: tempfile::TempDir, } @@ -947,19 +946,23 @@ mod tests { .await .unwrap(); let bob = store.create_user("Bob", None, "hash", false).await.unwrap(); - let repo = store - .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main") - .await - .unwrap(); Fixture { store, tahinli, bob, - repo_id: repo.id, _dir: dir, } } + /// A repo of its own, so issue numbers and repo-wide counts start clean per scenario. + async fn fresh_repo(store: &Store, owner: &str, name: &str) -> String { + store + .create_repo(owner, name, "", Visibility::Public, "main") + .await + .unwrap() + .id + } + fn all() -> IssueFilter { IssueFilter { state: None, @@ -972,76 +975,69 @@ mod tests { items.into_iter().map(|i| i.issue.title).collect() } + // Creating, filtering, paging and counting issues. Each scenario has its own repo. #[tokio::test] - async fn create_issue_numbers_start_at_one_and_advance() { + async fn issue_create_and_list_rules_hold_in_each_scenario() { let fx = fixture().await; - let a = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, " First ", "body") - .await - .unwrap(); - let b = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "Second", "") + let s = &fx.store; + + // Scenario 1: numbers start at one and advance; lookup by number. + let repo = fresh_repo(s, &fx.tahinli.id, "numbers").await; + let a = s + .create_issue(&repo, &fx.tahinli.id, " First ", "body") .await .unwrap(); - assert_eq!((a.number, b.number), (1, 2)); - assert_eq!(a.title, "First"); - assert_eq!(a.state, IssueState::Open); - assert_eq!(a.comment_count, 0); - let found = fx - .store - .issue_by_number(&fx.repo_id, 2) + let b = s + .create_issue(&repo, &fx.tahinli.id, "Second", "") .await - .unwrap() .unwrap(); - assert_eq!(found.id, b.id); + assert_eq!((a.number, b.number), (1, 2), "numbers: sequence"); + assert_eq!(a.title, "First", "numbers: title trimmed"); + assert_eq!(a.state, IssueState::Open, "numbers: starts open"); + assert_eq!(a.comment_count, 0, "numbers: no comments yet"); + let found = s.issue_by_number(&repo, 2).await.unwrap().unwrap(); + assert_eq!(found.id, b.id, "numbers: lookup by number"); assert!( - fx.store - .issue_by_number(&fx.repo_id, 3) - .await - .unwrap() - .is_none() + s.issue_by_number(&repo, 3).await.unwrap().is_none(), + "numbers: no third issue" ); - } - #[tokio::test] - async fn create_issue_validates_and_rejects_missing_repo() { - let fx = fixture().await; - assert!(matches!( - fx.store - .create_issue(&fx.repo_id, &fx.tahinli.id, " ", "") - .await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", &"x".repeat(MAX_BODY + 1)) - .await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store - .create_issue("missing", &fx.tahinli.id, "t", "") - .await, - Err(Error::NotFound) - )); - // A failed create does not consume a number. - let ok = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") + // Scenario 2: invalid input and missing repos are refused without taking a number. + let repo = fresh_repo(s, &fx.tahinli.id, "validate").await; + assert!( + matches!( + s.create_issue(&repo, &fx.tahinli.id, " ", "").await, + Err(Error::Invalid(_)) + ), + "validate: blank title" + ); + assert!( + matches!( + s.create_issue(&repo, &fx.tahinli.id, "t", &"x".repeat(MAX_BODY + 1)) + .await, + Err(Error::Invalid(_)) + ), + "validate: body too long" + ); + assert!( + matches!( + s.create_issue("missing", &fx.tahinli.id, "t", "").await, + Err(Error::NotFound) + ), + "validate: unknown repo" + ); + let ok = s + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); - assert_eq!(ok.number, 1); - } + assert_eq!(ok.number, 1, "validate: failed creates kept no number"); - #[tokio::test] - async fn concurrent_creates_get_unique_numbers_one_to_twenty() { - let fx = fixture().await; + // Scenario 3: concurrent creates still get unique numbers one to twenty. + let repo = fresh_repo(s, &fx.tahinli.id, "concurrent").await; let mut handles = Vec::new(); for i in 0..20 { - let store = fx.store.clone(); - let repo_id = fx.repo_id.clone(); + let store = s.clone(); + let repo_id = repo.clone(); let author = fx.tahinli.id.clone(); handles.push(tokio::spawn(async move { store @@ -1054,74 +1050,74 @@ mod tests { numbers.push(h.await.unwrap().unwrap().number); } numbers.sort_unstable(); - assert_eq!(numbers, (1..=20).collect::>()); - let (open, closed) = fx.store.issue_counts(&fx.repo_id).await.unwrap(); - assert_eq!((open, closed), (20, 0)); - } + assert_eq!( + numbers, + (1..=20).collect::>(), + "concurrent: unique numbers" + ); + assert_eq!( + s.issue_counts(&repo).await.unwrap(), + (20, 0), + "concurrent: all open" + ); - #[tokio::test] - async fn list_filters_state_author_and_query() { - let fx = fixture().await; - let a = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "Crash on start", "") + // Scenario 4: state, author and text filters. + let repo = fresh_repo(s, &fx.tahinli.id, "filters").await; + let a = s + .create_issue(&repo, &fx.tahinli.id, "Crash on start", "") .await .unwrap(); - fx.store - .create_issue(&fx.repo_id, &fx.bob.id, "Typo in docs", "") - .await - .unwrap(); - fx.store - .set_issue_state( - &a.id, - Some(&fx.tahinli.id), - IssueState::Closed, - Some(StateReason::Completed), - Json::Null, - ) + s.create_issue(&repo, &fx.bob.id, "Typo in docs", "") .await .unwrap(); + s.set_issue_state( + &a.id, + Some(&fx.tahinli.id), + IssueState::Closed, + Some(StateReason::Completed), + Json::Null, + ) + .await + .unwrap(); // Default filter (open only). - let open = IssueFilter::default(); assert_eq!( - titles(&fx.store, &fx.repo_id, &open).await, - vec!["Typo in docs"] + titles(s, &repo, &IssueFilter::default()).await, + vec!["Typo in docs"], + "filters: default is open only" ); - let (items, total) = fx.store.list_issues(&fx.repo_id, &all()).await.unwrap(); - assert_eq!((items.len(), total), (2, 2)); - + let (items, total) = s.list_issues(&repo, &all()).await.unwrap(); + assert_eq!((items.len(), total), (2, 2), "filters: all states"); let by_bob = IssueFilter { author: Some("BOB".into()), ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &by_bob).await, - vec!["Typo in docs"] + titles(s, &repo, &by_bob).await, + vec!["Typo in docs"], + "filters: author is case-insensitive" ); - let closed = IssueFilter { state: Some(IssueState::Closed), ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &closed).await, - vec!["Crash on start"] + titles(s, &repo, &closed).await, + vec!["Crash on start"], + "filters: closed" ); - let q = IssueFilter { query: Some("CRASH".into()), ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &q).await, - vec!["Crash on start"] + titles(s, &repo, &q).await, + vec!["Crash on start"], + "filters: text is case-insensitive" ); - } - #[tokio::test] - async fn query_treats_wildcards_literally() { - let fx = fixture().await; + // Scenario 5: wildcards in the text query are literal. + let repo = fresh_repo(s, &fx.tahinli.id, "wildcards").await; for t in [ "100% done", "100 done", @@ -1130,37 +1126,42 @@ mod tests { "back\\slash", "backXslash", ] { - fx.store - .create_issue(&fx.repo_id, &fx.tahinli.id, t, "") - .await - .unwrap(); + s.create_issue(&repo, &fx.tahinli.id, t, "").await.unwrap(); } let find = |q: &str| IssueFilter { query: Some(q.into()), ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &find("100%")).await, - vec!["100% done"] + titles(s, &repo, &find("100%")).await, + vec!["100% done"], + "wildcards: percent" ); assert_eq!( - titles(&fx.store, &fx.repo_id, &find("a_b")).await, - vec!["a_b"] + titles(s, &repo, &find("a_b")).await, + vec!["a_b"], + "wildcards: underscore" ); assert_eq!( - titles(&fx.store, &fx.repo_id, &find("\\")).await, - vec!["back\\slash"] + titles(s, &repo, &find("\\")).await, + vec!["back\\slash"], + "wildcards: backslash" + ); + assert_eq!( + titles(s, &repo, &find("%")).await.len(), + 1, + "wildcards: lone percent" + ); + assert_eq!( + titles(s, &repo, &find(" ")).await.len(), + 6, + "wildcards: blank query matches everything" ); - assert_eq!(titles(&fx.store, &fx.repo_id, &find("%")).await.len(), 1); - assert_eq!(titles(&fx.store, &fx.repo_id, &find(" ")).await.len(), 6); - } - #[tokio::test] - async fn list_paginates_and_clamps() { - let fx = fixture().await; + // Scenario 6: paging and clamping. + let repo = fresh_repo(s, &fx.tahinli.id, "pages").await; for i in 0..30 { - fx.store - .create_issue(&fx.repo_id, &fx.tahinli.id, &format!("issue {i}"), "") + s.create_issue(&repo, &fx.tahinli.id, &format!("issue {i}"), "") .await .unwrap(); } @@ -1169,46 +1170,26 @@ mod tests { per_page, ..all() }; - let (first, total) = fx - .store - .list_issues(&fx.repo_id, &page(1, 25)) - .await - .unwrap(); - assert_eq!((first.len(), total), (25, 30)); - assert_eq!(first[0].issue.number, 30, "newest first"); - let (second, _) = fx - .store - .list_issues(&fx.repo_id, &page(2, 25)) - .await - .unwrap(); - assert_eq!(second.len(), 5); - assert_eq!(second[4].issue.number, 1); + let (first, total) = s.list_issues(&repo, &page(1, 25)).await.unwrap(); + assert_eq!((first.len(), total), (25, 30), "pages: first page"); + assert_eq!(first[0].issue.number, 30, "pages: newest first"); + let (second, _) = s.list_issues(&repo, &page(2, 25)).await.unwrap(); + assert_eq!(second.len(), 5, "pages: second page size"); + assert_eq!(second[4].issue.number, 1, "pages: oldest last"); // per_page clamps to 1..=100, page to 1..=1000. - let (one, _) = fx - .store - .list_issues(&fx.repo_id, &page(0, 0)) - .await - .unwrap(); - assert_eq!(one.len(), 1); - assert_eq!(one[0].issue.number, 30); - let (big, _) = fx - .store - .list_issues(&fx.repo_id, &page(5000, 1000)) - .await - .unwrap(); - assert!(big.is_empty()); - } + let (one, _) = s.list_issues(&repo, &page(0, 0)).await.unwrap(); + assert_eq!(one.len(), 1, "pages: per_page and page clamp up"); + assert_eq!(one[0].issue.number, 30, "pages: clamped to first page"); + let (big, _) = s.list_issues(&repo, &page(5000, 1000)).await.unwrap(); + assert!(big.is_empty(), "pages: far past the end"); - #[tokio::test] - async fn list_sorts_by_number_and_update_time() { - let fx = fixture().await; - let first = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "one", "") + // Scenario 7: sorting by number and by last update. + let repo = fresh_repo(s, &fx.tahinli.id, "sorts").await; + let first = s + .create_issue(&repo, &fx.tahinli.id, "one", "") .await .unwrap(); - fx.store - .create_issue(&fx.repo_id, &fx.tahinli.id, "two", "") + s.create_issue(&repo, &fx.tahinli.id, "two", "") .await .unwrap(); let oldest = IssueFilter { @@ -1216,122 +1197,116 @@ mod tests { ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &oldest).await, - vec!["one", "two"] + titles(s, &repo, &oldest).await, + vec!["one", "two"], + "sorts: oldest first" ); - fx.store - .add_comment(&first.id, &fx.bob.id, "bump") - .await - .unwrap(); + s.add_comment(&first.id, &fx.bob.id, "bump").await.unwrap(); let recent = IssueFilter { sort: IssueSort::RecentlyUpdated, ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &recent).await, - vec!["one", "two"] + titles(s, &repo, &recent).await, + vec!["one", "two"], + "sorts: recently updated" ); - } - #[tokio::test] - async fn list_attaches_labels_and_assignees_in_batches() { - let fx = fixture().await; - let issue = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "labelled", "") - .await - .unwrap(); - let other = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "plain", "") + // Scenario 8: labels and assignees are attached, and filterable by name. + let repo = fresh_repo(s, &fx.tahinli.id, "labels").await; + let issue = s + .create_issue(&repo, &fx.tahinli.id, "labelled", "") .await .unwrap(); - let bug = fx - .store - .create_label(&fx.repo_id, "Bug", "#FF0000", "") + let other = s + .create_issue(&repo, &fx.tahinli.id, "plain", "") .await .unwrap(); - fx.store - .set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id)) + let bug = s.create_label(&repo, "Bug", "#FF0000", "").await.unwrap(); + s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id)) .await .unwrap(); - fx.store - .set_issue_assignees(&issue.id, &fx.tahinli.id, &["bob".to_string()]) + s.set_issue_assignees(&issue.id, &fx.tahinli.id, &["bob".to_string()]) .await .unwrap(); - let (items, _) = fx.store.list_issues(&fx.repo_id, &all()).await.unwrap(); + let (items, _) = s.list_issues(&repo, &all()).await.unwrap(); let labelled = items.iter().find(|i| i.issue.id == issue.id).unwrap(); - assert_eq!(labelled.labels.len(), 1); - assert_eq!(labelled.labels[0].name, "Bug"); - assert_eq!(labelled.assignees, vec!["Bob".to_string()]); - assert_eq!(labelled.author, "Tahinli"); + assert_eq!(labelled.labels.len(), 1, "labels: one attached"); + assert_eq!(labelled.labels[0].name, "Bug", "labels: name"); + assert_eq!( + labelled.assignees, + vec!["Bob".to_string()], + "labels: assignee" + ); + assert_eq!(labelled.author, "Tahinli", "labels: author"); let plain = items.iter().find(|i| i.issue.id == other.id).unwrap(); - assert!(plain.labels.is_empty() && plain.assignees.is_empty()); - - // Label and assignee filters, by name and username. + assert!( + plain.labels.is_empty() && plain.assignees.is_empty(), + "labels: untouched issue has none" + ); let by_label = IssueFilter { label: Some("bug".into()), ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &by_label).await, - vec!["labelled"] + titles(s, &repo, &by_label).await, + vec!["labelled"], + "labels: filter by label" ); let by_assignee = IssueFilter { assignee: Some("BOB".into()), ..all() }; assert_eq!( - titles(&fx.store, &fx.repo_id, &by_assignee).await, - vec!["labelled"] + titles(s, &repo, &by_assignee).await, + vec!["labelled"], + "labels: filter by assignee" ); - } - #[tokio::test] - async fn issue_counts_split_open_and_closed() { - let fx = fixture().await; - let a = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "a", "") + // Scenario 9: counts split open and closed. + let repo = fresh_repo(s, &fx.tahinli.id, "counts").await; + let a = s + .create_issue(&repo, &fx.tahinli.id, "a", "") .await .unwrap(); - fx.store - .create_issue(&fx.repo_id, &fx.tahinli.id, "b", "") + s.create_issue(&repo, &fx.tahinli.id, "b", "") .await .unwrap(); - fx.store - .set_issue_state(&a.id, None, IssueState::Closed, None, Json::Null) + s.set_issue_state(&a.id, None, IssueState::Closed, None, Json::Null) .await .unwrap(); - assert_eq!(fx.store.issue_counts(&fx.repo_id).await.unwrap(), (1, 1)); + assert_eq!( + s.issue_counts(&repo).await.unwrap(), + (1, 1), + "counts: one open, one closed" + ); } + // Editing, locking, state, comments, events and usernames. Each scenario has its own repo. #[tokio::test] - async fn edit_issue_saves_history_and_records_rename() { + async fn issue_edit_and_thread_rules_hold_in_each_scenario() { let fx = fixture().await; - let issue = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "old", "old body") - .await - .unwrap(); - fx.store - .edit_issue(&issue.id, &fx.bob.id, "new", "new body") + let s = &fx.store; + + // Scenario 1: an edit saves history and records a rename, body-only edits do not. + let repo = fresh_repo(s, &fx.tahinli.id, "edit").await; + let issue = s + .create_issue(&repo, &fx.tahinli.id, "old", "old body") .await .unwrap(); - let edited = fx - .store - .issue_by_number(&fx.repo_id, 1) + s.edit_issue(&issue.id, &fx.bob.id, "new", "new body") .await - .unwrap() .unwrap(); + let edited = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); assert_eq!( (edited.title.as_str(), edited.body.as_str()), - ("new", "new body") + ("new", "new body"), + "edit: stored" ); - assert_ne!(edited.updated_at, issue.created_at); + assert_ne!(edited.updated_at, issue.created_at, "edit: stamped"); - let conn = fx.store.inner.conn.lock().await; + let conn = s.inner.conn.lock().await; let mut rows = conn .query( "SELECT old_title, old_body, editor_id FROM edit_history WHERE target_kind = 'issue' AND target_id = ?1", @@ -1340,357 +1315,347 @@ mod tests { .await .unwrap(); let row = rows.next().await.unwrap().expect("history row"); - assert_eq!(col_text(&row, 0).unwrap(), "old"); - assert_eq!(col_text(&row, 1).unwrap(), "old body"); - assert_eq!(col_text(&row, 2).unwrap(), fx.bob.id); + assert_eq!(col_text(&row, 0).unwrap(), "old", "edit: history title"); + assert_eq!(col_text(&row, 1).unwrap(), "old body", "edit: history body"); + assert_eq!( + col_text(&row, 2).unwrap(), + fx.bob.id, + "edit: history editor" + ); drop(rows); drop(conn); - let events = fx.store.list_events(&issue.id).await.unwrap(); - assert_eq!(events.len(), 1); - assert_eq!(events[0].kind, "renamed"); + let events = s.list_events(&issue.id).await.unwrap(); + assert_eq!(events.len(), 1, "edit: one event"); + assert_eq!(events[0].kind, "renamed", "edit: rename event"); assert_eq!( events[0].data, - serde_json::json!({"from": "old", "to": "new"}) + serde_json::json!({"from": "old", "to": "new"}), + "edit: rename payload" + ); + assert_eq!( + events[0].actor_id.as_deref(), + Some(fx.bob.id.as_str()), + "edit: rename actor" ); - assert_eq!(events[0].actor_id.as_deref(), Some(fx.bob.id.as_str())); // Body-only edit: history yes, no rename event. - fx.store - .edit_issue(&issue.id, &fx.bob.id, "new", "newer") + s.edit_issue(&issue.id, &fx.bob.id, "new", "newer") .await .unwrap(); - assert_eq!(fx.store.list_events(&issue.id).await.unwrap().len(), 1); - assert!(matches!( - fx.store.edit_issue("missing", &fx.bob.id, "t", "").await, - Err(Error::NotFound) - )); - } + assert_eq!( + s.list_events(&issue.id).await.unwrap().len(), + 1, + "edit: body-only adds no event" + ); + assert!( + matches!( + s.edit_issue("missing", &fx.bob.id, "t", "").await, + Err(Error::NotFound) + ), + "edit: unknown issue" + ); - #[tokio::test] - async fn set_issue_locked_toggles_reason_and_records_events() { - let fx = fixture().await; - let issue = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") + // Scenario 2: locking toggles the reason and records events. + let repo = fresh_repo(s, &fx.tahinli.id, "locks").await; + let issue = s + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); - assert!(!issue.locked); - assert_eq!(issue.lock_reason, None); + assert!(!issue.locked, "lock: starts unlocked"); + assert_eq!(issue.lock_reason, None, "lock: no reason yet"); assert!( - !fx.store - .set_issue_locked(&issue.id, None, false, None) + !s.set_issue_locked(&issue.id, None, false, None) .await - .unwrap() + .unwrap(), + "lock: unlock when already open is no change" ); assert!( - fx.store - .set_issue_locked(&issue.id, Some(&fx.tahinli.id), true, Some("spam")) + s.set_issue_locked(&issue.id, Some(&fx.tahinli.id), true, Some("spam")) .await - .unwrap() + .unwrap(), + "lock: lock changes" ); - let locked = fx - .store - .issue_by_number(&fx.repo_id, 1) - .await - .unwrap() - .unwrap(); - assert!(locked.locked); - assert_eq!(locked.lock_reason.as_deref(), Some("spam")); + let locked = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); + assert!(locked.locked, "lock: locked"); + assert_eq!(locked.lock_reason.as_deref(), Some("spam"), "lock: reason"); assert!( - !fx.store - .set_issue_locked(&issue.id, None, true, Some("spam")) + !s.set_issue_locked(&issue.id, None, true, Some("spam")) .await - .unwrap() + .unwrap(), + "lock: lock again is no change" ); assert!( - fx.store - .set_issue_locked(&issue.id, Some(&fx.tahinli.id), false, Some("spam")) + s.set_issue_locked(&issue.id, Some(&fx.tahinli.id), false, Some("spam")) .await - .unwrap() + .unwrap(), + "lock: unlock changes" ); - let open = fx - .store - .issue_by_number(&fx.repo_id, 1) - .await - .unwrap() - .unwrap(); - assert!(!open.locked); - assert_eq!(open.lock_reason, None); + let open = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); + assert!(!open.locked, "lock: unlocked"); + assert_eq!(open.lock_reason, None, "lock: reason cleared"); - let err = fx - .store + let err = s .set_issue_locked(&issue.id, None, true, Some("bogus")) .await; - assert!(matches!(err, Err(Error::Invalid(_)))); + assert!( + matches!(err, Err(Error::Invalid(_))), + "lock: unknown reason" + ); - let kinds: Vec = fx - .store + let kinds: Vec = s .list_events(&issue.id) .await .unwrap() .into_iter() .map(|e| e.kind) .collect(); - assert_eq!(kinds, vec!["locked", "unlocked"]); - } + assert_eq!(kinds, vec!["locked", "unlocked"], "lock: events"); - #[tokio::test] - async fn set_issue_state_closes_reopens_and_records_events() { - let fx = fixture().await; - let issue = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") + // Scenario 3: closing and reopening record events and stamps. + let repo = fresh_repo(s, &fx.tahinli.id, "state").await; + let issue = s + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); assert!( - !fx.store - .set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null) + !s.set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null) .await - .unwrap() + .unwrap(), + "state: open when open is no change" ); assert!( - fx.store - .set_issue_state( - &issue.id, - Some(&fx.tahinli.id), - IssueState::Closed, - Some(StateReason::NotPlanned), - serde_json::json!({"x": 1}) - ) - .await - .unwrap() + s.set_issue_state( + &issue.id, + Some(&fx.tahinli.id), + IssueState::Closed, + Some(StateReason::NotPlanned), + serde_json::json!({"x": 1}) + ) + .await + .unwrap(), + "state: close changes" ); assert!( - !fx.store - .set_issue_state( - &issue.id, - None, - IssueState::Closed, - Some(StateReason::Completed), - Json::Null - ) - .await - .unwrap() + !s.set_issue_state( + &issue.id, + None, + IssueState::Closed, + Some(StateReason::Completed), + Json::Null + ) + .await + .unwrap(), + "state: close again is no change" ); - let closed = fx - .store - .issue_by_number(&fx.repo_id, 1) - .await - .unwrap() - .unwrap(); - assert_eq!(closed.state, IssueState::Closed); - assert_eq!(closed.state_reason, Some(StateReason::NotPlanned)); - assert!(closed.closed_at.is_some()); + let closed = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); + assert_eq!(closed.state, IssueState::Closed, "state: closed"); + assert_eq!( + closed.state_reason, + Some(StateReason::NotPlanned), + "state: reason" + ); + assert!(closed.closed_at.is_some(), "state: closed stamp"); assert!( - fx.store - .set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null) + s.set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null) .await - .unwrap() + .unwrap(), + "state: reopen changes" ); - let reopened = fx - .store - .issue_by_number(&fx.repo_id, 1) - .await - .unwrap() - .unwrap(); - assert_eq!(reopened.state, IssueState::Open); - assert_eq!(reopened.state_reason, None); - assert_eq!(reopened.closed_at, None); + let reopened = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); + assert_eq!(reopened.state, IssueState::Open, "state: reopened"); + assert_eq!(reopened.state_reason, None, "state: reason cleared"); + assert_eq!(reopened.closed_at, None, "state: closed stamp cleared"); - let events = fx.store.list_events(&issue.id).await.unwrap(); + let events = s.list_events(&issue.id).await.unwrap(); let kinds: Vec<&str> = events.iter().map(|e| e.kind.as_str()).collect(); - assert_eq!(kinds, vec!["closed", "reopened"]); + assert_eq!(kinds, vec!["closed", "reopened"], "state: events"); assert_eq!( events[0].data, - serde_json::json!({"x": 1, "reason": "not_planned"}) + serde_json::json!({"x": 1, "reason": "not_planned"}), + "state: close payload" ); - assert_eq!(events[1].data, serde_json::json!({})); - assert!(events[1].actor_id.is_none()); - - assert!(matches!( - fx.store - .set_issue_state("missing", None, IssueState::Closed, None, Json::Null) - .await, - Err(Error::NotFound) - )); - assert!(matches!( - fx.store - .set_issue_state(&issue.id, None, IssueState::Closed, None, Json::from(5)) - .await, - Err(Error::Invalid(_)) - )); - } + assert_eq!( + events[1].data, + serde_json::json!({}), + "state: reopen payload" + ); + assert!(events[1].actor_id.is_none(), "state: reopen without actor"); - #[tokio::test] - async fn comments_maintain_count_and_timestamps() { - let fx = fixture().await; - let issue = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") - .await - .unwrap(); - let c1 = fx - .store - .add_comment(&issue.id, &fx.bob.id, "first") + assert!( + matches!( + s.set_issue_state("missing", None, IssueState::Closed, None, Json::Null) + .await, + Err(Error::NotFound) + ), + "state: unknown issue" + ); + assert!( + matches!( + s.set_issue_state(&issue.id, None, IssueState::Closed, None, Json::from(5)) + .await, + Err(Error::Invalid(_)) + ), + "state: payload must be an object" + ); + + // Scenario 4: comments keep the count, timestamps and ordering in step. + let repo = fresh_repo(s, &fx.tahinli.id, "comments").await; + let issue = s + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); - let c2 = fx - .store + let c1 = s.add_comment(&issue.id, &fx.bob.id, "first").await.unwrap(); + let c2 = s .add_comment(&issue.id, &fx.tahinli.id, "second") .await .unwrap(); - assert!(!c1.edited); - let after_add = fx - .store - .issue_by_number(&fx.repo_id, 1) - .await - .unwrap() - .unwrap(); - assert_eq!(after_add.comment_count, 2); - assert_ne!(after_add.updated_at, issue.updated_at); - - assert!(matches!( - fx.store.add_comment(&issue.id, &fx.bob.id, " ").await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store.add_comment("missing", &fx.bob.id, "x").await, - Err(Error::NotFound) - )); - - let found = fx - .store - .comment_by_id(&issue.id, &c1.id) - .await - .unwrap() - .unwrap(); - assert_eq!(found.body, "first"); + assert!(!c1.edited, "comments: fresh comment not edited"); + let after_add = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); + assert_eq!(after_add.comment_count, 2, "comments: count after add"); + assert_ne!(after_add.updated_at, issue.updated_at, "comments: bumped"); + assert!( - fx.store - .comment_by_id("other", &c1.id) - .await - .unwrap() - .is_none() + matches!( + s.add_comment(&issue.id, &fx.bob.id, " ").await, + Err(Error::Invalid(_)) + ), + "comments: blank body" + ); + assert!( + matches!( + s.add_comment("missing", &fx.bob.id, "x").await, + Err(Error::NotFound) + ), + "comments: unknown issue" ); - fx.store - .edit_comment(&c1.id, &fx.bob.id, "edited") - .await - .unwrap(); - let edited = fx - .store - .comment_by_id(&issue.id, &c1.id) - .await - .unwrap() - .unwrap(); - assert_eq!(edited.body, "edited"); - assert!(edited.edited); - assert!(matches!( - fx.store.edit_comment("missing", &fx.bob.id, "x").await, - Err(Error::NotFound) - )); - assert!(matches!( - fx.store.edit_comment(&c1.id, &fx.bob.id, "").await, - Err(Error::Invalid(_)) - )); - - let comments = fx.store.list_comments(&issue.id).await.unwrap(); + let found = s.comment_by_id(&issue.id, &c1.id).await.unwrap().unwrap(); + assert_eq!(found.body, "first", "comments: lookup"); + assert!( + s.comment_by_id("other", &c1.id).await.unwrap().is_none(), + "comments: wrong issue finds nothing" + ); + + s.edit_comment(&c1.id, &fx.bob.id, "edited").await.unwrap(); + let edited = s.comment_by_id(&issue.id, &c1.id).await.unwrap().unwrap(); + assert_eq!(edited.body, "edited", "comments: edited body"); + assert!(edited.edited, "comments: edited flag"); + assert!( + matches!( + s.edit_comment("missing", &fx.bob.id, "x").await, + Err(Error::NotFound) + ), + "comments: edit unknown" + ); + assert!( + matches!( + s.edit_comment(&c1.id, &fx.bob.id, "").await, + Err(Error::Invalid(_)) + ), + "comments: edit to blank" + ); + + let comments = s.list_comments(&issue.id).await.unwrap(); let bodies: Vec<&str> = comments.iter().map(|c| c.body.as_str()).collect(); - assert_eq!(bodies, vec!["edited", "second"]); + assert_eq!(bodies, vec!["edited", "second"], "comments: order"); - fx.store.delete_comment(&c2.id).await.unwrap(); - let after_delete = fx - .store - .issue_by_number(&fx.repo_id, 1) - .await - .unwrap() - .unwrap(); - assert_eq!(after_delete.comment_count, 1); + s.delete_comment(&c2.id).await.unwrap(); + let after_delete = s.issue_by_number(&repo, 1).await.unwrap().unwrap(); + assert_eq!( + after_delete.comment_count, 1, + "comments: count after delete" + ); assert!( - fx.store - .comment_by_id(&issue.id, &c2.id) - .await - .unwrap() - .is_none() + s.comment_by_id(&issue.id, &c2.id).await.unwrap().is_none(), + "comments: deleted comment gone" + ); + assert!( + matches!(s.delete_comment(&c2.id).await, Err(Error::NotFound)), + "comments: delete twice" ); - assert!(matches!( - fx.store.delete_comment(&c2.id).await, - Err(Error::NotFound) - )); - } - #[tokio::test] - async fn add_event_validates_kind_and_issue() { - let fx = fixture().await; - let issue = fx - .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") - .await - .unwrap(); - fx.store - .add_event( - &issue.id, - None, - "referenced", - serde_json::json!({"commit": "abc"}), - ) + // Scenario 5: events validate their kind and their issue. + let repo = fresh_repo(s, &fx.tahinli.id, "events").await; + let issue = s + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); - fx.store - .add_event(&issue.id, Some(&fx.bob.id), "referenced", Json::Null) + s.add_event( + &issue.id, + None, + "referenced", + serde_json::json!({"commit": "abc"}), + ) + .await + .unwrap(); + s.add_event(&issue.id, Some(&fx.bob.id), "referenced", Json::Null) .await .unwrap(); - let events = fx.store.list_events(&issue.id).await.unwrap(); - assert_eq!(events.len(), 2); - assert_eq!(events[0].data, serde_json::json!({"commit": "abc"})); - assert_eq!(events[1].data, serde_json::json!({})); - assert!(matches!( - fx.store - .add_event(&issue.id, None, "exploded", Json::Null) - .await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store - .add_event(&issue.id, None, "referenced", Json::from(true)) - .await, - Err(Error::Invalid(_)) - )); - assert!(matches!( - fx.store - .add_event("missing", None, "referenced", Json::Null) - .await, - Err(Error::NotFound) - )); - } + let events = s.list_events(&issue.id).await.unwrap(); + assert_eq!(events.len(), 2, "events: both stored"); + assert_eq!( + events[0].data, + serde_json::json!({"commit": "abc"}), + "events: payload kept" + ); + assert_eq!( + events[1].data, + serde_json::json!({}), + "events: null becomes object" + ); + assert!( + matches!( + s.add_event(&issue.id, None, "exploded", Json::Null).await, + Err(Error::Invalid(_)) + ), + "events: unknown kind" + ); + assert!( + matches!( + s.add_event(&issue.id, None, "referenced", Json::from(true)) + .await, + Err(Error::Invalid(_)) + ), + "events: payload must be an object" + ); + assert!( + matches!( + s.add_event("missing", None, "referenced", Json::Null).await, + Err(Error::NotFound) + ), + "events: unknown issue" + ); - #[tokio::test] - async fn usernames_by_ids_is_batched_and_skips_unknown() { - let fx = fixture().await; + // Scenario 6: usernames are batched and unknown ids are skipped. let ids = vec![ fx.tahinli.id.clone(), fx.bob.id.clone(), "nobody".to_string(), ]; - let map = fx.store.usernames_by_ids(&ids).await.unwrap(); - assert_eq!(map.len(), 2); - assert_eq!(map[&fx.tahinli.id].0, "Tahinli"); - assert_eq!(map[&fx.bob.id].1, None); - assert!(fx.store.usernames_by_ids(&[]).await.unwrap().is_empty()); + let map = s.usernames_by_ids(&ids).await.unwrap(); + assert_eq!(map.len(), 2, "usernames: unknown skipped"); + assert_eq!(map[&fx.tahinli.id].0, "Tahinli", "usernames: name"); + assert_eq!(map[&fx.bob.id].1, None, "usernames: no display name"); + assert!( + s.usernames_by_ids(&[]).await.unwrap().is_empty(), + "usernames: empty input" + ); } + // Kept on its own store: it counts rows in whole tables, so other repos must not exist. #[tokio::test] async fn delete_repo_leaves_no_m3_rows() { let fx = fixture().await; + let repo = fresh_repo(&fx.store, &fx.tahinli.id, "proj").await; let issue = fx .store - .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "") + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); let comment = fx @@ -1708,7 +1673,7 @@ mod tests { .unwrap(); let label = fx .store - .create_label(&fx.repo_id, "bug", "#000000", "") + .create_label(&repo, "bug", "#000000", "") .await .unwrap(); fx.store @@ -1721,7 +1686,7 @@ mod tests { .unwrap(); let ms = fx .store - .create_milestone(&fx.repo_id, "v1", "", Some("2030-01-01")) + .create_milestone(&repo, "v1", "", Some("2030-01-01")) .await .unwrap(); fx.store @@ -1729,7 +1694,7 @@ mod tests { .await .unwrap(); - fx.store.delete_repo(&fx.repo_id).await.unwrap(); + fx.store.delete_repo(&repo).await.unwrap(); let conn = fx.store.inner.conn.lock().await; for table in [ diff --git a/crates/forge-core/src/store/labels.rs b/crates/forge-core/src/store/labels.rs index dd2aae9..49c405b 100644 --- a/crates/forge-core/src/store/labels.rs +++ b/crates/forge-core/src/store/labels.rs @@ -739,15 +739,13 @@ async fn commit_or_rollback(tx: Transaction<'_>, outcome: Result) -> Resul mod tests { use super::*; use crate::issues::{IssueEvent, StateReason}; - use crate::model::{Repo, Role, Visibility}; + use crate::model::{Role, Visibility}; struct Fixture { _dir: tempfile::TempDir, store: Store, tahinli: User, bob: User, - repo: Repo, - other: Repo, } async fn fixture() -> Fixture { @@ -758,24 +756,23 @@ mod tests { .await .unwrap(); let bob = store.create_user("Bob", None, "hash", false).await.unwrap(); - let repo = store - .create_repo(&tahinli.id, "repo", "", Visibility::Public, "main") - .await - .unwrap(); - let other = store - .create_repo(&tahinli.id, "other", "", Visibility::Public, "main") - .await - .unwrap(); Fixture { _dir: dir, store, tahinli, bob, - repo, - other, } } + /// A repo of its own, so label and milestone lists stay exact per scenario. + async fn fresh_repo(store: &Store, owner: &str, name: &str) -> String { + store + .create_repo(owner, name, "", Visibility::Public, "main") + .await + .unwrap() + .id + } + /// Data of every event of `kind`. fn data_of(events: &[IssueEvent], kind: &str) -> Vec { events @@ -789,385 +786,405 @@ mod tests { labels.iter().map(|l| l.name.as_str()).collect() } + // Repo labels: creating, defaults, listing, updating and deleting. + // Every scenario has its own repos, so lists and counts stay exact. #[tokio::test] - async fn create_label_normalizes_and_rejects_duplicates() { + async fn label_rules_hold_in_each_scenario() { let fx = fixture().await; - let label = fx - .store - .create_label(&fx.repo.id, " Bug ", "#FF0000", "broken") + let s = &fx.store; + let owner = &fx.tahinli.id; + + // Scenario 1: names are trimmed and colours normalized; duplicates are refused. + let repo = fresh_repo(s, owner, "labels-create").await; + let other = fresh_repo(s, owner, "labels-create-other").await; + let label = s + .create_label(&repo, " Bug ", "#FF0000", "broken") .await .unwrap(); - assert_eq!(label.name, "Bug"); - assert_eq!(label.color, "#ff0000"); - assert_eq!(label.description, "broken"); - assert!(matches!( - fx.store - .create_label(&fx.repo.id, "BUG", "#00ff00", "") - .await, - Err(Error::Conflict(_)) - )); + assert_eq!(label.name, "Bug", "create: name trimmed"); + assert_eq!(label.color, "#ff0000", "create: colour lowercased"); + assert_eq!(label.description, "broken", "create: description kept"); assert!( - fx.store - .create_label(&fx.repo.id, "bad", "red", "") - .await - .is_err() + matches!( + s.create_label(&repo, "BUG", "#00ff00", "").await, + Err(Error::Conflict(_)) + ), + "create: duplicate ignoring case" + ); + assert!( + s.create_label(&repo, "bad", "red", "").await.is_err(), + "create: colour must be hex" ); // The same name in another repo is a different label. - fx.store - .create_label(&fx.other.id, "bug", "#000000", "") - .await - .unwrap(); - } + s.create_label(&other, "bug", "#000000", "").await.unwrap(); - #[tokio::test] - async fn create_default_labels_adds_nine_idempotent_and_skips_taken_names() { - let fx = fixture().await; - let s = &fx.store; - // Case 1: an empty repo gets all nine defaults, and a second run adds nothing. + // Scenario 2: default labels are added once and a second run adds nothing. + let repo = fresh_repo(s, owner, "labels-defaults").await; assert_eq!( - s.create_default_labels(&fx.repo.id).await.unwrap(), + s.create_default_labels(&repo).await.unwrap(), 9, - "case empty repo" + "defaults: nine added" ); - let labels = s.list_labels(&fx.repo.id).await.unwrap(); - assert_eq!(labels.len(), 9, "case empty repo"); + let labels = s.list_labels(&repo).await.unwrap(); + assert_eq!(labels.len(), 9, "defaults: nine listed"); let bug = labels.iter().find(|l| l.name == "bug").unwrap(); - assert_eq!(bug.color, "#d73a4a", "case empty repo"); + assert_eq!(bug.color, "#d73a4a", "defaults: colour"); assert_eq!( bug.description, "Something isn't working", - "case empty repo" + "defaults: description" ); assert_eq!( - s.create_default_labels(&fx.repo.id).await.unwrap(), + s.create_default_labels(&repo).await.unwrap(), 0, - "case second run" + "defaults: second run adds nothing" ); assert_eq!( - s.list_labels(&fx.repo.id).await.unwrap().len(), + s.list_labels(&repo).await.unwrap().len(), 9, - "case second run" + "defaults: existing rows kept" ); - // Case 2: a name already taken (in another case) keeps the existing row. - s.create_label(&fx.other.id, "BUG", "#000000", "mine") + // Scenario 3: defaults skip names already taken. + let repo = fresh_repo(s, owner, "labels-taken").await; + s.create_label(&repo, "BUG", "#000000", "mine") .await .unwrap(); assert_eq!( - s.create_default_labels(&fx.other.id).await.unwrap(), + s.create_default_labels(&repo).await.unwrap(), 8, - "case taken name" + "taken: eight added" ); - let labels = s.list_labels(&fx.other.id).await.unwrap(); - assert_eq!(labels.len(), 9, "case taken name"); + let labels = s.list_labels(&repo).await.unwrap(); + assert_eq!(labels.len(), 9, "taken: nine listed"); let bug = labels.iter().find(|l| l.name == "BUG").unwrap(); - assert_eq!(bug.color, "#000000", "case taken name"); - assert_eq!(bug.description, "mine", "case taken name"); - } - - #[tokio::test] - async fn list_labels_is_sorted_and_scoped() { - let fx = fixture().await; - let s = &fx.store; - s.create_label(&fx.repo.id, "b", "#111111", "") - .await - .unwrap(); - s.create_label(&fx.repo.id, "A", "#111111", "") - .await - .unwrap(); - s.create_label(&fx.other.id, "c", "#111111", "") - .await - .unwrap(); + assert_eq!(bug.color, "#000000", "taken: own colour kept"); + assert_eq!(bug.description, "mine", "taken: own description kept"); + + // Scenario 4: listing is sorted and scoped to the repo. + let repo = fresh_repo(s, owner, "labels-sorted").await; + let other = fresh_repo(s, owner, "labels-sorted-other").await; + s.create_label(&repo, "b", "#111111", "").await.unwrap(); + s.create_label(&repo, "A", "#111111", "").await.unwrap(); + s.create_label(&other, "c", "#111111", "").await.unwrap(); assert_eq!( - names(&s.list_labels(&fx.repo.id).await.unwrap()), - ["A", "b"] + names(&s.list_labels(&repo).await.unwrap()), + ["A", "b"], + "sorted: case-insensitive order" + ); + assert_eq!( + names(&s.list_labels(&other).await.unwrap()), + ["c"], + "sorted: other repo only has its own" ); - assert_eq!(names(&s.list_labels(&fx.other.id).await.unwrap()), ["c"]); - } - - #[tokio::test] - async fn update_label_checks_conflicts_and_scope() { - let fx = fixture().await; - let s = &fx.store; - let bug = s - .create_label(&fx.repo.id, "bug", "#111111", "") - .await - .unwrap(); - s.create_label(&fx.repo.id, "docs", "#222222", "") - .await - .unwrap(); - let foreign = s - .create_label(&fx.other.id, "x", "#333333", "") - .await - .unwrap(); - assert!(matches!( - s.update_label(&fx.repo.id, &bug.id, "DOCS", "#111111", "") - .await, - Err(Error::Conflict(_)) - )); - assert!(matches!( - s.update_label(&fx.repo.id, &foreign.id, "y", "#111111", "") - .await, - Err(Error::NotFound) - )); - s.update_label(&fx.repo.id, &bug.id, "Defect", "#00FF00", "renamed") + // Scenario 5: update checks conflicts and scope. + let repo = fresh_repo(s, owner, "labels-update").await; + let other = fresh_repo(s, owner, "labels-update-other").await; + let bug = s.create_label(&repo, "bug", "#111111", "").await.unwrap(); + s.create_label(&repo, "docs", "#222222", "").await.unwrap(); + let foreign = s.create_label(&other, "x", "#333333", "").await.unwrap(); + assert!( + matches!( + s.update_label(&repo, &bug.id, "DOCS", "#111111", "").await, + Err(Error::Conflict(_)) + ), + "update: rename onto a taken name" + ); + assert!( + matches!( + s.update_label(&repo, &foreign.id, "y", "#111111", "").await, + Err(Error::NotFound) + ), + "update: label of another repo" + ); + s.update_label(&repo, &bug.id, "Defect", "#00FF00", "renamed") .await .unwrap(); - let labels = s.list_labels(&fx.repo.id).await.unwrap(); - assert_eq!(names(&labels), ["Defect", "docs"]); - assert_eq!(labels[0].color, "#00ff00"); - assert_eq!(labels[0].description, "renamed"); - // The foreign label was not touched. - assert_eq!(names(&s.list_labels(&fx.other.id).await.unwrap()), ["x"]); - } + let labels = s.list_labels(&repo).await.unwrap(); + assert_eq!(names(&labels), ["Defect", "docs"], "update: renamed"); + assert_eq!(labels[0].color, "#00ff00", "update: colour normalized"); + assert_eq!(labels[0].description, "renamed", "update: description"); + assert_eq!( + names(&s.list_labels(&other).await.unwrap()), + ["x"], + "update: foreign label untouched" + ); - #[tokio::test] - async fn delete_label_removes_links_and_is_scoped() { - let fx = fixture().await; - let s = &fx.store; - let bug = s - .create_label(&fx.repo.id, "bug", "#111111", "") - .await - .unwrap(); + // Scenario 6: delete removes links and is scoped to the repo. + let repo = fresh_repo(s, owner, "labels-delete").await; + let other = fresh_repo(s, owner, "labels-delete-other").await; + let bug = s.create_label(&repo, "bug", "#111111", "").await.unwrap(); let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") + .create_issue(&repo, &fx.tahinli.id, "t", "") .await .unwrap(); s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id)) .await .unwrap(); - - assert!(matches!( - s.delete_label(&fx.other.id, &bug.id).await, - Err(Error::NotFound) - )); - assert_eq!(s.issue_labels(&issue.id).await.unwrap().len(), 1); - - s.delete_label(&fx.repo.id, &bug.id).await.unwrap(); - assert!(s.issue_labels(&issue.id).await.unwrap().is_empty()); - assert!(s.list_labels(&fx.repo.id).await.unwrap().is_empty()); + assert!( + matches!(s.delete_label(&other, &bug.id).await, Err(Error::NotFound)), + "delete: wrong repo" + ); + assert_eq!( + s.issue_labels(&issue.id).await.unwrap().len(), + 1, + "delete: link kept after refused delete" + ); + s.delete_label(&repo, &bug.id).await.unwrap(); + assert!( + s.issue_labels(&issue.id).await.unwrap().is_empty(), + "delete: link removed" + ); + assert!( + s.list_labels(&repo).await.unwrap().is_empty(), + "delete: label gone" + ); } + // Labels and assignees on one issue. Each scenario has its own repo. #[tokio::test] - async fn set_issue_labels_diffs_and_records_events() { + async fn issue_label_and_assignee_rules_hold_in_each_scenario() { let fx = fixture().await; let s = &fx.store; - let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") - .await - .unwrap(); - let a = s - .create_label(&fx.repo.id, "a", "#111111", "") - .await - .unwrap(); - let b = s - .create_label(&fx.repo.id, "b", "#111111", "") - .await - .unwrap(); - let c = s - .create_label(&fx.repo.id, "c", "#111111", "") - .await - .unwrap(); + let owner = &fx.tahinli.id; - s.set_issue_labels(&issue.id, &fx.tahinli.id, &[a.id.clone(), b.id.clone()]) + // Scenario 1: label changes are diffed and recorded as events. + let repo = fresh_repo(s, owner, "issue-labels").await; + let issue = s.create_issue(&repo, owner, "t", "").await.unwrap(); + let a = s.create_label(&repo, "a", "#111111", "").await.unwrap(); + let b = s.create_label(&repo, "b", "#111111", "").await.unwrap(); + let c = s.create_label(&repo, "c", "#111111", "").await.unwrap(); + + s.set_issue_labels(&issue.id, owner, &[a.id.clone(), b.id.clone()]) .await .unwrap(); let events = s.list_events(&issue.id).await.unwrap(); let labeled = data_of(&events, "labeled"); - assert_eq!(labeled.len(), 2); - assert!(labeled.contains(&json!({"label": "a"}))); - assert!(labeled.contains(&json!({"label": "b"}))); - assert!(data_of(&events, "unlabeled").is_empty()); + assert_eq!(labeled.len(), 2, "labels: two added"); + assert!( + labeled.contains(&json!({"label": "a"})), + "labels: a recorded" + ); + assert!( + labeled.contains(&json!({"label": "b"})), + "labels: b recorded" + ); + assert!( + data_of(&events, "unlabeled").is_empty(), + "labels: none removed" + ); - s.set_issue_labels(&issue.id, &fx.tahinli.id, &[b.id.clone(), c.id.clone()]) + s.set_issue_labels(&issue.id, owner, &[b.id.clone(), c.id.clone()]) .await .unwrap(); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "unlabeled"), [json!({"label": "a"})]); - assert_eq!(data_of(&events, "labeled").len(), 3); - assert_eq!(names(&s.issue_labels(&issue.id).await.unwrap()), ["b", "c"]); + assert_eq!( + data_of(&events, "unlabeled"), + [json!({"label": "a"})], + "labels: a removed" + ); + assert_eq!(data_of(&events, "labeled").len(), 3, "labels: c added"); + assert_eq!( + names(&s.issue_labels(&issue.id).await.unwrap()), + ["b", "c"], + "labels: resulting set" + ); // Setting the same set again changes nothing and records nothing. - s.set_issue_labels(&issue.id, &fx.tahinli.id, &[c.id.clone(), b.id.clone()]) + s.set_issue_labels(&issue.id, owner, &[c.id.clone(), b.id.clone()]) .await .unwrap(); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "labeled").len(), 3); - assert_eq!(data_of(&events, "unlabeled").len(), 1); - } + assert_eq!( + data_of(&events, "labeled").len(), + 3, + "labels: same set, no add" + ); + assert_eq!( + data_of(&events, "unlabeled").len(), + 1, + "labels: same set, no remove" + ); - #[tokio::test] - async fn set_issue_labels_rejects_foreign_label_without_partial_write() { - let fx = fixture().await; - let s = &fx.store; - let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") - .await - .unwrap(); - let a = s - .create_label(&fx.repo.id, "a", "#111111", "") - .await - .unwrap(); - let b = s - .create_label(&fx.repo.id, "b", "#111111", "") - .await - .unwrap(); - let foreign = s - .create_label(&fx.other.id, "x", "#111111", "") - .await - .unwrap(); - s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&a.id)) + // Scenario 2: a label from another repo rejects the whole write. + let repo = fresh_repo(s, owner, "issue-labels-repo").await; + let other = fresh_repo(s, owner, "issue-labels-other").await; + let issue = s.create_issue(&repo, owner, "t", "").await.unwrap(); + let a = s.create_label(&repo, "a", "#111111", "").await.unwrap(); + let b = s.create_label(&repo, "b", "#111111", "").await.unwrap(); + let foreign = s.create_label(&other, "x", "#111111", "").await.unwrap(); + s.set_issue_labels(&issue.id, owner, std::slice::from_ref(&a.id)) .await .unwrap(); let err = s - .set_issue_labels( - &issue.id, - &fx.tahinli.id, - &[b.id.clone(), foreign.id.clone()], - ) + .set_issue_labels(&issue.id, owner, &[b.id.clone(), foreign.id.clone()]) .await; - assert!(matches!(err, Err(Error::Invalid(_)))); - assert_eq!(names(&s.issue_labels(&issue.id).await.unwrap()), ["a"]); + assert!(matches!(err, Err(Error::Invalid(_))), "foreign: refused"); + assert_eq!( + names(&s.issue_labels(&issue.id).await.unwrap()), + ["a"], + "foreign: no partial write" + ); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "labeled").len(), 1); - - assert!(matches!( - s.set_issue_labels("missing", &fx.tahinli.id, &[]).await, - Err(Error::NotFound) - )); - } + assert_eq!(data_of(&events, "labeled").len(), 1, "foreign: no event"); + assert!( + matches!( + s.set_issue_labels("missing", owner, &[]).await, + Err(Error::NotFound) + ), + "foreign: unknown issue" + ); - #[tokio::test] - async fn set_issue_assignees_dedups_resolves_and_records_events() { - let fx = fixture().await; - let s = &fx.store; - let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") - .await - .unwrap(); + // Scenario 3: assignees are deduplicated, resolved and recorded. + let repo = fresh_repo(s, owner, "assignees").await; + let issue = s.create_issue(&repo, owner, "t", "").await.unwrap(); let input = [ "bob".to_string(), "BOB".to_string(), " Tahinli ".to_string(), ]; - s.set_issue_assignees(&issue.id, &fx.tahinli.id, &input) + s.set_issue_assignees(&issue.id, owner, &input) .await .unwrap(); let assignees = s.issue_assignees(&issue.id).await.unwrap(); let display: Vec<&str> = assignees.iter().map(|(_, n)| n.as_str()).collect(); - assert_eq!(display, ["Bob", "Tahinli"]); + assert_eq!( + display, + ["Bob", "Tahinli"], + "assignees: deduplicated and resolved" + ); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "assigned").len(), 2); - assert!(data_of(&events, "assigned").contains(&json!({"user": "Bob"}))); + assert_eq!( + data_of(&events, "assigned").len(), + 2, + "assignees: two events" + ); + assert!( + data_of(&events, "assigned").contains(&json!({"user": "Bob"})), + "assignees: Bob recorded" + ); - s.set_issue_assignees(&issue.id, &fx.tahinli.id, &["tahinli".to_string()]) + s.set_issue_assignees(&issue.id, owner, &["tahinli".to_string()]) .await .unwrap(); let assignees = s.issue_assignees(&issue.id).await.unwrap(); - assert_eq!(assignees.len(), 1); - assert_eq!(assignees[0].0, fx.tahinli.id); + assert_eq!(assignees.len(), 1, "assignees: replaced"); + assert_eq!(assignees[0].0, fx.tahinli.id, "assignees: Tahinli kept"); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "unassigned"), [json!({"user": "Bob"})]); - } - - #[tokio::test] - async fn set_issue_assignees_enforces_limit_existence_and_read_access() { - let fx = fixture().await; - let s = &fx.store; - let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") - .await - .unwrap(); + assert_eq!( + data_of(&events, "unassigned"), + [json!({"user": "Bob"})], + "assignees: Bob removed" + ); + // Scenario 4: the limit, existence and read access are enforced. + let repo = fresh_repo(s, owner, "assign-limit").await; + let issue = s.create_issue(&repo, owner, "t", "").await.unwrap(); let mut usernames = Vec::new(); for i in 0..11 { let name = format!("user{i}"); s.create_user(&name, None, "hash", false).await.unwrap(); usernames.push(name); } - assert!(matches!( - s.set_issue_assignees(&issue.id, &fx.tahinli.id, &usernames) - .await, - Err(Error::Invalid(_)) - )); - s.set_issue_assignees(&issue.id, &fx.tahinli.id, &usernames[..10]) + assert!( + matches!( + s.set_issue_assignees(&issue.id, owner, &usernames).await, + Err(Error::Invalid(_)) + ), + "limit: eleven refused" + ); + s.set_issue_assignees(&issue.id, owner, &usernames[..10]) .await .unwrap(); - assert_eq!(s.issue_assignees(&issue.id).await.unwrap().len(), 10); - - assert!(matches!( - s.set_issue_assignees(&issue.id, &fx.tahinli.id, &["ghost".to_string()]) - .await, - Err(Error::Invalid(_)) - )); + assert_eq!( + s.issue_assignees(&issue.id).await.unwrap().len(), + 10, + "limit: ten allowed" + ); + assert!( + matches!( + s.set_issue_assignees(&issue.id, owner, &["ghost".to_string()]) + .await, + Err(Error::Invalid(_)) + ), + "limit: unknown user refused" + ); // Bob has no role on a private repo, so he cannot be assigned there. let secret = s - .create_repo(&fx.tahinli.id, "secret", "", Visibility::Private, "main") - .await - .unwrap(); - let secret_issue = s - .create_issue(&secret.id, &fx.tahinli.id, "t", "") + .create_repo(owner, "assign-secret", "", Visibility::Private, "main") .await .unwrap(); + let secret_issue = s.create_issue(&secret.id, owner, "t", "").await.unwrap(); let bob = ["bob".to_string()]; - assert!(matches!( - s.set_issue_assignees(&secret_issue.id, &fx.tahinli.id, &bob) - .await, - Err(Error::Invalid(_)) - )); + assert!( + matches!( + s.set_issue_assignees(&secret_issue.id, owner, &bob).await, + Err(Error::Invalid(_)) + ), + "access: no role refused" + ); s.set_collaborator(&secret.id, &fx.bob.id, Role::Read) .await .unwrap(); - s.set_issue_assignees(&secret_issue.id, &fx.tahinli.id, &bob) + s.set_issue_assignees(&secret_issue.id, owner, &bob) .await .unwrap(); - assert_eq!(s.issue_assignees(&secret_issue.id).await.unwrap().len(), 1); + assert_eq!( + s.issue_assignees(&secret_issue.id).await.unwrap().len(), + 1, + "access: reader can be assigned" + ); } + // Milestones: creating, listing, updating, linking to issues and deleting. #[tokio::test] - async fn milestones_create_validate_and_count_in_one_listing() { + async fn milestone_rules_hold_in_each_scenario() { let fx = fixture().await; let s = &fx.store; + let owner = &fx.tahinli.id; + + // Scenario 1: dates are validated, titles unique, counts shown in one listing. + let repo = fresh_repo(s, owner, "milestones").await; + let other = fresh_repo(s, owner, "milestones-other").await; let v1 = s - .create_milestone(&fx.repo.id, "v1", "first", Some("2026-12-31")) + .create_milestone(&repo, "v1", "first", Some("2026-12-31")) .await .unwrap(); - assert_eq!(v1.due_on.as_deref(), Some("2026-12-31")); + assert_eq!( + v1.due_on.as_deref(), + Some("2026-12-31"), + "create: due date kept" + ); for bad in ["2026/12/31", "2026-02-30", "2026-1-5", "2026-12-31T00:00"] { - assert!(matches!( - s.create_milestone(&fx.repo.id, "x", "", Some(bad)).await, - Err(Error::Invalid(_)) - )); + assert!( + matches!( + s.create_milestone(&repo, "x", "", Some(bad)).await, + Err(Error::Invalid(_)) + ), + "create: bad date {bad}" + ); } - assert!(matches!( - s.create_milestone(&fx.repo.id, "V1", "", None).await, - Err(Error::Conflict(_)) - )); + assert!( + matches!( + s.create_milestone(&repo, "V1", "", None).await, + Err(Error::Conflict(_)) + ), + "create: duplicate title ignoring case" + ); - let i1 = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "a", "") - .await - .unwrap(); - let i2 = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "b", "") + let i1 = s.create_issue(&repo, owner, "a", "").await.unwrap(); + let i2 = s.create_issue(&repo, owner, "b", "").await.unwrap(); + let i3 = s.create_issue(&repo, owner, "c", "").await.unwrap(); + s.set_issue_milestone(&i1.id, owner, Some(&v1.id)) .await .unwrap(); - let i3 = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "c", "") - .await - .unwrap(); - s.set_issue_milestone(&i1.id, &fx.tahinli.id, Some(&v1.id)) - .await - .unwrap(); - s.set_issue_milestone(&i2.id, &fx.tahinli.id, Some(&v1.id)) - .await - .unwrap(); - s.set_issue_milestone(&i3.id, &fx.tahinli.id, None) + s.set_issue_milestone(&i2.id, owner, Some(&v1.id)) .await .unwrap(); + s.set_issue_milestone(&i3.id, owner, None).await.unwrap(); s.set_issue_state( &i1.id, Some(&fx.tahinli.id), @@ -1178,184 +1195,184 @@ mod tests { .await .unwrap(); - let all = s.list_milestones(&fx.repo.id, None).await.unwrap(); - assert_eq!(all.len(), 1); - assert_eq!((all[0].open_issues, all[0].closed_issues), (1, 1)); + let all = s.list_milestones(&repo, None).await.unwrap(); + assert_eq!(all.len(), 1, "list: one milestone"); + assert_eq!( + (all[0].open_issues, all[0].closed_issues), + (1, 1), + "list: counts" + ); assert!( - s.list_milestones(&fx.repo.id, Some(IssueState::Closed)) + s.list_milestones(&repo, Some(IssueState::Closed)) .await .unwrap() - .is_empty() + .is_empty(), + "list: no closed milestone" ); assert_eq!( - s.list_milestones(&fx.repo.id, Some(IssueState::Open)) + s.list_milestones(&repo, Some(IssueState::Open)) .await .unwrap() .len(), - 1 + 1, + "list: one open milestone" ); - let got = s - .milestone_by_id(&fx.repo.id, &v1.id) - .await - .unwrap() - .unwrap(); - assert_eq!((got.open_issues, got.closed_issues), (1, 1)); + let got = s.milestone_by_id(&repo, &v1.id).await.unwrap().unwrap(); + assert_eq!( + (got.open_issues, got.closed_issues), + (1, 1), + "lookup: counts" + ); assert!( - s.milestone_by_id(&fx.other.id, &v1.id) - .await - .unwrap() - .is_none() + s.milestone_by_id(&other, &v1.id).await.unwrap().is_none(), + "lookup: other repo finds nothing" ); - } - #[tokio::test] - async fn update_milestone_sets_closed_at_and_is_scoped() { - let fx = fixture().await; - let s = &fx.store; + // Scenario 2: updates set and clear the closed stamp and stay scoped. + let repo = fresh_repo(s, owner, "milestone-update").await; + let other = fresh_repo(s, owner, "milestone-update-other").await; let v1 = s - .create_milestone(&fx.repo.id, "v1", "", Some("2026-12-31")) + .create_milestone(&repo, "v1", "", Some("2026-12-31")) .await .unwrap(); - assert!(matches!( - s.update_milestone(&fx.other.id, &v1.id, "v1", "", None, IssueState::Open) - .await, - Err(Error::NotFound) - )); - - s.update_milestone( - &fx.repo.id, - &v1.id, - "v1.0", - "done", - None, - IssueState::Closed, - ) - .await - .unwrap(); - let closed = s - .milestone_by_id(&fx.repo.id, &v1.id) + assert!( + matches!( + s.update_milestone(&other, &v1.id, "v1", "", None, IssueState::Open) + .await, + Err(Error::NotFound) + ), + "update: other repo refused" + ); + s.update_milestone(&repo, &v1.id, "v1.0", "done", None, IssueState::Closed) .await - .unwrap() .unwrap(); - assert_eq!(closed.title, "v1.0"); - assert_eq!(closed.description, "done"); - assert!(closed.due_on.is_none()); - assert!(matches!(closed.state, IssueState::Closed)); - assert!(closed.closed_at.is_some()); + let closed = s.milestone_by_id(&repo, &v1.id).await.unwrap().unwrap(); + assert_eq!(closed.title, "v1.0", "update: title"); + assert_eq!(closed.description, "done", "update: description"); + assert!(closed.due_on.is_none(), "update: due date cleared"); + assert!(matches!(closed.state, IssueState::Closed), "update: closed"); + assert!(closed.closed_at.is_some(), "update: closed stamp set"); - s.update_milestone(&fx.repo.id, &v1.id, "v1.0", "done", None, IssueState::Open) + s.update_milestone(&repo, &v1.id, "v1.0", "done", None, IssueState::Open) .await .unwrap(); - let reopened = s - .milestone_by_id(&fx.repo.id, &v1.id) - .await - .unwrap() - .unwrap(); - assert!(matches!(reopened.state, IssueState::Open)); - assert!(reopened.closed_at.is_none()); - } + let reopened = s.milestone_by_id(&repo, &v1.id).await.unwrap().unwrap(); + assert!( + matches!(reopened.state, IssueState::Open), + "update: reopened" + ); + assert!(reopened.closed_at.is_none(), "update: closed stamp cleared"); - #[tokio::test] - async fn set_issue_milestone_checks_repo_and_records_events() { - let fx = fixture().await; - let s = &fx.store; - let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") - .await - .unwrap(); - let v1 = s - .create_milestone(&fx.repo.id, "v1", "", None) - .await - .unwrap(); - let v2 = s - .create_milestone(&fx.repo.id, "v2", "", None) - .await - .unwrap(); - let foreign = s - .create_milestone(&fx.other.id, "v9", "", None) - .await - .unwrap(); + // Scenario 3: an issue takes only milestones of its own repo and records changes. + let repo = fresh_repo(s, owner, "issue-milestone").await; + let other = fresh_repo(s, owner, "issue-milestone-other").await; + let issue = s.create_issue(&repo, owner, "t", "").await.unwrap(); + let v1 = s.create_milestone(&repo, "v1", "", None).await.unwrap(); + let v2 = s.create_milestone(&repo, "v2", "", None).await.unwrap(); + let foreign = s.create_milestone(&other, "v9", "", None).await.unwrap(); - assert!(matches!( - s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&foreign.id)) - .await, - Err(Error::Invalid(_)) - )); + assert!( + matches!( + s.set_issue_milestone(&issue.id, owner, Some(&foreign.id)) + .await, + Err(Error::Invalid(_)) + ), + "issue milestone: other repo refused" + ); - s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&v1.id)) + s.set_issue_milestone(&issue.id, owner, Some(&v1.id)) .await .unwrap(); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "milestoned"), [json!({"title": "v1"})]); + assert_eq!( + data_of(&events, "milestoned"), + [json!({"title": "v1"})], + "issue milestone: set" + ); - s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&v2.id)) + s.set_issue_milestone(&issue.id, owner, Some(&v2.id)) .await .unwrap(); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "demilestoned"), [json!({"title": "v1"})]); - assert_eq!(data_of(&events, "milestoned").len(), 2); + assert_eq!( + data_of(&events, "demilestoned"), + [json!({"title": "v1"})], + "issue milestone: old one removed" + ); + assert_eq!( + data_of(&events, "milestoned").len(), + 2, + "issue milestone: new one set" + ); - s.set_issue_milestone(&issue.id, &fx.tahinli.id, None) - .await - .unwrap(); + s.set_issue_milestone(&issue.id, owner, None).await.unwrap(); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "demilestoned").len(), 2); - assert!(data_of(&events, "demilestoned").contains(&json!({"title": "v2"}))); + assert_eq!( + data_of(&events, "demilestoned").len(), + 2, + "issue milestone: cleared" + ); + assert!( + data_of(&events, "demilestoned").contains(&json!({"title": "v2"})), + "issue milestone: v2 recorded" + ); // Clearing again is a no-op. - s.set_issue_milestone(&issue.id, &fx.tahinli.id, None) - .await - .unwrap(); + s.set_issue_milestone(&issue.id, owner, None).await.unwrap(); let events = s.list_events(&issue.id).await.unwrap(); - assert_eq!(data_of(&events, "demilestoned").len(), 2); + assert_eq!( + data_of(&events, "demilestoned").len(), + 2, + "issue milestone: clearing twice records nothing" + ); let stored = s - .issue_by_number(&fx.repo.id, issue.number) + .issue_by_number(&repo, issue.number) .await .unwrap() .unwrap(); - assert!(stored.milestone_id.is_none()); - } + assert!( + stored.milestone_id.is_none(), + "issue milestone: none stored" + ); - #[tokio::test] - async fn delete_milestone_clears_issue_links_and_is_scoped() { - let fx = fixture().await; - let s = &fx.store; - let v1 = s - .create_milestone(&fx.repo.id, "v1", "", None) - .await - .unwrap(); - let issue = s - .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "") - .await - .unwrap(); - s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&v1.id)) + // Scenario 4: deleting a milestone clears issue links and is scoped. + let repo = fresh_repo(s, owner, "milestone-delete").await; + let other = fresh_repo(s, owner, "milestone-delete-other").await; + let v1 = s.create_milestone(&repo, "v1", "", None).await.unwrap(); + let issue = s.create_issue(&repo, owner, "t", "").await.unwrap(); + s.set_issue_milestone(&issue.id, owner, Some(&v1.id)) .await .unwrap(); - assert!(matches!( - s.delete_milestone(&fx.other.id, &v1.id).await, - Err(Error::NotFound) - )); + assert!( + matches!( + s.delete_milestone(&other, &v1.id).await, + Err(Error::NotFound) + ), + "delete: other repo refused" + ); let kept = s - .issue_by_number(&fx.repo.id, issue.number) + .issue_by_number(&repo, issue.number) .await .unwrap() .unwrap(); - assert_eq!(kept.milestone_id.as_deref(), Some(v1.id.as_str())); + assert_eq!( + kept.milestone_id.as_deref(), + Some(v1.id.as_str()), + "delete: link kept after refused delete" + ); - s.delete_milestone(&fx.repo.id, &v1.id).await.unwrap(); + s.delete_milestone(&repo, &v1.id).await.unwrap(); assert!( - s.milestone_by_id(&fx.repo.id, &v1.id) - .await - .unwrap() - .is_none() + s.milestone_by_id(&repo, &v1.id).await.unwrap().is_none(), + "delete: milestone gone" ); let after = s - .issue_by_number(&fx.repo.id, issue.number) + .issue_by_number(&repo, issue.number) .await .unwrap() .unwrap(); - assert!(after.milestone_id.is_none()); + assert!(after.milestone_id.is_none(), "delete: issue link cleared"); } } diff --git a/crates/forge-core/src/store/notifications.rs b/crates/forge-core/src/store/notifications.rs index ca08f61..a5521b2 100644 --- a/crates/forge-core/src/store/notifications.rs +++ b/crates/forge-core/src/store/notifications.rs @@ -454,108 +454,140 @@ mod tests { .reason } + /// A user with a unique name, so per-user inbox queries stay exact in a shared store. + async fn user(store: &Store, name: &str) -> String { + store + .create_user(name, None, "hash", false) + .await + .unwrap() + .id + } + + // Scenarios share one store. Each one uses its own issue, repo or fresh users + // wherever the assertions read repo-wide or per-user state. #[tokio::test] - async fn watch_level_defaults_to_participating_and_watchers_lists_only_watching() { + async fn watch_and_subscription_rules_hold_in_each_scenario() { let fx = fixture().await; let s = &fx.store; + + // Scenario 1: watch levels default to participating; watchers lists only watchers. + let repo = s + .create_repo(&fx.owner, "watch", "", Visibility::Public, "main") + .await + .unwrap() + .id; assert_eq!( - s.watch_level(&fx.tahinli, &fx.repo).await.unwrap(), - WatchLevel::Participating + s.watch_level(&fx.tahinli, &repo).await.unwrap(), + WatchLevel::Participating, + "watch: default level" ); - s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Watching) + s.set_watch(&fx.tahinli, &repo, WatchLevel::Watching) .await .unwrap(); - s.set_watch(&fx.bob, &fx.repo, WatchLevel::Ignoring) + s.set_watch(&fx.bob, &repo, WatchLevel::Ignoring) .await .unwrap(); assert_eq!( - s.watch_level(&fx.tahinli, &fx.repo).await.unwrap(), - WatchLevel::Watching + s.watch_level(&fx.tahinli, &repo).await.unwrap(), + WatchLevel::Watching, + "watch: set level is read back" ); - s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Participating) + s.set_watch(&fx.tahinli, &repo, WatchLevel::Participating) .await .unwrap(); - s.set_watch(&fx.bob, &fx.repo, WatchLevel::Watching) + s.set_watch(&fx.bob, &repo, WatchLevel::Watching) .await .unwrap(); - s.set_watch(&fx.owner, &fx.repo, WatchLevel::Watching) + s.set_watch(&fx.owner, &repo, WatchLevel::Watching) .await .unwrap(); - let watchers = s.watchers(&fx.repo).await.unwrap(); + let watchers = s.watchers(&repo).await.unwrap(); let mut expected = vec![fx.bob.clone(), fx.owner.clone()]; expected.sort(); - assert_eq!(watchers, expected); - } + assert_eq!(watchers, expected, "watch: only watching users listed"); - #[tokio::test] - async fn auto_subscribe_never_overrides_ignored_or_existing() { - let fx = fixture().await; - let s = &fx.store; - s.auto_subscribe(&fx.tahinli, &fx.issue_a, Reason::Comment) + // Scenario 2: auto-subscribe never overrides an ignore; explicit subscribe does. + let issue = s + .create_issue(&fx.repo, &fx.owner, "Third", "") + .await + .unwrap() + .id; + s.auto_subscribe(&fx.tahinli, &issue, Reason::Comment) .await .unwrap(); assert_eq!( - s.subscription(&fx.tahinli, &fx.issue_a).await.unwrap(), - Some((SubState::Subscribed, Reason::Comment)) + s.subscription(&fx.tahinli, &issue).await.unwrap(), + Some((SubState::Subscribed, Reason::Comment)), + "auto: first subscription" ); // Existing row stays as it was. - s.auto_subscribe(&fx.tahinli, &fx.issue_a, Reason::Mention) + s.auto_subscribe(&fx.tahinli, &issue, Reason::Mention) .await .unwrap(); assert_eq!( - s.subscription(&fx.tahinli, &fx.issue_a).await.unwrap(), - Some((SubState::Subscribed, Reason::Comment)) + s.subscription(&fx.tahinli, &issue).await.unwrap(), + Some((SubState::Subscribed, Reason::Comment)), + "auto: existing row kept" ); // An explicit ignore is never replaced by auto-subscribe. - s.subscribe(&fx.bob, &fx.issue_a, SubState::Ignored, Reason::Subscribed) + s.subscribe(&fx.bob, &issue, SubState::Ignored, Reason::Subscribed) .await .unwrap(); - s.auto_subscribe(&fx.bob, &fx.issue_a, Reason::Author) + s.auto_subscribe(&fx.bob, &issue, Reason::Author) .await .unwrap(); assert_eq!( - s.subscription(&fx.bob, &fx.issue_a).await.unwrap(), - Some((SubState::Ignored, Reason::Subscribed)) + s.subscription(&fx.bob, &issue).await.unwrap(), + Some((SubState::Ignored, Reason::Subscribed)), + "auto: ignore kept" ); // Explicit subscribe does replace. - s.subscribe(&fx.bob, &fx.issue_a, SubState::Subscribed, Reason::Author) + s.subscribe(&fx.bob, &issue, SubState::Subscribed, Reason::Author) .await .unwrap(); assert_eq!( - s.subscription(&fx.bob, &fx.issue_a).await.unwrap(), - Some((SubState::Subscribed, Reason::Author)) + s.subscription(&fx.bob, &issue).await.unwrap(), + Some((SubState::Subscribed, Reason::Author)), + "auto: explicit subscribe replaces" + ); + assert_eq!( + s.subscription(&fx.owner, &issue).await.unwrap(), + None, + "auto: untouched user has no row" ); - assert_eq!(s.subscription(&fx.owner, &fx.issue_a).await.unwrap(), None); - } - #[tokio::test] - async fn subscribers_lists_only_subscribed() { - let fx = fixture().await; - let s = &fx.store; - s.subscribe( - &fx.tahinli, - &fx.issue_a, - SubState::Subscribed, - Reason::Assign, - ) - .await - .unwrap(); - s.subscribe(&fx.bob, &fx.issue_a, SubState::Ignored, Reason::Subscribed) + // Scenario 3: subscribers lists only subscribed users of one issue. + let issue = s + .create_issue(&fx.repo, &fx.owner, "Fourth", "") + .await + .unwrap() + .id; + s.subscribe(&fx.tahinli, &issue, SubState::Subscribed, Reason::Assign) + .await + .unwrap(); + s.subscribe(&fx.bob, &issue, SubState::Ignored, Reason::Subscribed) .await .unwrap(); s.subscribe(&fx.owner, &fx.issue_b, SubState::Subscribed, Reason::Author) .await .unwrap(); - let subs = s.subscribers(&fx.issue_a).await.unwrap(); - assert_eq!(subs, vec![(fx.tahinli.clone(), Reason::Assign)]); + let subs = s.subscribers(&issue).await.unwrap(); + assert_eq!( + subs, + vec![(fx.tahinli.clone(), Reason::Assign)], + "subscribers: only subscribed users" + ); } #[tokio::test] - async fn upsert_keeps_stronger_reason_while_unread_and_replaces_after_read() { + async fn inbox_rules_hold_in_each_scenario() { let fx = fixture().await; let s = &fx.store; + + // Scenario 1: a stronger reason wins while unread; a read row is replaced. + let ivy = user(s, "Ivy").await; s.upsert_notification( - &fx.tahinli, + &ivy, &fx.repo, &fx.issue_a, Reason::Mention, @@ -565,7 +597,7 @@ mod tests { .await .unwrap(); s.upsert_notification( - &fx.tahinli, + &ivy, &fx.repo, &fx.issue_a, Reason::Comment, @@ -575,15 +607,20 @@ mod tests { .await .unwrap(); assert_eq!( - reason_of(s, &fx.tahinli, &fx.issue_a).await, - Reason::Mention + reason_of(s, &ivy, &fx.issue_a).await, + Reason::Mention, + "upsert: stronger reason kept" ); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1); + assert_eq!(s.unread_count(&ivy).await.unwrap(), 1, "upsert: one unread"); - s.mark_issue_read(&fx.tahinli, &fx.issue_a).await.unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0); + s.mark_issue_read(&ivy, &fx.issue_a).await.unwrap(); + assert_eq!( + s.unread_count(&ivy).await.unwrap(), + 0, + "upsert: marked read" + ); s.upsert_notification( - &fx.tahinli, + &ivy, &fx.repo, &fx.issue_a, Reason::Comment, @@ -593,14 +630,19 @@ mod tests { .await .unwrap(); assert_eq!( - reason_of(s, &fx.tahinli, &fx.issue_a).await, - Reason::Comment + reason_of(s, &ivy, &fx.issue_a).await, + Reason::Comment, + "upsert: read row replaced" + ); + assert_eq!( + s.unread_count(&ivy).await.unwrap(), + 1, + "upsert: unread again" ); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1); // A stronger reason while unread still wins. s.upsert_notification( - &fx.tahinli, + &ivy, &fx.repo, &fx.issue_a, Reason::Assign, @@ -609,22 +651,20 @@ mod tests { ) .await .unwrap(); - let inbox = s - .list_notifications(&fx.tahinli, false, None, 10) - .await - .unwrap(); - assert_eq!(inbox.len(), 1); - assert_eq!(inbox[0].reason, Reason::Assign); - assert_eq!(inbox[0].last_activity, "assigned"); - assert_eq!(inbox[0].last_actor_name, None); - } + let inbox = s.list_notifications(&ivy, false, None, 10).await.unwrap(); + assert_eq!(inbox.len(), 1, "upsert: still one row"); + assert_eq!( + inbox[0].reason, + Reason::Assign, + "upsert: assign wins while unread" + ); + assert_eq!(inbox[0].last_activity, "assigned", "upsert: activity text"); + assert_eq!(inbox[0].last_actor_name, None, "upsert: no actor given"); - #[tokio::test] - async fn list_orders_newest_first_and_pages_with_cursor() { - let fx = fixture().await; - let s = &fx.store; + // Scenario 2: listing is newest first, paged by cursor, and per user. + let kim = user(s, "Kim").await; s.upsert_notification( - &fx.tahinli, + &kim, &fx.repo, &fx.issue_a, Reason::Comment, @@ -634,7 +674,7 @@ mod tests { .await .unwrap(); s.upsert_notification( - &fx.tahinli, + &kim, &fx.repo, &fx.issue_b, Reason::Comment, @@ -643,62 +683,56 @@ mod tests { ) .await .unwrap(); - s.mark_issue_read(&fx.tahinli, &fx.issue_b).await.unwrap(); + s.mark_issue_read(&kim, &fx.issue_b).await.unwrap(); + + let all = s.list_notifications(&kim, false, None, 50).await.unwrap(); + assert_eq!(all.len(), 2, "list: two rows"); + assert_eq!(all[0].issue_id, fx.issue_b, "list: newest first"); + assert_eq!(all[1].issue_id, fx.issue_a, "list: oldest last"); + assert_eq!(all[0].repo_owner, "Owen", "list: repo owner"); + assert_eq!(all[0].repo_name, "proj", "list: repo name"); + assert_eq!(all[0].number, 2, "list: issue number"); + assert_eq!(all[0].issue_state, "open", "list: issue state"); + assert!(!all[0].is_pull, "list: issue, not a pull request"); + assert_eq!( + all[0].last_actor_name.as_deref(), + Some("Bob"), + "list: last actor" + ); - let all = s - .list_notifications(&fx.tahinli, false, None, 50) - .await - .unwrap(); - assert_eq!(all.len(), 2); - assert_eq!(all[0].issue_id, fx.issue_b); - assert_eq!(all[1].issue_id, fx.issue_a); - assert_eq!(all[0].repo_owner, "Owen"); - assert_eq!(all[0].repo_name, "proj"); - assert_eq!(all[0].number, 2); - assert_eq!(all[0].issue_state, "open"); - assert!(!all[0].is_pull); - assert_eq!(all[0].last_actor_name.as_deref(), Some("Bob")); - - let unread = s - .list_notifications(&fx.tahinli, true, None, 50) - .await - .unwrap(); - assert_eq!(unread.len(), 1); - assert_eq!(unread[0].issue_id, fx.issue_a); + let unread = s.list_notifications(&kim, true, None, 50).await.unwrap(); + assert_eq!(unread.len(), 1, "list: unread filter"); + assert_eq!(unread[0].issue_id, fx.issue_a, "list: unread row"); // Cursor: first page of one, then the rest after its stamp. - let first = s - .list_notifications(&fx.tahinli, false, None, 1) - .await - .unwrap(); - assert_eq!(first[0].issue_id, fx.issue_b); + let first = s.list_notifications(&kim, false, None, 1).await.unwrap(); + assert_eq!(first[0].issue_id, fx.issue_b, "cursor: first page"); let second = s - .list_notifications(&fx.tahinli, false, Some(&first[0].updated_at), 1) + .list_notifications(&kim, false, Some(&first[0].updated_at), 1) .await .unwrap(); - assert_eq!(second.len(), 1); - assert_eq!(second[0].issue_id, fx.issue_a); + assert_eq!(second.len(), 1, "cursor: second page size"); + assert_eq!(second[0].issue_id, fx.issue_a, "cursor: second page row"); let none = s - .list_notifications(&fx.tahinli, false, Some(&second[0].updated_at), 1) + .list_notifications(&kim, false, Some(&second[0].updated_at), 1) .await .unwrap(); - assert!(none.is_empty()); + assert!(none.is_empty(), "cursor: past the end"); // Other users see nothing. assert!( s.list_notifications(&fx.bob, false, None, 50) .await .unwrap() - .is_empty() + .is_empty(), + "list: other user sees nothing" ); - } - #[tokio::test] - async fn unread_count_and_mark_read_variants() { - let fx = fixture().await; - let s = &fx.store; + // Scenario 3: mark-read by id and by repo only touches the right rows. + let ned = user(s, "Ned").await; + let olive = user(s, "Olive").await; s.upsert_notification( - &fx.tahinli, + &ned, &fx.repo, &fx.issue_a, Reason::Comment, @@ -708,7 +742,7 @@ mod tests { .await .unwrap(); s.upsert_notification( - &fx.tahinli, + &ned, &fx.repo, &fx.issue_b, Reason::Comment, @@ -718,7 +752,7 @@ mod tests { .await .unwrap(); s.upsert_notification( - &fx.bob, + &olive, &fx.repo, &fx.issue_a, Reason::Comment, @@ -727,27 +761,25 @@ mod tests { ) .await .unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 2); + assert_eq!(s.unread_count(&ned).await.unwrap(), 2, "read: two unread"); // mark_read by id, and another user's id does nothing. - let inbox = s - .list_notifications(&fx.tahinli, false, None, 50) - .await - .unwrap(); - let bob_inbox = s - .list_notifications(&fx.bob, false, None, 50) - .await - .unwrap(); - s.mark_read(&fx.tahinli, &bob_inbox[0].id).await.unwrap(); - assert_eq!(s.unread_count(&fx.bob).await.unwrap(), 1); - s.mark_read(&fx.tahinli, &inbox[0].id).await.unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1); - s.mark_read(&fx.tahinli, &inbox[1].id).await.unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0); + let inbox = s.list_notifications(&ned, false, None, 50).await.unwrap(); + let olive_inbox = s.list_notifications(&olive, false, None, 50).await.unwrap(); + s.mark_read(&ned, &olive_inbox[0].id).await.unwrap(); + assert_eq!( + s.unread_count(&olive).await.unwrap(), + 1, + "read: foreign id ignored" + ); + s.mark_read(&ned, &inbox[0].id).await.unwrap(); + assert_eq!(s.unread_count(&ned).await.unwrap(), 1, "read: first by id"); + s.mark_read(&ned, &inbox[1].id).await.unwrap(); + assert_eq!(s.unread_count(&ned).await.unwrap(), 0, "read: second by id"); // mark_all_read per repo leaves other users alone. s.upsert_notification( - &fx.tahinli, + &ned, &fx.repo, &fx.issue_a, Reason::Comment, @@ -756,12 +788,16 @@ mod tests { ) .await .unwrap(); - s.mark_all_read(&fx.tahinli, Some(&fx.repo)).await.unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0); - assert_eq!(s.unread_count(&fx.bob).await.unwrap(), 1); + s.mark_all_read(&ned, Some(&fx.repo)).await.unwrap(); + assert_eq!(s.unread_count(&ned).await.unwrap(), 0, "read: all in repo"); + assert_eq!( + s.unread_count(&olive).await.unwrap(), + 1, + "read: other user kept" + ); s.upsert_notification( - &fx.tahinli, + &ned, &fx.repo, &fx.issue_b, Reason::Comment, @@ -770,20 +806,19 @@ mod tests { ) .await .unwrap(); - s.mark_all_read(&fx.tahinli, None).await.unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0); - s.mark_all_read(&fx.bob, Some("no-such-repo")) - .await - .unwrap(); - assert_eq!(s.unread_count(&fx.bob).await.unwrap(), 1); - } + s.mark_all_read(&ned, None).await.unwrap(); + assert_eq!(s.unread_count(&ned).await.unwrap(), 0, "read: all, no repo"); + s.mark_all_read(&olive, Some("no-such-repo")).await.unwrap(); + assert_eq!( + s.unread_count(&olive).await.unwrap(), + 1, + "read: unknown repo" + ); - #[tokio::test] - async fn mark_issue_read_only_touches_that_issue() { - let fx = fixture().await; - let s = &fx.store; + // Scenario 4: mark_issue_read only touches that issue. + let max = user(s, "Max").await; s.upsert_notification( - &fx.tahinli, + &max, &fx.repo, &fx.issue_a, Reason::Comment, @@ -793,7 +828,7 @@ mod tests { .await .unwrap(); s.upsert_notification( - &fx.tahinli, + &max, &fx.repo, &fx.issue_b, Reason::Comment, @@ -802,34 +837,47 @@ mod tests { ) .await .unwrap(); - s.mark_issue_read(&fx.tahinli, &fx.issue_a).await.unwrap(); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1); - let unread = s - .list_notifications(&fx.tahinli, true, None, 50) - .await - .unwrap(); - assert_eq!(unread[0].issue_id, fx.issue_b); + s.mark_issue_read(&max, &fx.issue_a).await.unwrap(); + assert_eq!( + s.unread_count(&max).await.unwrap(), + 1, + "issue read: one left" + ); + let unread = s.list_notifications(&max, true, None, 50).await.unwrap(); + assert_eq!( + unread[0].issue_id, fx.issue_b, + "issue read: the other issue" + ); } + // Deletions come last in their own store scope: each removes state other scenarios need. #[tokio::test] - async fn delete_repo_removes_notification_rows() { + async fn delete_rules_hold_in_each_scenario() { let fx = fixture().await; let s = &fx.store; - s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Watching) + + // Scenario 1: deleting a repo removes its notification rows, subs and watches. + let pat = user(s, "Pat").await; + let doomed = s + .create_repo(&fx.owner, "doomed", "", Visibility::Public, "main") + .await + .unwrap() + .id; + let doomed_issue = s + .create_issue(&doomed, &fx.owner, "Doomed", "") + .await + .unwrap() + .id; + s.set_watch(&pat, &doomed, WatchLevel::Watching) + .await + .unwrap(); + s.subscribe(&pat, &doomed_issue, SubState::Subscribed, Reason::Author) .await .unwrap(); - s.subscribe( - &fx.tahinli, - &fx.issue_a, - SubState::Subscribed, - Reason::Author, - ) - .await - .unwrap(); s.upsert_notification( - &fx.tahinli, - &fx.repo, - &fx.issue_a, + &pat, + &doomed, + &doomed_issue, Reason::Comment, "comment", Some(&fx.bob), @@ -837,66 +885,66 @@ mod tests { .await .unwrap(); - s.delete_repo(&fx.repo).await.unwrap(); + s.delete_repo(&doomed).await.unwrap(); assert_eq!( count( s, "SELECT COUNT(*) FROM notifications WHERE user_id = ?1", - &fx.tahinli + &pat ) .await, - 0 + 0, + "repo: notifications removed" ); assert_eq!( count( s, "SELECT COUNT(*) FROM issue_subscriptions WHERE user_id = ?1", - &fx.tahinli + &pat ) .await, - 0 + 0, + "repo: subscriptions removed" ); assert_eq!( count( s, "SELECT COUNT(*) FROM repo_watches WHERE user_id = ?1", - &fx.tahinli + &pat ) .await, - 0 + 0, + "repo: watches removed" + ); + assert_eq!( + s.unread_count(&pat).await.unwrap(), + 0, + "repo: nothing unread" ); - assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0); - } - #[tokio::test] - async fn delete_user_removes_own_rows_and_reassigns_last_actor_to_ghost() { - let fx = fixture().await; - let s = &fx.store; + // Scenario 2: deleting a user removes their rows and reassigns last actor to ghost. let ghost = s.ensure_ghost_user().await.unwrap(); - s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Watching) + let quin = user(s, "Quin").await; + let ray = user(s, "Ray").await; + s.set_watch(&quin, &fx.repo, WatchLevel::Watching) .await .unwrap(); - s.subscribe( - &fx.tahinli, - &fx.issue_a, - SubState::Subscribed, - Reason::Author, - ) - .await - .unwrap(); - // Bob's inbox row whose last actor is Tahinli, and Tahinli's own inbox row. + s.subscribe(&quin, &fx.issue_a, SubState::Subscribed, Reason::Author) + .await + .unwrap(); + // Ray's inbox row whose last actor is Quin, and Quin's own inbox row. s.upsert_notification( - &fx.bob, + &ray, &fx.repo, &fx.issue_a, Reason::Comment, "comment", - Some(&fx.tahinli), + Some(&quin), ) .await .unwrap(); s.upsert_notification( - &fx.tahinli, + &quin, &fx.repo, &fx.issue_b, Reason::Comment, @@ -906,40 +954,44 @@ mod tests { .await .unwrap(); - s.delete_user(&fx.tahinli, &ghost.id).await.unwrap(); + s.delete_user(&quin, &ghost.id).await.unwrap(); assert_eq!( count( s, "SELECT COUNT(*) FROM notifications WHERE user_id = ?1", - &fx.tahinli + &quin ) .await, - 0 + 0, + "user: notifications removed" ); assert_eq!( count( s, "SELECT COUNT(*) FROM issue_subscriptions WHERE user_id = ?1", - &fx.tahinli + &quin ) .await, - 0 + 0, + "user: subscriptions removed" ); assert_eq!( count( s, "SELECT COUNT(*) FROM repo_watches WHERE user_id = ?1", - &fx.tahinli + &quin ) .await, - 0 + 0, + "user: watches removed" + ); + let ray_inbox = s.list_notifications(&ray, false, None, 50).await.unwrap(); + assert_eq!(ray_inbox.len(), 1, "user: other user's row kept"); + assert_eq!( + ray_inbox[0].last_actor_name.as_deref(), + Some("ghost"), + "user: last actor is ghost" ); - let bob_inbox = s - .list_notifications(&fx.bob, false, None, 50) - .await - .unwrap(); - assert_eq!(bob_inbox.len(), 1); - assert_eq!(bob_inbox[0].last_actor_name.as_deref(), Some("ghost")); } } diff --git a/crates/forge-core/src/store/pinned.rs b/crates/forge-core/src/store/pinned.rs index 92df0e3..3f762d5 100644 --- a/crates/forge-core/src/store/pinned.rs +++ b/crates/forge-core/src/store/pinned.rs @@ -130,9 +130,12 @@ mod tests { (store, tahinli, repo.id, dir) } + // One store, one scenario per repo so issue numbers and pin slots never mix. #[tokio::test] - async fn pins_keep_order_and_stop_at_three() { + async fn pin_rules_hold_in_each_scenario() { let (store, tahinli, repo, _dir) = fixture().await; + + // Scenario 1: pins keep their order and a fourth pin is refused. let mut ids = Vec::new(); for i in 0..4 { let issue = store @@ -141,13 +144,27 @@ mod tests { .unwrap(); ids.push(issue.id); } - assert!(store.pin_issue(&repo, &ids[2]).await.unwrap()); - assert!(store.pin_issue(&repo, &ids[0]).await.unwrap()); - assert!(store.pin_issue(&repo, &ids[3]).await.unwrap()); + assert!( + store.pin_issue(&repo, &ids[2]).await.unwrap(), + "order: pin 3" + ); + assert!( + store.pin_issue(&repo, &ids[0]).await.unwrap(), + "order: pin 1" + ); + assert!( + store.pin_issue(&repo, &ids[3]).await.unwrap(), + "order: pin 4" + ); let full = store.pin_issue(&repo, &ids[1]).await; - assert!(matches!(full, Err(Error::Invalid(_)))); - assert!(!store.is_pinned(&repo, &ids[1]).await.unwrap()); - + assert!( + matches!(full, Err(Error::Invalid(_))), + "order: fourth pin refused" + ); + assert!( + !store.is_pinned(&repo, &ids[1]).await.unwrap(), + "order: refused issue stays unpinned" + ); let pinned: Vec = store .pinned_issues(&repo) .await @@ -155,43 +172,71 @@ mod tests { .iter() .map(|i| i.number) .collect(); - assert_eq!(pinned, vec![3, 1, 4]); - } + assert_eq!(pinned, vec![3, 1, 4], "order: listing"); - #[tokio::test] - async fn pinning_twice_is_idempotent_and_unpin_frees_a_slot() { - let (store, tahinli, repo, _dir) = fixture().await; - let mut ids = Vec::new(); + // Scenario 2: pinning twice is idempotent and unpinning frees a slot. + let repo2 = store + .create_repo( + &tahinli.id, + "forge-idempotent", + "", + Visibility::Public, + "main", + ) + .await + .unwrap() + .id; + let mut ids2 = Vec::new(); for i in 0..4 { let issue = store - .create_issue(&repo, &tahinli.id, &format!("issue {i}"), "") + .create_issue(&repo2, &tahinli.id, &format!("issue {i}"), "") .await .unwrap(); - ids.push(issue.id); + ids2.push(issue.id); } - for id in &ids[..3] { - store.pin_issue(&repo, id).await.unwrap(); + for id in &ids2[..3] { + store.pin_issue(&repo2, id).await.unwrap(); } - assert!(!store.pin_issue(&repo, &ids[0]).await.unwrap()); - assert_eq!(store.pinned_issues(&repo).await.unwrap().len(), 3); - - assert!(store.unpin_issue(&repo, &ids[1]).await.unwrap()); - assert!(!store.unpin_issue(&repo, &ids[1]).await.unwrap()); - assert!(!store.is_pinned(&repo, &ids[1]).await.unwrap()); - assert!(store.pin_issue(&repo, &ids[3]).await.unwrap()); + assert!( + !store.pin_issue(&repo2, &ids2[0]).await.unwrap(), + "idempotent: second pin is a no-op" + ); + assert_eq!( + store.pinned_issues(&repo2).await.unwrap().len(), + 3, + "idempotent: still three pins" + ); + assert!( + store.unpin_issue(&repo2, &ids2[1]).await.unwrap(), + "unpin: first" + ); + assert!( + !store.unpin_issue(&repo2, &ids2[1]).await.unwrap(), + "unpin: second is a no-op" + ); + assert!( + !store.is_pinned(&repo2, &ids2[1]).await.unwrap(), + "unpin: gone" + ); + assert!( + store.pin_issue(&repo2, &ids2[3]).await.unwrap(), + "unpin: slot reused" + ); let numbers: Vec = store - .pinned_issues(&repo) + .pinned_issues(&repo2) .await .unwrap() .iter() .map(|i| i.number) .collect(); - assert_eq!(numbers, vec![1, 3, 4]); - } + assert_eq!(numbers, vec![1, 3, 4], "unpin: listing"); - #[tokio::test] - async fn pull_requests_and_foreign_issues_cannot_be_pinned() { - let (store, tahinli, repo, _dir) = fixture().await; + // Scenario 3: pull requests and issues of another repo cannot be pinned. + let repo3 = store + .create_repo(&tahinli.id, "forge-foreign", "", Visibility::Public, "main") + .await + .unwrap() + .id; let other = store .create_repo(&tahinli.id, "other", "", Visibility::Public, "main") .await @@ -200,14 +245,20 @@ mod tests { .create_issue(&other.id, &tahinli.id, "elsewhere", "") .await .unwrap(); - assert!(matches!( - store.pin_issue(&repo, &foreign.id).await, - Err(Error::NotFound) - )); - assert!(matches!( - store.pin_issue(&repo, "missing").await, - Err(Error::NotFound) - )); + assert!( + matches!( + store.pin_issue(&repo3, &foreign.id).await, + Err(Error::NotFound) + ), + "foreign: issue of another repo" + ); + assert!( + matches!( + store.pin_issue(&repo3, "missing").await, + Err(Error::NotFound) + ), + "foreign: unknown id" + ); } #[tokio::test]