forge
Explore
Log in

Don't you dare go Hollow

Store tests share their store and merge into tables

Browse files at this commit

965436ce11332cea3c543910867bcaf227eaafa6Ahmet Kaan GümüşParent: 7824c81
  • test
    • store tests share their store and merge into tables
Show full message
✅ test: store tests share their store and merge into tables

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.

5 changed files +1480 -1332

crates/forge-core/src/store/discussions.rsmodified+265 -202
@@ -759,106 +759,107 @@ mod tests {
759 }759 }
760 }760 }
761 761
762- #[tokio::test]762+ async fn fresh_repo(store: &Store, owner: &str, name: &str) -> String {
763- async fn discussions_are_off_by_default_and_toggle() {763+ store
764- let fx = fixture().await;764+ .create_repo(owner, name, "", Visibility::Public, "main")
765- assert!(!fx.store.discussions_enabled(&fx.repo_id).await.unwrap());
766- fx.store
767- .set_discussions_enabled(&fx.repo_id, true)
768 .await765 .await
769- .unwrap();766+ .unwrap()
770- assert!(fx.store.discussions_enabled(&fx.repo_id).await.unwrap());767+ .id
771- assert!(matches!(
772- fx.store.set_discussions_enabled("missing", true).await,
773- Err(Error::NotFound)
774- ));
775 }768 }
776 769
770+ // Each scenario gets its own repo (numbers and lists stay isolated) and,
771+ // where global per-user queries are asserted, its own users.
777 #[tokio::test]772 #[tokio::test]
778- async fn numbers_share_the_issue_counter() {773+ async fn repo_level_rules_hold_in_each_scenario() {
779 let fx = fixture().await;774 let fx = fixture().await;
775+
776+ // Scenario 1: discussions are off by default and toggle per repo.
777+ let repo = fresh_repo(&fx.store, &fx.tahinli.id, "toggle").await;
778+ assert!(
779+ !fx.store.discussions_enabled(&repo).await.unwrap(),
780+ "toggle: off by default"
781+ );
782+ fx.store.set_discussions_enabled(&repo, true).await.unwrap();
783+ assert!(
784+ fx.store.discussions_enabled(&repo).await.unwrap(),
785+ "toggle: on after enabling"
786+ );
787+ assert!(
788+ matches!(
789+ fx.store.set_discussions_enabled("missing", true).await,
790+ Err(Error::NotFound)
791+ ),
792+ "toggle: unknown repo"
793+ );
794+
795+ // Scenario 2: discussion numbers share the issue counter.
796+ let repo = fresh_repo(&fx.store, &fx.tahinli.id, "numbers").await;
780 let issue = fx797 let issue = fx
781 .store798 .store
782- .create_issue(&fx.repo_id, &fx.tahinli.id, "first", "")799+ .create_issue(&repo, &fx.tahinli.id, "first", "")
783 .await800 .await
784 .unwrap();801 .unwrap();
785 let d1 = fx802 let d1 = fx
786 .store803 .store
787- .create_discussion(804+ .create_discussion(&repo, &fx.tahinli.id, DiscussionCategory::General, "a", "")
788- &fx.repo_id,
789- &fx.tahinli.id,
790- DiscussionCategory::General,
791- "a",
792- "",
793- )
794 .await805 .await
795 .unwrap();806 .unwrap();
796 let issue2 = fx807 let issue2 = fx
797 .store808 .store
798- .create_issue(&fx.repo_id, &fx.tahinli.id, "second", "")809+ .create_issue(&repo, &fx.tahinli.id, "second", "")
799 .await810 .await
800 .unwrap();811 .unwrap();
801- assert_eq!(issue.number, 1);812+ assert_eq!(issue.number, 1, "numbers: first issue");
802- assert_eq!(d1.number, 2);813+ assert_eq!(d1.number, 2, "numbers: discussion takes the next slot");
803- assert_eq!(issue2.number, 3);814+ assert_eq!(issue2.number, 3, "numbers: second issue");
804- let found = fx.store.discussion_by_number(&fx.repo_id, 2).await.unwrap();815+ let found = fx.store.discussion_by_number(&repo, 2).await.unwrap();
805- assert_eq!(found.map(|d| d.title), Some("a".to_string()));816+ assert_eq!(
806- }817+ found.map(|d| d.title),
818+ Some("a".to_string()),
819+ "numbers: lookup by number"
820+ );
807 821
808- #[tokio::test]822+ // Scenario 3: blank titles are refused and listings filter by category.
809- async fn create_rejects_blank_title_and_lists_by_category() {823+ let repo = fresh_repo(&fx.store, &fx.tahinli.id, "categories").await;
810- let fx = fixture().await;824+ assert!(
811- assert!(matches!(825+ matches!(
812- fx.store826+ fx.store
813- .create_discussion(827+ .create_discussion(&repo, &fx.tahinli.id, DiscussionCategory::Ideas, " ", "")
814- &fx.repo_id,828+ .await,
815- &fx.tahinli.id,829+ Err(Error::Invalid(_))
816- DiscussionCategory::Ideas,830+ ),
817- " ",831+ "categories: blank title refused"
818- ""832+ );
819- )
820- .await,
821- Err(Error::Invalid(_))
822- ));
823 fx.store833 fx.store
824- .create_discussion(834+ .create_discussion(&repo, &fx.tahinli.id, DiscussionCategory::Ideas, "idea", "")
825- &fx.repo_id,
826- &fx.tahinli.id,
827- DiscussionCategory::Ideas,
828- "idea",
829- "",
830- )
831 .await835 .await
832 .unwrap();836 .unwrap();
833 fx.store837 fx.store
834- .create_discussion(838+ .create_discussion(&repo, &fx.bob.id, DiscussionCategory::Qa, "question", "")
835- &fx.repo_id,
836- &fx.bob.id,
837- DiscussionCategory::Qa,
838- "question",
839- "",
840- )
841 .await839 .await
842 .unwrap();840 .unwrap();
843 let (all, total) = fx841 let (all, total) = fx
844 .store842 .store
845- .list_discussions(&fx.repo_id, &filter(None))843+ .list_discussions(&repo, &filter(None))
846 .await844 .await
847 .unwrap();845 .unwrap();
848- assert_eq!((all.len(), total), (2, 2));846+ assert_eq!((all.len(), total), (2, 2), "categories: unfiltered list");
849 let (qa, total) = fx847 let (qa, total) = fx
850 .store848 .store
851- .list_discussions(&fx.repo_id, &filter(Some(DiscussionCategory::Qa)))849+ .list_discussions(&repo, &filter(Some(DiscussionCategory::Qa)))
852 .await850 .await
853 .unwrap();851 .unwrap();
854- assert_eq!(total, 1);852+ assert_eq!(total, 1, "categories: qa total");
855- assert_eq!(qa[0].discussion.title, "question");853+ assert_eq!(qa[0].discussion.title, "question", "categories: qa title");
856- assert_eq!(qa[0].author, "Bob");854+ assert_eq!(qa[0].author, "Bob", "categories: qa author");
857 }855 }
858 856
857+ // Scenarios on one shared repo; every discussion is its own thread.
859 #[tokio::test]858 #[tokio::test]
860- async fn comments_nest_one_level_and_count() {859+ async fn thread_rules_hold_in_each_scenario() {
861 let fx = fixture().await;860 let fx = fixture().await;
861+
862+ // Scenario 1: comments nest one level deep and are counted.
862 let d = fx863 let d = fx
863 .store864 .store
864 .create_discussion(865 .create_discussion(
@@ -880,31 +881,42 @@ mod tests {
880 .add_discussion_comment(&d.id, &fx.tahinli.id, Some(&top.id), "hi back", false)881 .add_discussion_comment(&d.id, &fx.tahinli.id, Some(&top.id), "hi back", false)
881 .await882 .await
882 .unwrap();883 .unwrap();
883- assert_eq!(reply.parent_id.as_deref(), Some(top.id.as_str()));884+ assert_eq!(
885+ reply.parent_id.as_deref(),
886+ Some(top.id.as_str()),
887+ "nesting: reply points at its parent"
888+ );
884 let nested = fx889 let nested = fx
885 .store890 .store
886 .add_discussion_comment(&d.id, &fx.bob.id, Some(&reply.id), "too deep", false)891 .add_discussion_comment(&d.id, &fx.bob.id, Some(&reply.id), "too deep", false)
887 .await;892 .await;
888- assert!(matches!(nested, Err(Error::Invalid(_))));893+ assert!(
889- assert!(matches!(894+ matches!(nested, Err(Error::Invalid(_))),
890- fx.store895+ "nesting: too deep"
891- .add_discussion_comment(&d.id, &fx.bob.id, None, " ", false)896+ );
892- .await,897+ assert!(
893- Err(Error::Invalid(_))898+ matches!(
894- ));899+ fx.store
900+ .add_discussion_comment(&d.id, &fx.bob.id, None, " ", false)
901+ .await,
902+ Err(Error::Invalid(_))
903+ ),
904+ "nesting: blank body"
905+ );
895 let stored = fx906 let stored = fx
896 .store907 .store
897 .discussion_by_number(&fx.repo_id, d.number)908 .discussion_by_number(&fx.repo_id, d.number)
898 .await909 .await
899 .unwrap();910 .unwrap();
900- assert_eq!(stored.map(|d| d.comment_count), Some(2));911+ assert_eq!(
912+ stored.map(|d| d.comment_count),
913+ Some(2),
914+ "nesting: comment count"
915+ );
901 let all = fx.store.list_discussion_comments(&d.id).await.unwrap();916 let all = fx.store.list_discussion_comments(&d.id).await.unwrap();
902- assert_eq!(all.len(), 2);917+ assert_eq!(all.len(), 2, "nesting: listed comments");
903- }
904 918
905- #[tokio::test]919+ // Scenario 2: an answer is only accepted in Q&A and only from a live comment of it.
906- async fn answer_only_in_qa_and_only_a_live_comment_of_it() {
907- let fx = fixture().await;
908 let qa = fx920 let qa = fx
909 .store921 .store
910 .create_discussion(&fx.repo_id, &fx.tahinli.id, DiscussionCategory::Qa, "q", "")922 .create_discussion(&fx.repo_id, &fx.tahinli.id, DiscussionCategory::Qa, "q", "")
@@ -931,16 +943,22 @@ mod tests {
931 .add_discussion_comment(&general.id, &fx.bob.id, None, "x", false)943 .add_discussion_comment(&general.id, &fx.bob.id, None, "x", false)
932 .await944 .await
933 .unwrap();945 .unwrap();
934- assert!(matches!(946+ assert!(
935- fx.store947+ matches!(
936- .set_discussion_answer(&general.id, Some(&g.id))948+ fx.store
937- .await,949+ .set_discussion_answer(&general.id, Some(&g.id))
938- Err(Error::Invalid(_))950+ .await,
939- ));951+ Err(Error::Invalid(_))
940- assert!(matches!(952+ ),
941- fx.store.set_discussion_answer(&qa.id, Some(&g.id)).await,953+ "answer: refused outside Q&A"
942- Err(Error::Invalid(_))954+ );
943- ));955+ assert!(
956+ matches!(
957+ fx.store.set_discussion_answer(&qa.id, Some(&g.id)).await,
958+ Err(Error::Invalid(_))
959+ ),
960+ "answer: refused for a comment of another discussion"
961+ );
944 fx.store962 fx.store
945 .set_discussion_answer(&qa.id, Some(&c.id))963 .set_discussion_answer(&qa.id, Some(&c.id))
946 .await964 .await
@@ -950,19 +968,24 @@ mod tests {
950 .discussion_by_number(&fx.repo_id, qa.number)968 .discussion_by_number(&fx.repo_id, qa.number)
951 .await969 .await
952 .unwrap();970 .unwrap();
953- assert_eq!(stored.and_then(|d| d.answer_comment_id), Some(c.id.clone()));971+ assert_eq!(
972+ stored.and_then(|d| d.answer_comment_id),
973+ Some(c.id.clone()),
974+ "answer: accepted"
975+ );
954 fx.store.delete_discussion_comment(&c.id).await.unwrap();976 fx.store.delete_discussion_comment(&c.id).await.unwrap();
955 let stored = fx977 let stored = fx
956 .store978 .store
957 .discussion_by_number(&fx.repo_id, qa.number)979 .discussion_by_number(&fx.repo_id, qa.number)
958 .await980 .await
959 .unwrap();981 .unwrap();
960- assert_eq!(stored.and_then(|d| d.answer_comment_id), None);982+ assert_eq!(
961- }983+ stored.and_then(|d| d.answer_comment_id),
984+ None,
985+ "answer: cleared when the comment is deleted"
986+ );
962 987
963- #[tokio::test]988+ // Scenario 3: lock reports changes and a deleted comment keeps its replies.
964- async fn lock_reports_changes_and_delete_comment_keeps_replies() {
965- let fx = fixture().await;
966 let d = fx989 let d = fx
967 .store990 .store
968 .create_discussion(991 .create_discussion(
@@ -983,12 +1006,21 @@ mod tests {
983 .add_discussion_comment(&d.id, &fx.tahinli.id, Some(&top.id), "reply stays", false)1006 .add_discussion_comment(&d.id, &fx.tahinli.id, Some(&top.id), "reply stays", false)
984 .await1007 .await
985 .unwrap();1008 .unwrap();
986- assert!(fx.store.set_discussion_locked(&d.id, true).await.unwrap());1009+ assert!(
987- assert!(!fx.store.set_discussion_locked(&d.id, true).await.unwrap());1010+ fx.store.set_discussion_locked(&d.id, true).await.unwrap(),
988- assert!(matches!(1011+ "lock: first change reported"
989- fx.store.set_discussion_locked("missing", true).await,1012+ );
990- Err(Error::NotFound)1013+ assert!(
991- ));1014+ !fx.store.set_discussion_locked(&d.id, true).await.unwrap(),
1015+ "lock: repeat is no change"
1016+ );
1017+ assert!(
1018+ matches!(
1019+ fx.store.set_discussion_locked("missing", true).await,
1020+ Err(Error::NotFound)
1021+ ),
1022+ "lock: unknown discussion"
1023+ );
992 fx.store.delete_discussion_comment(&top.id).await.unwrap();1024 fx.store.delete_discussion_comment(&top.id).await.unwrap();
993 let gone = fx1025 let gone = fx
994 .store1026 .store
@@ -996,16 +1028,16 @@ mod tests {
996 .await1028 .await
997 .unwrap()1029 .unwrap()
998 .unwrap();1030 .unwrap();
999- assert!(gone.deleted);1031+ assert!(gone.deleted, "lock: parent tombstoned");
1000- assert!(gone.body.is_empty());1032+ assert!(gone.body.is_empty(), "lock: parent body cleared");
1001 let all = fx.store.list_discussion_comments(&d.id).await.unwrap();1033 let all = fx.store.list_discussion_comments(&d.id).await.unwrap();
1002- assert_eq!(all.len(), 2);1034+ assert_eq!(all.len(), 2, "lock: tombstone and reply both listed");
1003- assert!(all.iter().any(|c| c.body == "reply stays" && !c.deleted));1035+ assert!(
1004- }1036+ all.iter().any(|c| c.body == "reply stays" && !c.deleted),
1037+ "lock: reply survives"
1038+ );
1005 1039
1006- #[tokio::test]1040+ // Scenario 4: edit and delete remove or update the discussion and its rows.
1007- async fn edit_and_delete_discussion_remove_its_rows() {
1008- let fx = fixture().await;
1009 let d = fx1041 let d = fx
1010 .store1042 .store
1011 .create_discussion(1043 .create_discussion(
@@ -1026,7 +1058,11 @@ mod tests {
1026 .discussion_by_number(&fx.repo_id, d.number)1058 .discussion_by_number(&fx.repo_id, d.number)
1027 .await1059 .await
1028 .unwrap();1060 .unwrap();
1029- assert_eq!(edited.map(|d| d.title), Some("new".to_string()));1061+ assert_eq!(
1062+ edited.map(|d| d.title),
1063+ Some("new".to_string()),
1064+ "edit: title updated"
1065+ );
1030 fx.store1066 fx.store
1031 .add_discussion_comment(&d.id, &fx.bob.id, None, "c", false)1067 .add_discussion_comment(&d.id, &fx.bob.id, None, "c", false)
1032 .await1068 .await
@@ -1037,71 +1073,26 @@ mod tests {
1037 .discussion_by_number(&fx.repo_id, d.number)1073 .discussion_by_number(&fx.repo_id, d.number)
1038 .await1074 .await
1039 .unwrap()1075 .unwrap()
1040- .is_none()1076+ .is_none(),
1077+ "delete: discussion gone"
1041 );1078 );
1042 assert!(1079 assert!(
1043 fx.store1080 fx.store
1044 .list_discussion_comments(&d.id)1081 .list_discussion_comments(&d.id)
1045 .await1082 .await
1046 .unwrap()1083 .unwrap()
1047- .is_empty()1084+ .is_empty(),
1085+ "delete: comments gone"
1048 );1086 );
1049- assert!(matches!(
1050- fx.store.delete_discussion(&d.id).await,
1051- Err(Error::NotFound)
1052- ));
1053- }
1054-
1055- #[tokio::test]
1056- async fn mentions_go_unread_and_refresh_on_repeat() {
1057- let fx = fixture().await;
1058- let d = fx
1059- .store
1060- .create_discussion(
1061- &fx.repo_id,
1062- &fx.tahinli.id,
1063- DiscussionCategory::General,
1064- "t",
1065- "",
1066- )
1067- .await
1068- .unwrap();
1069- fx.store
1070- .record_discussion_mentions(
1071- &fx.repo_id,
1072- &d.id,
1073- &fx.tahinli.id,
1074- std::slice::from_ref(&fx.bob.id),
1075- )
1076- .await
1077- .unwrap();
1078- fx.store
1079- .record_discussion_mentions(
1080- &fx.repo_id,
1081- &d.id,
1082- &fx.tahinli.id,
1083- std::slice::from_ref(&fx.bob.id),
1084- )
1085- .await
1086- .unwrap();
1087- let rows = fx
1088- .store
1089- .unread_discussion_mentions(&fx.bob.id)
1090- .await
1091- .unwrap();
1092- assert_eq!(rows, vec![(d.id.clone(), fx.repo_id.clone())]);
1093 assert!(1087 assert!(
1094- fx.store1088+ matches!(
1095- .unread_discussion_mentions(&fx.tahinli.id)1089+ fx.store.delete_discussion(&d.id).await,
1096- .await1090+ Err(Error::NotFound)
1097- .unwrap()1091+ ),
1098- .is_empty()1092+ "delete: second delete is not found"
1099 );1093 );
1100- }
1101 1094
1102- #[tokio::test]1095+ // Scenario 5: a locked discussion takes comments only from triage.
1103- async fn locked_discussion_takes_comments_only_from_triage() {
1104- let fx = fixture().await;
1105 let d = fx1096 let d = fx
1106 .store1097 .store
1107 .create_discussion(1098 .create_discussion(
@@ -1118,14 +1109,17 @@ mod tests {
1118 .store1109 .store
1119 .add_discussion_comment(&d.id, &fx.bob.id, None, "hi", false)1110 .add_discussion_comment(&d.id, &fx.bob.id, None, "hi", false)
1120 .await;1111 .await;
1121- assert!(matches!(refused, Err(Error::Forbidden(_))));1112+ assert!(
1113+ matches!(refused, Err(Error::Forbidden(_))),
1114+ "locked: plain comment refused"
1115+ );
1122 let stored = fx1116 let stored = fx
1123 .store1117 .store
1124 .discussion_by_number(&fx.repo_id, d.number)1118 .discussion_by_number(&fx.repo_id, d.number)
1125 .await1119 .await
1126 .unwrap()1120 .unwrap()
1127 .unwrap();1121 .unwrap();
1128- assert_eq!(stored.comment_count, 0);1122+ assert_eq!(stored.comment_count, 0, "locked: nothing was stored");
1129 fx.store1123 fx.store
1130 .add_discussion_comment(&d.id, &fx.bob.id, None, "maintainer note", true)1124 .add_discussion_comment(&d.id, &fx.bob.id, None, "maintainer note", true)
1131 .await1125 .await
@@ -1137,90 +1131,159 @@ mod tests {
1137 .unwrap();1131 .unwrap();
1138 }1132 }
1139 1133
1134+ // Each scenario uses fresh users so the global per-user inbox queries stay exact.
1140 #[tokio::test]1135 #[tokio::test]
1141- async fn replies_and_mentions_reach_the_inbox() {1136+ async fn inbox_rules_hold_in_each_scenario() {
1142 let fx = fixture().await;1137 let fx = fixture().await;
1138+
1139+ // Scenario 1: repeated mentions are stored once and show as unread.
1140+ let ana = fx
1141+ .store
1142+ .create_user("Ana", None, "hash", false)
1143+ .await
1144+ .unwrap();
1145+ let ben = fx
1146+ .store
1147+ .create_user("Ben", None, "hash", false)
1148+ .await
1149+ .unwrap();
1150+ let repo = fresh_repo(&fx.store, &ana.id, "mentions").await;
1143 let d = fx1151 let d = fx
1144 .store1152 .store
1145- .create_discussion(&fx.repo_id, &fx.tahinli.id, DiscussionCategory::Qa, "Q", "")1153+ .create_discussion(&repo, &ana.id, DiscussionCategory::General, "t", "")
1146 .await1154 .await
1147 .unwrap();1155 .unwrap();
1148- // Bob's comment on Tahinli's discussion gives Tahinli a reply row.1156+ for _ in 0..2 {
1157+ fx.store
1158+ .record_discussion_mentions(&repo, &d.id, &ana.id, std::slice::from_ref(&ben.id))
1159+ .await
1160+ .unwrap();
1161+ }
1162+ let rows = fx.store.unread_discussion_mentions(&ben.id).await.unwrap();
1163+ assert_eq!(
1164+ rows,
1165+ vec![(d.id.clone(), repo.clone())],
1166+ "mentions: one row after repeats"
1167+ );
1168+ assert!(
1169+ fx.store
1170+ .unread_discussion_mentions(&ana.id)
1171+ .await
1172+ .unwrap()
1173+ .is_empty(),
1174+ "mentions: author is not mentioned"
1175+ );
1176+
1177+ // Scenario 2: replies, mentions and mark-read reach the owner's inbox.
1178+ let cora = fx
1179+ .store
1180+ .create_user("Cora", None, "hash", false)
1181+ .await
1182+ .unwrap();
1183+ let dev = fx
1184+ .store
1185+ .create_user("Dev", None, "hash", false)
1186+ .await
1187+ .unwrap();
1188+ let repo = fresh_repo(&fx.store, &cora.id, "replies").await;
1189+ let d = fx
1190+ .store
1191+ .create_discussion(&repo, &cora.id, DiscussionCategory::Qa, "Q", "")
1192+ .await
1193+ .unwrap();
1194+ // Dev's comment on Cora's discussion gives Cora a reply row.
1149 fx.store1195 fx.store
1150- .add_discussion_comment(&d.id, &fx.bob.id, None, "answer", false)1196+ .add_discussion_comment(&d.id, &dev.id, None, "answer", false)
1151 .await1197 .await
1152 .unwrap();1198 .unwrap();
1153- // Tahinli's own comment gives her nothing.1199+ // Cora's own comment gives her nothing.
1154 fx.store1200 fx.store
1155- .add_discussion_comment(&d.id, &fx.tahinli.id, None, "thanks", false)1201+ .add_discussion_comment(&d.id, &cora.id, None, "thanks", false)
1156 .await1202 .await
1157 .unwrap();1203 .unwrap();
1158 let rows = fx1204 let rows = fx
1159 .store1205 .store
1160- .list_discussion_notifications(&fx.tahinli.id, true, None, 50)1206+ .list_discussion_notifications(&cora.id, true, None, 50)
1161 .await1207 .await
1162 .unwrap();1208 .unwrap();
1163- assert_eq!(rows.len(), 1);1209+ assert_eq!(rows.len(), 1, "replies: one row");
1164- assert_eq!(rows[0].reason, DiscussionReason::Reply);1210+ assert_eq!(rows[0].reason, DiscussionReason::Reply, "replies: reason");
1165- assert_eq!(rows[0].title, "Q");1211+ assert_eq!(rows[0].title, "Q", "replies: title");
1166- assert_eq!(rows[0].number, d.number);1212+ assert_eq!(rows[0].number, d.number, "replies: number");
1167- assert_eq!(rows[0].repo_owner, "Tahinli");1213+ assert_eq!(rows[0].repo_owner, "Cora", "replies: owner");
1168- assert_eq!(rows[0].last_actor_name.as_deref(), Some("Bob"));1214+ assert_eq!(
1215+ rows[0].last_actor_name.as_deref(),
1216+ Some("Dev"),
1217+ "replies: last actor"
1218+ );
1169 1219
1170 // A mention upgrades the row, and a later reply does not downgrade it.1220 // A mention upgrades the row, and a later reply does not downgrade it.
1171 fx.store1221 fx.store
1172- .record_discussion_mentions(1222+ .record_discussion_mentions(&repo, &d.id, &dev.id, std::slice::from_ref(&cora.id))
1173- &fx.repo_id,
1174- &d.id,
1175- &fx.bob.id,
1176- std::slice::from_ref(&fx.tahinli.id),
1177- )
1178 .await1223 .await
1179 .unwrap();1224 .unwrap();
1180 fx.store1225 fx.store
1181- .add_discussion_comment(&d.id, &fx.bob.id, None, "again", false)1226+ .add_discussion_comment(&d.id, &dev.id, None, "again", false)
1182 .await1227 .await
1183 .unwrap();1228 .unwrap();
1184 let rows = fx1229 let rows = fx
1185 .store1230 .store
1186- .list_discussion_notifications(&fx.tahinli.id, true, None, 50)1231+ .list_discussion_notifications(&cora.id, true, None, 50)
1187 .await1232 .await
1188 .unwrap();1233 .unwrap();
1189- assert_eq!(rows.len(), 1);1234+ assert_eq!(rows.len(), 1, "upgrade: still one row");
1190- assert_eq!(rows[0].reason, DiscussionReason::Mention);1235+ assert_eq!(
1191- assert_eq!(fx.store.unread_count(&fx.tahinli.id).await.unwrap(), 1);1236+ rows[0].reason,
1237+ DiscussionReason::Mention,
1238+ "upgrade: mention wins"
1239+ );
1240+ assert_eq!(
1241+ fx.store.unread_count(&cora.id).await.unwrap(),
1242+ 1,
1243+ "upgrade: unread count"
1244+ );
1192 1245
1193- // Another user's mark-read changes nothing.1246+ // Another user's mark-read changes nothing; the owner's mark-all clears it.
1194 fx.store1247 fx.store
1195- .mark_discussion_notification_read(&fx.bob.id, &d.id)1248+ .mark_discussion_notification_read(&dev.id, &d.id)
1196 .await1249 .await
1197 .unwrap();1250 .unwrap();
1198- assert_eq!(fx.store.unread_count(&fx.tahinli.id).await.unwrap(), 1);1251+ assert_eq!(
1252+ fx.store.unread_count(&cora.id).await.unwrap(),
1253+ 1,
1254+ "mark-read: other user is ignored"
1255+ );
1199 fx.store1256 fx.store
1200- .mark_all_discussion_notifications_read(&fx.tahinli.id, Some(&fx.repo_id))1257+ .mark_all_discussion_notifications_read(&cora.id, Some(&repo))
1201 .await1258 .await
1202 .unwrap();1259 .unwrap();
1203- assert_eq!(fx.store.unread_count(&fx.tahinli.id).await.unwrap(), 0);1260+ assert_eq!(
1261+ fx.store.unread_count(&cora.id).await.unwrap(),
1262+ 0,
1263+ "mark-read: cleared"
1264+ );
1204 assert!(1265 assert!(
1205 fx.store1266 fx.store
1206- .list_discussion_notifications(&fx.tahinli.id, true, None, 50)1267+ .list_discussion_notifications(&cora.id, true, None, 50)
1207 .await1268 .await
1208 .unwrap()1269 .unwrap()
1209- .is_empty()1270+ .is_empty(),
1271+ "mark-read: no unread rows"
1210 );1272 );
1211 let all = fx1273 let all = fx
1212 .store1274 .store
1213- .list_discussion_notifications(&fx.tahinli.id, false, None, 50)1275+ .list_discussion_notifications(&cora.id, false, None, 50)
1214 .await1276 .await
1215 .unwrap();1277 .unwrap();
1216- assert_eq!(all.len(), 1);1278+ assert_eq!(all.len(), 1, "mark-read: read rows still listed");
1217- assert!(!all[0].unread);1279+ assert!(!all[0].unread, "mark-read: row is read");
1218 assert!(1280 assert!(
1219 fx.store1281 fx.store
1220- .discussion_notification(&fx.bob.id, &d.id)1282+ .discussion_notification(&dev.id, &d.id)
1221 .await1283 .await
1222 .unwrap()1284 .unwrap()
1223- .is_none()1285+ .is_none(),
1286+ "mark-read: no row for the commenter"
1224 );1287 );
1225 }1288 }
1226 }1289 }
crates/forge-core/src/store/issues.rsmodified+452 -487
@@ -935,7 +935,6 @@ mod tests {
935 store: Store,935 store: Store,
936 tahinli: User,936 tahinli: User,
937 bob: User,937 bob: User,
938- repo_id: String,
939 _dir: tempfile::TempDir,938 _dir: tempfile::TempDir,
940 }939 }
941 940
@@ -947,19 +946,23 @@ mod tests {
947 .await946 .await
948 .unwrap();947 .unwrap();
949 let bob = store.create_user("Bob", None, "hash", false).await.unwrap();948 let bob = store.create_user("Bob", None, "hash", false).await.unwrap();
950- let repo = store
951- .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main")
952- .await
953- .unwrap();
954 Fixture {949 Fixture {
955 store,950 store,
956 tahinli,951 tahinli,
957 bob,952 bob,
958- repo_id: repo.id,
959 _dir: dir,953 _dir: dir,
960 }954 }
961 }955 }
962 956
957+ /// A repo of its own, so issue numbers and repo-wide counts start clean per scenario.
958+ async fn fresh_repo(store: &Store, owner: &str, name: &str) -> String {
959+ store
960+ .create_repo(owner, name, "", Visibility::Public, "main")
961+ .await
962+ .unwrap()
963+ .id
964+ }
965+
963 fn all() -> IssueFilter {966 fn all() -> IssueFilter {
964 IssueFilter {967 IssueFilter {
965 state: None,968 state: None,
@@ -972,76 +975,69 @@ mod tests {
972 items.into_iter().map(|i| i.issue.title).collect()975 items.into_iter().map(|i| i.issue.title).collect()
973 }976 }
974 977
978+ // Creating, filtering, paging and counting issues. Each scenario has its own repo.
975 #[tokio::test]979 #[tokio::test]
976- async fn create_issue_numbers_start_at_one_and_advance() {980+ async fn issue_create_and_list_rules_hold_in_each_scenario() {
977 let fx = fixture().await;981 let fx = fixture().await;
978- let a = fx982+ let s = &fx.store;
979- .store983+
980- .create_issue(&fx.repo_id, &fx.tahinli.id, " First ", "body")984+ // Scenario 1: numbers start at one and advance; lookup by number.
981- .await985+ let repo = fresh_repo(s, &fx.tahinli.id, "numbers").await;
982- .unwrap();986+ let a = s
983- let b = fx987+ .create_issue(&repo, &fx.tahinli.id, " First ", "body")
984- .store
985- .create_issue(&fx.repo_id, &fx.tahinli.id, "Second", "")
986 .await988 .await
987 .unwrap();989 .unwrap();
988- assert_eq!((a.number, b.number), (1, 2));990+ let b = s
989- assert_eq!(a.title, "First");991+ .create_issue(&repo, &fx.tahinli.id, "Second", "")
990- assert_eq!(a.state, IssueState::Open);
991- assert_eq!(a.comment_count, 0);
992- let found = fx
993- .store
994- .issue_by_number(&fx.repo_id, 2)
995 .await992 .await
996- .unwrap()
997 .unwrap();993 .unwrap();
998- assert_eq!(found.id, b.id);994+ assert_eq!((a.number, b.number), (1, 2), "numbers: sequence");
995+ assert_eq!(a.title, "First", "numbers: title trimmed");
996+ assert_eq!(a.state, IssueState::Open, "numbers: starts open");
997+ assert_eq!(a.comment_count, 0, "numbers: no comments yet");
998+ let found = s.issue_by_number(&repo, 2).await.unwrap().unwrap();
999+ assert_eq!(found.id, b.id, "numbers: lookup by number");
999 assert!(1000 assert!(
1000- fx.store1001+ s.issue_by_number(&repo, 3).await.unwrap().is_none(),
1001- .issue_by_number(&fx.repo_id, 3)1002+ "numbers: no third issue"
1002- .await
1003- .unwrap()
1004- .is_none()
1005 );1003 );
1006- }
1007 1004
1008- #[tokio::test]1005+ // Scenario 2: invalid input and missing repos are refused without taking a number.
1009- async fn create_issue_validates_and_rejects_missing_repo() {1006+ let repo = fresh_repo(s, &fx.tahinli.id, "validate").await;
1010- let fx = fixture().await;1007+ assert!(
1011- assert!(matches!(1008+ matches!(
1012- fx.store1009+ s.create_issue(&repo, &fx.tahinli.id, " ", "").await,
1013- .create_issue(&fx.repo_id, &fx.tahinli.id, " ", "")1010+ Err(Error::Invalid(_))
1014- .await,1011+ ),
1015- Err(Error::Invalid(_))1012+ "validate: blank title"
1016- ));1013+ );
1017- assert!(matches!(1014+ assert!(
1018- fx.store1015+ matches!(
1019- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", &"x".repeat(MAX_BODY + 1))1016+ s.create_issue(&repo, &fx.tahinli.id, "t", &"x".repeat(MAX_BODY + 1))
1020- .await,1017+ .await,
1021- Err(Error::Invalid(_))1018+ Err(Error::Invalid(_))
1022- ));1019+ ),
1023- assert!(matches!(1020+ "validate: body too long"
1024- fx.store1021+ );
1025- .create_issue("missing", &fx.tahinli.id, "t", "")1022+ assert!(
1026- .await,1023+ matches!(
1027- Err(Error::NotFound)1024+ s.create_issue("missing", &fx.tahinli.id, "t", "").await,
1028- ));1025+ Err(Error::NotFound)
1029- // A failed create does not consume a number.1026+ ),
1030- let ok = fx1027+ "validate: unknown repo"
1031- .store1028+ );
1032- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "")1029+ let ok = s
1030+ .create_issue(&repo, &fx.tahinli.id, "t", "")
1033 .await1031 .await
1034 .unwrap();1032 .unwrap();
1035- assert_eq!(ok.number, 1);1033+ assert_eq!(ok.number, 1, "validate: failed creates kept no number");
1036- }
1037 1034
1038- #[tokio::test]1035+ // Scenario 3: concurrent creates still get unique numbers one to twenty.
1039- async fn concurrent_creates_get_unique_numbers_one_to_twenty() {1036+ let repo = fresh_repo(s, &fx.tahinli.id, "concurrent").await;
1040- let fx = fixture().await;
1041 let mut handles = Vec::new();1037 let mut handles = Vec::new();
1042 for i in 0..20 {1038 for i in 0..20 {
1043- let store = fx.store.clone();1039+ let store = s.clone();
1044- let repo_id = fx.repo_id.clone();1040+ let repo_id = repo.clone();
1045 let author = fx.tahinli.id.clone();1041 let author = fx.tahinli.id.clone();
1046 handles.push(tokio::spawn(async move {1042 handles.push(tokio::spawn(async move {
1047 store1043 store
@@ -1054,74 +1050,74 @@ mod tests {
1054 numbers.push(h.await.unwrap().unwrap().number);1050 numbers.push(h.await.unwrap().unwrap().number);
1055 }1051 }
1056 numbers.sort_unstable();1052 numbers.sort_unstable();
1057- assert_eq!(numbers, (1..=20).collect::<Vec<i64>>());1053+ assert_eq!(
1058- let (open, closed) = fx.store.issue_counts(&fx.repo_id).await.unwrap();1054+ numbers,
1059- assert_eq!((open, closed), (20, 0));1055+ (1..=20).collect::<Vec<i64>>(),
1060- }1056+ "concurrent: unique numbers"
1057+ );
1058+ assert_eq!(
1059+ s.issue_counts(&repo).await.unwrap(),
1060+ (20, 0),
1061+ "concurrent: all open"
1062+ );
1061 1063
1062- #[tokio::test]1064+ // Scenario 4: state, author and text filters.
1063- async fn list_filters_state_author_and_query() {1065+ let repo = fresh_repo(s, &fx.tahinli.id, "filters").await;
1064- let fx = fixture().await;1066+ let a = s
1065- let a = fx1067+ .create_issue(&repo, &fx.tahinli.id, "Crash on start", "")
1066- .store
1067- .create_issue(&fx.repo_id, &fx.tahinli.id, "Crash on start", "")
1068 .await1068 .await
1069 .unwrap();1069 .unwrap();
1070- fx.store1070+ s.create_issue(&repo, &fx.bob.id, "Typo in docs", "")
1071- .create_issue(&fx.repo_id, &fx.bob.id, "Typo in docs", "")
1072- .await
1073- .unwrap();
1074- fx.store
1075- .set_issue_state(
1076- &a.id,
1077- Some(&fx.tahinli.id),
1078- IssueState::Closed,
1079- Some(StateReason::Completed),
1080- Json::Null,
1081- )
1082 .await1071 .await
1083 .unwrap();1072 .unwrap();
1073+ s.set_issue_state(
1074+ &a.id,
1075+ Some(&fx.tahinli.id),
1076+ IssueState::Closed,
1077+ Some(StateReason::Completed),
1078+ Json::Null,
1079+ )
1080+ .await
1081+ .unwrap();
1084 1082
1085 // Default filter (open only).1083 // Default filter (open only).
1086- let open = IssueFilter::default();
1087 assert_eq!(1084 assert_eq!(
1088- titles(&fx.store, &fx.repo_id, &open).await,1085+ titles(s, &repo, &IssueFilter::default()).await,
1089- vec!["Typo in docs"]1086+ vec!["Typo in docs"],
1087+ "filters: default is open only"
1090 );1088 );
1091- let (items, total) = fx.store.list_issues(&fx.repo_id, &all()).await.unwrap();1089+ let (items, total) = s.list_issues(&repo, &all()).await.unwrap();
1092- assert_eq!((items.len(), total), (2, 2));1090+ assert_eq!((items.len(), total), (2, 2), "filters: all states");
1093-
1094 let by_bob = IssueFilter {1091 let by_bob = IssueFilter {
1095 author: Some("BOB".into()),1092 author: Some("BOB".into()),
1096 ..all()1093 ..all()
1097 };1094 };
1098 assert_eq!(1095 assert_eq!(
1099- titles(&fx.store, &fx.repo_id, &by_bob).await,1096+ titles(s, &repo, &by_bob).await,
1100- vec!["Typo in docs"]1097+ vec!["Typo in docs"],
1098+ "filters: author is case-insensitive"
1101 );1099 );
1102-
1103 let closed = IssueFilter {1100 let closed = IssueFilter {
1104 state: Some(IssueState::Closed),1101 state: Some(IssueState::Closed),
1105 ..all()1102 ..all()
1106 };1103 };
1107 assert_eq!(1104 assert_eq!(
1108- titles(&fx.store, &fx.repo_id, &closed).await,1105+ titles(s, &repo, &closed).await,
1109- vec!["Crash on start"]1106+ vec!["Crash on start"],
1107+ "filters: closed"
1110 );1108 );
1111-
1112 let q = IssueFilter {1109 let q = IssueFilter {
1113 query: Some("CRASH".into()),1110 query: Some("CRASH".into()),
1114 ..all()1111 ..all()
1115 };1112 };
1116 assert_eq!(1113 assert_eq!(
1117- titles(&fx.store, &fx.repo_id, &q).await,1114+ titles(s, &repo, &q).await,
1118- vec!["Crash on start"]1115+ vec!["Crash on start"],
1116+ "filters: text is case-insensitive"
1119 );1117 );
1120- }
1121 1118
1122- #[tokio::test]1119+ // Scenario 5: wildcards in the text query are literal.
1123- async fn query_treats_wildcards_literally() {1120+ let repo = fresh_repo(s, &fx.tahinli.id, "wildcards").await;
1124- let fx = fixture().await;
1125 for t in [1121 for t in [
1126 "100% done",1122 "100% done",
1127 "100 done",1123 "100 done",
@@ -1130,37 +1126,42 @@ mod tests {
1130 "back\\slash",1126 "back\\slash",
1131 "backXslash",1127 "backXslash",
1132 ] {1128 ] {
1133- fx.store1129+ s.create_issue(&repo, &fx.tahinli.id, t, "").await.unwrap();
1134- .create_issue(&fx.repo_id, &fx.tahinli.id, t, "")
1135- .await
1136- .unwrap();
1137 }1130 }
1138 let find = |q: &str| IssueFilter {1131 let find = |q: &str| IssueFilter {
1139 query: Some(q.into()),1132 query: Some(q.into()),
1140 ..all()1133 ..all()
1141 };1134 };
1142 assert_eq!(1135 assert_eq!(
1143- titles(&fx.store, &fx.repo_id, &find("100%")).await,1136+ titles(s, &repo, &find("100%")).await,
1144- vec!["100% done"]1137+ vec!["100% done"],
1138+ "wildcards: percent"
1145 );1139 );
1146 assert_eq!(1140 assert_eq!(
1147- titles(&fx.store, &fx.repo_id, &find("a_b")).await,1141+ titles(s, &repo, &find("a_b")).await,
1148- vec!["a_b"]1142+ vec!["a_b"],
1143+ "wildcards: underscore"
1149 );1144 );
1150 assert_eq!(1145 assert_eq!(
1151- titles(&fx.store, &fx.repo_id, &find("\\")).await,1146+ titles(s, &repo, &find("\\")).await,
1152- vec!["back\\slash"]1147+ vec!["back\\slash"],
1148+ "wildcards: backslash"
1149+ );
1150+ assert_eq!(
1151+ titles(s, &repo, &find("%")).await.len(),
1152+ 1,
1153+ "wildcards: lone percent"
1154+ );
1155+ assert_eq!(
1156+ titles(s, &repo, &find(" ")).await.len(),
1157+ 6,
1158+ "wildcards: blank query matches everything"
1153 );1159 );
1154- assert_eq!(titles(&fx.store, &fx.repo_id, &find("%")).await.len(), 1);
1155- assert_eq!(titles(&fx.store, &fx.repo_id, &find(" ")).await.len(), 6);
1156- }
1157 1160
1158- #[tokio::test]1161+ // Scenario 6: paging and clamping.
1159- async fn list_paginates_and_clamps() {1162+ let repo = fresh_repo(s, &fx.tahinli.id, "pages").await;
1160- let fx = fixture().await;
1161 for i in 0..30 {1163 for i in 0..30 {
1162- fx.store1164+ s.create_issue(&repo, &fx.tahinli.id, &format!("issue {i}"), "")
1163- .create_issue(&fx.repo_id, &fx.tahinli.id, &format!("issue {i}"), "")
1164 .await1165 .await
1165 .unwrap();1166 .unwrap();
1166 }1167 }
@@ -1169,46 +1170,26 @@ mod tests {
1169 per_page,1170 per_page,
1170 ..all()1171 ..all()
1171 };1172 };
1172- let (first, total) = fx1173+ let (first, total) = s.list_issues(&repo, &page(1, 25)).await.unwrap();
1173- .store1174+ assert_eq!((first.len(), total), (25, 30), "pages: first page");
1174- .list_issues(&fx.repo_id, &page(1, 25))1175+ assert_eq!(first[0].issue.number, 30, "pages: newest first");
1175- .await1176+ let (second, _) = s.list_issues(&repo, &page(2, 25)).await.unwrap();
1176- .unwrap();1177+ assert_eq!(second.len(), 5, "pages: second page size");
1177- assert_eq!((first.len(), total), (25, 30));1178+ assert_eq!(second[4].issue.number, 1, "pages: oldest last");
1178- assert_eq!(first[0].issue.number, 30, "newest first");
1179- let (second, _) = fx
1180- .store
1181- .list_issues(&fx.repo_id, &page(2, 25))
1182- .await
1183- .unwrap();
1184- assert_eq!(second.len(), 5);
1185- assert_eq!(second[4].issue.number, 1);
1186 // per_page clamps to 1..=100, page to 1..=1000.1179 // per_page clamps to 1..=100, page to 1..=1000.
1187- let (one, _) = fx1180+ let (one, _) = s.list_issues(&repo, &page(0, 0)).await.unwrap();
1188- .store1181+ assert_eq!(one.len(), 1, "pages: per_page and page clamp up");
1189- .list_issues(&fx.repo_id, &page(0, 0))1182+ assert_eq!(one[0].issue.number, 30, "pages: clamped to first page");
1190- .await1183+ let (big, _) = s.list_issues(&repo, &page(5000, 1000)).await.unwrap();
1191- .unwrap();1184+ assert!(big.is_empty(), "pages: far past the end");
1192- assert_eq!(one.len(), 1);
1193- assert_eq!(one[0].issue.number, 30);
1194- let (big, _) = fx
1195- .store
1196- .list_issues(&fx.repo_id, &page(5000, 1000))
1197- .await
1198- .unwrap();
1199- assert!(big.is_empty());
1200- }
1201 1185
1202- #[tokio::test]1186+ // Scenario 7: sorting by number and by last update.
1203- async fn list_sorts_by_number_and_update_time() {1187+ let repo = fresh_repo(s, &fx.tahinli.id, "sorts").await;
1204- let fx = fixture().await;1188+ let first = s
1205- let first = fx1189+ .create_issue(&repo, &fx.tahinli.id, "one", "")
1206- .store
1207- .create_issue(&fx.repo_id, &fx.tahinli.id, "one", "")
1208 .await1190 .await
1209 .unwrap();1191 .unwrap();
1210- fx.store1192+ s.create_issue(&repo, &fx.tahinli.id, "two", "")
1211- .create_issue(&fx.repo_id, &fx.tahinli.id, "two", "")
1212 .await1193 .await
1213 .unwrap();1194 .unwrap();
1214 let oldest = IssueFilter {1195 let oldest = IssueFilter {
@@ -1216,122 +1197,116 @@ mod tests {
1216 ..all()1197 ..all()
1217 };1198 };
1218 assert_eq!(1199 assert_eq!(
1219- titles(&fx.store, &fx.repo_id, &oldest).await,1200+ titles(s, &repo, &oldest).await,
1220- vec!["one", "two"]1201+ vec!["one", "two"],
1202+ "sorts: oldest first"
1221 );1203 );
1222- fx.store1204+ s.add_comment(&first.id, &fx.bob.id, "bump").await.unwrap();
1223- .add_comment(&first.id, &fx.bob.id, "bump")
1224- .await
1225- .unwrap();
1226 let recent = IssueFilter {1205 let recent = IssueFilter {
1227 sort: IssueSort::RecentlyUpdated,1206 sort: IssueSort::RecentlyUpdated,
1228 ..all()1207 ..all()
1229 };1208 };
1230 assert_eq!(1209 assert_eq!(
1231- titles(&fx.store, &fx.repo_id, &recent).await,1210+ titles(s, &repo, &recent).await,
1232- vec!["one", "two"]1211+ vec!["one", "two"],
1212+ "sorts: recently updated"
1233 );1213 );
1234- }
1235 1214
1236- #[tokio::test]1215+ // Scenario 8: labels and assignees are attached, and filterable by name.
1237- async fn list_attaches_labels_and_assignees_in_batches() {1216+ let repo = fresh_repo(s, &fx.tahinli.id, "labels").await;
1238- let fx = fixture().await;1217+ let issue = s
1239- let issue = fx1218+ .create_issue(&repo, &fx.tahinli.id, "labelled", "")
1240- .store
1241- .create_issue(&fx.repo_id, &fx.tahinli.id, "labelled", "")
1242- .await
1243- .unwrap();
1244- let other = fx
1245- .store
1246- .create_issue(&fx.repo_id, &fx.tahinli.id, "plain", "")
1247 .await1219 .await
1248 .unwrap();1220 .unwrap();
1249- let bug = fx1221+ let other = s
1250- .store1222+ .create_issue(&repo, &fx.tahinli.id, "plain", "")
1251- .create_label(&fx.repo_id, "Bug", "#FF0000", "")
1252 .await1223 .await
1253 .unwrap();1224 .unwrap();
1254- fx.store1225+ let bug = s.create_label(&repo, "Bug", "#FF0000", "").await.unwrap();
1255- .set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id))1226+ s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id))
1256 .await1227 .await
1257 .unwrap();1228 .unwrap();
1258- fx.store1229+ s.set_issue_assignees(&issue.id, &fx.tahinli.id, &["bob".to_string()])
1259- .set_issue_assignees(&issue.id, &fx.tahinli.id, &["bob".to_string()])
1260 .await1230 .await
1261 .unwrap();1231 .unwrap();
1262 1232
1263- let (items, _) = fx.store.list_issues(&fx.repo_id, &all()).await.unwrap();1233+ let (items, _) = s.list_issues(&repo, &all()).await.unwrap();
1264 let labelled = items.iter().find(|i| i.issue.id == issue.id).unwrap();1234 let labelled = items.iter().find(|i| i.issue.id == issue.id).unwrap();
1265- assert_eq!(labelled.labels.len(), 1);1235+ assert_eq!(labelled.labels.len(), 1, "labels: one attached");
1266- assert_eq!(labelled.labels[0].name, "Bug");1236+ assert_eq!(labelled.labels[0].name, "Bug", "labels: name");
1267- assert_eq!(labelled.assignees, vec!["Bob".to_string()]);1237+ assert_eq!(
1268- assert_eq!(labelled.author, "Tahinli");1238+ labelled.assignees,
1239+ vec!["Bob".to_string()],
1240+ "labels: assignee"
1241+ );
1242+ assert_eq!(labelled.author, "Tahinli", "labels: author");
1269 let plain = items.iter().find(|i| i.issue.id == other.id).unwrap();1243 let plain = items.iter().find(|i| i.issue.id == other.id).unwrap();
1270- assert!(plain.labels.is_empty() && plain.assignees.is_empty());1244+ assert!(
1271-1245+ plain.labels.is_empty() && plain.assignees.is_empty(),
1272- // Label and assignee filters, by name and username.1246+ "labels: untouched issue has none"
1247+ );
1273 let by_label = IssueFilter {1248 let by_label = IssueFilter {
1274 label: Some("bug".into()),1249 label: Some("bug".into()),
1275 ..all()1250 ..all()
1276 };1251 };
1277 assert_eq!(1252 assert_eq!(
1278- titles(&fx.store, &fx.repo_id, &by_label).await,1253+ titles(s, &repo, &by_label).await,
1279- vec!["labelled"]1254+ vec!["labelled"],
1255+ "labels: filter by label"
1280 );1256 );
1281 let by_assignee = IssueFilter {1257 let by_assignee = IssueFilter {
1282 assignee: Some("BOB".into()),1258 assignee: Some("BOB".into()),
1283 ..all()1259 ..all()
1284 };1260 };
1285 assert_eq!(1261 assert_eq!(
1286- titles(&fx.store, &fx.repo_id, &by_assignee).await,1262+ titles(s, &repo, &by_assignee).await,
1287- vec!["labelled"]1263+ vec!["labelled"],
1264+ "labels: filter by assignee"
1288 );1265 );
1289- }
1290 1266
1291- #[tokio::test]1267+ // Scenario 9: counts split open and closed.
1292- async fn issue_counts_split_open_and_closed() {1268+ let repo = fresh_repo(s, &fx.tahinli.id, "counts").await;
1293- let fx = fixture().await;1269+ let a = s
1294- let a = fx1270+ .create_issue(&repo, &fx.tahinli.id, "a", "")
1295- .store
1296- .create_issue(&fx.repo_id, &fx.tahinli.id, "a", "")
1297 .await1271 .await
1298 .unwrap();1272 .unwrap();
1299- fx.store1273+ s.create_issue(&repo, &fx.tahinli.id, "b", "")
1300- .create_issue(&fx.repo_id, &fx.tahinli.id, "b", "")
1301 .await1274 .await
1302 .unwrap();1275 .unwrap();
1303- fx.store1276+ s.set_issue_state(&a.id, None, IssueState::Closed, None, Json::Null)
1304- .set_issue_state(&a.id, None, IssueState::Closed, None, Json::Null)
1305 .await1277 .await
1306 .unwrap();1278 .unwrap();
1307- assert_eq!(fx.store.issue_counts(&fx.repo_id).await.unwrap(), (1, 1));1279+ assert_eq!(
1280+ s.issue_counts(&repo).await.unwrap(),
1281+ (1, 1),
1282+ "counts: one open, one closed"
1283+ );
1308 }1284 }
1309 1285
1286+ // Editing, locking, state, comments, events and usernames. Each scenario has its own repo.
1310 #[tokio::test]1287 #[tokio::test]
1311- async fn edit_issue_saves_history_and_records_rename() {1288+ async fn issue_edit_and_thread_rules_hold_in_each_scenario() {
1312 let fx = fixture().await;1289 let fx = fixture().await;
1313- let issue = fx1290+ let s = &fx.store;
1314- .store1291+
1315- .create_issue(&fx.repo_id, &fx.tahinli.id, "old", "old body")1292+ // Scenario 1: an edit saves history and records a rename, body-only edits do not.
1316- .await1293+ let repo = fresh_repo(s, &fx.tahinli.id, "edit").await;
1317- .unwrap();1294+ let issue = s
1318- fx.store1295+ .create_issue(&repo, &fx.tahinli.id, "old", "old body")
1319- .edit_issue(&issue.id, &fx.bob.id, "new", "new body")
1320 .await1296 .await
1321 .unwrap();1297 .unwrap();
1322- let edited = fx1298+ s.edit_issue(&issue.id, &fx.bob.id, "new", "new body")
1323- .store
1324- .issue_by_number(&fx.repo_id, 1)
1325 .await1299 .await
1326- .unwrap()
1327 .unwrap();1300 .unwrap();
1301+ let edited = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1328 assert_eq!(1302 assert_eq!(
1329 (edited.title.as_str(), edited.body.as_str()),1303 (edited.title.as_str(), edited.body.as_str()),
1330- ("new", "new body")1304+ ("new", "new body"),
1305+ "edit: stored"
1331 );1306 );
1332- assert_ne!(edited.updated_at, issue.created_at);1307+ assert_ne!(edited.updated_at, issue.created_at, "edit: stamped");
1333 1308
1334- let conn = fx.store.inner.conn.lock().await;1309+ let conn = s.inner.conn.lock().await;
1335 let mut rows = conn1310 let mut rows = conn
1336 .query(1311 .query(
1337 "SELECT old_title, old_body, editor_id FROM edit_history WHERE target_kind = 'issue' AND target_id = ?1",1312 "SELECT old_title, old_body, editor_id FROM edit_history WHERE target_kind = 'issue' AND target_id = ?1",
@@ -1340,357 +1315,347 @@ mod tests {
1340 .await1315 .await
1341 .unwrap();1316 .unwrap();
1342 let row = rows.next().await.unwrap().expect("history row");1317 let row = rows.next().await.unwrap().expect("history row");
1343- assert_eq!(col_text(&row, 0).unwrap(), "old");1318+ assert_eq!(col_text(&row, 0).unwrap(), "old", "edit: history title");
1344- assert_eq!(col_text(&row, 1).unwrap(), "old body");1319+ assert_eq!(col_text(&row, 1).unwrap(), "old body", "edit: history body");
1345- assert_eq!(col_text(&row, 2).unwrap(), fx.bob.id);1320+ assert_eq!(
1321+ col_text(&row, 2).unwrap(),
1322+ fx.bob.id,
1323+ "edit: history editor"
1324+ );
1346 drop(rows);1325 drop(rows);
1347 drop(conn);1326 drop(conn);
1348 1327
1349- let events = fx.store.list_events(&issue.id).await.unwrap();1328+ let events = s.list_events(&issue.id).await.unwrap();
1350- assert_eq!(events.len(), 1);1329+ assert_eq!(events.len(), 1, "edit: one event");
1351- assert_eq!(events[0].kind, "renamed");1330+ assert_eq!(events[0].kind, "renamed", "edit: rename event");
1352 assert_eq!(1331 assert_eq!(
1353 events[0].data,1332 events[0].data,
1354- serde_json::json!({"from": "old", "to": "new"})1333+ serde_json::json!({"from": "old", "to": "new"}),
1334+ "edit: rename payload"
1335+ );
1336+ assert_eq!(
1337+ events[0].actor_id.as_deref(),
1338+ Some(fx.bob.id.as_str()),
1339+ "edit: rename actor"
1355 );1340 );
1356- assert_eq!(events[0].actor_id.as_deref(), Some(fx.bob.id.as_str()));
1357 1341
1358 // Body-only edit: history yes, no rename event.1342 // Body-only edit: history yes, no rename event.
1359- fx.store1343+ s.edit_issue(&issue.id, &fx.bob.id, "new", "newer")
1360- .edit_issue(&issue.id, &fx.bob.id, "new", "newer")
1361 .await1344 .await
1362 .unwrap();1345 .unwrap();
1363- assert_eq!(fx.store.list_events(&issue.id).await.unwrap().len(), 1);1346+ assert_eq!(
1364- assert!(matches!(1347+ s.list_events(&issue.id).await.unwrap().len(),
1365- fx.store.edit_issue("missing", &fx.bob.id, "t", "").await,1348+ 1,
1366- Err(Error::NotFound)1349+ "edit: body-only adds no event"
1367- ));1350+ );
1368- }1351+ assert!(
1352+ matches!(
1353+ s.edit_issue("missing", &fx.bob.id, "t", "").await,
1354+ Err(Error::NotFound)
1355+ ),
1356+ "edit: unknown issue"
1357+ );
1369 1358
1370- #[tokio::test]1359+ // Scenario 2: locking toggles the reason and records events.
1371- async fn set_issue_locked_toggles_reason_and_records_events() {1360+ let repo = fresh_repo(s, &fx.tahinli.id, "locks").await;
1372- let fx = fixture().await;1361+ let issue = s
1373- let issue = fx1362+ .create_issue(&repo, &fx.tahinli.id, "t", "")
1374- .store
1375- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "")
1376 .await1363 .await
1377 .unwrap();1364 .unwrap();
1378- assert!(!issue.locked);1365+ assert!(!issue.locked, "lock: starts unlocked");
1379- assert_eq!(issue.lock_reason, None);1366+ assert_eq!(issue.lock_reason, None, "lock: no reason yet");
1380 1367
1381 assert!(1368 assert!(
1382- !fx.store1369+ !s.set_issue_locked(&issue.id, None, false, None)
1383- .set_issue_locked(&issue.id, None, false, None)
1384 .await1370 .await
1385- .unwrap()1371+ .unwrap(),
1372+ "lock: unlock when already open is no change"
1386 );1373 );
1387 assert!(1374 assert!(
1388- fx.store1375+ s.set_issue_locked(&issue.id, Some(&fx.tahinli.id), true, Some("spam"))
1389- .set_issue_locked(&issue.id, Some(&fx.tahinli.id), true, Some("spam"))
1390 .await1376 .await
1391- .unwrap()1377+ .unwrap(),
1378+ "lock: lock changes"
1392 );1379 );
1393- let locked = fx1380+ let locked = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1394- .store1381+ assert!(locked.locked, "lock: locked");
1395- .issue_by_number(&fx.repo_id, 1)1382+ assert_eq!(locked.lock_reason.as_deref(), Some("spam"), "lock: reason");
1396- .await
1397- .unwrap()
1398- .unwrap();
1399- assert!(locked.locked);
1400- assert_eq!(locked.lock_reason.as_deref(), Some("spam"));
1401 1383
1402 assert!(1384 assert!(
1403- !fx.store1385+ !s.set_issue_locked(&issue.id, None, true, Some("spam"))
1404- .set_issue_locked(&issue.id, None, true, Some("spam"))
1405 .await1386 .await
1406- .unwrap()1387+ .unwrap(),
1388+ "lock: lock again is no change"
1407 );1389 );
1408 assert!(1390 assert!(
1409- fx.store1391+ s.set_issue_locked(&issue.id, Some(&fx.tahinli.id), false, Some("spam"))
1410- .set_issue_locked(&issue.id, Some(&fx.tahinli.id), false, Some("spam"))
1411 .await1392 .await
1412- .unwrap()1393+ .unwrap(),
1394+ "lock: unlock changes"
1413 );1395 );
1414- let open = fx1396+ let open = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1415- .store1397+ assert!(!open.locked, "lock: unlocked");
1416- .issue_by_number(&fx.repo_id, 1)1398+ assert_eq!(open.lock_reason, None, "lock: reason cleared");
1417- .await
1418- .unwrap()
1419- .unwrap();
1420- assert!(!open.locked);
1421- assert_eq!(open.lock_reason, None);
1422 1399
1423- let err = fx1400+ let err = s
1424- .store
1425 .set_issue_locked(&issue.id, None, true, Some("bogus"))1401 .set_issue_locked(&issue.id, None, true, Some("bogus"))
1426 .await;1402 .await;
1427- assert!(matches!(err, Err(Error::Invalid(_))));1403+ assert!(
1404+ matches!(err, Err(Error::Invalid(_))),
1405+ "lock: unknown reason"
1406+ );
1428 1407
1429- let kinds: Vec<String> = fx1408+ let kinds: Vec<String> = s
1430- .store
1431 .list_events(&issue.id)1409 .list_events(&issue.id)
1432 .await1410 .await
1433 .unwrap()1411 .unwrap()
1434 .into_iter()1412 .into_iter()
1435 .map(|e| e.kind)1413 .map(|e| e.kind)
1436 .collect();1414 .collect();
1437- assert_eq!(kinds, vec!["locked", "unlocked"]);1415+ assert_eq!(kinds, vec!["locked", "unlocked"], "lock: events");
1438- }
1439 1416
1440- #[tokio::test]1417+ // Scenario 3: closing and reopening record events and stamps.
1441- async fn set_issue_state_closes_reopens_and_records_events() {1418+ let repo = fresh_repo(s, &fx.tahinli.id, "state").await;
1442- let fx = fixture().await;1419+ let issue = s
1443- let issue = fx1420+ .create_issue(&repo, &fx.tahinli.id, "t", "")
1444- .store
1445- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "")
1446 .await1421 .await
1447 .unwrap();1422 .unwrap();
1448 1423
1449 assert!(1424 assert!(
1450- !fx.store1425+ !s.set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null)
1451- .set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null)
1452 .await1426 .await
1453- .unwrap()1427+ .unwrap(),
1428+ "state: open when open is no change"
1454 );1429 );
1455 assert!(1430 assert!(
1456- fx.store1431+ s.set_issue_state(
1457- .set_issue_state(1432+ &issue.id,
1458- &issue.id,1433+ Some(&fx.tahinli.id),
1459- Some(&fx.tahinli.id),1434+ IssueState::Closed,
1460- IssueState::Closed,1435+ Some(StateReason::NotPlanned),
1461- Some(StateReason::NotPlanned),1436+ serde_json::json!({"x": 1})
1462- serde_json::json!({"x": 1})1437+ )
1463- )1438+ .await
1464- .await1439+ .unwrap(),
1465- .unwrap()1440+ "state: close changes"
1466 );1441 );
1467 assert!(1442 assert!(
1468- !fx.store1443+ !s.set_issue_state(
1469- .set_issue_state(1444+ &issue.id,
1470- &issue.id,1445+ None,
1471- None,1446+ IssueState::Closed,
1472- IssueState::Closed,1447+ Some(StateReason::Completed),
1473- Some(StateReason::Completed),1448+ Json::Null
1474- Json::Null1449+ )
1475- )1450+ .await
1476- .await1451+ .unwrap(),
1477- .unwrap()1452+ "state: close again is no change"
1478 );1453 );
1479 1454
1480- let closed = fx1455+ let closed = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1481- .store1456+ assert_eq!(closed.state, IssueState::Closed, "state: closed");
1482- .issue_by_number(&fx.repo_id, 1)1457+ assert_eq!(
1483- .await1458+ closed.state_reason,
1484- .unwrap()1459+ Some(StateReason::NotPlanned),
1485- .unwrap();1460+ "state: reason"
1486- assert_eq!(closed.state, IssueState::Closed);1461+ );
1487- assert_eq!(closed.state_reason, Some(StateReason::NotPlanned));1462+ assert!(closed.closed_at.is_some(), "state: closed stamp");
1488- assert!(closed.closed_at.is_some());
1489 1463
1490 assert!(1464 assert!(
1491- fx.store1465+ s.set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null)
1492- .set_issue_state(&issue.id, None, IssueState::Open, None, Json::Null)
1493 .await1466 .await
1494- .unwrap()1467+ .unwrap(),
1468+ "state: reopen changes"
1495 );1469 );
1496- let reopened = fx1470+ let reopened = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1497- .store1471+ assert_eq!(reopened.state, IssueState::Open, "state: reopened");
1498- .issue_by_number(&fx.repo_id, 1)1472+ assert_eq!(reopened.state_reason, None, "state: reason cleared");
1499- .await1473+ assert_eq!(reopened.closed_at, None, "state: closed stamp cleared");
1500- .unwrap()
1501- .unwrap();
1502- assert_eq!(reopened.state, IssueState::Open);
1503- assert_eq!(reopened.state_reason, None);
1504- assert_eq!(reopened.closed_at, None);
1505 1474
1506- let events = fx.store.list_events(&issue.id).await.unwrap();1475+ let events = s.list_events(&issue.id).await.unwrap();
1507 let kinds: Vec<&str> = events.iter().map(|e| e.kind.as_str()).collect();1476 let kinds: Vec<&str> = events.iter().map(|e| e.kind.as_str()).collect();
1508- assert_eq!(kinds, vec!["closed", "reopened"]);1477+ assert_eq!(kinds, vec!["closed", "reopened"], "state: events");
1509 assert_eq!(1478 assert_eq!(
1510 events[0].data,1479 events[0].data,
1511- serde_json::json!({"x": 1, "reason": "not_planned"})1480+ serde_json::json!({"x": 1, "reason": "not_planned"}),
1481+ "state: close payload"
1512 );1482 );
1513- assert_eq!(events[1].data, serde_json::json!({}));1483+ assert_eq!(
1514- assert!(events[1].actor_id.is_none());1484+ events[1].data,
1515-1485+ serde_json::json!({}),
1516- assert!(matches!(1486+ "state: reopen payload"
1517- fx.store1487+ );
1518- .set_issue_state("missing", None, IssueState::Closed, None, Json::Null)1488+ assert!(events[1].actor_id.is_none(), "state: reopen without actor");
1519- .await,
1520- Err(Error::NotFound)
1521- ));
1522- assert!(matches!(
1523- fx.store
1524- .set_issue_state(&issue.id, None, IssueState::Closed, None, Json::from(5))
1525- .await,
1526- Err(Error::Invalid(_))
1527- ));
1528- }
1529 1489
1530- #[tokio::test]1490+ assert!(
1531- async fn comments_maintain_count_and_timestamps() {1491+ matches!(
1532- let fx = fixture().await;1492+ s.set_issue_state("missing", None, IssueState::Closed, None, Json::Null)
1533- let issue = fx1493+ .await,
1534- .store1494+ Err(Error::NotFound)
1535- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "")1495+ ),
1536- .await1496+ "state: unknown issue"
1537- .unwrap();1497+ );
1538- let c1 = fx1498+ assert!(
1539- .store1499+ matches!(
1540- .add_comment(&issue.id, &fx.bob.id, "first")1500+ s.set_issue_state(&issue.id, None, IssueState::Closed, None, Json::from(5))
1501+ .await,
1502+ Err(Error::Invalid(_))
1503+ ),
1504+ "state: payload must be an object"
1505+ );
1506+
1507+ // Scenario 4: comments keep the count, timestamps and ordering in step.
1508+ let repo = fresh_repo(s, &fx.tahinli.id, "comments").await;
1509+ let issue = s
1510+ .create_issue(&repo, &fx.tahinli.id, "t", "")
1541 .await1511 .await
1542 .unwrap();1512 .unwrap();
1543- let c2 = fx1513+ let c1 = s.add_comment(&issue.id, &fx.bob.id, "first").await.unwrap();
1544- .store1514+ let c2 = s
1545 .add_comment(&issue.id, &fx.tahinli.id, "second")1515 .add_comment(&issue.id, &fx.tahinli.id, "second")
1546 .await1516 .await
1547 .unwrap();1517 .unwrap();
1548- assert!(!c1.edited);1518+ assert!(!c1.edited, "comments: fresh comment not edited");
1549- let after_add = fx1519+ let after_add = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1550- .store1520+ assert_eq!(after_add.comment_count, 2, "comments: count after add");
1551- .issue_by_number(&fx.repo_id, 1)1521+ assert_ne!(after_add.updated_at, issue.updated_at, "comments: bumped");
1552- .await1522+
1553- .unwrap()
1554- .unwrap();
1555- assert_eq!(after_add.comment_count, 2);
1556- assert_ne!(after_add.updated_at, issue.updated_at);
1557-
1558- assert!(matches!(
1559- fx.store.add_comment(&issue.id, &fx.bob.id, " ").await,
1560- Err(Error::Invalid(_))
1561- ));
1562- assert!(matches!(
1563- fx.store.add_comment("missing", &fx.bob.id, "x").await,
1564- Err(Error::NotFound)
1565- ));
1566-
1567- let found = fx
1568- .store
1569- .comment_by_id(&issue.id, &c1.id)
1570- .await
1571- .unwrap()
1572- .unwrap();
1573- assert_eq!(found.body, "first");
1574 assert!(1523 assert!(
1575- fx.store1524+ matches!(
1576- .comment_by_id("other", &c1.id)1525+ s.add_comment(&issue.id, &fx.bob.id, " ").await,
1577- .await1526+ Err(Error::Invalid(_))
1578- .unwrap()1527+ ),
1579- .is_none()1528+ "comments: blank body"
1529+ );
1530+ assert!(
1531+ matches!(
1532+ s.add_comment("missing", &fx.bob.id, "x").await,
1533+ Err(Error::NotFound)
1534+ ),
1535+ "comments: unknown issue"
1580 );1536 );
1581 1537
1582- fx.store1538+ let found = s.comment_by_id(&issue.id, &c1.id).await.unwrap().unwrap();
1583- .edit_comment(&c1.id, &fx.bob.id, "edited")1539+ assert_eq!(found.body, "first", "comments: lookup");
1584- .await1540+ assert!(
1585- .unwrap();1541+ s.comment_by_id("other", &c1.id).await.unwrap().is_none(),
1586- let edited = fx1542+ "comments: wrong issue finds nothing"
1587- .store1543+ );
1588- .comment_by_id(&issue.id, &c1.id)1544+
1589- .await1545+ s.edit_comment(&c1.id, &fx.bob.id, "edited").await.unwrap();
1590- .unwrap()1546+ let edited = s.comment_by_id(&issue.id, &c1.id).await.unwrap().unwrap();
1591- .unwrap();1547+ assert_eq!(edited.body, "edited", "comments: edited body");
1592- assert_eq!(edited.body, "edited");1548+ assert!(edited.edited, "comments: edited flag");
1593- assert!(edited.edited);1549+ assert!(
1594- assert!(matches!(1550+ matches!(
1595- fx.store.edit_comment("missing", &fx.bob.id, "x").await,1551+ s.edit_comment("missing", &fx.bob.id, "x").await,
1596- Err(Error::NotFound)1552+ Err(Error::NotFound)
1597- ));1553+ ),
1598- assert!(matches!(1554+ "comments: edit unknown"
1599- fx.store.edit_comment(&c1.id, &fx.bob.id, "").await,1555+ );
1600- Err(Error::Invalid(_))1556+ assert!(
1601- ));1557+ matches!(
1602-1558+ s.edit_comment(&c1.id, &fx.bob.id, "").await,
1603- let comments = fx.store.list_comments(&issue.id).await.unwrap();1559+ Err(Error::Invalid(_))
1560+ ),
1561+ "comments: edit to blank"
1562+ );
1563+
1564+ let comments = s.list_comments(&issue.id).await.unwrap();
1604 let bodies: Vec<&str> = comments.iter().map(|c| c.body.as_str()).collect();1565 let bodies: Vec<&str> = comments.iter().map(|c| c.body.as_str()).collect();
1605- assert_eq!(bodies, vec!["edited", "second"]);1566+ assert_eq!(bodies, vec!["edited", "second"], "comments: order");
1606 1567
1607- fx.store.delete_comment(&c2.id).await.unwrap();1568+ s.delete_comment(&c2.id).await.unwrap();
1608- let after_delete = fx1569+ let after_delete = s.issue_by_number(&repo, 1).await.unwrap().unwrap();
1609- .store1570+ assert_eq!(
1610- .issue_by_number(&fx.repo_id, 1)1571+ after_delete.comment_count, 1,
1611- .await1572+ "comments: count after delete"
1612- .unwrap()1573+ );
1613- .unwrap();
1614- assert_eq!(after_delete.comment_count, 1);
1615 assert!(1574 assert!(
1616- fx.store1575+ s.comment_by_id(&issue.id, &c2.id).await.unwrap().is_none(),
1617- .comment_by_id(&issue.id, &c2.id)1576+ "comments: deleted comment gone"
1618- .await1577+ );
1619- .unwrap()1578+ assert!(
1620- .is_none()1579+ matches!(s.delete_comment(&c2.id).await, Err(Error::NotFound)),
1580+ "comments: delete twice"
1621 );1581 );
1622- assert!(matches!(
1623- fx.store.delete_comment(&c2.id).await,
1624- Err(Error::NotFound)
1625- ));
1626- }
1627 1582
1628- #[tokio::test]1583+ // Scenario 5: events validate their kind and their issue.
1629- async fn add_event_validates_kind_and_issue() {1584+ let repo = fresh_repo(s, &fx.tahinli.id, "events").await;
1630- let fx = fixture().await;1585+ let issue = s
1631- let issue = fx1586+ .create_issue(&repo, &fx.tahinli.id, "t", "")
1632- .store
1633- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "")
1634- .await
1635- .unwrap();
1636- fx.store
1637- .add_event(
1638- &issue.id,
1639- None,
1640- "referenced",
1641- serde_json::json!({"commit": "abc"}),
1642- )
1643 .await1587 .await
1644 .unwrap();1588 .unwrap();
1645- fx.store1589+ s.add_event(
1646- .add_event(&issue.id, Some(&fx.bob.id), "referenced", Json::Null)1590+ &issue.id,
1591+ None,
1592+ "referenced",
1593+ serde_json::json!({"commit": "abc"}),
1594+ )
1595+ .await
1596+ .unwrap();
1597+ s.add_event(&issue.id, Some(&fx.bob.id), "referenced", Json::Null)
1647 .await1598 .await
1648 .unwrap();1599 .unwrap();
1649- let events = fx.store.list_events(&issue.id).await.unwrap();1600+ let events = s.list_events(&issue.id).await.unwrap();
1650- assert_eq!(events.len(), 2);1601+ assert_eq!(events.len(), 2, "events: both stored");
1651- assert_eq!(events[0].data, serde_json::json!({"commit": "abc"}));1602+ assert_eq!(
1652- assert_eq!(events[1].data, serde_json::json!({}));1603+ events[0].data,
1653- assert!(matches!(1604+ serde_json::json!({"commit": "abc"}),
1654- fx.store1605+ "events: payload kept"
1655- .add_event(&issue.id, None, "exploded", Json::Null)1606+ );
1656- .await,1607+ assert_eq!(
1657- Err(Error::Invalid(_))1608+ events[1].data,
1658- ));1609+ serde_json::json!({}),
1659- assert!(matches!(1610+ "events: null becomes object"
1660- fx.store1611+ );
1661- .add_event(&issue.id, None, "referenced", Json::from(true))1612+ assert!(
1662- .await,1613+ matches!(
1663- Err(Error::Invalid(_))1614+ s.add_event(&issue.id, None, "exploded", Json::Null).await,
1664- ));1615+ Err(Error::Invalid(_))
1665- assert!(matches!(1616+ ),
1666- fx.store1617+ "events: unknown kind"
1667- .add_event("missing", None, "referenced", Json::Null)1618+ );
1668- .await,1619+ assert!(
1669- Err(Error::NotFound)1620+ matches!(
1670- ));1621+ s.add_event(&issue.id, None, "referenced", Json::from(true))
1671- }1622+ .await,
1623+ Err(Error::Invalid(_))
1624+ ),
1625+ "events: payload must be an object"
1626+ );
1627+ assert!(
1628+ matches!(
1629+ s.add_event("missing", None, "referenced", Json::Null).await,
1630+ Err(Error::NotFound)
1631+ ),
1632+ "events: unknown issue"
1633+ );
1672 1634
1673- #[tokio::test]1635+ // Scenario 6: usernames are batched and unknown ids are skipped.
1674- async fn usernames_by_ids_is_batched_and_skips_unknown() {
1675- let fx = fixture().await;
1676 let ids = vec![1636 let ids = vec![
1677 fx.tahinli.id.clone(),1637 fx.tahinli.id.clone(),
1678 fx.bob.id.clone(),1638 fx.bob.id.clone(),
1679 "nobody".to_string(),1639 "nobody".to_string(),
1680 ];1640 ];
1681- let map = fx.store.usernames_by_ids(&ids).await.unwrap();1641+ let map = s.usernames_by_ids(&ids).await.unwrap();
1682- assert_eq!(map.len(), 2);1642+ assert_eq!(map.len(), 2, "usernames: unknown skipped");
1683- assert_eq!(map[&fx.tahinli.id].0, "Tahinli");1643+ assert_eq!(map[&fx.tahinli.id].0, "Tahinli", "usernames: name");
1684- assert_eq!(map[&fx.bob.id].1, None);1644+ assert_eq!(map[&fx.bob.id].1, None, "usernames: no display name");
1685- assert!(fx.store.usernames_by_ids(&[]).await.unwrap().is_empty());1645+ assert!(
1646+ s.usernames_by_ids(&[]).await.unwrap().is_empty(),
1647+ "usernames: empty input"
1648+ );
1686 }1649 }
1687 1650
1651+ // Kept on its own store: it counts rows in whole tables, so other repos must not exist.
1688 #[tokio::test]1652 #[tokio::test]
1689 async fn delete_repo_leaves_no_m3_rows() {1653 async fn delete_repo_leaves_no_m3_rows() {
1690 let fx = fixture().await;1654 let fx = fixture().await;
1655+ let repo = fresh_repo(&fx.store, &fx.tahinli.id, "proj").await;
1691 let issue = fx1656 let issue = fx
1692 .store1657 .store
1693- .create_issue(&fx.repo_id, &fx.tahinli.id, "t", "")1658+ .create_issue(&repo, &fx.tahinli.id, "t", "")
1694 .await1659 .await
1695 .unwrap();1660 .unwrap();
1696 let comment = fx1661 let comment = fx
@@ -1708,7 +1673,7 @@ mod tests {
1708 .unwrap();1673 .unwrap();
1709 let label = fx1674 let label = fx
1710 .store1675 .store
1711- .create_label(&fx.repo_id, "bug", "#000000", "")1676+ .create_label(&repo, "bug", "#000000", "")
1712 .await1677 .await
1713 .unwrap();1678 .unwrap();
1714 fx.store1679 fx.store
@@ -1721,7 +1686,7 @@ mod tests {
1721 .unwrap();1686 .unwrap();
1722 let ms = fx1687 let ms = fx
1723 .store1688 .store
1724- .create_milestone(&fx.repo_id, "v1", "", Some("2030-01-01"))1689+ .create_milestone(&repo, "v1", "", Some("2030-01-01"))
1725 .await1690 .await
1726 .unwrap();1691 .unwrap();
1727 fx.store1692 fx.store
@@ -1729,7 +1694,7 @@ mod tests {
1729 .await1694 .await
1730 .unwrap();1695 .unwrap();
1731 1696
1732- fx.store.delete_repo(&fx.repo_id).await.unwrap();1697+ fx.store.delete_repo(&repo).await.unwrap();
1733 1698
1734 let conn = fx.store.inner.conn.lock().await;1699 let conn = fx.store.inner.conn.lock().await;
1735 for table in [1700 for table in [
crates/forge-core/src/store/labels.rsmodified+415 -398
@@ -739,15 +739,13 @@ async fn commit_or_rollback<T>(tx: Transaction<'_>, outcome: Result<T>) -> Resul
739 mod tests {739 mod tests {
740 use super::*;740 use super::*;
741 use crate::issues::{IssueEvent, StateReason};741 use crate::issues::{IssueEvent, StateReason};
742- use crate::model::{Repo, Role, Visibility};742+ use crate::model::{Role, Visibility};
743 743
744 struct Fixture {744 struct Fixture {
745 _dir: tempfile::TempDir,745 _dir: tempfile::TempDir,
746 store: Store,746 store: Store,
747 tahinli: User,747 tahinli: User,
748 bob: User,748 bob: User,
749- repo: Repo,
750- other: Repo,
751 }749 }
752 750
753 async fn fixture() -> Fixture {751 async fn fixture() -> Fixture {
@@ -758,24 +756,23 @@ mod tests {
758 .await756 .await
759 .unwrap();757 .unwrap();
760 let bob = store.create_user("Bob", None, "hash", false).await.unwrap();758 let bob = store.create_user("Bob", None, "hash", false).await.unwrap();
761- let repo = store
762- .create_repo(&tahinli.id, "repo", "", Visibility::Public, "main")
763- .await
764- .unwrap();
765- let other = store
766- .create_repo(&tahinli.id, "other", "", Visibility::Public, "main")
767- .await
768- .unwrap();
769 Fixture {759 Fixture {
770 _dir: dir,760 _dir: dir,
771 store,761 store,
772 tahinli,762 tahinli,
773 bob,763 bob,
774- repo,
775- other,
776 }764 }
777 }765 }
778 766
767+ /// A repo of its own, so label and milestone lists stay exact per scenario.
768+ async fn fresh_repo(store: &Store, owner: &str, name: &str) -> String {
769+ store
770+ .create_repo(owner, name, "", Visibility::Public, "main")
771+ .await
772+ .unwrap()
773+ .id
774+ }
775+
779 /// Data of every event of `kind`.776 /// Data of every event of `kind`.
780 fn data_of(events: &[IssueEvent], kind: &str) -> Vec<Value> {777 fn data_of(events: &[IssueEvent], kind: &str) -> Vec<Value> {
781 events778 events
@@ -789,385 +786,405 @@ mod tests {
789 labels.iter().map(|l| l.name.as_str()).collect()786 labels.iter().map(|l| l.name.as_str()).collect()
790 }787 }
791 788
789+ // Repo labels: creating, defaults, listing, updating and deleting.
790+ // Every scenario has its own repos, so lists and counts stay exact.
792 #[tokio::test]791 #[tokio::test]
793- async fn create_label_normalizes_and_rejects_duplicates() {792+ async fn label_rules_hold_in_each_scenario() {
794 let fx = fixture().await;793 let fx = fixture().await;
795- let label = fx794+ let s = &fx.store;
796- .store795+ let owner = &fx.tahinli.id;
797- .create_label(&fx.repo.id, " Bug ", "#FF0000", "broken")796+
797+ // Scenario 1: names are trimmed and colours normalized; duplicates are refused.
798+ let repo = fresh_repo(s, owner, "labels-create").await;
799+ let other = fresh_repo(s, owner, "labels-create-other").await;
800+ let label = s
801+ .create_label(&repo, " Bug ", "#FF0000", "broken")
798 .await802 .await
799 .unwrap();803 .unwrap();
800- assert_eq!(label.name, "Bug");804+ assert_eq!(label.name, "Bug", "create: name trimmed");
801- assert_eq!(label.color, "#ff0000");805+ assert_eq!(label.color, "#ff0000", "create: colour lowercased");
802- assert_eq!(label.description, "broken");806+ assert_eq!(label.description, "broken", "create: description kept");
803- assert!(matches!(
804- fx.store
805- .create_label(&fx.repo.id, "BUG", "#00ff00", "")
806- .await,
807- Err(Error::Conflict(_))
808- ));
809 assert!(807 assert!(
810- fx.store808+ matches!(
811- .create_label(&fx.repo.id, "bad", "red", "")809+ s.create_label(&repo, "BUG", "#00ff00", "").await,
812- .await810+ Err(Error::Conflict(_))
813- .is_err()811+ ),
812+ "create: duplicate ignoring case"
813+ );
814+ assert!(
815+ s.create_label(&repo, "bad", "red", "").await.is_err(),
816+ "create: colour must be hex"
814 );817 );
815 // The same name in another repo is a different label.818 // The same name in another repo is a different label.
816- fx.store819+ s.create_label(&other, "bug", "#000000", "").await.unwrap();
817- .create_label(&fx.other.id, "bug", "#000000", "")
818- .await
819- .unwrap();
820- }
821 820
822- #[tokio::test]821+ // Scenario 2: default labels are added once and a second run adds nothing.
823- async fn create_default_labels_adds_nine_idempotent_and_skips_taken_names() {822+ let repo = fresh_repo(s, owner, "labels-defaults").await;
824- let fx = fixture().await;
825- let s = &fx.store;
826- // Case 1: an empty repo gets all nine defaults, and a second run adds nothing.
827 assert_eq!(823 assert_eq!(
828- s.create_default_labels(&fx.repo.id).await.unwrap(),824+ s.create_default_labels(&repo).await.unwrap(),
829 9,825 9,
830- "case empty repo"826+ "defaults: nine added"
831 );827 );
832- let labels = s.list_labels(&fx.repo.id).await.unwrap();828+ let labels = s.list_labels(&repo).await.unwrap();
833- assert_eq!(labels.len(), 9, "case empty repo");829+ assert_eq!(labels.len(), 9, "defaults: nine listed");
834 let bug = labels.iter().find(|l| l.name == "bug").unwrap();830 let bug = labels.iter().find(|l| l.name == "bug").unwrap();
835- assert_eq!(bug.color, "#d73a4a", "case empty repo");831+ assert_eq!(bug.color, "#d73a4a", "defaults: colour");
836 assert_eq!(832 assert_eq!(
837 bug.description, "Something isn't working",833 bug.description, "Something isn't working",
838- "case empty repo"834+ "defaults: description"
839 );835 );
840 assert_eq!(836 assert_eq!(
841- s.create_default_labels(&fx.repo.id).await.unwrap(),837+ s.create_default_labels(&repo).await.unwrap(),
842 0,838 0,
843- "case second run"839+ "defaults: second run adds nothing"
844 );840 );
845 assert_eq!(841 assert_eq!(
846- s.list_labels(&fx.repo.id).await.unwrap().len(),842+ s.list_labels(&repo).await.unwrap().len(),
847 9,843 9,
848- "case second run"844+ "defaults: existing rows kept"
849 );845 );
850 846
851- // Case 2: a name already taken (in another case) keeps the existing row.847+ // Scenario 3: defaults skip names already taken.
852- s.create_label(&fx.other.id, "BUG", "#000000", "mine")848+ let repo = fresh_repo(s, owner, "labels-taken").await;
849+ s.create_label(&repo, "BUG", "#000000", "mine")
853 .await850 .await
854 .unwrap();851 .unwrap();
855 assert_eq!(852 assert_eq!(
856- s.create_default_labels(&fx.other.id).await.unwrap(),853+ s.create_default_labels(&repo).await.unwrap(),
857 8,854 8,
858- "case taken name"855+ "taken: eight added"
859 );856 );
860- let labels = s.list_labels(&fx.other.id).await.unwrap();857+ let labels = s.list_labels(&repo).await.unwrap();
861- assert_eq!(labels.len(), 9, "case taken name");858+ assert_eq!(labels.len(), 9, "taken: nine listed");
862 let bug = labels.iter().find(|l| l.name == "BUG").unwrap();859 let bug = labels.iter().find(|l| l.name == "BUG").unwrap();
863- assert_eq!(bug.color, "#000000", "case taken name");860+ assert_eq!(bug.color, "#000000", "taken: own colour kept");
864- assert_eq!(bug.description, "mine", "case taken name");861+ assert_eq!(bug.description, "mine", "taken: own description kept");
865- }862+
866-863+ // Scenario 4: listing is sorted and scoped to the repo.
867- #[tokio::test]864+ let repo = fresh_repo(s, owner, "labels-sorted").await;
868- async fn list_labels_is_sorted_and_scoped() {865+ let other = fresh_repo(s, owner, "labels-sorted-other").await;
869- let fx = fixture().await;866+ s.create_label(&repo, "b", "#111111", "").await.unwrap();
870- let s = &fx.store;867+ s.create_label(&repo, "A", "#111111", "").await.unwrap();
871- s.create_label(&fx.repo.id, "b", "#111111", "")868+ s.create_label(&other, "c", "#111111", "").await.unwrap();
872- .await
873- .unwrap();
874- s.create_label(&fx.repo.id, "A", "#111111", "")
875- .await
876- .unwrap();
877- s.create_label(&fx.other.id, "c", "#111111", "")
878- .await
879- .unwrap();
880 assert_eq!(869 assert_eq!(
881- names(&s.list_labels(&fx.repo.id).await.unwrap()),870+ names(&s.list_labels(&repo).await.unwrap()),
882- ["A", "b"]871+ ["A", "b"],
872+ "sorted: case-insensitive order"
873+ );
874+ assert_eq!(
875+ names(&s.list_labels(&other).await.unwrap()),
876+ ["c"],
877+ "sorted: other repo only has its own"
883 );878 );
884- assert_eq!(names(&s.list_labels(&fx.other.id).await.unwrap()), ["c"]);
885- }
886-
887- #[tokio::test]
888- async fn update_label_checks_conflicts_and_scope() {
889- let fx = fixture().await;
890- let s = &fx.store;
891- let bug = s
892- .create_label(&fx.repo.id, "bug", "#111111", "")
893- .await
894- .unwrap();
895- s.create_label(&fx.repo.id, "docs", "#222222", "")
896- .await
897- .unwrap();
898- let foreign = s
899- .create_label(&fx.other.id, "x", "#333333", "")
900- .await
901- .unwrap();
902 879
903- assert!(matches!(880+ // Scenario 5: update checks conflicts and scope.
904- s.update_label(&fx.repo.id, &bug.id, "DOCS", "#111111", "")881+ let repo = fresh_repo(s, owner, "labels-update").await;
905- .await,882+ let other = fresh_repo(s, owner, "labels-update-other").await;
906- Err(Error::Conflict(_))883+ let bug = s.create_label(&repo, "bug", "#111111", "").await.unwrap();
907- ));884+ s.create_label(&repo, "docs", "#222222", "").await.unwrap();
908- assert!(matches!(885+ let foreign = s.create_label(&other, "x", "#333333", "").await.unwrap();
909- s.update_label(&fx.repo.id, &foreign.id, "y", "#111111", "")886+ assert!(
910- .await,887+ matches!(
911- Err(Error::NotFound)888+ s.update_label(&repo, &bug.id, "DOCS", "#111111", "").await,
912- ));889+ Err(Error::Conflict(_))
913- s.update_label(&fx.repo.id, &bug.id, "Defect", "#00FF00", "renamed")890+ ),
891+ "update: rename onto a taken name"
892+ );
893+ assert!(
894+ matches!(
895+ s.update_label(&repo, &foreign.id, "y", "#111111", "").await,
896+ Err(Error::NotFound)
897+ ),
898+ "update: label of another repo"
899+ );
900+ s.update_label(&repo, &bug.id, "Defect", "#00FF00", "renamed")
914 .await901 .await
915 .unwrap();902 .unwrap();
916- let labels = s.list_labels(&fx.repo.id).await.unwrap();903+ let labels = s.list_labels(&repo).await.unwrap();
917- assert_eq!(names(&labels), ["Defect", "docs"]);904+ assert_eq!(names(&labels), ["Defect", "docs"], "update: renamed");
918- assert_eq!(labels[0].color, "#00ff00");905+ assert_eq!(labels[0].color, "#00ff00", "update: colour normalized");
919- assert_eq!(labels[0].description, "renamed");906+ assert_eq!(labels[0].description, "renamed", "update: description");
920- // The foreign label was not touched.907+ assert_eq!(
921- assert_eq!(names(&s.list_labels(&fx.other.id).await.unwrap()), ["x"]);908+ names(&s.list_labels(&other).await.unwrap()),
922- }909+ ["x"],
910+ "update: foreign label untouched"
911+ );
923 912
924- #[tokio::test]913+ // Scenario 6: delete removes links and is scoped to the repo.
925- async fn delete_label_removes_links_and_is_scoped() {914+ let repo = fresh_repo(s, owner, "labels-delete").await;
926- let fx = fixture().await;915+ let other = fresh_repo(s, owner, "labels-delete-other").await;
927- let s = &fx.store;916+ let bug = s.create_label(&repo, "bug", "#111111", "").await.unwrap();
928- let bug = s
929- .create_label(&fx.repo.id, "bug", "#111111", "")
930- .await
931- .unwrap();
932 let issue = s917 let issue = s
933- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")918+ .create_issue(&repo, &fx.tahinli.id, "t", "")
934 .await919 .await
935 .unwrap();920 .unwrap();
936 s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id))921 s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&bug.id))
937 .await922 .await
938 .unwrap();923 .unwrap();
939-924+ assert!(
940- assert!(matches!(925+ matches!(s.delete_label(&other, &bug.id).await, Err(Error::NotFound)),
941- s.delete_label(&fx.other.id, &bug.id).await,926+ "delete: wrong repo"
942- Err(Error::NotFound)927+ );
943- ));928+ assert_eq!(
944- assert_eq!(s.issue_labels(&issue.id).await.unwrap().len(), 1);929+ s.issue_labels(&issue.id).await.unwrap().len(),
945-930+ 1,
946- s.delete_label(&fx.repo.id, &bug.id).await.unwrap();931+ "delete: link kept after refused delete"
947- assert!(s.issue_labels(&issue.id).await.unwrap().is_empty());932+ );
948- assert!(s.list_labels(&fx.repo.id).await.unwrap().is_empty());933+ s.delete_label(&repo, &bug.id).await.unwrap();
934+ assert!(
935+ s.issue_labels(&issue.id).await.unwrap().is_empty(),
936+ "delete: link removed"
937+ );
938+ assert!(
939+ s.list_labels(&repo).await.unwrap().is_empty(),
940+ "delete: label gone"
941+ );
949 }942 }
950 943
944+ // Labels and assignees on one issue. Each scenario has its own repo.
951 #[tokio::test]945 #[tokio::test]
952- async fn set_issue_labels_diffs_and_records_events() {946+ async fn issue_label_and_assignee_rules_hold_in_each_scenario() {
953 let fx = fixture().await;947 let fx = fixture().await;
954 let s = &fx.store;948 let s = &fx.store;
955- let issue = s949+ let owner = &fx.tahinli.id;
956- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")
957- .await
958- .unwrap();
959- let a = s
960- .create_label(&fx.repo.id, "a", "#111111", "")
961- .await
962- .unwrap();
963- let b = s
964- .create_label(&fx.repo.id, "b", "#111111", "")
965- .await
966- .unwrap();
967- let c = s
968- .create_label(&fx.repo.id, "c", "#111111", "")
969- .await
970- .unwrap();
971 950
972- s.set_issue_labels(&issue.id, &fx.tahinli.id, &[a.id.clone(), b.id.clone()])951+ // Scenario 1: label changes are diffed and recorded as events.
952+ let repo = fresh_repo(s, owner, "issue-labels").await;
953+ let issue = s.create_issue(&repo, owner, "t", "").await.unwrap();
954+ let a = s.create_label(&repo, "a", "#111111", "").await.unwrap();
955+ let b = s.create_label(&repo, "b", "#111111", "").await.unwrap();
956+ let c = s.create_label(&repo, "c", "#111111", "").await.unwrap();
957+
958+ s.set_issue_labels(&issue.id, owner, &[a.id.clone(), b.id.clone()])
973 .await959 .await
974 .unwrap();960 .unwrap();
975 let events = s.list_events(&issue.id).await.unwrap();961 let events = s.list_events(&issue.id).await.unwrap();
976 let labeled = data_of(&events, "labeled");962 let labeled = data_of(&events, "labeled");
977- assert_eq!(labeled.len(), 2);963+ assert_eq!(labeled.len(), 2, "labels: two added");
978- assert!(labeled.contains(&json!({"label": "a"})));964+ assert!(
979- assert!(labeled.contains(&json!({"label": "b"})));965+ labeled.contains(&json!({"label": "a"})),
980- assert!(data_of(&events, "unlabeled").is_empty());966+ "labels: a recorded"
967+ );
968+ assert!(
969+ labeled.contains(&json!({"label": "b"})),
970+ "labels: b recorded"
971+ );
972+ assert!(
973+ data_of(&events, "unlabeled").is_empty(),
974+ "labels: none removed"
975+ );
981 976
982- s.set_issue_labels(&issue.id, &fx.tahinli.id, &[b.id.clone(), c.id.clone()])977+ s.set_issue_labels(&issue.id, owner, &[b.id.clone(), c.id.clone()])
983 .await978 .await
984 .unwrap();979 .unwrap();
985 let events = s.list_events(&issue.id).await.unwrap();980 let events = s.list_events(&issue.id).await.unwrap();
986- assert_eq!(data_of(&events, "unlabeled"), [json!({"label": "a"})]);981+ assert_eq!(
987- assert_eq!(data_of(&events, "labeled").len(), 3);982+ data_of(&events, "unlabeled"),
988- assert_eq!(names(&s.issue_labels(&issue.id).await.unwrap()), ["b", "c"]);983+ [json!({"label": "a"})],
984+ "labels: a removed"
985+ );
986+ assert_eq!(data_of(&events, "labeled").len(), 3, "labels: c added");
987+ assert_eq!(
988+ names(&s.issue_labels(&issue.id).await.unwrap()),
989+ ["b", "c"],
990+ "labels: resulting set"
991+ );
989 992
990 // Setting the same set again changes nothing and records nothing.993 // Setting the same set again changes nothing and records nothing.
991- s.set_issue_labels(&issue.id, &fx.tahinli.id, &[c.id.clone(), b.id.clone()])994+ s.set_issue_labels(&issue.id, owner, &[c.id.clone(), b.id.clone()])
992 .await995 .await
993 .unwrap();996 .unwrap();
994 let events = s.list_events(&issue.id).await.unwrap();997 let events = s.list_events(&issue.id).await.unwrap();
995- assert_eq!(data_of(&events, "labeled").len(), 3);998+ assert_eq!(
996- assert_eq!(data_of(&events, "unlabeled").len(), 1);999+ data_of(&events, "labeled").len(),
997- }1000+ 3,
1001+ "labels: same set, no add"
1002+ );
1003+ assert_eq!(
1004+ data_of(&events, "unlabeled").len(),
1005+ 1,
1006+ "labels: same set, no remove"
1007+ );
998 1008
999- #[tokio::test]1009+ // Scenario 2: a label from another repo rejects the whole write.
1000- async fn set_issue_labels_rejects_foreign_label_without_partial_write() {1010+ let repo = fresh_repo(s, owner, "issue-labels-repo").await;
1001- let fx = fixture().await;1011+ let other = fresh_repo(s, owner, "issue-labels-other").await;
1002- let s = &fx.store;1012+ let issue = s.create_issue(&repo, owner, "t", "").await.unwrap();
1003- let issue = s1013+ let a = s.create_label(&repo, "a", "#111111", "").await.unwrap();
1004- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")1014+ let b = s.create_label(&repo, "b", "#111111", "").await.unwrap();
1005- .await1015+ let foreign = s.create_label(&other, "x", "#111111", "").await.unwrap();
1006- .unwrap();1016+ s.set_issue_labels(&issue.id, owner, std::slice::from_ref(&a.id))
1007- let a = s
1008- .create_label(&fx.repo.id, "a", "#111111", "")
1009- .await
1010- .unwrap();
1011- let b = s
1012- .create_label(&fx.repo.id, "b", "#111111", "")
1013- .await
1014- .unwrap();
1015- let foreign = s
1016- .create_label(&fx.other.id, "x", "#111111", "")
1017- .await
1018- .unwrap();
1019- s.set_issue_labels(&issue.id, &fx.tahinli.id, std::slice::from_ref(&a.id))
1020 .await1017 .await
1021 .unwrap();1018 .unwrap();
1022 1019
1023 let err = s1020 let err = s
1024- .set_issue_labels(1021+ .set_issue_labels(&issue.id, owner, &[b.id.clone(), foreign.id.clone()])
1025- &issue.id,
1026- &fx.tahinli.id,
1027- &[b.id.clone(), foreign.id.clone()],
1028- )
1029 .await;1022 .await;
1030- assert!(matches!(err, Err(Error::Invalid(_))));1023+ assert!(matches!(err, Err(Error::Invalid(_))), "foreign: refused");
1031- assert_eq!(names(&s.issue_labels(&issue.id).await.unwrap()), ["a"]);1024+ assert_eq!(
1025+ names(&s.issue_labels(&issue.id).await.unwrap()),
1026+ ["a"],
1027+ "foreign: no partial write"
1028+ );
1032 let events = s.list_events(&issue.id).await.unwrap();1029 let events = s.list_events(&issue.id).await.unwrap();
1033- assert_eq!(data_of(&events, "labeled").len(), 1);1030+ assert_eq!(data_of(&events, "labeled").len(), 1, "foreign: no event");
1034-1031+ assert!(
1035- assert!(matches!(1032+ matches!(
1036- s.set_issue_labels("missing", &fx.tahinli.id, &[]).await,1033+ s.set_issue_labels("missing", owner, &[]).await,
1037- Err(Error::NotFound)1034+ Err(Error::NotFound)
1038- ));1035+ ),
1039- }1036+ "foreign: unknown issue"
1037+ );
1040 1038
1041- #[tokio::test]1039+ // Scenario 3: assignees are deduplicated, resolved and recorded.
1042- async fn set_issue_assignees_dedups_resolves_and_records_events() {1040+ let repo = fresh_repo(s, owner, "assignees").await;
1043- let fx = fixture().await;1041+ let issue = s.create_issue(&repo, owner, "t", "").await.unwrap();
1044- let s = &fx.store;
1045- let issue = s
1046- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")
1047- .await
1048- .unwrap();
1049 let input = [1042 let input = [
1050 "bob".to_string(),1043 "bob".to_string(),
1051 "BOB".to_string(),1044 "BOB".to_string(),
1052 " Tahinli ".to_string(),1045 " Tahinli ".to_string(),
1053 ];1046 ];
1054- s.set_issue_assignees(&issue.id, &fx.tahinli.id, &input)1047+ s.set_issue_assignees(&issue.id, owner, &input)
1055 .await1048 .await
1056 .unwrap();1049 .unwrap();
1057 1050
1058 let assignees = s.issue_assignees(&issue.id).await.unwrap();1051 let assignees = s.issue_assignees(&issue.id).await.unwrap();
1059 let display: Vec<&str> = assignees.iter().map(|(_, n)| n.as_str()).collect();1052 let display: Vec<&str> = assignees.iter().map(|(_, n)| n.as_str()).collect();
1060- assert_eq!(display, ["Bob", "Tahinli"]);1053+ assert_eq!(
1054+ display,
1055+ ["Bob", "Tahinli"],
1056+ "assignees: deduplicated and resolved"
1057+ );
1061 let events = s.list_events(&issue.id).await.unwrap();1058 let events = s.list_events(&issue.id).await.unwrap();
1062- assert_eq!(data_of(&events, "assigned").len(), 2);1059+ assert_eq!(
1063- assert!(data_of(&events, "assigned").contains(&json!({"user": "Bob"})));1060+ data_of(&events, "assigned").len(),
1061+ 2,
1062+ "assignees: two events"
1063+ );
1064+ assert!(
1065+ data_of(&events, "assigned").contains(&json!({"user": "Bob"})),
1066+ "assignees: Bob recorded"
1067+ );
1064 1068
1065- s.set_issue_assignees(&issue.id, &fx.tahinli.id, &["tahinli".to_string()])1069+ s.set_issue_assignees(&issue.id, owner, &["tahinli".to_string()])
1066 .await1070 .await
1067 .unwrap();1071 .unwrap();
1068 let assignees = s.issue_assignees(&issue.id).await.unwrap();1072 let assignees = s.issue_assignees(&issue.id).await.unwrap();
1069- assert_eq!(assignees.len(), 1);1073+ assert_eq!(assignees.len(), 1, "assignees: replaced");
1070- assert_eq!(assignees[0].0, fx.tahinli.id);1074+ assert_eq!(assignees[0].0, fx.tahinli.id, "assignees: Tahinli kept");
1071 let events = s.list_events(&issue.id).await.unwrap();1075 let events = s.list_events(&issue.id).await.unwrap();
1072- assert_eq!(data_of(&events, "unassigned"), [json!({"user": "Bob"})]);1076+ assert_eq!(
1073- }1077+ data_of(&events, "unassigned"),
1074-1078+ [json!({"user": "Bob"})],
1075- #[tokio::test]1079+ "assignees: Bob removed"
1076- async fn set_issue_assignees_enforces_limit_existence_and_read_access() {1080+ );
1077- let fx = fixture().await;
1078- let s = &fx.store;
1079- let issue = s
1080- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")
1081- .await
1082- .unwrap();
1083 1081
1082+ // Scenario 4: the limit, existence and read access are enforced.
1083+ let repo = fresh_repo(s, owner, "assign-limit").await;
1084+ let issue = s.create_issue(&repo, owner, "t", "").await.unwrap();
1084 let mut usernames = Vec::new();1085 let mut usernames = Vec::new();
1085 for i in 0..11 {1086 for i in 0..11 {
1086 let name = format!("user{i}");1087 let name = format!("user{i}");
1087 s.create_user(&name, None, "hash", false).await.unwrap();1088 s.create_user(&name, None, "hash", false).await.unwrap();
1088 usernames.push(name);1089 usernames.push(name);
1089 }1090 }
1090- assert!(matches!(1091+ assert!(
1091- s.set_issue_assignees(&issue.id, &fx.tahinli.id, &usernames)1092+ matches!(
1092- .await,1093+ s.set_issue_assignees(&issue.id, owner, &usernames).await,
1093- Err(Error::Invalid(_))1094+ Err(Error::Invalid(_))
1094- ));1095+ ),
1095- s.set_issue_assignees(&issue.id, &fx.tahinli.id, &usernames[..10])1096+ "limit: eleven refused"
1097+ );
1098+ s.set_issue_assignees(&issue.id, owner, &usernames[..10])
1096 .await1099 .await
1097 .unwrap();1100 .unwrap();
1098- assert_eq!(s.issue_assignees(&issue.id).await.unwrap().len(), 10);1101+ assert_eq!(
1099-1102+ s.issue_assignees(&issue.id).await.unwrap().len(),
1100- assert!(matches!(1103+ 10,
1101- s.set_issue_assignees(&issue.id, &fx.tahinli.id, &["ghost".to_string()])1104+ "limit: ten allowed"
1102- .await,1105+ );
1103- Err(Error::Invalid(_))1106+ assert!(
1104- ));1107+ matches!(
1108+ s.set_issue_assignees(&issue.id, owner, &["ghost".to_string()])
1109+ .await,
1110+ Err(Error::Invalid(_))
1111+ ),
1112+ "limit: unknown user refused"
1113+ );
1105 1114
1106 // Bob has no role on a private repo, so he cannot be assigned there.1115 // Bob has no role on a private repo, so he cannot be assigned there.
1107 let secret = s1116 let secret = s
1108- .create_repo(&fx.tahinli.id, "secret", "", Visibility::Private, "main")1117+ .create_repo(owner, "assign-secret", "", Visibility::Private, "main")
1109- .await
1110- .unwrap();
1111- let secret_issue = s
1112- .create_issue(&secret.id, &fx.tahinli.id, "t", "")
1113 .await1118 .await
1114 .unwrap();1119 .unwrap();
1120+ let secret_issue = s.create_issue(&secret.id, owner, "t", "").await.unwrap();
1115 let bob = ["bob".to_string()];1121 let bob = ["bob".to_string()];
1116- assert!(matches!(1122+ assert!(
1117- s.set_issue_assignees(&secret_issue.id, &fx.tahinli.id, &bob)1123+ matches!(
1118- .await,1124+ s.set_issue_assignees(&secret_issue.id, owner, &bob).await,
1119- Err(Error::Invalid(_))1125+ Err(Error::Invalid(_))
1120- ));1126+ ),
1127+ "access: no role refused"
1128+ );
1121 s.set_collaborator(&secret.id, &fx.bob.id, Role::Read)1129 s.set_collaborator(&secret.id, &fx.bob.id, Role::Read)
1122 .await1130 .await
1123 .unwrap();1131 .unwrap();
1124- s.set_issue_assignees(&secret_issue.id, &fx.tahinli.id, &bob)1132+ s.set_issue_assignees(&secret_issue.id, owner, &bob)
1125 .await1133 .await
1126 .unwrap();1134 .unwrap();
1127- assert_eq!(s.issue_assignees(&secret_issue.id).await.unwrap().len(), 1);1135+ assert_eq!(
1136+ s.issue_assignees(&secret_issue.id).await.unwrap().len(),
1137+ 1,
1138+ "access: reader can be assigned"
1139+ );
1128 }1140 }
1129 1141
1142+ // Milestones: creating, listing, updating, linking to issues and deleting.
1130 #[tokio::test]1143 #[tokio::test]
1131- async fn milestones_create_validate_and_count_in_one_listing() {1144+ async fn milestone_rules_hold_in_each_scenario() {
1132 let fx = fixture().await;1145 let fx = fixture().await;
1133 let s = &fx.store;1146 let s = &fx.store;
1147+ let owner = &fx.tahinli.id;
1148+
1149+ // Scenario 1: dates are validated, titles unique, counts shown in one listing.
1150+ let repo = fresh_repo(s, owner, "milestones").await;
1151+ let other = fresh_repo(s, owner, "milestones-other").await;
1134 let v1 = s1152 let v1 = s
1135- .create_milestone(&fx.repo.id, "v1", "first", Some("2026-12-31"))1153+ .create_milestone(&repo, "v1", "first", Some("2026-12-31"))
1136 .await1154 .await
1137 .unwrap();1155 .unwrap();
1138- assert_eq!(v1.due_on.as_deref(), Some("2026-12-31"));1156+ assert_eq!(
1157+ v1.due_on.as_deref(),
1158+ Some("2026-12-31"),
1159+ "create: due date kept"
1160+ );
1139 for bad in ["2026/12/31", "2026-02-30", "2026-1-5", "2026-12-31T00:00"] {1161 for bad in ["2026/12/31", "2026-02-30", "2026-1-5", "2026-12-31T00:00"] {
1140- assert!(matches!(1162+ assert!(
1141- s.create_milestone(&fx.repo.id, "x", "", Some(bad)).await,1163+ matches!(
1142- Err(Error::Invalid(_))1164+ s.create_milestone(&repo, "x", "", Some(bad)).await,
1143- ));1165+ Err(Error::Invalid(_))
1166+ ),
1167+ "create: bad date {bad}"
1168+ );
1144 }1169 }
1145- assert!(matches!(1170+ assert!(
1146- s.create_milestone(&fx.repo.id, "V1", "", None).await,1171+ matches!(
1147- Err(Error::Conflict(_))1172+ s.create_milestone(&repo, "V1", "", None).await,
1148- ));1173+ Err(Error::Conflict(_))
1174+ ),
1175+ "create: duplicate title ignoring case"
1176+ );
1149 1177
1150- let i1 = s1178+ let i1 = s.create_issue(&repo, owner, "a", "").await.unwrap();
1151- .create_issue(&fx.repo.id, &fx.tahinli.id, "a", "")1179+ let i2 = s.create_issue(&repo, owner, "b", "").await.unwrap();
1152- .await1180+ let i3 = s.create_issue(&repo, owner, "c", "").await.unwrap();
1153- .unwrap();1181+ s.set_issue_milestone(&i1.id, owner, Some(&v1.id))
1154- let i2 = s
1155- .create_issue(&fx.repo.id, &fx.tahinli.id, "b", "")
1156 .await1182 .await
1157 .unwrap();1183 .unwrap();
1158- let i3 = s1184+ s.set_issue_milestone(&i2.id, owner, Some(&v1.id))
1159- .create_issue(&fx.repo.id, &fx.tahinli.id, "c", "")
1160- .await
1161- .unwrap();
1162- s.set_issue_milestone(&i1.id, &fx.tahinli.id, Some(&v1.id))
1163- .await
1164- .unwrap();
1165- s.set_issue_milestone(&i2.id, &fx.tahinli.id, Some(&v1.id))
1166- .await
1167- .unwrap();
1168- s.set_issue_milestone(&i3.id, &fx.tahinli.id, None)
1169 .await1185 .await
1170 .unwrap();1186 .unwrap();
1187+ s.set_issue_milestone(&i3.id, owner, None).await.unwrap();
1171 s.set_issue_state(1188 s.set_issue_state(
1172 &i1.id,1189 &i1.id,
1173 Some(&fx.tahinli.id),1190 Some(&fx.tahinli.id),
@@ -1178,184 +1195,184 @@ mod tests {
1178 .await1195 .await
1179 .unwrap();1196 .unwrap();
1180 1197
1181- let all = s.list_milestones(&fx.repo.id, None).await.unwrap();1198+ let all = s.list_milestones(&repo, None).await.unwrap();
1182- assert_eq!(all.len(), 1);1199+ assert_eq!(all.len(), 1, "list: one milestone");
1183- assert_eq!((all[0].open_issues, all[0].closed_issues), (1, 1));1200+ assert_eq!(
1201+ (all[0].open_issues, all[0].closed_issues),
1202+ (1, 1),
1203+ "list: counts"
1204+ );
1184 assert!(1205 assert!(
1185- s.list_milestones(&fx.repo.id, Some(IssueState::Closed))1206+ s.list_milestones(&repo, Some(IssueState::Closed))
1186 .await1207 .await
1187 .unwrap()1208 .unwrap()
1188- .is_empty()1209+ .is_empty(),
1210+ "list: no closed milestone"
1189 );1211 );
1190 assert_eq!(1212 assert_eq!(
1191- s.list_milestones(&fx.repo.id, Some(IssueState::Open))1213+ s.list_milestones(&repo, Some(IssueState::Open))
1192 .await1214 .await
1193 .unwrap()1215 .unwrap()
1194 .len(),1216 .len(),
1195- 11217+ 1,
1218+ "list: one open milestone"
1196 );1219 );
1197 1220
1198- let got = s1221+ let got = s.milestone_by_id(&repo, &v1.id).await.unwrap().unwrap();
1199- .milestone_by_id(&fx.repo.id, &v1.id)1222+ assert_eq!(
1200- .await1223+ (got.open_issues, got.closed_issues),
1201- .unwrap()1224+ (1, 1),
1202- .unwrap();1225+ "lookup: counts"
1203- assert_eq!((got.open_issues, got.closed_issues), (1, 1));1226+ );
1204 assert!(1227 assert!(
1205- s.milestone_by_id(&fx.other.id, &v1.id)1228+ s.milestone_by_id(&other, &v1.id).await.unwrap().is_none(),
1206- .await1229+ "lookup: other repo finds nothing"
1207- .unwrap()
1208- .is_none()
1209 );1230 );
1210- }
1211 1231
1212- #[tokio::test]1232+ // Scenario 2: updates set and clear the closed stamp and stay scoped.
1213- async fn update_milestone_sets_closed_at_and_is_scoped() {1233+ let repo = fresh_repo(s, owner, "milestone-update").await;
1214- let fx = fixture().await;1234+ let other = fresh_repo(s, owner, "milestone-update-other").await;
1215- let s = &fx.store;
1216 let v1 = s1235 let v1 = s
1217- .create_milestone(&fx.repo.id, "v1", "", Some("2026-12-31"))1236+ .create_milestone(&repo, "v1", "", Some("2026-12-31"))
1218 .await1237 .await
1219 .unwrap();1238 .unwrap();
1220- assert!(matches!(1239+ assert!(
1221- s.update_milestone(&fx.other.id, &v1.id, "v1", "", None, IssueState::Open)1240+ matches!(
1222- .await,1241+ s.update_milestone(&other, &v1.id, "v1", "", None, IssueState::Open)
1223- Err(Error::NotFound)1242+ .await,
1224- ));1243+ Err(Error::NotFound)
1225-1244+ ),
1226- s.update_milestone(1245+ "update: other repo refused"
1227- &fx.repo.id,1246+ );
1228- &v1.id,1247+ s.update_milestone(&repo, &v1.id, "v1.0", "done", None, IssueState::Closed)
1229- "v1.0",
1230- "done",
1231- None,
1232- IssueState::Closed,
1233- )
1234- .await
1235- .unwrap();
1236- let closed = s
1237- .milestone_by_id(&fx.repo.id, &v1.id)
1238 .await1248 .await
1239- .unwrap()
1240 .unwrap();1249 .unwrap();
1241- assert_eq!(closed.title, "v1.0");1250+ let closed = s.milestone_by_id(&repo, &v1.id).await.unwrap().unwrap();
1242- assert_eq!(closed.description, "done");1251+ assert_eq!(closed.title, "v1.0", "update: title");
1243- assert!(closed.due_on.is_none());1252+ assert_eq!(closed.description, "done", "update: description");
1244- assert!(matches!(closed.state, IssueState::Closed));1253+ assert!(closed.due_on.is_none(), "update: due date cleared");
1245- assert!(closed.closed_at.is_some());1254+ assert!(matches!(closed.state, IssueState::Closed), "update: closed");
1255+ assert!(closed.closed_at.is_some(), "update: closed stamp set");
1246 1256
1247- s.update_milestone(&fx.repo.id, &v1.id, "v1.0", "done", None, IssueState::Open)1257+ s.update_milestone(&repo, &v1.id, "v1.0", "done", None, IssueState::Open)
1248 .await1258 .await
1249 .unwrap();1259 .unwrap();
1250- let reopened = s1260+ let reopened = s.milestone_by_id(&repo, &v1.id).await.unwrap().unwrap();
1251- .milestone_by_id(&fx.repo.id, &v1.id)1261+ assert!(
1252- .await1262+ matches!(reopened.state, IssueState::Open),
1253- .unwrap()1263+ "update: reopened"
1254- .unwrap();1264+ );
1255- assert!(matches!(reopened.state, IssueState::Open));1265+ assert!(reopened.closed_at.is_none(), "update: closed stamp cleared");
1256- assert!(reopened.closed_at.is_none());
1257- }
1258 1266
1259- #[tokio::test]1267+ // Scenario 3: an issue takes only milestones of its own repo and records changes.
1260- async fn set_issue_milestone_checks_repo_and_records_events() {1268+ let repo = fresh_repo(s, owner, "issue-milestone").await;
1261- let fx = fixture().await;1269+ let other = fresh_repo(s, owner, "issue-milestone-other").await;
1262- let s = &fx.store;1270+ let issue = s.create_issue(&repo, owner, "t", "").await.unwrap();
1263- let issue = s1271+ let v1 = s.create_milestone(&repo, "v1", "", None).await.unwrap();
1264- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")1272+ let v2 = s.create_milestone(&repo, "v2", "", None).await.unwrap();
1265- .await1273+ let foreign = s.create_milestone(&other, "v9", "", None).await.unwrap();
1266- .unwrap();
1267- let v1 = s
1268- .create_milestone(&fx.repo.id, "v1", "", None)
1269- .await
1270- .unwrap();
1271- let v2 = s
1272- .create_milestone(&fx.repo.id, "v2", "", None)
1273- .await
1274- .unwrap();
1275- let foreign = s
1276- .create_milestone(&fx.other.id, "v9", "", None)
1277- .await
1278- .unwrap();
1279 1274
1280- assert!(matches!(1275+ assert!(
1281- s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&foreign.id))1276+ matches!(
1282- .await,1277+ s.set_issue_milestone(&issue.id, owner, Some(&foreign.id))
1283- Err(Error::Invalid(_))1278+ .await,
1284- ));1279+ Err(Error::Invalid(_))
1280+ ),
1281+ "issue milestone: other repo refused"
1282+ );
1285 1283
1286- s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&v1.id))1284+ s.set_issue_milestone(&issue.id, owner, Some(&v1.id))
1287 .await1285 .await
1288 .unwrap();1286 .unwrap();
1289 let events = s.list_events(&issue.id).await.unwrap();1287 let events = s.list_events(&issue.id).await.unwrap();
1290- assert_eq!(data_of(&events, "milestoned"), [json!({"title": "v1"})]);1288+ assert_eq!(
1289+ data_of(&events, "milestoned"),
1290+ [json!({"title": "v1"})],
1291+ "issue milestone: set"
1292+ );
1291 1293
1292- s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&v2.id))1294+ s.set_issue_milestone(&issue.id, owner, Some(&v2.id))
1293 .await1295 .await
1294 .unwrap();1296 .unwrap();
1295 let events = s.list_events(&issue.id).await.unwrap();1297 let events = s.list_events(&issue.id).await.unwrap();
1296- assert_eq!(data_of(&events, "demilestoned"), [json!({"title": "v1"})]);1298+ assert_eq!(
1297- assert_eq!(data_of(&events, "milestoned").len(), 2);1299+ data_of(&events, "demilestoned"),
1300+ [json!({"title": "v1"})],
1301+ "issue milestone: old one removed"
1302+ );
1303+ assert_eq!(
1304+ data_of(&events, "milestoned").len(),
1305+ 2,
1306+ "issue milestone: new one set"
1307+ );
1298 1308
1299- s.set_issue_milestone(&issue.id, &fx.tahinli.id, None)1309+ s.set_issue_milestone(&issue.id, owner, None).await.unwrap();
1300- .await
1301- .unwrap();
1302 let events = s.list_events(&issue.id).await.unwrap();1310 let events = s.list_events(&issue.id).await.unwrap();
1303- assert_eq!(data_of(&events, "demilestoned").len(), 2);1311+ assert_eq!(
1304- assert!(data_of(&events, "demilestoned").contains(&json!({"title": "v2"})));1312+ data_of(&events, "demilestoned").len(),
1313+ 2,
1314+ "issue milestone: cleared"
1315+ );
1316+ assert!(
1317+ data_of(&events, "demilestoned").contains(&json!({"title": "v2"})),
1318+ "issue milestone: v2 recorded"
1319+ );
1305 1320
1306 // Clearing again is a no-op.1321 // Clearing again is a no-op.
1307- s.set_issue_milestone(&issue.id, &fx.tahinli.id, None)1322+ s.set_issue_milestone(&issue.id, owner, None).await.unwrap();
1308- .await
1309- .unwrap();
1310 let events = s.list_events(&issue.id).await.unwrap();1323 let events = s.list_events(&issue.id).await.unwrap();
1311- assert_eq!(data_of(&events, "demilestoned").len(), 2);1324+ assert_eq!(
1325+ data_of(&events, "demilestoned").len(),
1326+ 2,
1327+ "issue milestone: clearing twice records nothing"
1328+ );
1312 let stored = s1329 let stored = s
1313- .issue_by_number(&fx.repo.id, issue.number)1330+ .issue_by_number(&repo, issue.number)
1314 .await1331 .await
1315 .unwrap()1332 .unwrap()
1316 .unwrap();1333 .unwrap();
1317- assert!(stored.milestone_id.is_none());1334+ assert!(
1318- }1335+ stored.milestone_id.is_none(),
1336+ "issue milestone: none stored"
1337+ );
1319 1338
1320- #[tokio::test]1339+ // Scenario 4: deleting a milestone clears issue links and is scoped.
1321- async fn delete_milestone_clears_issue_links_and_is_scoped() {1340+ let repo = fresh_repo(s, owner, "milestone-delete").await;
1322- let fx = fixture().await;1341+ let other = fresh_repo(s, owner, "milestone-delete-other").await;
1323- let s = &fx.store;1342+ let v1 = s.create_milestone(&repo, "v1", "", None).await.unwrap();
1324- let v1 = s1343+ let issue = s.create_issue(&repo, owner, "t", "").await.unwrap();
1325- .create_milestone(&fx.repo.id, "v1", "", None)1344+ s.set_issue_milestone(&issue.id, owner, Some(&v1.id))
1326- .await
1327- .unwrap();
1328- let issue = s
1329- .create_issue(&fx.repo.id, &fx.tahinli.id, "t", "")
1330- .await
1331- .unwrap();
1332- s.set_issue_milestone(&issue.id, &fx.tahinli.id, Some(&v1.id))
1333 .await1345 .await
1334 .unwrap();1346 .unwrap();
1335 1347
1336- assert!(matches!(1348+ assert!(
1337- s.delete_milestone(&fx.other.id, &v1.id).await,1349+ matches!(
1338- Err(Error::NotFound)1350+ s.delete_milestone(&other, &v1.id).await,
1339- ));1351+ Err(Error::NotFound)
1352+ ),
1353+ "delete: other repo refused"
1354+ );
1340 let kept = s1355 let kept = s
1341- .issue_by_number(&fx.repo.id, issue.number)1356+ .issue_by_number(&repo, issue.number)
1342 .await1357 .await
1343 .unwrap()1358 .unwrap()
1344 .unwrap();1359 .unwrap();
1345- assert_eq!(kept.milestone_id.as_deref(), Some(v1.id.as_str()));1360+ assert_eq!(
1361+ kept.milestone_id.as_deref(),
1362+ Some(v1.id.as_str()),
1363+ "delete: link kept after refused delete"
1364+ );
1346 1365
1347- s.delete_milestone(&fx.repo.id, &v1.id).await.unwrap();1366+ s.delete_milestone(&repo, &v1.id).await.unwrap();
1348 assert!(1367 assert!(
1349- s.milestone_by_id(&fx.repo.id, &v1.id)1368+ s.milestone_by_id(&repo, &v1.id).await.unwrap().is_none(),
1350- .await1369+ "delete: milestone gone"
1351- .unwrap()
1352- .is_none()
1353 );1370 );
1354 let after = s1371 let after = s
1355- .issue_by_number(&fx.repo.id, issue.number)1372+ .issue_by_number(&repo, issue.number)
1356 .await1373 .await
1357 .unwrap()1374 .unwrap()
1358 .unwrap();1375 .unwrap();
1359- assert!(after.milestone_id.is_none());1376+ assert!(after.milestone_id.is_none(), "delete: issue link cleared");
1360 }1377 }
1361 }1378 }
crates/forge-core/src/store/notifications.rsmodified+259 -207
@@ -454,108 +454,140 @@ mod tests {
454 .reason454 .reason
455 }455 }
456 456
457+ /// A user with a unique name, so per-user inbox queries stay exact in a shared store.
458+ async fn user(store: &Store, name: &str) -> String {
459+ store
460+ .create_user(name, None, "hash", false)
461+ .await
462+ .unwrap()
463+ .id
464+ }
465+
466+ // Scenarios share one store. Each one uses its own issue, repo or fresh users
467+ // wherever the assertions read repo-wide or per-user state.
457 #[tokio::test]468 #[tokio::test]
458- async fn watch_level_defaults_to_participating_and_watchers_lists_only_watching() {469+ async fn watch_and_subscription_rules_hold_in_each_scenario() {
459 let fx = fixture().await;470 let fx = fixture().await;
460 let s = &fx.store;471 let s = &fx.store;
472+
473+ // Scenario 1: watch levels default to participating; watchers lists only watchers.
474+ let repo = s
475+ .create_repo(&fx.owner, "watch", "", Visibility::Public, "main")
476+ .await
477+ .unwrap()
478+ .id;
461 assert_eq!(479 assert_eq!(
462- s.watch_level(&fx.tahinli, &fx.repo).await.unwrap(),480+ s.watch_level(&fx.tahinli, &repo).await.unwrap(),
463- WatchLevel::Participating481+ WatchLevel::Participating,
482+ "watch: default level"
464 );483 );
465- s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Watching)484+ s.set_watch(&fx.tahinli, &repo, WatchLevel::Watching)
466 .await485 .await
467 .unwrap();486 .unwrap();
468- s.set_watch(&fx.bob, &fx.repo, WatchLevel::Ignoring)487+ s.set_watch(&fx.bob, &repo, WatchLevel::Ignoring)
469 .await488 .await
470 .unwrap();489 .unwrap();
471 assert_eq!(490 assert_eq!(
472- s.watch_level(&fx.tahinli, &fx.repo).await.unwrap(),491+ s.watch_level(&fx.tahinli, &repo).await.unwrap(),
473- WatchLevel::Watching492+ WatchLevel::Watching,
493+ "watch: set level is read back"
474 );494 );
475- s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Participating)495+ s.set_watch(&fx.tahinli, &repo, WatchLevel::Participating)
476 .await496 .await
477 .unwrap();497 .unwrap();
478- s.set_watch(&fx.bob, &fx.repo, WatchLevel::Watching)498+ s.set_watch(&fx.bob, &repo, WatchLevel::Watching)
479 .await499 .await
480 .unwrap();500 .unwrap();
481- s.set_watch(&fx.owner, &fx.repo, WatchLevel::Watching)501+ s.set_watch(&fx.owner, &repo, WatchLevel::Watching)
482 .await502 .await
483 .unwrap();503 .unwrap();
484- let watchers = s.watchers(&fx.repo).await.unwrap();504+ let watchers = s.watchers(&repo).await.unwrap();
485 let mut expected = vec![fx.bob.clone(), fx.owner.clone()];505 let mut expected = vec![fx.bob.clone(), fx.owner.clone()];
486 expected.sort();506 expected.sort();
487- assert_eq!(watchers, expected);507+ assert_eq!(watchers, expected, "watch: only watching users listed");
488- }
489 508
490- #[tokio::test]509+ // Scenario 2: auto-subscribe never overrides an ignore; explicit subscribe does.
491- async fn auto_subscribe_never_overrides_ignored_or_existing() {510+ let issue = s
492- let fx = fixture().await;511+ .create_issue(&fx.repo, &fx.owner, "Third", "")
493- let s = &fx.store;512+ .await
494- s.auto_subscribe(&fx.tahinli, &fx.issue_a, Reason::Comment)513+ .unwrap()
514+ .id;
515+ s.auto_subscribe(&fx.tahinli, &issue, Reason::Comment)
495 .await516 .await
496 .unwrap();517 .unwrap();
497 assert_eq!(518 assert_eq!(
498- s.subscription(&fx.tahinli, &fx.issue_a).await.unwrap(),519+ s.subscription(&fx.tahinli, &issue).await.unwrap(),
499- Some((SubState::Subscribed, Reason::Comment))520+ Some((SubState::Subscribed, Reason::Comment)),
521+ "auto: first subscription"
500 );522 );
501 // Existing row stays as it was.523 // Existing row stays as it was.
502- s.auto_subscribe(&fx.tahinli, &fx.issue_a, Reason::Mention)524+ s.auto_subscribe(&fx.tahinli, &issue, Reason::Mention)
503 .await525 .await
504 .unwrap();526 .unwrap();
505 assert_eq!(527 assert_eq!(
506- s.subscription(&fx.tahinli, &fx.issue_a).await.unwrap(),528+ s.subscription(&fx.tahinli, &issue).await.unwrap(),
507- Some((SubState::Subscribed, Reason::Comment))529+ Some((SubState::Subscribed, Reason::Comment)),
530+ "auto: existing row kept"
508 );531 );
509 // An explicit ignore is never replaced by auto-subscribe.532 // An explicit ignore is never replaced by auto-subscribe.
510- s.subscribe(&fx.bob, &fx.issue_a, SubState::Ignored, Reason::Subscribed)533+ s.subscribe(&fx.bob, &issue, SubState::Ignored, Reason::Subscribed)
511 .await534 .await
512 .unwrap();535 .unwrap();
513- s.auto_subscribe(&fx.bob, &fx.issue_a, Reason::Author)536+ s.auto_subscribe(&fx.bob, &issue, Reason::Author)
514 .await537 .await
515 .unwrap();538 .unwrap();
516 assert_eq!(539 assert_eq!(
517- s.subscription(&fx.bob, &fx.issue_a).await.unwrap(),540+ s.subscription(&fx.bob, &issue).await.unwrap(),
518- Some((SubState::Ignored, Reason::Subscribed))541+ Some((SubState::Ignored, Reason::Subscribed)),
542+ "auto: ignore kept"
519 );543 );
520 // Explicit subscribe does replace.544 // Explicit subscribe does replace.
521- s.subscribe(&fx.bob, &fx.issue_a, SubState::Subscribed, Reason::Author)545+ s.subscribe(&fx.bob, &issue, SubState::Subscribed, Reason::Author)
522 .await546 .await
523 .unwrap();547 .unwrap();
524 assert_eq!(548 assert_eq!(
525- s.subscription(&fx.bob, &fx.issue_a).await.unwrap(),549+ s.subscription(&fx.bob, &issue).await.unwrap(),
526- Some((SubState::Subscribed, Reason::Author))550+ Some((SubState::Subscribed, Reason::Author)),
551+ "auto: explicit subscribe replaces"
552+ );
553+ assert_eq!(
554+ s.subscription(&fx.owner, &issue).await.unwrap(),
555+ None,
556+ "auto: untouched user has no row"
527 );557 );
528- assert_eq!(s.subscription(&fx.owner, &fx.issue_a).await.unwrap(), None);
529- }
530 558
531- #[tokio::test]559+ // Scenario 3: subscribers lists only subscribed users of one issue.
532- async fn subscribers_lists_only_subscribed() {560+ let issue = s
533- let fx = fixture().await;561+ .create_issue(&fx.repo, &fx.owner, "Fourth", "")
534- let s = &fx.store;562+ .await
535- s.subscribe(563+ .unwrap()
536- &fx.tahinli,564+ .id;
537- &fx.issue_a,565+ s.subscribe(&fx.tahinli, &issue, SubState::Subscribed, Reason::Assign)
538- SubState::Subscribed,566+ .await
539- Reason::Assign,567+ .unwrap();
540- )568+ s.subscribe(&fx.bob, &issue, SubState::Ignored, Reason::Subscribed)
541- .await
542- .unwrap();
543- s.subscribe(&fx.bob, &fx.issue_a, SubState::Ignored, Reason::Subscribed)
544 .await569 .await
545 .unwrap();570 .unwrap();
546 s.subscribe(&fx.owner, &fx.issue_b, SubState::Subscribed, Reason::Author)571 s.subscribe(&fx.owner, &fx.issue_b, SubState::Subscribed, Reason::Author)
547 .await572 .await
548 .unwrap();573 .unwrap();
549- let subs = s.subscribers(&fx.issue_a).await.unwrap();574+ let subs = s.subscribers(&issue).await.unwrap();
550- assert_eq!(subs, vec![(fx.tahinli.clone(), Reason::Assign)]);575+ assert_eq!(
576+ subs,
577+ vec![(fx.tahinli.clone(), Reason::Assign)],
578+ "subscribers: only subscribed users"
579+ );
551 }580 }
552 581
553 #[tokio::test]582 #[tokio::test]
554- async fn upsert_keeps_stronger_reason_while_unread_and_replaces_after_read() {583+ async fn inbox_rules_hold_in_each_scenario() {
555 let fx = fixture().await;584 let fx = fixture().await;
556 let s = &fx.store;585 let s = &fx.store;
586+
587+ // Scenario 1: a stronger reason wins while unread; a read row is replaced.
588+ let ivy = user(s, "Ivy").await;
557 s.upsert_notification(589 s.upsert_notification(
558- &fx.tahinli,590+ &ivy,
559 &fx.repo,591 &fx.repo,
560 &fx.issue_a,592 &fx.issue_a,
561 Reason::Mention,593 Reason::Mention,
@@ -565,7 +597,7 @@ mod tests {
565 .await597 .await
566 .unwrap();598 .unwrap();
567 s.upsert_notification(599 s.upsert_notification(
568- &fx.tahinli,600+ &ivy,
569 &fx.repo,601 &fx.repo,
570 &fx.issue_a,602 &fx.issue_a,
571 Reason::Comment,603 Reason::Comment,
@@ -575,15 +607,20 @@ mod tests {
575 .await607 .await
576 .unwrap();608 .unwrap();
577 assert_eq!(609 assert_eq!(
578- reason_of(s, &fx.tahinli, &fx.issue_a).await,610+ reason_of(s, &ivy, &fx.issue_a).await,
579- Reason::Mention611+ Reason::Mention,
612+ "upsert: stronger reason kept"
580 );613 );
581- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1);614+ assert_eq!(s.unread_count(&ivy).await.unwrap(), 1, "upsert: one unread");
582 615
583- s.mark_issue_read(&fx.tahinli, &fx.issue_a).await.unwrap();616+ s.mark_issue_read(&ivy, &fx.issue_a).await.unwrap();
584- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0);617+ assert_eq!(
618+ s.unread_count(&ivy).await.unwrap(),
619+ 0,
620+ "upsert: marked read"
621+ );
585 s.upsert_notification(622 s.upsert_notification(
586- &fx.tahinli,623+ &ivy,
587 &fx.repo,624 &fx.repo,
588 &fx.issue_a,625 &fx.issue_a,
589 Reason::Comment,626 Reason::Comment,
@@ -593,14 +630,19 @@ mod tests {
593 .await630 .await
594 .unwrap();631 .unwrap();
595 assert_eq!(632 assert_eq!(
596- reason_of(s, &fx.tahinli, &fx.issue_a).await,633+ reason_of(s, &ivy, &fx.issue_a).await,
597- Reason::Comment634+ Reason::Comment,
635+ "upsert: read row replaced"
636+ );
637+ assert_eq!(
638+ s.unread_count(&ivy).await.unwrap(),
639+ 1,
640+ "upsert: unread again"
598 );641 );
599- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1);
600 642
601 // A stronger reason while unread still wins.643 // A stronger reason while unread still wins.
602 s.upsert_notification(644 s.upsert_notification(
603- &fx.tahinli,645+ &ivy,
604 &fx.repo,646 &fx.repo,
605 &fx.issue_a,647 &fx.issue_a,
606 Reason::Assign,648 Reason::Assign,
@@ -609,22 +651,20 @@ mod tests {
609 )651 )
610 .await652 .await
611 .unwrap();653 .unwrap();
612- let inbox = s654+ let inbox = s.list_notifications(&ivy, false, None, 10).await.unwrap();
613- .list_notifications(&fx.tahinli, false, None, 10)655+ assert_eq!(inbox.len(), 1, "upsert: still one row");
614- .await656+ assert_eq!(
615- .unwrap();657+ inbox[0].reason,
616- assert_eq!(inbox.len(), 1);658+ Reason::Assign,
617- assert_eq!(inbox[0].reason, Reason::Assign);659+ "upsert: assign wins while unread"
618- assert_eq!(inbox[0].last_activity, "assigned");660+ );
619- assert_eq!(inbox[0].last_actor_name, None);661+ assert_eq!(inbox[0].last_activity, "assigned", "upsert: activity text");
620- }662+ assert_eq!(inbox[0].last_actor_name, None, "upsert: no actor given");
621 663
622- #[tokio::test]664+ // Scenario 2: listing is newest first, paged by cursor, and per user.
623- async fn list_orders_newest_first_and_pages_with_cursor() {665+ let kim = user(s, "Kim").await;
624- let fx = fixture().await;
625- let s = &fx.store;
626 s.upsert_notification(666 s.upsert_notification(
627- &fx.tahinli,667+ &kim,
628 &fx.repo,668 &fx.repo,
629 &fx.issue_a,669 &fx.issue_a,
630 Reason::Comment,670 Reason::Comment,
@@ -634,7 +674,7 @@ mod tests {
634 .await674 .await
635 .unwrap();675 .unwrap();
636 s.upsert_notification(676 s.upsert_notification(
637- &fx.tahinli,677+ &kim,
638 &fx.repo,678 &fx.repo,
639 &fx.issue_b,679 &fx.issue_b,
640 Reason::Comment,680 Reason::Comment,
@@ -643,62 +683,56 @@ mod tests {
643 )683 )
644 .await684 .await
645 .unwrap();685 .unwrap();
646- s.mark_issue_read(&fx.tahinli, &fx.issue_b).await.unwrap();686+ s.mark_issue_read(&kim, &fx.issue_b).await.unwrap();
687+
688+ let all = s.list_notifications(&kim, false, None, 50).await.unwrap();
689+ assert_eq!(all.len(), 2, "list: two rows");
690+ assert_eq!(all[0].issue_id, fx.issue_b, "list: newest first");
691+ assert_eq!(all[1].issue_id, fx.issue_a, "list: oldest last");
692+ assert_eq!(all[0].repo_owner, "Owen", "list: repo owner");
693+ assert_eq!(all[0].repo_name, "proj", "list: repo name");
694+ assert_eq!(all[0].number, 2, "list: issue number");
695+ assert_eq!(all[0].issue_state, "open", "list: issue state");
696+ assert!(!all[0].is_pull, "list: issue, not a pull request");
697+ assert_eq!(
698+ all[0].last_actor_name.as_deref(),
699+ Some("Bob"),
700+ "list: last actor"
701+ );
647 702
648- let all = s703+ let unread = s.list_notifications(&kim, true, None, 50).await.unwrap();
649- .list_notifications(&fx.tahinli, false, None, 50)704+ assert_eq!(unread.len(), 1, "list: unread filter");
650- .await705+ assert_eq!(unread[0].issue_id, fx.issue_a, "list: unread row");
651- .unwrap();
652- assert_eq!(all.len(), 2);
653- assert_eq!(all[0].issue_id, fx.issue_b);
654- assert_eq!(all[1].issue_id, fx.issue_a);
655- assert_eq!(all[0].repo_owner, "Owen");
656- assert_eq!(all[0].repo_name, "proj");
657- assert_eq!(all[0].number, 2);
658- assert_eq!(all[0].issue_state, "open");
659- assert!(!all[0].is_pull);
660- assert_eq!(all[0].last_actor_name.as_deref(), Some("Bob"));
661-
662- let unread = s
663- .list_notifications(&fx.tahinli, true, None, 50)
664- .await
665- .unwrap();
666- assert_eq!(unread.len(), 1);
667- assert_eq!(unread[0].issue_id, fx.issue_a);
668 706
669 // Cursor: first page of one, then the rest after its stamp.707 // Cursor: first page of one, then the rest after its stamp.
670- let first = s708+ let first = s.list_notifications(&kim, false, None, 1).await.unwrap();
671- .list_notifications(&fx.tahinli, false, None, 1)709+ assert_eq!(first[0].issue_id, fx.issue_b, "cursor: first page");
672- .await
673- .unwrap();
674- assert_eq!(first[0].issue_id, fx.issue_b);
675 let second = s710 let second = s
676- .list_notifications(&fx.tahinli, false, Some(&first[0].updated_at), 1)711+ .list_notifications(&kim, false, Some(&first[0].updated_at), 1)
677 .await712 .await
678 .unwrap();713 .unwrap();
679- assert_eq!(second.len(), 1);714+ assert_eq!(second.len(), 1, "cursor: second page size");
680- assert_eq!(second[0].issue_id, fx.issue_a);715+ assert_eq!(second[0].issue_id, fx.issue_a, "cursor: second page row");
681 let none = s716 let none = s
682- .list_notifications(&fx.tahinli, false, Some(&second[0].updated_at), 1)717+ .list_notifications(&kim, false, Some(&second[0].updated_at), 1)
683 .await718 .await
684 .unwrap();719 .unwrap();
685- assert!(none.is_empty());720+ assert!(none.is_empty(), "cursor: past the end");
686 721
687 // Other users see nothing.722 // Other users see nothing.
688 assert!(723 assert!(
689 s.list_notifications(&fx.bob, false, None, 50)724 s.list_notifications(&fx.bob, false, None, 50)
690 .await725 .await
691 .unwrap()726 .unwrap()
692- .is_empty()727+ .is_empty(),
728+ "list: other user sees nothing"
693 );729 );
694- }
695 730
696- #[tokio::test]731+ // Scenario 3: mark-read by id and by repo only touches the right rows.
697- async fn unread_count_and_mark_read_variants() {732+ let ned = user(s, "Ned").await;
698- let fx = fixture().await;733+ let olive = user(s, "Olive").await;
699- let s = &fx.store;
700 s.upsert_notification(734 s.upsert_notification(
701- &fx.tahinli,735+ &ned,
702 &fx.repo,736 &fx.repo,
703 &fx.issue_a,737 &fx.issue_a,
704 Reason::Comment,738 Reason::Comment,
@@ -708,7 +742,7 @@ mod tests {
708 .await742 .await
709 .unwrap();743 .unwrap();
710 s.upsert_notification(744 s.upsert_notification(
711- &fx.tahinli,745+ &ned,
712 &fx.repo,746 &fx.repo,
713 &fx.issue_b,747 &fx.issue_b,
714 Reason::Comment,748 Reason::Comment,
@@ -718,7 +752,7 @@ mod tests {
718 .await752 .await
719 .unwrap();753 .unwrap();
720 s.upsert_notification(754 s.upsert_notification(
721- &fx.bob,755+ &olive,
722 &fx.repo,756 &fx.repo,
723 &fx.issue_a,757 &fx.issue_a,
724 Reason::Comment,758 Reason::Comment,
@@ -727,27 +761,25 @@ mod tests {
727 )761 )
728 .await762 .await
729 .unwrap();763 .unwrap();
730- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 2);764+ assert_eq!(s.unread_count(&ned).await.unwrap(), 2, "read: two unread");
731 765
732 // mark_read by id, and another user's id does nothing.766 // mark_read by id, and another user's id does nothing.
733- let inbox = s767+ let inbox = s.list_notifications(&ned, false, None, 50).await.unwrap();
734- .list_notifications(&fx.tahinli, false, None, 50)768+ let olive_inbox = s.list_notifications(&olive, false, None, 50).await.unwrap();
735- .await769+ s.mark_read(&ned, &olive_inbox[0].id).await.unwrap();
736- .unwrap();770+ assert_eq!(
737- let bob_inbox = s771+ s.unread_count(&olive).await.unwrap(),
738- .list_notifications(&fx.bob, false, None, 50)772+ 1,
739- .await773+ "read: foreign id ignored"
740- .unwrap();774+ );
741- s.mark_read(&fx.tahinli, &bob_inbox[0].id).await.unwrap();775+ s.mark_read(&ned, &inbox[0].id).await.unwrap();
742- assert_eq!(s.unread_count(&fx.bob).await.unwrap(), 1);776+ assert_eq!(s.unread_count(&ned).await.unwrap(), 1, "read: first by id");
743- s.mark_read(&fx.tahinli, &inbox[0].id).await.unwrap();777+ s.mark_read(&ned, &inbox[1].id).await.unwrap();
744- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1);778+ assert_eq!(s.unread_count(&ned).await.unwrap(), 0, "read: second by id");
745- s.mark_read(&fx.tahinli, &inbox[1].id).await.unwrap();
746- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0);
747 779
748 // mark_all_read per repo leaves other users alone.780 // mark_all_read per repo leaves other users alone.
749 s.upsert_notification(781 s.upsert_notification(
750- &fx.tahinli,782+ &ned,
751 &fx.repo,783 &fx.repo,
752 &fx.issue_a,784 &fx.issue_a,
753 Reason::Comment,785 Reason::Comment,
@@ -756,12 +788,16 @@ mod tests {
756 )788 )
757 .await789 .await
758 .unwrap();790 .unwrap();
759- s.mark_all_read(&fx.tahinli, Some(&fx.repo)).await.unwrap();791+ s.mark_all_read(&ned, Some(&fx.repo)).await.unwrap();
760- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0);792+ assert_eq!(s.unread_count(&ned).await.unwrap(), 0, "read: all in repo");
761- assert_eq!(s.unread_count(&fx.bob).await.unwrap(), 1);793+ assert_eq!(
794+ s.unread_count(&olive).await.unwrap(),
795+ 1,
796+ "read: other user kept"
797+ );
762 798
763 s.upsert_notification(799 s.upsert_notification(
764- &fx.tahinli,800+ &ned,
765 &fx.repo,801 &fx.repo,
766 &fx.issue_b,802 &fx.issue_b,
767 Reason::Comment,803 Reason::Comment,
@@ -770,20 +806,19 @@ mod tests {
770 )806 )
771 .await807 .await
772 .unwrap();808 .unwrap();
773- s.mark_all_read(&fx.tahinli, None).await.unwrap();809+ s.mark_all_read(&ned, None).await.unwrap();
774- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0);810+ assert_eq!(s.unread_count(&ned).await.unwrap(), 0, "read: all, no repo");
775- s.mark_all_read(&fx.bob, Some("no-such-repo"))811+ s.mark_all_read(&olive, Some("no-such-repo")).await.unwrap();
776- .await812+ assert_eq!(
777- .unwrap();813+ s.unread_count(&olive).await.unwrap(),
778- assert_eq!(s.unread_count(&fx.bob).await.unwrap(), 1);814+ 1,
779- }815+ "read: unknown repo"
816+ );
780 817
781- #[tokio::test]818+ // Scenario 4: mark_issue_read only touches that issue.
782- async fn mark_issue_read_only_touches_that_issue() {819+ let max = user(s, "Max").await;
783- let fx = fixture().await;
784- let s = &fx.store;
785 s.upsert_notification(820 s.upsert_notification(
786- &fx.tahinli,821+ &max,
787 &fx.repo,822 &fx.repo,
788 &fx.issue_a,823 &fx.issue_a,
789 Reason::Comment,824 Reason::Comment,
@@ -793,7 +828,7 @@ mod tests {
793 .await828 .await
794 .unwrap();829 .unwrap();
795 s.upsert_notification(830 s.upsert_notification(
796- &fx.tahinli,831+ &max,
797 &fx.repo,832 &fx.repo,
798 &fx.issue_b,833 &fx.issue_b,
799 Reason::Comment,834 Reason::Comment,
@@ -802,34 +837,47 @@ mod tests {
802 )837 )
803 .await838 .await
804 .unwrap();839 .unwrap();
805- s.mark_issue_read(&fx.tahinli, &fx.issue_a).await.unwrap();840+ s.mark_issue_read(&max, &fx.issue_a).await.unwrap();
806- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 1);841+ assert_eq!(
807- let unread = s842+ s.unread_count(&max).await.unwrap(),
808- .list_notifications(&fx.tahinli, true, None, 50)843+ 1,
809- .await844+ "issue read: one left"
810- .unwrap();845+ );
811- assert_eq!(unread[0].issue_id, fx.issue_b);846+ let unread = s.list_notifications(&max, true, None, 50).await.unwrap();
847+ assert_eq!(
848+ unread[0].issue_id, fx.issue_b,
849+ "issue read: the other issue"
850+ );
812 }851 }
813 852
853+ // Deletions come last in their own store scope: each removes state other scenarios need.
814 #[tokio::test]854 #[tokio::test]
815- async fn delete_repo_removes_notification_rows() {855+ async fn delete_rules_hold_in_each_scenario() {
816 let fx = fixture().await;856 let fx = fixture().await;
817 let s = &fx.store;857 let s = &fx.store;
818- s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Watching)858+
859+ // Scenario 1: deleting a repo removes its notification rows, subs and watches.
860+ let pat = user(s, "Pat").await;
861+ let doomed = s
862+ .create_repo(&fx.owner, "doomed", "", Visibility::Public, "main")
863+ .await
864+ .unwrap()
865+ .id;
866+ let doomed_issue = s
867+ .create_issue(&doomed, &fx.owner, "Doomed", "")
868+ .await
869+ .unwrap()
870+ .id;
871+ s.set_watch(&pat, &doomed, WatchLevel::Watching)
872+ .await
873+ .unwrap();
874+ s.subscribe(&pat, &doomed_issue, SubState::Subscribed, Reason::Author)
819 .await875 .await
820 .unwrap();876 .unwrap();
821- s.subscribe(
822- &fx.tahinli,
823- &fx.issue_a,
824- SubState::Subscribed,
825- Reason::Author,
826- )
827- .await
828- .unwrap();
829 s.upsert_notification(877 s.upsert_notification(
830- &fx.tahinli,878+ &pat,
831- &fx.repo,879+ &doomed,
832- &fx.issue_a,880+ &doomed_issue,
833 Reason::Comment,881 Reason::Comment,
834 "comment",882 "comment",
835 Some(&fx.bob),883 Some(&fx.bob),
@@ -837,66 +885,66 @@ mod tests {
837 .await885 .await
838 .unwrap();886 .unwrap();
839 887
840- s.delete_repo(&fx.repo).await.unwrap();888+ s.delete_repo(&doomed).await.unwrap();
841 assert_eq!(889 assert_eq!(
842 count(890 count(
843 s,891 s,
844 "SELECT COUNT(*) FROM notifications WHERE user_id = ?1",892 "SELECT COUNT(*) FROM notifications WHERE user_id = ?1",
845- &fx.tahinli893+ &pat
846 )894 )
847 .await,895 .await,
848- 0896+ 0,
897+ "repo: notifications removed"
849 );898 );
850 assert_eq!(899 assert_eq!(
851 count(900 count(
852 s,901 s,
853 "SELECT COUNT(*) FROM issue_subscriptions WHERE user_id = ?1",902 "SELECT COUNT(*) FROM issue_subscriptions WHERE user_id = ?1",
854- &fx.tahinli903+ &pat
855 )904 )
856 .await,905 .await,
857- 0906+ 0,
907+ "repo: subscriptions removed"
858 );908 );
859 assert_eq!(909 assert_eq!(
860 count(910 count(
861 s,911 s,
862 "SELECT COUNT(*) FROM repo_watches WHERE user_id = ?1",912 "SELECT COUNT(*) FROM repo_watches WHERE user_id = ?1",
863- &fx.tahinli913+ &pat
864 )914 )
865 .await,915 .await,
866- 0916+ 0,
917+ "repo: watches removed"
918+ );
919+ assert_eq!(
920+ s.unread_count(&pat).await.unwrap(),
921+ 0,
922+ "repo: nothing unread"
867 );923 );
868- assert_eq!(s.unread_count(&fx.tahinli).await.unwrap(), 0);
869- }
870 924
871- #[tokio::test]925+ // Scenario 2: deleting a user removes their rows and reassigns last actor to ghost.
872- async fn delete_user_removes_own_rows_and_reassigns_last_actor_to_ghost() {
873- let fx = fixture().await;
874- let s = &fx.store;
875 let ghost = s.ensure_ghost_user().await.unwrap();926 let ghost = s.ensure_ghost_user().await.unwrap();
876- s.set_watch(&fx.tahinli, &fx.repo, WatchLevel::Watching)927+ let quin = user(s, "Quin").await;
928+ let ray = user(s, "Ray").await;
929+ s.set_watch(&quin, &fx.repo, WatchLevel::Watching)
877 .await930 .await
878 .unwrap();931 .unwrap();
879- s.subscribe(932+ s.subscribe(&quin, &fx.issue_a, SubState::Subscribed, Reason::Author)
880- &fx.tahinli,933+ .await
881- &fx.issue_a,934+ .unwrap();
882- SubState::Subscribed,935+ // Ray's inbox row whose last actor is Quin, and Quin's own inbox row.
883- Reason::Author,
884- )
885- .await
886- .unwrap();
887- // Bob's inbox row whose last actor is Tahinli, and Tahinli's own inbox row.
888 s.upsert_notification(936 s.upsert_notification(
889- &fx.bob,937+ &ray,
890 &fx.repo,938 &fx.repo,
891 &fx.issue_a,939 &fx.issue_a,
892 Reason::Comment,940 Reason::Comment,
893 "comment",941 "comment",
894- Some(&fx.tahinli),942+ Some(&quin),
895 )943 )
896 .await944 .await
897 .unwrap();945 .unwrap();
898 s.upsert_notification(946 s.upsert_notification(
899- &fx.tahinli,947+ &quin,
900 &fx.repo,948 &fx.repo,
901 &fx.issue_b,949 &fx.issue_b,
902 Reason::Comment,950 Reason::Comment,
@@ -906,40 +954,44 @@ mod tests {
906 .await954 .await
907 .unwrap();955 .unwrap();
908 956
909- s.delete_user(&fx.tahinli, &ghost.id).await.unwrap();957+ s.delete_user(&quin, &ghost.id).await.unwrap();
910 958
911 assert_eq!(959 assert_eq!(
912 count(960 count(
913 s,961 s,
914 "SELECT COUNT(*) FROM notifications WHERE user_id = ?1",962 "SELECT COUNT(*) FROM notifications WHERE user_id = ?1",
915- &fx.tahinli963+ &quin
916 )964 )
917 .await,965 .await,
918- 0966+ 0,
967+ "user: notifications removed"
919 );968 );
920 assert_eq!(969 assert_eq!(
921 count(970 count(
922 s,971 s,
923 "SELECT COUNT(*) FROM issue_subscriptions WHERE user_id = ?1",972 "SELECT COUNT(*) FROM issue_subscriptions WHERE user_id = ?1",
924- &fx.tahinli973+ &quin
925 )974 )
926 .await,975 .await,
927- 0976+ 0,
977+ "user: subscriptions removed"
928 );978 );
929 assert_eq!(979 assert_eq!(
930 count(980 count(
931 s,981 s,
932 "SELECT COUNT(*) FROM repo_watches WHERE user_id = ?1",982 "SELECT COUNT(*) FROM repo_watches WHERE user_id = ?1",
933- &fx.tahinli983+ &quin
934 )984 )
935 .await,985 .await,
936- 0986+ 0,
987+ "user: watches removed"
988+ );
989+ let ray_inbox = s.list_notifications(&ray, false, None, 50).await.unwrap();
990+ assert_eq!(ray_inbox.len(), 1, "user: other user's row kept");
991+ assert_eq!(
992+ ray_inbox[0].last_actor_name.as_deref(),
993+ Some("ghost"),
994+ "user: last actor is ghost"
937 );995 );
938- let bob_inbox = s
939- .list_notifications(&fx.bob, false, None, 50)
940- .await
941- .unwrap();
942- assert_eq!(bob_inbox.len(), 1);
943- assert_eq!(bob_inbox[0].last_actor_name.as_deref(), Some("ghost"));
944 }996 }
945 }997 }
crates/forge-core/src/store/pinned.rsmodified+89 -38
@@ -130,9 +130,12 @@ mod tests {
130 (store, tahinli, repo.id, dir)130 (store, tahinli, repo.id, dir)
131 }131 }
132 132
133+ // One store, one scenario per repo so issue numbers and pin slots never mix.
133 #[tokio::test]134 #[tokio::test]
134- async fn pins_keep_order_and_stop_at_three() {135+ async fn pin_rules_hold_in_each_scenario() {
135 let (store, tahinli, repo, _dir) = fixture().await;136 let (store, tahinli, repo, _dir) = fixture().await;
137+
138+ // Scenario 1: pins keep their order and a fourth pin is refused.
136 let mut ids = Vec::new();139 let mut ids = Vec::new();
137 for i in 0..4 {140 for i in 0..4 {
138 let issue = store141 let issue = store
@@ -141,13 +144,27 @@ mod tests {
141 .unwrap();144 .unwrap();
142 ids.push(issue.id);145 ids.push(issue.id);
143 }146 }
144- assert!(store.pin_issue(&repo, &ids[2]).await.unwrap());147+ assert!(
145- assert!(store.pin_issue(&repo, &ids[0]).await.unwrap());148+ store.pin_issue(&repo, &ids[2]).await.unwrap(),
146- assert!(store.pin_issue(&repo, &ids[3]).await.unwrap());149+ "order: pin 3"
150+ );
151+ assert!(
152+ store.pin_issue(&repo, &ids[0]).await.unwrap(),
153+ "order: pin 1"
154+ );
155+ assert!(
156+ store.pin_issue(&repo, &ids[3]).await.unwrap(),
157+ "order: pin 4"
158+ );
147 let full = store.pin_issue(&repo, &ids[1]).await;159 let full = store.pin_issue(&repo, &ids[1]).await;
148- assert!(matches!(full, Err(Error::Invalid(_))));160+ assert!(
149- assert!(!store.is_pinned(&repo, &ids[1]).await.unwrap());161+ matches!(full, Err(Error::Invalid(_))),
150-162+ "order: fourth pin refused"
163+ );
164+ assert!(
165+ !store.is_pinned(&repo, &ids[1]).await.unwrap(),
166+ "order: refused issue stays unpinned"
167+ );
151 let pinned: Vec<i64> = store168 let pinned: Vec<i64> = store
152 .pinned_issues(&repo)169 .pinned_issues(&repo)
153 .await170 .await
@@ -155,43 +172,71 @@ mod tests {
155 .iter()172 .iter()
156 .map(|i| i.number)173 .map(|i| i.number)
157 .collect();174 .collect();
158- assert_eq!(pinned, vec![3, 1, 4]);175+ assert_eq!(pinned, vec![3, 1, 4], "order: listing");
159- }
160 176
161- #[tokio::test]177+ // Scenario 2: pinning twice is idempotent and unpinning frees a slot.
162- async fn pinning_twice_is_idempotent_and_unpin_frees_a_slot() {178+ let repo2 = store
163- let (store, tahinli, repo, _dir) = fixture().await;179+ .create_repo(
164- let mut ids = Vec::new();180+ &tahinli.id,
181+ "forge-idempotent",
182+ "",
183+ Visibility::Public,
184+ "main",
185+ )
186+ .await
187+ .unwrap()
188+ .id;
189+ let mut ids2 = Vec::new();
165 for i in 0..4 {190 for i in 0..4 {
166 let issue = store191 let issue = store
167- .create_issue(&repo, &tahinli.id, &format!("issue {i}"), "")192+ .create_issue(&repo2, &tahinli.id, &format!("issue {i}"), "")
168 .await193 .await
169 .unwrap();194 .unwrap();
170- ids.push(issue.id);195+ ids2.push(issue.id);
171 }196 }
172- for id in &ids[..3] {197+ for id in &ids2[..3] {
173- store.pin_issue(&repo, id).await.unwrap();198+ store.pin_issue(&repo2, id).await.unwrap();
174 }199 }
175- assert!(!store.pin_issue(&repo, &ids[0]).await.unwrap());200+ assert!(
176- assert_eq!(store.pinned_issues(&repo).await.unwrap().len(), 3);201+ !store.pin_issue(&repo2, &ids2[0]).await.unwrap(),
177-202+ "idempotent: second pin is a no-op"
178- assert!(store.unpin_issue(&repo, &ids[1]).await.unwrap());203+ );
179- assert!(!store.unpin_issue(&repo, &ids[1]).await.unwrap());204+ assert_eq!(
180- assert!(!store.is_pinned(&repo, &ids[1]).await.unwrap());205+ store.pinned_issues(&repo2).await.unwrap().len(),
181- assert!(store.pin_issue(&repo, &ids[3]).await.unwrap());206+ 3,
207+ "idempotent: still three pins"
208+ );
209+ assert!(
210+ store.unpin_issue(&repo2, &ids2[1]).await.unwrap(),
211+ "unpin: first"
212+ );
213+ assert!(
214+ !store.unpin_issue(&repo2, &ids2[1]).await.unwrap(),
215+ "unpin: second is a no-op"
216+ );
217+ assert!(
218+ !store.is_pinned(&repo2, &ids2[1]).await.unwrap(),
219+ "unpin: gone"
220+ );
221+ assert!(
222+ store.pin_issue(&repo2, &ids2[3]).await.unwrap(),
223+ "unpin: slot reused"
224+ );
182 let numbers: Vec<i64> = store225 let numbers: Vec<i64> = store
183- .pinned_issues(&repo)226+ .pinned_issues(&repo2)
184 .await227 .await
185 .unwrap()228 .unwrap()
186 .iter()229 .iter()
187 .map(|i| i.number)230 .map(|i| i.number)
188 .collect();231 .collect();
189- assert_eq!(numbers, vec![1, 3, 4]);232+ assert_eq!(numbers, vec![1, 3, 4], "unpin: listing");
190- }
191 233
192- #[tokio::test]234+ // Scenario 3: pull requests and issues of another repo cannot be pinned.
193- async fn pull_requests_and_foreign_issues_cannot_be_pinned() {235+ let repo3 = store
194- let (store, tahinli, repo, _dir) = fixture().await;236+ .create_repo(&tahinli.id, "forge-foreign", "", Visibility::Public, "main")
237+ .await
238+ .unwrap()
239+ .id;
195 let other = store240 let other = store
196 .create_repo(&tahinli.id, "other", "", Visibility::Public, "main")241 .create_repo(&tahinli.id, "other", "", Visibility::Public, "main")
197 .await242 .await
@@ -200,14 +245,20 @@ mod tests {
200 .create_issue(&other.id, &tahinli.id, "elsewhere", "")245 .create_issue(&other.id, &tahinli.id, "elsewhere", "")
201 .await246 .await
202 .unwrap();247 .unwrap();
203- assert!(matches!(248+ assert!(
204- store.pin_issue(&repo, &foreign.id).await,249+ matches!(
205- Err(Error::NotFound)250+ store.pin_issue(&repo3, &foreign.id).await,
206- ));251+ Err(Error::NotFound)
207- assert!(matches!(252+ ),
208- store.pin_issue(&repo, "missing").await,253+ "foreign: issue of another repo"
209- Err(Error::NotFound)254+ );
210- ));255+ assert!(
256+ matches!(
257+ store.pin_issue(&repo3, "missing").await,
258+ Err(Error::NotFound)
259+ ),
260+ "foreign: unknown id"
261+ );
211 }262 }
212 263
213 #[tokio::test]264 #[tokio::test]