forge
Explore
Log in

Don't you dare go Hollow

Store tests share their store and merge into tables

Browse files at this commit

7a15066c93d6e3f6632a9146dc339159838db412Ahmet Kaan GümüşParent: 965436c
  • test
    • store tests share their store and merge into tables
Show full message
✅ test: store tests share their store and merge into tables

Test counts per file, before -> after (58 -> 33 in scope):
mirrors 9 -> 5, releases 6 -> 2, repo_listing 5 -> 1, profile_pins 5 -> 1,
advisories 5 -> 3, signatures 4 -> 3, backup_runs 4 -> 3, admin_alerts 2 -> 1,
lfs 2 -> 1, push_recent 2 -> 1, replication 2 -> 1, wiki 2 -> 1,
snippets 4 -> 4, migrate 4 -> 4, contributions 1 -> 1, health 1 -> 1.

Scenarios that ran on a fresh store now share one store, each on its own
repo or owner, with the case label on every assert. No test was deleted
for lack of logic. The persisted snippet visibility and signature status
literals now have explicit checks.
crates/forge-core/src/store/admin_alerts.rsmodified+11 -6
@@ -311,9 +311,7 @@ mod tests {
311 (store, dir)311 (store, dir)
312 }312 }
313 313
314- #[tokio::test]314+ async fn an_alert_is_raised_once_until_it_resolves(store: &Store) {
315- async fn an_alert_is_raised_once_until_it_resolves() {
316- let (store, _dir) = store().await;
317 assert!(315 assert!(
318 store316 store
319 .raise_alert("runner_offline:r1", "runner_offline", "ci-1", "")317 .raise_alert("runner_offline:r1", "runner_offline", "ci-1", "")
@@ -352,9 +350,7 @@ mod tests {
352 );350 );
353 }351 }
354 352
355- #[tokio::test]353+ async fn events_fire_once_and_are_never_active(store: &Store) {
356- async fn events_fire_once_and_are_never_active() {
357- let (store, _dir) = store().await;
358 assert!(354 assert!(
359 store355 store
360 .record_alert_event("ci_killed:run1", "ci_killed", "ci-1", "")356 .record_alert_event("ci_killed:run1", "ci_killed", "ci-1", "")
@@ -369,4 +365,13 @@ mod tests {
369 );365 );
370 assert!(store.active_alerts().await.unwrap().is_empty());366 assert!(store.active_alerts().await.unwrap().is_empty());
371 }367 }
368+
369+ // One store serves both. Events run first: they must not be active, and
370+ // the raised alert below would otherwise show up in that empty check.
371+ #[tokio::test]
372+ async fn events_and_raised_alerts_share_one_store() {
373+ let (store, _dir) = store().await;
374+ events_fire_once_and_are_never_active(&store).await;
375+ an_alert_is_raised_once_until_it_resolves(&store).await;
376+ }
372 }377 }
crates/forge-core/src/store/advisories.rsmodified+70 -55
@@ -575,10 +575,14 @@ mod tests {
575 (store, repo.id)575 (store, repo.id)
576 }576 }
577 577
578+ // Advisories are global, so each block replaces the advisory set it needs
579+ // before scanning. The per-repo alert checks use their own repo.
578 #[tokio::test]580 #[tokio::test]
579- async fn scan_opens_reconciles_and_keeps_dismissed() {581+ async fn scans_reconcile_alerts_and_replace_per_ecosystem() {
580 let dir = tempfile::tempdir().unwrap();582 let dir = tempfile::tempdir().unwrap();
581 let (store, repo) = store_with_repo(&dir).await;583 let (store, repo) = store_with_repo(&dir).await;
584+
585+ // A scan opens alerts, closes fixed ones, and keeps dismissed ones.
582 store586 store
583 .replace_advisories(587 .replace_advisories(
584 "crates.io",588 "crates.io",
@@ -599,9 +603,14 @@ mod tests {
599 ScanOutcome {603 ScanOutcome {
600 opened: 2,604 opened: 2,
601 fixed: 0605 fixed: 0
602- }606+ },
607+ "first scan opens both"
608+ );
609+ assert_eq!(
610+ store.dependency_count(&repo).await.unwrap(),
611+ 2,
612+ "two dependencies"
603 );613 );
604- assert_eq!(store.dependency_count(&repo).await.unwrap(), 2);
605 614
606 // Upgrading `other` past its fix closes its alert. `old` stays vulnerable.615 // Upgrading `other` past its fix closes its alert. `old` stays vulnerable.
607 let second = store616 let second = store
@@ -613,7 +622,8 @@ mod tests {
613 ScanOutcome {622 ScanOutcome {
614 opened: 0,623 opened: 0,
615 fixed: 1624 fixed: 1
616- }625+ },
626+ "upgrade closes one"
617 );627 );
618 let third = store628 let third = store
619 .scan_repo(&repo, &[dep("old", "1.0.0")])629 .scan_repo(&repo, &[dep("old", "1.0.0")])
@@ -624,36 +634,39 @@ mod tests {
624 ScanOutcome {634 ScanOutcome {
625 opened: 0,635 opened: 0,
626 fixed: 0636 fixed: 0
627- }637+ },
638+ "nothing changes"
628 );639 );
629 640
630 let alerts = store.list_alerts(&repo).await.unwrap();641 let alerts = store.list_alerts(&repo).await.unwrap();
631- assert_eq!(alerts.len(), 2);642+ assert_eq!(alerts.len(), 2, "two alerts on the repo");
632 let open = alerts643 let open = alerts
633 .iter()644 .iter()
634 .find(|a| a.key.advisory_id == "RUSTSEC-1")645 .find(|a| a.key.advisory_id == "RUSTSEC-1")
635 .unwrap();646 .unwrap();
636- assert_eq!(open.state, AlertState::Open);647+ assert_eq!(open.state, AlertState::Open, "open alert");
637- assert_eq!(open.summary, "RUSTSEC-1 summary");648+ assert_eq!(open.summary, "RUSTSEC-1 summary", "summary copied");
638- assert_eq!(open.severity, "HIGH");649+ assert_eq!(open.severity, "HIGH", "severity copied");
639 let fixed = alerts650 let fixed = alerts
640 .iter()651 .iter()
641 .find(|a| a.key.advisory_id == "RUSTSEC-2")652 .find(|a| a.key.advisory_id == "RUSTSEC-2")
642 .unwrap();653 .unwrap();
643- assert_eq!(fixed.state, AlertState::Fixed);654+ assert_eq!(fixed.state, AlertState::Fixed, "fixed alert");
644 655
645 // Dismiss, then a scan keeps it dismissed; a reopen moves it back.656 // Dismiss, then a scan keeps it dismissed; a reopen moves it back.
646 assert!(657 assert!(
647 store658 store
648 .set_alert_state(&repo, &open.key, AlertState::Open, AlertState::Dismissed)659 .set_alert_state(&repo, &open.key, AlertState::Open, AlertState::Dismissed)
649 .await660 .await
650- .unwrap()661+ .unwrap(),
662+ "dismiss applied"
651 );663 );
652 assert!(664 assert!(
653 !store665 !store
654 .set_alert_state(&repo, &open.key, AlertState::Open, AlertState::Dismissed)666 .set_alert_state(&repo, &open.key, AlertState::Open, AlertState::Dismissed)
655 .await667 .await
656- .unwrap()668+ .unwrap(),
669+ "second dismiss is a no-op"
657 );670 );
658 let kept = store671 let kept = store
659 .scan_repo(&repo, &[dep("old", "1.0.0")])672 .scan_repo(&repo, &[dep("old", "1.0.0")])
@@ -664,34 +677,38 @@ mod tests {
664 ScanOutcome {677 ScanOutcome {
665 opened: 0,678 opened: 0,
666 fixed: 0679 fixed: 0
667- }680+ },
681+ "dismissed alert stays put"
668 );682 );
669 assert!(683 assert!(
670 store684 store
671 .set_alert_state(&repo, &open.key, AlertState::Dismissed, AlertState::Open)685 .set_alert_state(&repo, &open.key, AlertState::Dismissed, AlertState::Open)
672 .await686 .await
673- .unwrap()687+ .unwrap(),
688+ "reopen applied"
674 );689 );
675- }
676 690
677- #[tokio::test]691+ // A fixed alert reopens when the vulnerable match returns (own repo).
678- async fn fixed_alert_reopens_when_the_match_returns() {692+ let owner = store.repo_by_id(&repo).await.unwrap().unwrap().owner_id;
679- let dir = tempfile::tempdir().unwrap();693+ let back = store
680- let (store, repo) = store_with_repo(&dir).await;694+ .create_repo(&owner, "back", "", crate::Visibility::Private, "main")
695+ .await
696+ .unwrap()
697+ .id;
681 store698 store
682 .replace_advisories("crates.io", &[advisory("RUSTSEC-3", "back", "2.0.0")])699 .replace_advisories("crates.io", &[advisory("RUSTSEC-3", "back", "2.0.0")])
683 .await700 .await
684 .unwrap();701 .unwrap();
685 store702 store
686- .scan_repo(&repo, &[dep("back", "1.0.0")])703+ .scan_repo(&back, &[dep("back", "1.0.0")])
687 .await704 .await
688 .unwrap();705 .unwrap();
689 store706 store
690- .scan_repo(&repo, &[dep("back", "2.0.0")])707+ .scan_repo(&back, &[dep("back", "2.0.0")])
691 .await708 .await
692 .unwrap();709 .unwrap();
693 let again = store710 let again = store
694- .scan_repo(&repo, &[dep("back", "1.0.1")])711+ .scan_repo(&back, &[dep("back", "1.0.1")])
695 .await712 .await
696 .unwrap();713 .unwrap();
697 assert_eq!(714 assert_eq!(
@@ -699,11 +716,38 @@ mod tests {
699 ScanOutcome {716 ScanOutcome {
700 opened: 1,717 opened: 1,
701 fixed: 0718 fixed: 0
702- }719+ },
720+ "the match returns and reopens"
703 );721 );
704- let alerts = store.list_alerts(&repo).await.unwrap();722+ let alerts = store.list_alerts(&back).await.unwrap();
705- assert_eq!(alerts.len(), 2);723+ assert_eq!(alerts.len(), 2, "alert count on the back repo");
706- assert_eq!(alerts[0].state, AlertState::Open);724+ assert_eq!(alerts[0].state, AlertState::Open, "reopened alert is open");
725+
726+ // Replacing advisories for one ecosystem leaves the others alone.
727+ store
728+ .replace_advisories("crates.io", &[advisory("RUSTSEC-4", "a", "2.0.0")])
729+ .await
730+ .unwrap();
731+ store.replace_advisories("npm", &[]).await.unwrap();
732+ assert_eq!(
733+ store
734+ .advisories_for_package("crates.io", "a")
735+ .await
736+ .unwrap()
737+ .len(),
738+ 1,
739+ "crates.io advisory kept"
740+ );
741+ store
742+ .replace_advisories("crates.io", &[advisory("RUSTSEC-5", "a", "3.0.0")])
743+ .await
744+ .unwrap();
745+ let rows = store
746+ .advisories_for_package("crates.io", "a")
747+ .await
748+ .unwrap();
749+ assert_eq!(rows.len(), 1, "replaced in place");
750+ assert_eq!(rows[0].id, "RUSTSEC-5", "the new advisory");
707 }751 }
708 752
709 #[tokio::test]753 #[tokio::test]
@@ -756,33 +800,4 @@ mod tests {
756 .unwrap();800 .unwrap();
757 assert!(unread.is_empty());801 assert!(unread.is_empty());
758 }802 }
759-
760- #[tokio::test]
761- async fn replacing_advisories_is_scoped_to_one_ecosystem() {
762- let dir = tempfile::tempdir().unwrap();
763- let (store, _repo) = store_with_repo(&dir).await;
764- store
765- .replace_advisories("crates.io", &[advisory("RUSTSEC-4", "a", "2.0.0")])
766- .await
767- .unwrap();
768- store.replace_advisories("npm", &[]).await.unwrap();
769- assert_eq!(
770- store
771- .advisories_for_package("crates.io", "a")
772- .await
773- .unwrap()
774- .len(),
775- 1
776- );
777- store
778- .replace_advisories("crates.io", &[advisory("RUSTSEC-5", "a", "3.0.0")])
779- .await
780- .unwrap();
781- let rows = store
782- .advisories_for_package("crates.io", "a")
783- .await
784- .unwrap();
785- assert_eq!(rows.len(), 1);
786- assert_eq!(rows[0].id, "RUSTSEC-5");
787- }
788 }803 }
crates/forge-core/src/store/backup_runs.rsmodified+27 -21
@@ -228,6 +228,33 @@ mod tests {
228 #[tokio::test]228 #[tokio::test]
229 async fn start_and_finish_round_trip() {229 async fn start_and_finish_round_trip() {
230 let (_dir, store) = store().await;230 let (_dir, store) = store().await;
231+
232+ // The repo list starts empty and follows repo creation.
233+ assert!(
234+ store.repo_ids().await.expect("ids").is_empty(),
235+ "no repos yet"
236+ );
237+ let user = store
238+ .create_user("tahinli", None, "hash", false)
239+ .await
240+ .expect("user");
241+ let repo = store
242+ .create_repo(
243+ &user.id,
244+ "demo",
245+ "",
246+ crate::model::Visibility::Private,
247+ "main",
248+ )
249+ .await
250+ .expect("repo");
251+ assert_eq!(
252+ store.repo_ids().await.expect("ids"),
253+ vec![repo.id],
254+ "one repo listed"
255+ );
256+
257+ // A run starts unfinished, then finishes with its outcome.
231 let id = store258 let id = store
232 .start_run(RunKind::Backup, RunTrigger::Manual)259 .start_run(RunKind::Backup, RunTrigger::Manual)
233 .await260 .await
@@ -325,25 +352,4 @@ mod tests {
325 assert_eq!(runs[1].id, ids[1]);352 assert_eq!(runs[1].id, ids[1]);
326 assert_eq!(store.recent_runs(0).await.expect("recent").len(), 1);353 assert_eq!(store.recent_runs(0).await.expect("recent").len(), 1);
327 }354 }
328-
329- #[tokio::test]
330- async fn repo_ids_lists_every_repo() {
331- let (_dir, store) = store().await;
332- assert!(store.repo_ids().await.expect("ids").is_empty());
333- let user = store
334- .create_user("tahinli", None, "hash", false)
335- .await
336- .expect("user");
337- let repo = store
338- .create_repo(
339- &user.id,
340- "demo",
341- "",
342- crate::model::Visibility::Private,
343- "main",
344- )
345- .await
346- .expect("repo");
347- assert_eq!(store.repo_ids().await.expect("ids"), vec![repo.id]);
348- }
349 }355 }
crates/forge-core/src/store/lfs.rsmodified+29 -17
@@ -71,30 +71,42 @@ mod tests {
71 (store, repo.id, user.id)71 (store, repo.id, user.id)
72 }72 }
73 73
74+ // One store serves both checks. Deleting a repo runs first, so the link
75+ // check below starts from a store with no links at all.
74 #[tokio::test]76 #[tokio::test]
75- async fn links_are_per_repo_and_idempotent() {77+ async fn lfs_links_per_repo_idempotent_and_removed_with_repo() {
76 let dir = tempfile::tempdir().unwrap();78 let dir = tempfile::tempdir().unwrap();
77- let (store, repo_id, _) = repo_fixture(&dir).await;79+ let (store, repo_id, user_id) = repo_fixture(&dir).await;
78- assert_eq!(store.lfs_object_size(&repo_id, OID).await.unwrap(), None);80+
79- assert!(!store.lfs_oid_is_linked(OID).await.unwrap());81+ let gone = store
82+ .create_repo(&user_id, "gone", "", Visibility::Public, "main")
83+ .await
84+ .unwrap()
85+ .id;
86+ store.insert_lfs_object(&gone, OID, 3).await.unwrap();
87+ store.delete_repo(&gone).await.unwrap();
88+ assert!(
89+ !store.lfs_oid_is_linked(OID).await.unwrap(),
90+ "delete_repo drops the link"
91+ );
92+
93+ assert_eq!(
94+ store.lfs_object_size(&repo_id, OID).await.unwrap(),
95+ None,
96+ "not linked yet"
97+ );
98+ assert!(
99+ !store.lfs_oid_is_linked(OID).await.unwrap(),
100+ "no link anywhere"
101+ );
80 102
81 store.insert_lfs_object(&repo_id, OID, 12).await.unwrap();103 store.insert_lfs_object(&repo_id, OID, 12).await.unwrap();
82 store.insert_lfs_object(&repo_id, OID, 12).await.unwrap();104 store.insert_lfs_object(&repo_id, OID, 12).await.unwrap();
83 assert_eq!(105 assert_eq!(
84 store.lfs_object_size(&repo_id, OID).await.unwrap(),106 store.lfs_object_size(&repo_id, OID).await.unwrap(),
85- Some(12)107+ Some(12),
108+ "one link, size kept"
86 );109 );
87- assert!(store.lfs_oid_is_linked(OID).await.unwrap());110+ assert!(store.lfs_oid_is_linked(OID).await.unwrap(), "linked");
88- }
89-
90- #[tokio::test]
91- async fn delete_repo_removes_lfs_rows() {
92- let dir = tempfile::tempdir().unwrap();
93- let (store, repo_id, _) = repo_fixture(&dir).await;
94- store.insert_lfs_object(&repo_id, OID, 3).await.unwrap();
95-
96- store.delete_repo(&repo_id).await.unwrap();
97-
98- assert!(!store.lfs_oid_is_linked(OID).await.unwrap());
99 }111 }
100 }112 }
crates/forge-core/src/store/mirrors.rsmodified+97 -71
@@ -424,9 +424,21 @@ mod tests {
424 );424 );
425 }425 }
426 426
427+ async fn add_repo(store: &Store, owner: &User, name: &str) -> Repo {
428+ store
429+ .create_repo(&owner.id, name, "", Visibility::Public, "main")
430+ .await
431+ .unwrap()
432+ }
433+
434+ // Pull and push rules, interval checks, the result/delete path, repo
435+ // deletion and the push-mirror cap share one store. Each block works on
436+ // its own repo so the cases cannot interfere with each other.
427 #[tokio::test]437 #[tokio::test]
428- async fn one_pull_mirror_per_repo() {438+ async fn mirror_rules_and_lifecycle_per_repo() {
429- let (store, _tahinli, repo, _dir) = setup().await;439+ let (store, tahinli, repo, _dir) = setup().await;
440+
441+ // One pull mirror per repo; push mirrors are not limited by that slot.
430 let first = store442 let first = store
431 .create_mirror(&pull(&repo, "https://a.example/x.git"))443 .create_mirror(&pull(&repo, "https://a.example/x.git"))
432 .await444 .await
@@ -435,34 +447,105 @@ mod tests {
435 assert!(first.enabled);447 assert!(first.enabled);
436 assert_eq!(448 assert_eq!(
437 store.pull_mirror(&repo.id).await.unwrap().map(|m| m.id),449 store.pull_mirror(&repo.id).await.unwrap().map(|m| m.id),
438- Some(first.id.clone())450+ Some(first.id.clone()),
451+ "pull slot holds the first mirror"
439 );452 );
440 let second = store453 let second = store
441 .create_mirror(&pull(&repo, "https://b.example/y.git"))454 .create_mirror(&pull(&repo, "https://b.example/y.git"))
442 .await;455 .await;
443- assert!(matches!(second, Err(Error::Conflict(_))), "{second:?}");456+ assert!(
444- // Push mirrors are not limited by the pull slot.457+ matches!(second, Err(Error::Conflict(_))),
458+ "second pull: {second:?}"
459+ );
445 store460 store
446 .create_mirror(&push(&repo, "https://c.example/z.git"))461 .create_mirror(&push(&repo, "https://c.example/z.git"))
447 .await462 .await
448 .unwrap();463 .unwrap();
449- assert_eq!(store.list_mirrors(&repo.id).await.unwrap().len(), 2);464+ assert_eq!(
450- }465+ store.list_mirrors(&repo.id).await.unwrap().len(),
466+ 2,
467+ "one pull plus one push"
468+ );
451 469
452- #[tokio::test]470+ // Intervals: 5 minutes is too short, 0 means manual sync only.
453- async fn at_most_ten_push_mirrors() {471+ let interval_repo = add_repo(&store, &tahinli, "interval").await;
454- let (store, _tahinli, repo, _dir) = setup().await;472+ let too_short = store
473+ .create_mirror(&NewMirror {
474+ interval_minutes: 5,
475+ ..pull(&interval_repo, "https://a.example/x.git")
476+ })
477+ .await;
478+ assert!(
479+ matches!(too_short, Err(Error::Invalid(_))),
480+ "5 minutes: {too_short:?}"
481+ );
482+ let manual = store
483+ .create_mirror(&NewMirror {
484+ interval_minutes: 0,
485+ ..pull(&interval_repo, "https://a.example/x.git")
486+ })
487+ .await
488+ .unwrap();
489+ assert_eq!(manual.interval_minutes, 0, "manual interval kept");
490+
491+ // Recording a result, then deleting the mirror.
492+ let record_repo = add_repo(&store, &tahinli, "record").await;
493+ let m = store
494+ .create_mirror(&pull(&record_repo, "https://a.example/x.git"))
495+ .await
496+ .unwrap();
497+ let next = stamp_after(Duration::from_secs(3600));
498+ store
499+ .record_mirror_result(&m.id, false, Some("boom"), &next)
500+ .await
501+ .unwrap();
502+ let after = store.mirror(&m.id).await.unwrap().unwrap();
503+ assert_eq!(after.last_ok, Some(false), "result recorded");
504+ assert_eq!(after.last_error.as_deref(), Some("boom"), "error recorded");
505+ assert!(after.last_sync_at.is_some(), "sync time recorded");
506+ assert_eq!(after.next_sync_at, next, "next sync recorded");
507+ store.delete_mirror(&m.id).await.unwrap();
508+ assert!(
509+ store.mirror(&m.id).await.unwrap().is_none(),
510+ "mirror deleted"
511+ );
512+ assert!(
513+ matches!(store.delete_mirror(&m.id).await, Err(Error::NotFound)),
514+ "deleting twice is NotFound"
515+ );
516+
517+ // Deleting the repo removes its mirrors.
518+ let gone = add_repo(&store, &tahinli, "gone").await;
519+ let g = store
520+ .create_mirror(&pull(&gone, "https://a.example/x.git"))
521+ .await
522+ .unwrap();
523+ store.delete_repo(&gone.id).await.unwrap();
524+ assert!(
525+ store.mirror(&g.id).await.unwrap().is_none(),
526+ "delete_repo removes mirrors"
527+ );
528+
529+ // The push cap: ten per repo, the eleventh is refused.
530+ let ten = add_repo(&store, &tahinli, "ten").await;
455 for n in 0..MAX_PUSH_MIRRORS {531 for n in 0..MAX_PUSH_MIRRORS {
456 store532 store
457- .create_mirror(&push(&repo, &format!("https://h{n}.example/r.git")))533+ .create_mirror(&push(&ten, &format!("https://h{n}.example/r.git")))
458 .await534 .await
459 .unwrap();535 .unwrap();
460 }536 }
461 let eleventh = store537 let eleventh = store
462- .create_mirror(&push(&repo, "https://h11.example/r.git"))538+ .create_mirror(&push(&ten, "https://h11.example/r.git"))
463 .await;539 .await;
464- assert!(matches!(eleventh, Err(Error::Invalid(_))), "{eleventh:?}");540+ assert!(
465- assert_eq!(store.list_mirrors(&repo.id).await.unwrap().len(), 10);541+ matches!(eleventh, Err(Error::Invalid(_))),
542+ "eleventh push: {eleventh:?}"
543+ );
544+ assert_eq!(
545+ store.list_mirrors(&ten.id).await.unwrap().len(),
546+ 10,
547+ "cap holds"
548+ );
466 }549 }
467 550
468 #[tokio::test]551 #[tokio::test]
@@ -631,61 +714,4 @@ mod tests {
631 store.schedule_mirror(&future.id, &now).await.unwrap();714 store.schedule_mirror(&future.id, &now).await.unwrap();
632 assert_eq!(store.due_mirrors(&now, 10).await.unwrap().len(), 3);715 assert_eq!(store.due_mirrors(&now, 10).await.unwrap().len(), 3);
633 }716 }
634-
635- #[tokio::test]
636- async fn record_result_and_delete() {
637- let (store, _tahinli, repo, _dir) = setup().await;
638- let m = store
639- .create_mirror(&pull(&repo, "https://a.example/x.git"))
640- .await
641- .unwrap();
642- let next = stamp_after(Duration::from_secs(3600));
643- store
644- .record_mirror_result(&m.id, false, Some("boom"), &next)
645- .await
646- .unwrap();
647- let after = store.mirror(&m.id).await.unwrap().unwrap();
648- assert_eq!(after.last_ok, Some(false));
649- assert_eq!(after.last_error.as_deref(), Some("boom"));
650- assert!(after.last_sync_at.is_some());
651- assert_eq!(after.next_sync_at, next);
652-
653- store.delete_mirror(&m.id).await.unwrap();
654- assert!(store.mirror(&m.id).await.unwrap().is_none());
655- assert!(matches!(
656- store.delete_mirror(&m.id).await,
657- Err(Error::NotFound)
658- ));
659- }
660-
661- #[tokio::test]
662- async fn interval_is_validated() {
663- let (store, _tahinli, repo, _dir) = setup().await;
664- let too_short = store
665- .create_mirror(&NewMirror {
666- interval_minutes: 5,
667- ..pull(&repo, "https://a.example/x.git")
668- })
669- .await;
670- assert!(matches!(too_short, Err(Error::Invalid(_))));
671- let manual = store
672- .create_mirror(&NewMirror {
673- interval_minutes: 0,
674- ..pull(&repo, "https://a.example/x.git")
675- })
676- .await
677- .unwrap();
678- assert_eq!(manual.interval_minutes, 0);
679- }
680-
681- #[tokio::test]
682- async fn delete_repo_removes_mirrors() {
683- let (store, _tahinli, repo, _dir) = setup().await;
684- let m = store
685- .create_mirror(&pull(&repo, "https://a.example/x.git"))
686- .await
687- .unwrap();
688- store.delete_repo(&repo.id).await.unwrap();
689- assert!(store.mirror(&m.id).await.unwrap().is_none());
690- }
691 }717 }
crates/forge-core/src/store/profile_pins.rsmodified+72 -47
@@ -146,68 +146,93 @@ mod tests {
146 .collect()146 .collect()
147 }147 }
148 148
149+ // One store serves every scenario. Each scenario gets its own owner, so
150+ // pin sets and the "repo-N" names never collide.
149 #[tokio::test]151 #[tokio::test]
150- async fn pins_keep_the_order_given_and_replace_the_old_set() {152+ async fn profile_pin_rules() {
151- let (store, tahinli, _, _dir) = fixture().await;153+ let (store, tahinli, foreign, _dir) = fixture().await;
154+
155+ // Pins keep the order given and each save replaces the old set.
152 let ids = make_repos(&store, &tahinli, 3).await;156 let ids = make_repos(&store, &tahinli, 3).await;
153 store157 store
154 .set_profile_pins(&tahinli, &[ids[2].clone(), ids[0].clone()])158 .set_profile_pins(&tahinli, &[ids[2].clone(), ids[0].clone()])
155 .await159 .await
156 .unwrap();160 .unwrap();
157- assert_eq!(pinned_names(&store, &tahinli).await, ["repo-2", "repo-0"]);161+ assert_eq!(
158-162+ pinned_names(&store, &tahinli).await,
163+ ["repo-2", "repo-0"],
164+ "order kept"
165+ );
159 store166 store
160 .set_profile_pins(&tahinli, &[ids[1].clone()])167 .set_profile_pins(&tahinli, &[ids[1].clone()])
161 .await168 .await
162 .unwrap();169 .unwrap();
163- assert_eq!(pinned_names(&store, &tahinli).await, ["repo-1"]);170+ assert_eq!(
164-171+ pinned_names(&store, &tahinli).await,
172+ ["repo-1"],
173+ "old set replaced"
174+ );
165 store.set_profile_pins(&tahinli, &[]).await.unwrap();175 store.set_profile_pins(&tahinli, &[]).await.unwrap();
166- assert!(pinned_names(&store, &tahinli).await.is_empty());176+ assert!(
167- }177+ pinned_names(&store, &tahinli).await.is_empty(),
168-178+ "empty clears the pins"
169- #[tokio::test]179+ );
170- async fn more_than_six_pins_is_refused_and_keeps_the_old_pins() {180+
171- let (store, tahinli, _, _dir) = fixture().await;181+ // More than six pins is refused and the old pins stay.
172- let ids = make_repos(&store, &tahinli, 7).await;182+ let big = store.create_user("big", None, "h", false).await.unwrap();
173- store.set_profile_pins(&tahinli, &ids[..2]).await.unwrap();183+ let ids = make_repos(&store, &big.id, 7).await;
174- let too_many = store.set_profile_pins(&tahinli, &ids).await;184+ store.set_profile_pins(&big.id, &ids[..2]).await.unwrap();
175- assert!(matches!(too_many, Err(Error::Invalid(_))));185+ let too_many = store.set_profile_pins(&big.id, &ids).await;
176- assert_eq!(pinned_names(&store, &tahinli).await, ["repo-0", "repo-1"]);186+ assert!(
177- }187+ matches!(too_many, Err(Error::Invalid(_))),
178-188+ "seven pins: {too_many:?}"
179- #[tokio::test]189+ );
180- async fn foreign_and_repeated_repos_are_refused_atomically() {190+ assert_eq!(
181- let (store, tahinli, foreign, _dir) = fixture().await;191+ pinned_names(&store, &big.id).await,
182- let ids = make_repos(&store, &tahinli, 1).await;192+ ["repo-0", "repo-1"],
183- store.set_profile_pins(&tahinli, &ids[..1]).await.unwrap();193+ "old pins kept"
184-194+ );
195+
196+ // Foreign and repeated repos are refused atomically.
197+ let strict = store.create_user("strict", None, "h", false).await.unwrap();
198+ let ids = make_repos(&store, &strict.id, 1).await;
199+ store.set_profile_pins(&strict.id, &ids[..1]).await.unwrap();
185 let stolen = store200 let stolen = store
186- .set_profile_pins(&tahinli, &[ids[0].clone(), foreign])201+ .set_profile_pins(&strict.id, &[ids[0].clone(), foreign.clone()])
187 .await;202 .await;
188- assert!(matches!(stolen, Err(Error::Invalid(_))));203+ assert!(
204+ matches!(stolen, Err(Error::Invalid(_))),
205+ "foreign repo: {stolen:?}"
206+ );
189 let repeated = store207 let repeated = store
190- .set_profile_pins(&tahinli, &[ids[0].clone(), ids[0].clone()])208+ .set_profile_pins(&strict.id, &[ids[0].clone(), ids[0].clone()])
191 .await;209 .await;
192- assert!(matches!(repeated, Err(Error::Invalid(_))));210+ assert!(
193- assert_eq!(pinned_names(&store, &tahinli).await, ["repo-0"]);211+ matches!(repeated, Err(Error::Invalid(_))),
194- }212+ "repeated repo: {repeated:?}"
195-213+ );
196- #[tokio::test]214+ assert_eq!(
197- async fn deleting_a_pinned_repo_removes_its_pin() {215+ pinned_names(&store, &strict.id).await,
198- let (store, tahinli, _, _dir) = fixture().await;216+ ["repo-0"],
199- let ids = make_repos(&store, &tahinli, 2).await;217+ "nothing changed"
200- store.set_profile_pins(&tahinli, &ids).await.unwrap();218+ );
219+
220+ // Deleting a pinned repo removes its pin.
221+ let gone = store.create_user("gone", None, "h", false).await.unwrap();
222+ let ids = make_repos(&store, &gone.id, 2).await;
223+ store.set_profile_pins(&gone.id, &ids).await.unwrap();
201 store.delete_repo(&ids[0]).await.unwrap();224 store.delete_repo(&ids[0]).await.unwrap();
202- assert_eq!(pinned_names(&store, &tahinli).await, ["repo-1"]);225+ assert_eq!(
203- }226+ pinned_names(&store, &gone.id).await,
204-227+ ["repo-1"],
205- #[tokio::test]228+ "deleted pin removed"
206- async fn owner_repos_by_activity_lists_every_owned_repo() {229+ );
207- let (store, tahinli, foreign, _dir) = fixture().await;230+
208- make_repos(&store, &tahinli, 2).await;231+ // The activity list holds every owned repo and no foreign one.
209- let listed = store.owner_repos_by_activity(&tahinli).await.unwrap();232+ let active = store.create_user("active", None, "h", false).await.unwrap();
210- assert_eq!(listed.len(), 2);233+ make_repos(&store, &active.id, 2).await;
211- assert!(listed.iter().all(|r| r.id != foreign));234+ let listed = store.owner_repos_by_activity(&active.id).await.unwrap();
235+ assert_eq!(listed.len(), 2, "both owned repos listed");
236+ assert!(listed.iter().all(|r| r.id != foreign), "no foreign repo");
212 }237 }
213 }238 }
crates/forge-core/src/store/push_recent.rsmodified+8 -8
@@ -116,8 +116,10 @@ mod tests {
116 (dir, store, tahinli.id, bob.id, demo.id, other.id)116 (dir, store, tahinli.id, bob.id, demo.id, other.id)
117 }117 }
118 118
119+ // One store serves both checks. The first one needs the store to hold only
120+ // its own pushes, so the "now" push of the second one runs after it.
119 #[tokio::test]121 #[tokio::test]
120- async fn keeps_latest_per_branch_and_skips_deletions_and_others() {122+ async fn recent_branch_pushes_rules_and_empty_inputs() {
121 let (_dir, store, tahinli, bob, demo, other) = store_with_repos().await;123 let (_dir, store, tahinli, bob, demo, other) = store_with_repos().await;
122 let since = stamp_before(Duration::from_secs(3600));124 let since = stamp_before(Duration::from_secs(3600));
123 let older = stamp_before(Duration::from_secs(600));125 let older = stamp_before(Duration::from_secs(600));
@@ -169,13 +171,11 @@ mod tests {
169 .unwrap();171 .unwrap();
170 assert_eq!(172 assert_eq!(
171 rows,173 rows,
172- vec![(demo.clone(), "feature".to_string(), newer.clone())]174+ vec![(demo.clone(), "feature".to_string(), newer.clone())],
175+ "only the newest recent branch push"
173 );176 );
174- }
175 177
176- #[tokio::test]178+ // No repo ids, or a `since` in the future, gives nothing.
177- async fn empty_repo_list_and_future_since() {
178- let (_dir, store, tahinli, _bob, demo, _other) = store_with_repos().await;
179 store179 store
180 .record_push_event(&demo, &tahinli, "refs/heads/feature", ZERO, OID, "test")180 .record_push_event(&demo, &tahinli, "refs/heads/feature", ZERO, OID, "test")
181 .await181 .await
@@ -184,12 +184,12 @@ mod tests {
184 .recent_branch_pushes(&[], &tahinli, &stamp_before(Duration::from_secs(3600)))184 .recent_branch_pushes(&[], &tahinli, &stamp_before(Duration::from_secs(3600)))
185 .await185 .await
186 .unwrap();186 .unwrap();
187- assert!(none.is_empty());187+ assert!(none.is_empty(), "empty repo list");
188 let future = stamp_after(Duration::from_secs(3600));188 let future = stamp_after(Duration::from_secs(3600));
189 let none = store189 let none = store
190 .recent_branch_pushes(&[demo.as_str()], &tahinli, &future)190 .recent_branch_pushes(&[demo.as_str()], &tahinli, &future)
191 .await191 .await
192 .unwrap();192 .unwrap();
193- assert!(none.is_empty());193+ assert!(none.is_empty(), "future since");
194 }194 }
195 }195 }
crates/forge-core/src/store/releases.rsmodified+90 -53
@@ -398,24 +398,44 @@ mod tests {
398 }398 }
399 }399 }
400 400
401+ async fn add_repo(store: &Store, user: &str, name: &str) -> String {
402+ store
403+ .create_repo(user, name, "", crate::Visibility::Public, "main")
404+ .await
405+ .unwrap()
406+ .id
407+ }
408+
409+ // Every scenario gets its own repo in one shared store, so the per-repo
410+ // assertions cannot see another scenario's releases or assets.
401 #[tokio::test]411 #[tokio::test]
402- async fn drafts_are_unpublished_and_tags_are_unique_per_repo() {412+ async fn release_lifecycle_per_repo() {
403- let (_dir, store, user, repo) = store_with_repo().await;413+ let (_dir, store, user, _repo) = store_with_repo().await;
414+
415+ // Drafts stay unpublished, and tags are unique per repo.
416+ let repo = add_repo(&store, &user, "drafts").await;
404 let draft = store417 let draft = store
405 .create_release(&new_release(&repo, &user, "v1", true))418 .create_release(&new_release(&repo, &user, "v1", true))
406 .await419 .await
407 .unwrap();420 .unwrap();
408- assert!(draft.draft && draft.published_at.is_none());421+ assert!(
409- assert!(store.latest_release(&repo).await.unwrap().is_none());422+ draft.draft && draft.published_at.is_none(),
423+ "draft is unpublished"
424+ );
425+ assert!(
426+ store.latest_release(&repo).await.unwrap().is_none(),
427+ "no latest draft"
428+ );
410 let again = store429 let again = store
411 .create_release(&new_release(&repo, &user, "v1", false))430 .create_release(&new_release(&repo, &user, "v1", false))
412 .await;431 .await;
413- assert!(matches!(again, Err(Error::Conflict(_))));432+ assert!(
414- }433+ matches!(again, Err(Error::Conflict(_))),
434+ "duplicate tag: {again:?}"
435+ );
415 436
416- #[tokio::test]437+ // Publishing sets published_at once; a prerelease is not "latest".
417- async fn publishing_sets_published_at_once_and_latest_skips_prereleases() {438+ let repo = add_repo(&store, &user, "publish").await;
418- let (_dir, store, user, repo) = store_with_repo().await;
419 let rel = store439 let rel = store
420 .create_release(&new_release(&repo, &user, "v1", true))440 .create_release(&new_release(&repo, &user, "v1", true))
421 .await441 .await
@@ -426,10 +446,13 @@ mod tests {
426 draft: false,446 draft: false,
427 prerelease: false,447 prerelease: false,
428 };448 };
429- assert!(store.update_release(&rel.id, &edit).await.unwrap());449+ assert!(
450+ store.update_release(&rel.id, &edit).await.unwrap(),
451+ "publish applied"
452+ );
430 let published = store.release(&rel.id).await.unwrap().unwrap();453 let published = store.release(&rel.id).await.unwrap().unwrap();
431 let stamp = published.published_at.clone();454 let stamp = published.published_at.clone();
432- assert!(stamp.is_some());455+ assert!(stamp.is_some(), "publish stamps the release");
433 let edit = ReleaseEdit {456 let edit = ReleaseEdit {
434 title: "v1b",457 title: "v1b",
435 body: "",458 body: "",
@@ -439,7 +462,8 @@ mod tests {
439 store.update_release(&rel.id, &edit).await.unwrap();462 store.update_release(&rel.id, &edit).await.unwrap();
440 assert_eq!(463 assert_eq!(
441 store.release(&rel.id).await.unwrap().unwrap().published_at,464 store.release(&rel.id).await.unwrap().unwrap().published_at,
442- stamp465+ stamp,
466+ "the stamp is set once"
443 );467 );
444 let pre = store468 let pre = store
445 .create_release(&NewRelease {469 .create_release(&NewRelease {
@@ -448,16 +472,15 @@ mod tests {
448 })472 })
449 .await473 .await
450 .unwrap();474 .unwrap();
451- assert!(pre.published_at.is_some());475+ assert!(pre.published_at.is_some(), "prerelease is published");
452 assert_eq!(476 assert_eq!(
453 store.latest_release(&repo).await.unwrap().unwrap().id,477 store.latest_release(&repo).await.unwrap().unwrap().id,
454- rel.id478+ rel.id,
479+ "latest skips the prerelease"
455 );480 );
456- }
457 481
458- #[tokio::test]482+ // Downloads are counted; deleting a release returns its blob ids.
459- async fn assets_count_downloads_and_deleting_a_release_returns_its_blob_ids() {483+ let repo = add_repo(&store, &user, "assets").await;
460- let (_dir, store, user, repo) = store_with_repo().await;
461 let rel = store484 let rel = store
462 .create_release(&new_release(&repo, &user, "v1", false))485 .create_release(&new_release(&repo, &user, "v1", false))
463 .await486 .await
@@ -474,7 +497,7 @@ mod tests {
474 })497 })
475 .await498 .await
476 .unwrap();499 .unwrap();
477- assert_eq!(asset.size, 3);500+ assert_eq!(asset.size, 3, "asset size");
478 store.count_asset_download("a1").await.unwrap();501 store.count_asset_download("a1").await.unwrap();
479 store.count_asset_download("a1").await.unwrap();502 store.count_asset_download("a1").await.unwrap();
480 let counted = store503 let counted = store
@@ -482,27 +505,33 @@ mod tests {
482 .await505 .await
483 .unwrap()506 .unwrap()
484 .unwrap();507 .unwrap();
485- assert_eq!(counted.download_count, 2);508+ assert_eq!(counted.download_count, 2, "two downloads counted");
486 assert_eq!(509 assert_eq!(
487 store.repo_release_asset_ids(&repo).await.unwrap(),510 store.repo_release_asset_ids(&repo).await.unwrap(),
488- vec!["a1".to_string()]511+ vec!["a1".to_string()],
512+ "repo lists its asset"
489 );513 );
490 let removed = store.delete_release(&rel.id).await.unwrap();514 let removed = store.delete_release(&rel.id).await.unwrap();
491- assert_eq!(removed, Some(vec!["a1".to_string()]));515+ assert_eq!(removed, Some(vec!["a1".to_string()]), "blob ids returned");
492- assert!(store.release(&rel.id).await.unwrap().is_none());516+ assert!(
493- assert_eq!(store.delete_release(&rel.id).await.unwrap(), None);517+ store.release(&rel.id).await.unwrap().is_none(),
494- }518+ "release deleted"
519+ );
520+ assert_eq!(
521+ store.delete_release(&rel.id).await.unwrap(),
522+ None,
523+ "second delete"
524+ );
495 525
496- #[tokio::test]526+ // Replacing an asset swaps its file but keeps its name and counter.
497- async fn replacing_an_asset_swaps_its_file_and_keeps_name_and_count() {527+ let repo = add_repo(&store, &user, "replace").await;
498- let (_dir, store, user, repo) = store_with_repo().await;
499 let rel = store528 let rel = store
500 .create_release(&new_release(&repo, &user, "v1", false))529 .create_release(&new_release(&repo, &user, "v1", false))
501 .await530 .await
502 .unwrap();531 .unwrap();
503 store532 store
504 .add_asset(&NewAsset {533 .add_asset(&NewAsset {
505- id: "a1",534+ id: "b1",
506 release_id: &rel.id,535 release_id: &rel.id,
507 name: "app.bin",536 name: "app.bin",
508 size: 3,537 size: 3,
@@ -512,13 +541,12 @@ mod tests {
512 })541 })
513 .await542 .await
514 .unwrap();543 .unwrap();
515- store.count_asset_download("a1").await.unwrap();544+ store.count_asset_download("b1").await.unwrap();
516-
517 let replaced = store545 let replaced = store
518 .replace_asset(546 .replace_asset(
519- "a1",547+ "b1",
520 &NewAsset {548 &NewAsset {
521- id: "a2",549+ id: "b2",
522 release_id: &rel.id,550 release_id: &rel.id,
523 name: "app.bin",551 name: "app.bin",
524 size: 5,552 size: 5,
@@ -529,23 +557,26 @@ mod tests {
529 )557 )
530 .await558 .await
531 .unwrap();559 .unwrap();
532- assert_eq!(replaced.id, "a2");560+ assert_eq!(replaced.id, "b2", "new id");
533- assert_eq!(replaced.name, "app.bin");561+ assert_eq!(replaced.name, "app.bin", "name kept");
534- assert_eq!(replaced.size, 5);562+ assert_eq!(replaced.size, 5, "new size");
535- assert_eq!(replaced.sha256, "y");563+ assert_eq!(replaced.sha256, "y", "new hash");
536- assert_eq!(replaced.content_type, "text/plain");564+ assert_eq!(replaced.content_type, "text/plain", "new content type");
537 assert_eq!(replaced.download_count, 1, "the counter survives");565 assert_eq!(replaced.download_count, 1, "the counter survives");
538- assert!(store.release_asset_by_id("a1").await.unwrap().is_none());566+ assert!(
567+ store.release_asset_by_id("b1").await.unwrap().is_none(),
568+ "old id gone"
569+ );
539 assert_eq!(570 assert_eq!(
540 store.repo_release_asset_ids(&repo).await.unwrap(),571 store.repo_release_asset_ids(&repo).await.unwrap(),
541- vec!["a2".to_string()]572+ vec!["b2".to_string()],
573+ "only the new asset remains"
542 );574 );
543-
544 let gone = store575 let gone = store
545 .replace_asset(576 .replace_asset(
546- "a1",577+ "b1",
547 &NewAsset {578 &NewAsset {
548- id: "a3",579+ id: "b3",
549 release_id: &rel.id,580 release_id: &rel.id,
550 name: "app.bin",581 name: "app.bin",
551 size: 1,582 size: 1,
@@ -555,12 +586,13 @@ mod tests {
555 },586 },
556 )587 )
557 .await;588 .await;
558- assert!(matches!(gone, Err(Error::NotFound)), "{gone:?}");589+ assert!(
559- }590+ matches!(gone, Err(Error::NotFound)),
591+ "replacing a missing asset: {gone:?}"
592+ );
560 593
561- #[tokio::test]594+ // Retagging moves the release, but not onto a tag another release uses.
562- async fn retagging_moves_the_release_and_refuses_a_tag_another_release_uses() {595+ let repo = add_repo(&store, &user, "retag").await;
563- let (_dir, store, user, repo) = store_with_repo().await;
564 let first = store596 let first = store
565 .create_release(&new_release(&repo, &user, "v1", true))597 .create_release(&new_release(&repo, &user, "v1", true))
566 .await598 .await
@@ -574,18 +606,23 @@ mod tests {
574 store606 store
575 .set_release_tag(&first.id, "v1b", new_target)607 .set_release_tag(&first.id, "v1b", new_target)
576 .await608 .await
577- .unwrap()609+ .unwrap(),
610+ "retag applied"
578 );611 );
579 let moved = store.release(&first.id).await.unwrap().unwrap();612 let moved = store.release(&first.id).await.unwrap().unwrap();
580- assert_eq!(moved.tag, "v1b");613+ assert_eq!(moved.tag, "v1b", "tag moved");
581- assert_eq!(moved.target_oid, new_target);614+ assert_eq!(moved.target_oid, new_target, "target moved");
582 let taken = store.set_release_tag(&first.id, "v2", new_target).await;615 let taken = store.set_release_tag(&first.id, "v2", new_target).await;
583- assert!(matches!(taken, Err(Error::Conflict(_))), "{taken:?}");616+ assert!(
617+ matches!(taken, Err(Error::Conflict(_))),
618+ "taken tag: {taken:?}"
619+ );
584 assert!(620 assert!(
585 !store621 !store
586 .set_release_tag("missing", "v9", new_target)622 .set_release_tag("missing", "v9", new_target)
587 .await623 .await
588- .unwrap()624+ .unwrap(),
625+ "unknown release is not retagged"
589 );626 );
590 }627 }
591 628
crates/forge-core/src/store/replication.rsmodified+46 -42
@@ -123,9 +123,43 @@ mod tests {
123 (dir, store)123 (dir, store)
124 }124 }
125 125
126+ // One store serves both checks. The list check runs first, so its exact
127+ // subject list is not affected by the "r1" row used further down.
126 #[tokio::test]128 #[tokio::test]
127- async fn ok_and_error_keep_the_other_fields() {129+ async fn status_list_delete_and_ok_or_error_keep_other_fields() {
128 let (_dir, store) = store().await;130 let (_dir, store) = store().await;
131+
132+ store.record_replication_ok("db", None).await.expect("db");
133+ store
134+ .record_replication_error("b-repo", "x")
135+ .await
136+ .expect("b");
137+ store
138+ .record_replication_error("a-repo", "y")
139+ .await
140+ .expect("a");
141+ let subjects: Vec<String> = store
142+ .list_replication_status()
143+ .await
144+ .expect("list")
145+ .into_iter()
146+ .map(|s| s.subject)
147+ .collect();
148+ assert_eq!(subjects, ["a-repo", "b-repo", "db"], "ordered by subject");
149+
150+ store
151+ .delete_replication_status("a-repo")
152+ .await
153+ .expect("delete");
154+ assert!(
155+ store
156+ .replication_status("a-repo")
157+ .await
158+ .expect("get")
159+ .is_none(),
160+ "deleted row is gone"
161+ );
162+
129 assert!(store.replication_status("r1").await.expect("get").is_none());163 assert!(store.replication_status("r1").await.expect("get").is_none());
130 164
131 store165 store
@@ -137,9 +171,9 @@ mod tests {
137 .await171 .await
138 .expect("get")172 .expect("get")
139 .expect("row");173 .expect("row");
140- assert!(first.last_ok_at.is_some());174+ assert!(first.last_ok_at.is_some(), "success stamped");
141- assert_eq!(first.refs_sha256.as_deref(), Some("abc"));175+ assert_eq!(first.refs_sha256.as_deref(), Some("abc"), "refs hash kept");
142- assert_eq!(first.last_error, None);176+ assert_eq!(first.last_error, None, "no error yet");
143 177
144 store178 store
145 .record_replication_error("r1", "push failed")179 .record_replication_error("r1", "push failed")
@@ -150,12 +184,16 @@ mod tests {
150 .await184 .await
151 .expect("get")185 .expect("get")
152 .expect("row");186 .expect("row");
153- assert_eq!(failed.last_error.as_deref(), Some("push failed"));187+ assert_eq!(
188+ failed.last_error.as_deref(),
189+ Some("push failed"),
190+ "error stored"
191+ );
154 assert_eq!(192 assert_eq!(
155 failed.last_ok_at, first.last_ok_at,193 failed.last_ok_at, first.last_ok_at,
156 "a failure keeps the last success"194 "a failure keeps the last success"
157 );195 );
158- assert_eq!(failed.refs_sha256.as_deref(), Some("abc"));196+ assert_eq!(failed.refs_sha256.as_deref(), Some("abc"), "refs hash kept");
159 197
160 store.record_replication_ok("r1", None).await.expect("ok");198 store.record_replication_ok("r1", None).await.expect("ok");
161 let healed = store199 let healed = store
@@ -163,41 +201,7 @@ mod tests {
163 .await201 .await
164 .expect("get")202 .expect("get")
165 .expect("row");203 .expect("row");
166- assert_eq!(healed.last_error, None);204+ assert_eq!(healed.last_error, None, "success clears the error");
167- assert_eq!(healed.refs_sha256.as_deref(), Some("abc"));205+ assert_eq!(healed.refs_sha256.as_deref(), Some("abc"), "refs hash kept");
168- }
169-
170- #[tokio::test]
171- async fn list_is_ordered_and_delete_removes_rows() {
172- let (_dir, store) = store().await;
173- store.record_replication_ok("db", None).await.expect("db");
174- store
175- .record_replication_error("b-repo", "x")
176- .await
177- .expect("b");
178- store
179- .record_replication_error("a-repo", "y")
180- .await
181- .expect("a");
182- let subjects: Vec<String> = store
183- .list_replication_status()
184- .await
185- .expect("list")
186- .into_iter()
187- .map(|s| s.subject)
188- .collect();
189- assert_eq!(subjects, ["a-repo", "b-repo", "db"]);
190-
191- store
192- .delete_replication_status("a-repo")
193- .await
194- .expect("delete");
195- assert!(
196- store
197- .replication_status("a-repo")
198- .await
199- .expect("get")
200- .is_none()
201- );
202 }206 }
203 }207 }
crates/forge-core/src/store/repo_listing.rsmodified+19 -21
@@ -237,9 +237,7 @@ mod tests {
237 .id237 .id
238 }238 }
239 239
240- #[tokio::test]240+ async fn strangers_and_anonymous_viewers_never_see_private_repos(fx: &Fixture) {
241- async fn strangers_and_anonymous_viewers_never_see_private_repos() {
242- let fx = fixture().await;
243 let stranger = fx241 let stranger = fx
244 .store242 .store
245 .create_user("mira", None, "h", false)243 .create_user("mira", None, "h", false)
@@ -254,9 +252,7 @@ mod tests {
254 );252 );
255 }253 }
256 254
257- #[tokio::test]255+ async fn a_collaborator_sees_the_private_repo_they_work_on(fx: &Fixture) {
258- async fn a_collaborator_sees_the_private_repo_they_work_on() {
259- let fx = fixture().await;
260 let carol = fx256 let carol = fx
261 .store257 .store
262 .create_user("carol", None, "h", false)258 .create_user("carol", None, "h", false)
@@ -274,9 +270,7 @@ mod tests {
274 );270 );
275 }271 }
276 272
277- #[tokio::test]273+ async fn an_admin_sees_everything(fx: &Fixture) {
278- async fn an_admin_sees_everything() {
279- let fx = fixture().await;
280 let admin = fx.store.create_user("root", None, "h", true).await.unwrap();274 let admin = fx.store.create_user("root", None, "h", true).await.unwrap();
281 let scope = RepoScope::OwnedBy(&fx.owner.id);275 let scope = RepoScope::OwnedBy(&fx.owner.id);
282 assert_eq!(276 assert_eq!(
@@ -308,13 +302,11 @@ mod tests {
308 }302 }
309 }303 }
310 304
311- #[tokio::test]305+ async fn org_roles_match_org_repo_role(fx: &Fixture) {
312- async fn org_roles_match_org_repo_role() {
313- let fx = fixture().await;
314 let org = fx.store.create_org("acme", &fx.owner.id).await.unwrap();306 let org = fx.store.create_org("acme", &fx.owner.id).await.unwrap();
315 let member = fx307 let member = fx
316 .store308 .store
317- .create_user("mira", None, "h", false)309+ .create_user("ines", None, "h", false)
318 .await310 .await
319 .unwrap();311 .unwrap();
320 let outsider = fx.store.create_user("zed", None, "h", false).await.unwrap();312 let outsider = fx.store.create_user("zed", None, "h", false).await.unwrap();
@@ -369,14 +361,8 @@ mod tests {
369 check(&outsider, &[]).await;361 check(&outsider, &[]).await;
370 }362 }
371 363
372- #[tokio::test]364+ async fn stars_scope_filters_search_and_pages(fx: &Fixture) {
373- async fn stars_scope_filters_search_and_pages() {365+ let fan = fx.store.create_user("fan", None, "h", false).await.unwrap();
374- let fx = fixture().await;
375- let fan = fx
376- .store
377- .create_user("mira", None, "h", false)
378- .await
379- .unwrap();
380 for name in ["open", "closed"] {366 for name in ["open", "closed"] {
381 let id = repo_id(&fx.store, "tahinli", name).await;367 let id = repo_id(&fx.store, "tahinli", name).await;
382 fx.store.star(&fan.id, &id).await.unwrap();368 fx.store.star(&fan.id, &id).await.unwrap();
@@ -406,4 +392,16 @@ mod tests {
406 assert_eq!(rows.len(), 1);392 assert_eq!(rows.len(), 1);
407 assert_eq!(rows[0].0.name, "open");393 assert_eq!(rows[0].0.name, "open");
408 }394 }
395+
396+ // The listing rules share one fixture; each check only adds what it needs,
397+ // and the collaborator grant runs before the later checks.
398+ #[tokio::test]
399+ async fn repo_visibility_and_listing_rules() {
400+ let fx = fixture().await;
401+ strangers_and_anonymous_viewers_never_see_private_repos(&fx).await;
402+ a_collaborator_sees_the_private_repo_they_work_on(&fx).await;
403+ an_admin_sees_everything(&fx).await;
404+ org_roles_match_org_repo_role(&fx).await;
405+ stars_scope_filters_search_and_pages(&fx).await;
406+ }
409 }407 }
crates/forge-core/src/store/signatures.rsmodified+47 -16
@@ -323,37 +323,48 @@ mod tests {
323 }323 }
324 324
325 #[tokio::test]325 #[tokio::test]
326- async fn gpg_key_round_trip_and_duplicate_fingerprint() {326+ async fn gpg_keys_add_list_and_delete_only_for_owner() {
327 let (store, tahinli, _, _dir) = setup().await;327 let (store, tahinli, _, _dir) = setup().await;
328 let emails = vec!["tahinli@example.com".to_string()];328 let emails = vec!["tahinli@example.com".to_string()];
329 let key = store329 let key = store
330 .add_gpg_key(&tahinli, "ABCD", "-----BEGIN-----", &emails)330 .add_gpg_key(&tahinli, "ABCD", "-----BEGIN-----", &emails)
331 .await331 .await
332 .unwrap();332 .unwrap();
333- assert_eq!(key.emails, emails);333+ assert_eq!(key.emails, emails, "emails kept");
334 let listed = store.list_gpg_keys(&tahinli).await.unwrap();334 let listed = store.list_gpg_keys(&tahinli).await.unwrap();
335- assert_eq!(listed, vec![key.clone()]);335+ assert_eq!(listed, vec![key.clone()], "listed once");
336- assert_eq!(store.all_gpg_keys().await.unwrap().len(), 1);336+ assert_eq!(
337+ store.all_gpg_keys().await.unwrap().len(),
338+ 1,
339+ "one key overall"
340+ );
337 let dup = store.add_gpg_key(&tahinli, "ABCD", "x", &emails).await;341 let dup = store.add_gpg_key(&tahinli, "ABCD", "x", &emails).await;
338- assert!(matches!(dup, Err(Error::Conflict(_))));342+ assert!(
339- }343+ matches!(dup, Err(Error::Conflict(_))),
344+ "duplicate fingerprint: {dup:?}"
345+ );
340 346
341- #[tokio::test]347+ // Another user cannot delete the key; the owner can.
342- async fn delete_gpg_key_only_for_owner() {
343- let (store, tahinli, _, _dir) = setup().await;
344 let bob = store.create_user("bob", None, "hash", false).await.unwrap();348 let bob = store.create_user("bob", None, "hash", false).await.unwrap();
345- let key = store.add_gpg_key(&tahinli, "ABCD", "x", &[]).await.unwrap();
346 assert!(349 assert!(
347 store350 store
348 .delete_gpg_key(&bob.id, &key.id)351 .delete_gpg_key(&bob.id, &key.id)
349 .await352 .await
350 .unwrap()353 .unwrap()
351- .is_none()354+ .is_none(),
355+ "not the owner"
356+ );
357+ assert_eq!(
358+ store.list_gpg_keys(&tahinli).await.unwrap().len(),
359+ 1,
360+ "still there"
352 );361 );
353- assert_eq!(store.list_gpg_keys(&tahinli).await.unwrap().len(), 1);
354 let removed = store.delete_gpg_key(&tahinli, &key.id).await.unwrap();362 let removed = store.delete_gpg_key(&tahinli, &key.id).await.unwrap();
355- assert_eq!(removed, Some(key));363+ assert_eq!(removed, Some(key), "owner removes it");
356- assert!(store.list_gpg_keys(&tahinli).await.unwrap().is_empty());364+ assert!(
365+ store.list_gpg_keys(&tahinli).await.unwrap().is_empty(),
366+ "list empty"
367+ );
357 }368 }
358 369
359 #[tokio::test]370 #[tokio::test]
@@ -411,11 +422,31 @@ mod tests {
411 SigStatus::UnknownKey,422 SigStatus::UnknownKey,
412 SigStatus::Bad,423 SigStatus::Bad,
413 ] {424 ] {
414- assert_eq!(s.as_str().parse::<SigStatus>().unwrap(), s);425+ assert_eq!(
426+ s.as_str().parse::<SigStatus>().unwrap(),
427+ s,
428+ "round trip {s:?}"
429+ );
415 }430 }
416 for k in [SigKind::Ssh, SigKind::Gpg] {431 for k in [SigKind::Ssh, SigKind::Gpg] {
417- assert_eq!(k.as_str().parse::<SigKind>().unwrap(), k);432+ assert_eq!(
433+ k.as_str().parse::<SigKind>().unwrap(),
434+ k,
435+ "round trip {k:?}"
436+ );
437+ }
438+ // Stored in the signature cache, so the literals must not change.
439+ for (s, literal) in [
440+ (SigStatus::Verified, "verified"),
441+ (SigStatus::Unverified, "unverified"),
442+ (SigStatus::Unsigned, "unsigned"),
443+ (SigStatus::UnknownKey, "unknown_key"),
444+ (SigStatus::Bad, "bad"),
445+ ] {
446+ assert_eq!(s.as_str(), literal, "stored literal of {s:?}");
418 }447 }
448+ assert_eq!(SigKind::Ssh.as_str(), "ssh", "stored literal of ssh");
449+ assert_eq!(SigKind::Gpg.as_str(), "gpg", "stored literal of gpg");
419 assert!("nope".parse::<SigStatus>().is_err());450 assert!("nope".parse::<SigStatus>().is_err());
420 }451 }
421 }452 }
crates/forge-core/src/store/snippets.rsmodified+18 -1
@@ -492,7 +492,24 @@ mod tests {
492 #[test]492 #[test]
493 fn visibility_names_round_trip() {493 fn visibility_names_round_trip() {
494 for v in SnippetVisibility::ALL {494 for v in SnippetVisibility::ALL {
495- assert_eq!(SnippetVisibility::parse(v.as_str()), Some(v));495+ assert_eq!(
496+ SnippetVisibility::parse(v.as_str()),
497+ Some(v),
498+ "round trip {v:?}"
499+ );
500+ }
501+ // The stored literals: changing one breaks saved snippets.
502+ for (v, literal) in [
503+ (SnippetVisibility::Public, "public"),
504+ (SnippetVisibility::Unlisted, "unlisted"),
505+ (SnippetVisibility::Private, "private"),
506+ ] {
507+ assert_eq!(v.as_str(), literal, "stored literal of {v:?}");
508+ assert_eq!(
509+ SnippetVisibility::parse(literal),
510+ Some(v),
511+ "parse {literal}"
512+ );
496 }513 }
497 assert_eq!(SnippetVisibility::parse("secret"), None);514 assert_eq!(SnippetVisibility::parse("secret"), None);
498 }515 }
crates/forge-core/src/store/wiki.rsmodified+21 -19
@@ -44,7 +44,7 @@ mod tests {
44 use crate::model::Visibility;44 use crate::model::Visibility;
45 45
46 #[tokio::test]46 #[tokio::test]
47- async fn wiki_is_on_by_default_and_can_be_switched() {47+ async fn wiki_is_on_by_default_can_be_switched_and_unknown_repo_is_not_found() {
48 let dir = tempfile::tempdir().unwrap();48 let dir = tempfile::tempdir().unwrap();
49 let store = Store::open(&dir.path().join("forge.db")).await.unwrap();49 let store = Store::open(&dir.path().join("forge.db")).await.unwrap();
50 let tahinli = store50 let tahinli = store
@@ -55,26 +55,28 @@ mod tests {
55 .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main")55 .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main")
56 .await56 .await
57 .unwrap();57 .unwrap();
58- assert!(store.wiki_enabled(&repo.id).await.unwrap());58+ assert!(store.wiki_enabled(&repo.id).await.unwrap(), "on by default");
59 store.set_wiki_enabled(&repo.id, false).await.unwrap();59 store.set_wiki_enabled(&repo.id, false).await.unwrap();
60- assert!(!store.wiki_enabled(&repo.id).await.unwrap());60+ assert!(!store.wiki_enabled(&repo.id).await.unwrap(), "switched off");
61 store.set_wiki_enabled(&repo.id, true).await.unwrap();61 store.set_wiki_enabled(&repo.id, true).await.unwrap();
62- assert!(store.wiki_enabled(&repo.id).await.unwrap());62+ assert!(store.wiki_enabled(&repo.id).await.unwrap(), "switched on");
63- }
64 63
65- #[tokio::test]64+ // An id that is no repo is not found, for read and for write.
66- async fn unknown_repo_is_not_found() {65+ assert!(
67- let dir = tempfile::tempdir().unwrap();66+ matches!(
68- let store = Store::open(&dir.path().join("forge.db")).await.unwrap();67+ store.wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV").await,
69- assert!(matches!(68+ Err(Error::NotFound)
70- store.wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV").await,69+ ),
71- Err(Error::NotFound)70+ "unknown repo read"
72- ));71+ );
73- assert!(matches!(72+ assert!(
74- store73+ matches!(
75- .set_wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV", false)74+ store
76- .await,75+ .set_wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV", false)
77- Err(Error::NotFound)76+ .await,
78- ));77+ Err(Error::NotFound)
78+ ),
79+ "unknown repo write"
80+ );
79 }81 }
80 }82 }