diff --git a/crates/forge-core/src/store/admin_alerts.rs b/crates/forge-core/src/store/admin_alerts.rs index 0255b4a..273b6e1 100644 --- a/crates/forge-core/src/store/admin_alerts.rs +++ b/crates/forge-core/src/store/admin_alerts.rs @@ -311,9 +311,7 @@ mod tests { (store, dir) } - #[tokio::test] - async fn an_alert_is_raised_once_until_it_resolves() { - let (store, _dir) = store().await; + async fn an_alert_is_raised_once_until_it_resolves(store: &Store) { assert!( store .raise_alert("runner_offline:r1", "runner_offline", "ci-1", "") @@ -352,9 +350,7 @@ mod tests { ); } - #[tokio::test] - async fn events_fire_once_and_are_never_active() { - let (store, _dir) = store().await; + async fn events_fire_once_and_are_never_active(store: &Store) { assert!( store .record_alert_event("ci_killed:run1", "ci_killed", "ci-1", "") @@ -369,4 +365,13 @@ mod tests { ); assert!(store.active_alerts().await.unwrap().is_empty()); } + + // One store serves both. Events run first: they must not be active, and + // the raised alert below would otherwise show up in that empty check. + #[tokio::test] + async fn events_and_raised_alerts_share_one_store() { + let (store, _dir) = store().await; + events_fire_once_and_are_never_active(&store).await; + an_alert_is_raised_once_until_it_resolves(&store).await; + } } diff --git a/crates/forge-core/src/store/advisories.rs b/crates/forge-core/src/store/advisories.rs index d76a3e3..2bf40e1 100644 --- a/crates/forge-core/src/store/advisories.rs +++ b/crates/forge-core/src/store/advisories.rs @@ -575,10 +575,14 @@ mod tests { (store, repo.id) } + // Advisories are global, so each block replaces the advisory set it needs + // before scanning. The per-repo alert checks use their own repo. #[tokio::test] - async fn scan_opens_reconciles_and_keeps_dismissed() { + async fn scans_reconcile_alerts_and_replace_per_ecosystem() { let dir = tempfile::tempdir().unwrap(); let (store, repo) = store_with_repo(&dir).await; + + // A scan opens alerts, closes fixed ones, and keeps dismissed ones. store .replace_advisories( "crates.io", @@ -599,9 +603,14 @@ mod tests { ScanOutcome { opened: 2, fixed: 0 - } + }, + "first scan opens both" + ); + assert_eq!( + store.dependency_count(&repo).await.unwrap(), + 2, + "two dependencies" ); - assert_eq!(store.dependency_count(&repo).await.unwrap(), 2); // Upgrading `other` past its fix closes its alert. `old` stays vulnerable. let second = store @@ -613,7 +622,8 @@ mod tests { ScanOutcome { opened: 0, fixed: 1 - } + }, + "upgrade closes one" ); let third = store .scan_repo(&repo, &[dep("old", "1.0.0")]) @@ -624,36 +634,39 @@ mod tests { ScanOutcome { opened: 0, fixed: 0 - } + }, + "nothing changes" ); let alerts = store.list_alerts(&repo).await.unwrap(); - assert_eq!(alerts.len(), 2); + assert_eq!(alerts.len(), 2, "two alerts on the repo"); let open = alerts .iter() .find(|a| a.key.advisory_id == "RUSTSEC-1") .unwrap(); - assert_eq!(open.state, AlertState::Open); - assert_eq!(open.summary, "RUSTSEC-1 summary"); - assert_eq!(open.severity, "HIGH"); + assert_eq!(open.state, AlertState::Open, "open alert"); + assert_eq!(open.summary, "RUSTSEC-1 summary", "summary copied"); + assert_eq!(open.severity, "HIGH", "severity copied"); let fixed = alerts .iter() .find(|a| a.key.advisory_id == "RUSTSEC-2") .unwrap(); - assert_eq!(fixed.state, AlertState::Fixed); + assert_eq!(fixed.state, AlertState::Fixed, "fixed alert"); // Dismiss, then a scan keeps it dismissed; a reopen moves it back. assert!( store .set_alert_state(&repo, &open.key, AlertState::Open, AlertState::Dismissed) .await - .unwrap() + .unwrap(), + "dismiss applied" ); assert!( !store .set_alert_state(&repo, &open.key, AlertState::Open, AlertState::Dismissed) .await - .unwrap() + .unwrap(), + "second dismiss is a no-op" ); let kept = store .scan_repo(&repo, &[dep("old", "1.0.0")]) @@ -664,34 +677,38 @@ mod tests { ScanOutcome { opened: 0, fixed: 0 - } + }, + "dismissed alert stays put" ); assert!( store .set_alert_state(&repo, &open.key, AlertState::Dismissed, AlertState::Open) .await - .unwrap() + .unwrap(), + "reopen applied" ); - } - #[tokio::test] - async fn fixed_alert_reopens_when_the_match_returns() { - let dir = tempfile::tempdir().unwrap(); - let (store, repo) = store_with_repo(&dir).await; + // A fixed alert reopens when the vulnerable match returns (own repo). + let owner = store.repo_by_id(&repo).await.unwrap().unwrap().owner_id; + let back = store + .create_repo(&owner, "back", "", crate::Visibility::Private, "main") + .await + .unwrap() + .id; store .replace_advisories("crates.io", &[advisory("RUSTSEC-3", "back", "2.0.0")]) .await .unwrap(); store - .scan_repo(&repo, &[dep("back", "1.0.0")]) + .scan_repo(&back, &[dep("back", "1.0.0")]) .await .unwrap(); store - .scan_repo(&repo, &[dep("back", "2.0.0")]) + .scan_repo(&back, &[dep("back", "2.0.0")]) .await .unwrap(); let again = store - .scan_repo(&repo, &[dep("back", "1.0.1")]) + .scan_repo(&back, &[dep("back", "1.0.1")]) .await .unwrap(); assert_eq!( @@ -699,11 +716,38 @@ mod tests { ScanOutcome { opened: 1, fixed: 0 - } + }, + "the match returns and reopens" ); - let alerts = store.list_alerts(&repo).await.unwrap(); - assert_eq!(alerts.len(), 2); - assert_eq!(alerts[0].state, AlertState::Open); + let alerts = store.list_alerts(&back).await.unwrap(); + assert_eq!(alerts.len(), 2, "alert count on the back repo"); + assert_eq!(alerts[0].state, AlertState::Open, "reopened alert is open"); + + // Replacing advisories for one ecosystem leaves the others alone. + store + .replace_advisories("crates.io", &[advisory("RUSTSEC-4", "a", "2.0.0")]) + .await + .unwrap(); + store.replace_advisories("npm", &[]).await.unwrap(); + assert_eq!( + store + .advisories_for_package("crates.io", "a") + .await + .unwrap() + .len(), + 1, + "crates.io advisory kept" + ); + store + .replace_advisories("crates.io", &[advisory("RUSTSEC-5", "a", "3.0.0")]) + .await + .unwrap(); + let rows = store + .advisories_for_package("crates.io", "a") + .await + .unwrap(); + assert_eq!(rows.len(), 1, "replaced in place"); + assert_eq!(rows[0].id, "RUSTSEC-5", "the new advisory"); } #[tokio::test] @@ -756,33 +800,4 @@ mod tests { .unwrap(); assert!(unread.is_empty()); } - - #[tokio::test] - async fn replacing_advisories_is_scoped_to_one_ecosystem() { - let dir = tempfile::tempdir().unwrap(); - let (store, _repo) = store_with_repo(&dir).await; - store - .replace_advisories("crates.io", &[advisory("RUSTSEC-4", "a", "2.0.0")]) - .await - .unwrap(); - store.replace_advisories("npm", &[]).await.unwrap(); - assert_eq!( - store - .advisories_for_package("crates.io", "a") - .await - .unwrap() - .len(), - 1 - ); - store - .replace_advisories("crates.io", &[advisory("RUSTSEC-5", "a", "3.0.0")]) - .await - .unwrap(); - let rows = store - .advisories_for_package("crates.io", "a") - .await - .unwrap(); - assert_eq!(rows.len(), 1); - assert_eq!(rows[0].id, "RUSTSEC-5"); - } } diff --git a/crates/forge-core/src/store/backup_runs.rs b/crates/forge-core/src/store/backup_runs.rs index fbb3ca8..f23961d 100644 --- a/crates/forge-core/src/store/backup_runs.rs +++ b/crates/forge-core/src/store/backup_runs.rs @@ -228,6 +228,33 @@ mod tests { #[tokio::test] async fn start_and_finish_round_trip() { let (_dir, store) = store().await; + + // The repo list starts empty and follows repo creation. + assert!( + store.repo_ids().await.expect("ids").is_empty(), + "no repos yet" + ); + let user = store + .create_user("tahinli", None, "hash", false) + .await + .expect("user"); + let repo = store + .create_repo( + &user.id, + "demo", + "", + crate::model::Visibility::Private, + "main", + ) + .await + .expect("repo"); + assert_eq!( + store.repo_ids().await.expect("ids"), + vec![repo.id], + "one repo listed" + ); + + // A run starts unfinished, then finishes with its outcome. let id = store .start_run(RunKind::Backup, RunTrigger::Manual) .await @@ -325,25 +352,4 @@ mod tests { assert_eq!(runs[1].id, ids[1]); assert_eq!(store.recent_runs(0).await.expect("recent").len(), 1); } - - #[tokio::test] - async fn repo_ids_lists_every_repo() { - let (_dir, store) = store().await; - assert!(store.repo_ids().await.expect("ids").is_empty()); - let user = store - .create_user("tahinli", None, "hash", false) - .await - .expect("user"); - let repo = store - .create_repo( - &user.id, - "demo", - "", - crate::model::Visibility::Private, - "main", - ) - .await - .expect("repo"); - assert_eq!(store.repo_ids().await.expect("ids"), vec![repo.id]); - } } diff --git a/crates/forge-core/src/store/lfs.rs b/crates/forge-core/src/store/lfs.rs index 3e3795a..0683aa3 100644 --- a/crates/forge-core/src/store/lfs.rs +++ b/crates/forge-core/src/store/lfs.rs @@ -71,30 +71,42 @@ mod tests { (store, repo.id, user.id) } + // One store serves both checks. Deleting a repo runs first, so the link + // check below starts from a store with no links at all. #[tokio::test] - async fn links_are_per_repo_and_idempotent() { + async fn lfs_links_per_repo_idempotent_and_removed_with_repo() { let dir = tempfile::tempdir().unwrap(); - let (store, repo_id, _) = repo_fixture(&dir).await; - assert_eq!(store.lfs_object_size(&repo_id, OID).await.unwrap(), None); - assert!(!store.lfs_oid_is_linked(OID).await.unwrap()); + let (store, repo_id, user_id) = repo_fixture(&dir).await; + + let gone = store + .create_repo(&user_id, "gone", "", Visibility::Public, "main") + .await + .unwrap() + .id; + store.insert_lfs_object(&gone, OID, 3).await.unwrap(); + store.delete_repo(&gone).await.unwrap(); + assert!( + !store.lfs_oid_is_linked(OID).await.unwrap(), + "delete_repo drops the link" + ); + + assert_eq!( + store.lfs_object_size(&repo_id, OID).await.unwrap(), + None, + "not linked yet" + ); + assert!( + !store.lfs_oid_is_linked(OID).await.unwrap(), + "no link anywhere" + ); store.insert_lfs_object(&repo_id, OID, 12).await.unwrap(); store.insert_lfs_object(&repo_id, OID, 12).await.unwrap(); assert_eq!( store.lfs_object_size(&repo_id, OID).await.unwrap(), - Some(12) + Some(12), + "one link, size kept" ); - assert!(store.lfs_oid_is_linked(OID).await.unwrap()); - } - - #[tokio::test] - async fn delete_repo_removes_lfs_rows() { - let dir = tempfile::tempdir().unwrap(); - let (store, repo_id, _) = repo_fixture(&dir).await; - store.insert_lfs_object(&repo_id, OID, 3).await.unwrap(); - - store.delete_repo(&repo_id).await.unwrap(); - - assert!(!store.lfs_oid_is_linked(OID).await.unwrap()); + assert!(store.lfs_oid_is_linked(OID).await.unwrap(), "linked"); } } diff --git a/crates/forge-core/src/store/mirrors.rs b/crates/forge-core/src/store/mirrors.rs index a16e360..cf2f05b 100644 --- a/crates/forge-core/src/store/mirrors.rs +++ b/crates/forge-core/src/store/mirrors.rs @@ -424,9 +424,21 @@ mod tests { ); } + async fn add_repo(store: &Store, owner: &User, name: &str) -> Repo { + store + .create_repo(&owner.id, name, "", Visibility::Public, "main") + .await + .unwrap() + } + + // Pull and push rules, interval checks, the result/delete path, repo + // deletion and the push-mirror cap share one store. Each block works on + // its own repo so the cases cannot interfere with each other. #[tokio::test] - async fn one_pull_mirror_per_repo() { - let (store, _tahinli, repo, _dir) = setup().await; + async fn mirror_rules_and_lifecycle_per_repo() { + let (store, tahinli, repo, _dir) = setup().await; + + // One pull mirror per repo; push mirrors are not limited by that slot. let first = store .create_mirror(&pull(&repo, "https://a.example/x.git")) .await @@ -435,34 +447,105 @@ mod tests { assert!(first.enabled); assert_eq!( store.pull_mirror(&repo.id).await.unwrap().map(|m| m.id), - Some(first.id.clone()) + Some(first.id.clone()), + "pull slot holds the first mirror" ); let second = store .create_mirror(&pull(&repo, "https://b.example/y.git")) .await; - assert!(matches!(second, Err(Error::Conflict(_))), "{second:?}"); - // Push mirrors are not limited by the pull slot. + assert!( + matches!(second, Err(Error::Conflict(_))), + "second pull: {second:?}" + ); store .create_mirror(&push(&repo, "https://c.example/z.git")) .await .unwrap(); - assert_eq!(store.list_mirrors(&repo.id).await.unwrap().len(), 2); - } + assert_eq!( + store.list_mirrors(&repo.id).await.unwrap().len(), + 2, + "one pull plus one push" + ); - #[tokio::test] - async fn at_most_ten_push_mirrors() { - let (store, _tahinli, repo, _dir) = setup().await; + // Intervals: 5 minutes is too short, 0 means manual sync only. + let interval_repo = add_repo(&store, &tahinli, "interval").await; + let too_short = store + .create_mirror(&NewMirror { + interval_minutes: 5, + ..pull(&interval_repo, "https://a.example/x.git") + }) + .await; + assert!( + matches!(too_short, Err(Error::Invalid(_))), + "5 minutes: {too_short:?}" + ); + let manual = store + .create_mirror(&NewMirror { + interval_minutes: 0, + ..pull(&interval_repo, "https://a.example/x.git") + }) + .await + .unwrap(); + assert_eq!(manual.interval_minutes, 0, "manual interval kept"); + + // Recording a result, then deleting the mirror. + let record_repo = add_repo(&store, &tahinli, "record").await; + let m = store + .create_mirror(&pull(&record_repo, "https://a.example/x.git")) + .await + .unwrap(); + let next = stamp_after(Duration::from_secs(3600)); + store + .record_mirror_result(&m.id, false, Some("boom"), &next) + .await + .unwrap(); + let after = store.mirror(&m.id).await.unwrap().unwrap(); + assert_eq!(after.last_ok, Some(false), "result recorded"); + assert_eq!(after.last_error.as_deref(), Some("boom"), "error recorded"); + assert!(after.last_sync_at.is_some(), "sync time recorded"); + assert_eq!(after.next_sync_at, next, "next sync recorded"); + store.delete_mirror(&m.id).await.unwrap(); + assert!( + store.mirror(&m.id).await.unwrap().is_none(), + "mirror deleted" + ); + assert!( + matches!(store.delete_mirror(&m.id).await, Err(Error::NotFound)), + "deleting twice is NotFound" + ); + + // Deleting the repo removes its mirrors. + let gone = add_repo(&store, &tahinli, "gone").await; + let g = store + .create_mirror(&pull(&gone, "https://a.example/x.git")) + .await + .unwrap(); + store.delete_repo(&gone.id).await.unwrap(); + assert!( + store.mirror(&g.id).await.unwrap().is_none(), + "delete_repo removes mirrors" + ); + + // The push cap: ten per repo, the eleventh is refused. + let ten = add_repo(&store, &tahinli, "ten").await; for n in 0..MAX_PUSH_MIRRORS { store - .create_mirror(&push(&repo, &format!("https://h{n}.example/r.git"))) + .create_mirror(&push(&ten, &format!("https://h{n}.example/r.git"))) .await .unwrap(); } let eleventh = store - .create_mirror(&push(&repo, "https://h11.example/r.git")) + .create_mirror(&push(&ten, "https://h11.example/r.git")) .await; - assert!(matches!(eleventh, Err(Error::Invalid(_))), "{eleventh:?}"); - assert_eq!(store.list_mirrors(&repo.id).await.unwrap().len(), 10); + assert!( + matches!(eleventh, Err(Error::Invalid(_))), + "eleventh push: {eleventh:?}" + ); + assert_eq!( + store.list_mirrors(&ten.id).await.unwrap().len(), + 10, + "cap holds" + ); } #[tokio::test] @@ -631,61 +714,4 @@ mod tests { store.schedule_mirror(&future.id, &now).await.unwrap(); assert_eq!(store.due_mirrors(&now, 10).await.unwrap().len(), 3); } - - #[tokio::test] - async fn record_result_and_delete() { - let (store, _tahinli, repo, _dir) = setup().await; - let m = store - .create_mirror(&pull(&repo, "https://a.example/x.git")) - .await - .unwrap(); - let next = stamp_after(Duration::from_secs(3600)); - store - .record_mirror_result(&m.id, false, Some("boom"), &next) - .await - .unwrap(); - let after = store.mirror(&m.id).await.unwrap().unwrap(); - assert_eq!(after.last_ok, Some(false)); - assert_eq!(after.last_error.as_deref(), Some("boom")); - assert!(after.last_sync_at.is_some()); - assert_eq!(after.next_sync_at, next); - - store.delete_mirror(&m.id).await.unwrap(); - assert!(store.mirror(&m.id).await.unwrap().is_none()); - assert!(matches!( - store.delete_mirror(&m.id).await, - Err(Error::NotFound) - )); - } - - #[tokio::test] - async fn interval_is_validated() { - let (store, _tahinli, repo, _dir) = setup().await; - let too_short = store - .create_mirror(&NewMirror { - interval_minutes: 5, - ..pull(&repo, "https://a.example/x.git") - }) - .await; - assert!(matches!(too_short, Err(Error::Invalid(_)))); - let manual = store - .create_mirror(&NewMirror { - interval_minutes: 0, - ..pull(&repo, "https://a.example/x.git") - }) - .await - .unwrap(); - assert_eq!(manual.interval_minutes, 0); - } - - #[tokio::test] - async fn delete_repo_removes_mirrors() { - let (store, _tahinli, repo, _dir) = setup().await; - let m = store - .create_mirror(&pull(&repo, "https://a.example/x.git")) - .await - .unwrap(); - store.delete_repo(&repo.id).await.unwrap(); - assert!(store.mirror(&m.id).await.unwrap().is_none()); - } } diff --git a/crates/forge-core/src/store/profile_pins.rs b/crates/forge-core/src/store/profile_pins.rs index ac2c592..5b443ff 100644 --- a/crates/forge-core/src/store/profile_pins.rs +++ b/crates/forge-core/src/store/profile_pins.rs @@ -146,68 +146,93 @@ mod tests { .collect() } + // One store serves every scenario. Each scenario gets its own owner, so + // pin sets and the "repo-N" names never collide. #[tokio::test] - async fn pins_keep_the_order_given_and_replace_the_old_set() { - let (store, tahinli, _, _dir) = fixture().await; + async fn profile_pin_rules() { + let (store, tahinli, foreign, _dir) = fixture().await; + + // Pins keep the order given and each save replaces the old set. let ids = make_repos(&store, &tahinli, 3).await; store .set_profile_pins(&tahinli, &[ids[2].clone(), ids[0].clone()]) .await .unwrap(); - assert_eq!(pinned_names(&store, &tahinli).await, ["repo-2", "repo-0"]); - + assert_eq!( + pinned_names(&store, &tahinli).await, + ["repo-2", "repo-0"], + "order kept" + ); store .set_profile_pins(&tahinli, &[ids[1].clone()]) .await .unwrap(); - assert_eq!(pinned_names(&store, &tahinli).await, ["repo-1"]); - + assert_eq!( + pinned_names(&store, &tahinli).await, + ["repo-1"], + "old set replaced" + ); store.set_profile_pins(&tahinli, &[]).await.unwrap(); - assert!(pinned_names(&store, &tahinli).await.is_empty()); - } - - #[tokio::test] - async fn more_than_six_pins_is_refused_and_keeps_the_old_pins() { - let (store, tahinli, _, _dir) = fixture().await; - let ids = make_repos(&store, &tahinli, 7).await; - store.set_profile_pins(&tahinli, &ids[..2]).await.unwrap(); - let too_many = store.set_profile_pins(&tahinli, &ids).await; - assert!(matches!(too_many, Err(Error::Invalid(_)))); - assert_eq!(pinned_names(&store, &tahinli).await, ["repo-0", "repo-1"]); - } - - #[tokio::test] - async fn foreign_and_repeated_repos_are_refused_atomically() { - let (store, tahinli, foreign, _dir) = fixture().await; - let ids = make_repos(&store, &tahinli, 1).await; - store.set_profile_pins(&tahinli, &ids[..1]).await.unwrap(); - + assert!( + pinned_names(&store, &tahinli).await.is_empty(), + "empty clears the pins" + ); + + // More than six pins is refused and the old pins stay. + let big = store.create_user("big", None, "h", false).await.unwrap(); + let ids = make_repos(&store, &big.id, 7).await; + store.set_profile_pins(&big.id, &ids[..2]).await.unwrap(); + let too_many = store.set_profile_pins(&big.id, &ids).await; + assert!( + matches!(too_many, Err(Error::Invalid(_))), + "seven pins: {too_many:?}" + ); + assert_eq!( + pinned_names(&store, &big.id).await, + ["repo-0", "repo-1"], + "old pins kept" + ); + + // Foreign and repeated repos are refused atomically. + let strict = store.create_user("strict", None, "h", false).await.unwrap(); + let ids = make_repos(&store, &strict.id, 1).await; + store.set_profile_pins(&strict.id, &ids[..1]).await.unwrap(); let stolen = store - .set_profile_pins(&tahinli, &[ids[0].clone(), foreign]) + .set_profile_pins(&strict.id, &[ids[0].clone(), foreign.clone()]) .await; - assert!(matches!(stolen, Err(Error::Invalid(_)))); + assert!( + matches!(stolen, Err(Error::Invalid(_))), + "foreign repo: {stolen:?}" + ); let repeated = store - .set_profile_pins(&tahinli, &[ids[0].clone(), ids[0].clone()]) + .set_profile_pins(&strict.id, &[ids[0].clone(), ids[0].clone()]) .await; - assert!(matches!(repeated, Err(Error::Invalid(_)))); - assert_eq!(pinned_names(&store, &tahinli).await, ["repo-0"]); - } - - #[tokio::test] - async fn deleting_a_pinned_repo_removes_its_pin() { - let (store, tahinli, _, _dir) = fixture().await; - let ids = make_repos(&store, &tahinli, 2).await; - store.set_profile_pins(&tahinli, &ids).await.unwrap(); + assert!( + matches!(repeated, Err(Error::Invalid(_))), + "repeated repo: {repeated:?}" + ); + assert_eq!( + pinned_names(&store, &strict.id).await, + ["repo-0"], + "nothing changed" + ); + + // Deleting a pinned repo removes its pin. + let gone = store.create_user("gone", None, "h", false).await.unwrap(); + let ids = make_repos(&store, &gone.id, 2).await; + store.set_profile_pins(&gone.id, &ids).await.unwrap(); store.delete_repo(&ids[0]).await.unwrap(); - assert_eq!(pinned_names(&store, &tahinli).await, ["repo-1"]); - } - - #[tokio::test] - async fn owner_repos_by_activity_lists_every_owned_repo() { - let (store, tahinli, foreign, _dir) = fixture().await; - make_repos(&store, &tahinli, 2).await; - let listed = store.owner_repos_by_activity(&tahinli).await.unwrap(); - assert_eq!(listed.len(), 2); - assert!(listed.iter().all(|r| r.id != foreign)); + assert_eq!( + pinned_names(&store, &gone.id).await, + ["repo-1"], + "deleted pin removed" + ); + + // The activity list holds every owned repo and no foreign one. + let active = store.create_user("active", None, "h", false).await.unwrap(); + make_repos(&store, &active.id, 2).await; + let listed = store.owner_repos_by_activity(&active.id).await.unwrap(); + assert_eq!(listed.len(), 2, "both owned repos listed"); + assert!(listed.iter().all(|r| r.id != foreign), "no foreign repo"); } } diff --git a/crates/forge-core/src/store/push_recent.rs b/crates/forge-core/src/store/push_recent.rs index 7799e40..5d0444b 100644 --- a/crates/forge-core/src/store/push_recent.rs +++ b/crates/forge-core/src/store/push_recent.rs @@ -116,8 +116,10 @@ mod tests { (dir, store, tahinli.id, bob.id, demo.id, other.id) } + // One store serves both checks. The first one needs the store to hold only + // its own pushes, so the "now" push of the second one runs after it. #[tokio::test] - async fn keeps_latest_per_branch_and_skips_deletions_and_others() { + async fn recent_branch_pushes_rules_and_empty_inputs() { let (_dir, store, tahinli, bob, demo, other) = store_with_repos().await; let since = stamp_before(Duration::from_secs(3600)); let older = stamp_before(Duration::from_secs(600)); @@ -169,13 +171,11 @@ mod tests { .unwrap(); assert_eq!( rows, - vec![(demo.clone(), "feature".to_string(), newer.clone())] + vec![(demo.clone(), "feature".to_string(), newer.clone())], + "only the newest recent branch push" ); - } - #[tokio::test] - async fn empty_repo_list_and_future_since() { - let (_dir, store, tahinli, _bob, demo, _other) = store_with_repos().await; + // No repo ids, or a `since` in the future, gives nothing. store .record_push_event(&demo, &tahinli, "refs/heads/feature", ZERO, OID, "test") .await @@ -184,12 +184,12 @@ mod tests { .recent_branch_pushes(&[], &tahinli, &stamp_before(Duration::from_secs(3600))) .await .unwrap(); - assert!(none.is_empty()); + assert!(none.is_empty(), "empty repo list"); let future = stamp_after(Duration::from_secs(3600)); let none = store .recent_branch_pushes(&[demo.as_str()], &tahinli, &future) .await .unwrap(); - assert!(none.is_empty()); + assert!(none.is_empty(), "future since"); } } diff --git a/crates/forge-core/src/store/releases.rs b/crates/forge-core/src/store/releases.rs index a2aee5f..de7042a 100644 --- a/crates/forge-core/src/store/releases.rs +++ b/crates/forge-core/src/store/releases.rs @@ -398,24 +398,44 @@ mod tests { } } + async fn add_repo(store: &Store, user: &str, name: &str) -> String { + store + .create_repo(user, name, "", crate::Visibility::Public, "main") + .await + .unwrap() + .id + } + + // Every scenario gets its own repo in one shared store, so the per-repo + // assertions cannot see another scenario's releases or assets. #[tokio::test] - async fn drafts_are_unpublished_and_tags_are_unique_per_repo() { - let (_dir, store, user, repo) = store_with_repo().await; + async fn release_lifecycle_per_repo() { + let (_dir, store, user, _repo) = store_with_repo().await; + + // Drafts stay unpublished, and tags are unique per repo. + let repo = add_repo(&store, &user, "drafts").await; let draft = store .create_release(&new_release(&repo, &user, "v1", true)) .await .unwrap(); - assert!(draft.draft && draft.published_at.is_none()); - assert!(store.latest_release(&repo).await.unwrap().is_none()); + assert!( + draft.draft && draft.published_at.is_none(), + "draft is unpublished" + ); + assert!( + store.latest_release(&repo).await.unwrap().is_none(), + "no latest draft" + ); let again = store .create_release(&new_release(&repo, &user, "v1", false)) .await; - assert!(matches!(again, Err(Error::Conflict(_)))); - } + assert!( + matches!(again, Err(Error::Conflict(_))), + "duplicate tag: {again:?}" + ); - #[tokio::test] - async fn publishing_sets_published_at_once_and_latest_skips_prereleases() { - let (_dir, store, user, repo) = store_with_repo().await; + // Publishing sets published_at once; a prerelease is not "latest". + let repo = add_repo(&store, &user, "publish").await; let rel = store .create_release(&new_release(&repo, &user, "v1", true)) .await @@ -426,10 +446,13 @@ mod tests { draft: false, prerelease: false, }; - assert!(store.update_release(&rel.id, &edit).await.unwrap()); + assert!( + store.update_release(&rel.id, &edit).await.unwrap(), + "publish applied" + ); let published = store.release(&rel.id).await.unwrap().unwrap(); let stamp = published.published_at.clone(); - assert!(stamp.is_some()); + assert!(stamp.is_some(), "publish stamps the release"); let edit = ReleaseEdit { title: "v1b", body: "", @@ -439,7 +462,8 @@ mod tests { store.update_release(&rel.id, &edit).await.unwrap(); assert_eq!( store.release(&rel.id).await.unwrap().unwrap().published_at, - stamp + stamp, + "the stamp is set once" ); let pre = store .create_release(&NewRelease { @@ -448,16 +472,15 @@ mod tests { }) .await .unwrap(); - assert!(pre.published_at.is_some()); + assert!(pre.published_at.is_some(), "prerelease is published"); assert_eq!( store.latest_release(&repo).await.unwrap().unwrap().id, - rel.id + rel.id, + "latest skips the prerelease" ); - } - #[tokio::test] - async fn assets_count_downloads_and_deleting_a_release_returns_its_blob_ids() { - let (_dir, store, user, repo) = store_with_repo().await; + // Downloads are counted; deleting a release returns its blob ids. + let repo = add_repo(&store, &user, "assets").await; let rel = store .create_release(&new_release(&repo, &user, "v1", false)) .await @@ -474,7 +497,7 @@ mod tests { }) .await .unwrap(); - assert_eq!(asset.size, 3); + assert_eq!(asset.size, 3, "asset size"); store.count_asset_download("a1").await.unwrap(); store.count_asset_download("a1").await.unwrap(); let counted = store @@ -482,27 +505,33 @@ mod tests { .await .unwrap() .unwrap(); - assert_eq!(counted.download_count, 2); + assert_eq!(counted.download_count, 2, "two downloads counted"); assert_eq!( store.repo_release_asset_ids(&repo).await.unwrap(), - vec!["a1".to_string()] + vec!["a1".to_string()], + "repo lists its asset" ); let removed = store.delete_release(&rel.id).await.unwrap(); - assert_eq!(removed, Some(vec!["a1".to_string()])); - assert!(store.release(&rel.id).await.unwrap().is_none()); - assert_eq!(store.delete_release(&rel.id).await.unwrap(), None); - } + assert_eq!(removed, Some(vec!["a1".to_string()]), "blob ids returned"); + assert!( + store.release(&rel.id).await.unwrap().is_none(), + "release deleted" + ); + assert_eq!( + store.delete_release(&rel.id).await.unwrap(), + None, + "second delete" + ); - #[tokio::test] - async fn replacing_an_asset_swaps_its_file_and_keeps_name_and_count() { - let (_dir, store, user, repo) = store_with_repo().await; + // Replacing an asset swaps its file but keeps its name and counter. + let repo = add_repo(&store, &user, "replace").await; let rel = store .create_release(&new_release(&repo, &user, "v1", false)) .await .unwrap(); store .add_asset(&NewAsset { - id: "a1", + id: "b1", release_id: &rel.id, name: "app.bin", size: 3, @@ -512,13 +541,12 @@ mod tests { }) .await .unwrap(); - store.count_asset_download("a1").await.unwrap(); - + store.count_asset_download("b1").await.unwrap(); let replaced = store .replace_asset( - "a1", + "b1", &NewAsset { - id: "a2", + id: "b2", release_id: &rel.id, name: "app.bin", size: 5, @@ -529,23 +557,26 @@ mod tests { ) .await .unwrap(); - assert_eq!(replaced.id, "a2"); - assert_eq!(replaced.name, "app.bin"); - assert_eq!(replaced.size, 5); - assert_eq!(replaced.sha256, "y"); - assert_eq!(replaced.content_type, "text/plain"); + assert_eq!(replaced.id, "b2", "new id"); + assert_eq!(replaced.name, "app.bin", "name kept"); + assert_eq!(replaced.size, 5, "new size"); + assert_eq!(replaced.sha256, "y", "new hash"); + assert_eq!(replaced.content_type, "text/plain", "new content type"); assert_eq!(replaced.download_count, 1, "the counter survives"); - assert!(store.release_asset_by_id("a1").await.unwrap().is_none()); + assert!( + store.release_asset_by_id("b1").await.unwrap().is_none(), + "old id gone" + ); assert_eq!( store.repo_release_asset_ids(&repo).await.unwrap(), - vec!["a2".to_string()] + vec!["b2".to_string()], + "only the new asset remains" ); - let gone = store .replace_asset( - "a1", + "b1", &NewAsset { - id: "a3", + id: "b3", release_id: &rel.id, name: "app.bin", size: 1, @@ -555,12 +586,13 @@ mod tests { }, ) .await; - assert!(matches!(gone, Err(Error::NotFound)), "{gone:?}"); - } + assert!( + matches!(gone, Err(Error::NotFound)), + "replacing a missing asset: {gone:?}" + ); - #[tokio::test] - async fn retagging_moves_the_release_and_refuses_a_tag_another_release_uses() { - let (_dir, store, user, repo) = store_with_repo().await; + // Retagging moves the release, but not onto a tag another release uses. + let repo = add_repo(&store, &user, "retag").await; let first = store .create_release(&new_release(&repo, &user, "v1", true)) .await @@ -574,18 +606,23 @@ mod tests { store .set_release_tag(&first.id, "v1b", new_target) .await - .unwrap() + .unwrap(), + "retag applied" ); let moved = store.release(&first.id).await.unwrap().unwrap(); - assert_eq!(moved.tag, "v1b"); - assert_eq!(moved.target_oid, new_target); + assert_eq!(moved.tag, "v1b", "tag moved"); + assert_eq!(moved.target_oid, new_target, "target moved"); let taken = store.set_release_tag(&first.id, "v2", new_target).await; - assert!(matches!(taken, Err(Error::Conflict(_))), "{taken:?}"); + assert!( + matches!(taken, Err(Error::Conflict(_))), + "taken tag: {taken:?}" + ); assert!( !store .set_release_tag("missing", "v9", new_target) .await - .unwrap() + .unwrap(), + "unknown release is not retagged" ); } diff --git a/crates/forge-core/src/store/replication.rs b/crates/forge-core/src/store/replication.rs index 129f9bf..ed4437b 100644 --- a/crates/forge-core/src/store/replication.rs +++ b/crates/forge-core/src/store/replication.rs @@ -123,9 +123,43 @@ mod tests { (dir, store) } + // One store serves both checks. The list check runs first, so its exact + // subject list is not affected by the "r1" row used further down. #[tokio::test] - async fn ok_and_error_keep_the_other_fields() { + async fn status_list_delete_and_ok_or_error_keep_other_fields() { let (_dir, store) = store().await; + + store.record_replication_ok("db", None).await.expect("db"); + store + .record_replication_error("b-repo", "x") + .await + .expect("b"); + store + .record_replication_error("a-repo", "y") + .await + .expect("a"); + let subjects: Vec = store + .list_replication_status() + .await + .expect("list") + .into_iter() + .map(|s| s.subject) + .collect(); + assert_eq!(subjects, ["a-repo", "b-repo", "db"], "ordered by subject"); + + store + .delete_replication_status("a-repo") + .await + .expect("delete"); + assert!( + store + .replication_status("a-repo") + .await + .expect("get") + .is_none(), + "deleted row is gone" + ); + assert!(store.replication_status("r1").await.expect("get").is_none()); store @@ -137,9 +171,9 @@ mod tests { .await .expect("get") .expect("row"); - assert!(first.last_ok_at.is_some()); - assert_eq!(first.refs_sha256.as_deref(), Some("abc")); - assert_eq!(first.last_error, None); + assert!(first.last_ok_at.is_some(), "success stamped"); + assert_eq!(first.refs_sha256.as_deref(), Some("abc"), "refs hash kept"); + assert_eq!(first.last_error, None, "no error yet"); store .record_replication_error("r1", "push failed") @@ -150,12 +184,16 @@ mod tests { .await .expect("get") .expect("row"); - assert_eq!(failed.last_error.as_deref(), Some("push failed")); + assert_eq!( + failed.last_error.as_deref(), + Some("push failed"), + "error stored" + ); assert_eq!( failed.last_ok_at, first.last_ok_at, "a failure keeps the last success" ); - assert_eq!(failed.refs_sha256.as_deref(), Some("abc")); + assert_eq!(failed.refs_sha256.as_deref(), Some("abc"), "refs hash kept"); store.record_replication_ok("r1", None).await.expect("ok"); let healed = store @@ -163,41 +201,7 @@ mod tests { .await .expect("get") .expect("row"); - assert_eq!(healed.last_error, None); - assert_eq!(healed.refs_sha256.as_deref(), Some("abc")); - } - - #[tokio::test] - async fn list_is_ordered_and_delete_removes_rows() { - let (_dir, store) = store().await; - store.record_replication_ok("db", None).await.expect("db"); - store - .record_replication_error("b-repo", "x") - .await - .expect("b"); - store - .record_replication_error("a-repo", "y") - .await - .expect("a"); - let subjects: Vec = store - .list_replication_status() - .await - .expect("list") - .into_iter() - .map(|s| s.subject) - .collect(); - assert_eq!(subjects, ["a-repo", "b-repo", "db"]); - - store - .delete_replication_status("a-repo") - .await - .expect("delete"); - assert!( - store - .replication_status("a-repo") - .await - .expect("get") - .is_none() - ); + assert_eq!(healed.last_error, None, "success clears the error"); + assert_eq!(healed.refs_sha256.as_deref(), Some("abc"), "refs hash kept"); } } diff --git a/crates/forge-core/src/store/repo_listing.rs b/crates/forge-core/src/store/repo_listing.rs index dfcfccf..dded9db 100644 --- a/crates/forge-core/src/store/repo_listing.rs +++ b/crates/forge-core/src/store/repo_listing.rs @@ -237,9 +237,7 @@ mod tests { .id } - #[tokio::test] - async fn strangers_and_anonymous_viewers_never_see_private_repos() { - let fx = fixture().await; + async fn strangers_and_anonymous_viewers_never_see_private_repos(fx: &Fixture) { let stranger = fx .store .create_user("mira", None, "h", false) @@ -254,9 +252,7 @@ mod tests { ); } - #[tokio::test] - async fn a_collaborator_sees_the_private_repo_they_work_on() { - let fx = fixture().await; + async fn a_collaborator_sees_the_private_repo_they_work_on(fx: &Fixture) { let carol = fx .store .create_user("carol", None, "h", false) @@ -274,9 +270,7 @@ mod tests { ); } - #[tokio::test] - async fn an_admin_sees_everything() { - let fx = fixture().await; + async fn an_admin_sees_everything(fx: &Fixture) { let admin = fx.store.create_user("root", None, "h", true).await.unwrap(); let scope = RepoScope::OwnedBy(&fx.owner.id); assert_eq!( @@ -308,13 +302,11 @@ mod tests { } } - #[tokio::test] - async fn org_roles_match_org_repo_role() { - let fx = fixture().await; + async fn org_roles_match_org_repo_role(fx: &Fixture) { let org = fx.store.create_org("acme", &fx.owner.id).await.unwrap(); let member = fx .store - .create_user("mira", None, "h", false) + .create_user("ines", None, "h", false) .await .unwrap(); let outsider = fx.store.create_user("zed", None, "h", false).await.unwrap(); @@ -369,14 +361,8 @@ mod tests { check(&outsider, &[]).await; } - #[tokio::test] - async fn stars_scope_filters_search_and_pages() { - let fx = fixture().await; - let fan = fx - .store - .create_user("mira", None, "h", false) - .await - .unwrap(); + async fn stars_scope_filters_search_and_pages(fx: &Fixture) { + let fan = fx.store.create_user("fan", None, "h", false).await.unwrap(); for name in ["open", "closed"] { let id = repo_id(&fx.store, "tahinli", name).await; fx.store.star(&fan.id, &id).await.unwrap(); @@ -406,4 +392,16 @@ mod tests { assert_eq!(rows.len(), 1); assert_eq!(rows[0].0.name, "open"); } + + // The listing rules share one fixture; each check only adds what it needs, + // and the collaborator grant runs before the later checks. + #[tokio::test] + async fn repo_visibility_and_listing_rules() { + let fx = fixture().await; + strangers_and_anonymous_viewers_never_see_private_repos(&fx).await; + a_collaborator_sees_the_private_repo_they_work_on(&fx).await; + an_admin_sees_everything(&fx).await; + org_roles_match_org_repo_role(&fx).await; + stars_scope_filters_search_and_pages(&fx).await; + } } diff --git a/crates/forge-core/src/store/signatures.rs b/crates/forge-core/src/store/signatures.rs index 6e0e369..e6d59eb 100644 --- a/crates/forge-core/src/store/signatures.rs +++ b/crates/forge-core/src/store/signatures.rs @@ -323,37 +323,48 @@ mod tests { } #[tokio::test] - async fn gpg_key_round_trip_and_duplicate_fingerprint() { + async fn gpg_keys_add_list_and_delete_only_for_owner() { let (store, tahinli, _, _dir) = setup().await; let emails = vec!["tahinli@example.com".to_string()]; let key = store .add_gpg_key(&tahinli, "ABCD", "-----BEGIN-----", &emails) .await .unwrap(); - assert_eq!(key.emails, emails); + assert_eq!(key.emails, emails, "emails kept"); let listed = store.list_gpg_keys(&tahinli).await.unwrap(); - assert_eq!(listed, vec![key.clone()]); - assert_eq!(store.all_gpg_keys().await.unwrap().len(), 1); + assert_eq!(listed, vec![key.clone()], "listed once"); + assert_eq!( + store.all_gpg_keys().await.unwrap().len(), + 1, + "one key overall" + ); let dup = store.add_gpg_key(&tahinli, "ABCD", "x", &emails).await; - assert!(matches!(dup, Err(Error::Conflict(_)))); - } + assert!( + matches!(dup, Err(Error::Conflict(_))), + "duplicate fingerprint: {dup:?}" + ); - #[tokio::test] - async fn delete_gpg_key_only_for_owner() { - let (store, tahinli, _, _dir) = setup().await; + // Another user cannot delete the key; the owner can. let bob = store.create_user("bob", None, "hash", false).await.unwrap(); - let key = store.add_gpg_key(&tahinli, "ABCD", "x", &[]).await.unwrap(); assert!( store .delete_gpg_key(&bob.id, &key.id) .await .unwrap() - .is_none() + .is_none(), + "not the owner" + ); + assert_eq!( + store.list_gpg_keys(&tahinli).await.unwrap().len(), + 1, + "still there" ); - assert_eq!(store.list_gpg_keys(&tahinli).await.unwrap().len(), 1); let removed = store.delete_gpg_key(&tahinli, &key.id).await.unwrap(); - assert_eq!(removed, Some(key)); - assert!(store.list_gpg_keys(&tahinli).await.unwrap().is_empty()); + assert_eq!(removed, Some(key), "owner removes it"); + assert!( + store.list_gpg_keys(&tahinli).await.unwrap().is_empty(), + "list empty" + ); } #[tokio::test] @@ -411,11 +422,31 @@ mod tests { SigStatus::UnknownKey, SigStatus::Bad, ] { - assert_eq!(s.as_str().parse::().unwrap(), s); + assert_eq!( + s.as_str().parse::().unwrap(), + s, + "round trip {s:?}" + ); } for k in [SigKind::Ssh, SigKind::Gpg] { - assert_eq!(k.as_str().parse::().unwrap(), k); + assert_eq!( + k.as_str().parse::().unwrap(), + k, + "round trip {k:?}" + ); + } + // Stored in the signature cache, so the literals must not change. + for (s, literal) in [ + (SigStatus::Verified, "verified"), + (SigStatus::Unverified, "unverified"), + (SigStatus::Unsigned, "unsigned"), + (SigStatus::UnknownKey, "unknown_key"), + (SigStatus::Bad, "bad"), + ] { + assert_eq!(s.as_str(), literal, "stored literal of {s:?}"); } + assert_eq!(SigKind::Ssh.as_str(), "ssh", "stored literal of ssh"); + assert_eq!(SigKind::Gpg.as_str(), "gpg", "stored literal of gpg"); assert!("nope".parse::().is_err()); } } diff --git a/crates/forge-core/src/store/snippets.rs b/crates/forge-core/src/store/snippets.rs index 7131738..b001e1a 100644 --- a/crates/forge-core/src/store/snippets.rs +++ b/crates/forge-core/src/store/snippets.rs @@ -492,7 +492,24 @@ mod tests { #[test] fn visibility_names_round_trip() { for v in SnippetVisibility::ALL { - assert_eq!(SnippetVisibility::parse(v.as_str()), Some(v)); + assert_eq!( + SnippetVisibility::parse(v.as_str()), + Some(v), + "round trip {v:?}" + ); + } + // The stored literals: changing one breaks saved snippets. + for (v, literal) in [ + (SnippetVisibility::Public, "public"), + (SnippetVisibility::Unlisted, "unlisted"), + (SnippetVisibility::Private, "private"), + ] { + assert_eq!(v.as_str(), literal, "stored literal of {v:?}"); + assert_eq!( + SnippetVisibility::parse(literal), + Some(v), + "parse {literal}" + ); } assert_eq!(SnippetVisibility::parse("secret"), None); } diff --git a/crates/forge-core/src/store/wiki.rs b/crates/forge-core/src/store/wiki.rs index 9f414b1..4a98090 100644 --- a/crates/forge-core/src/store/wiki.rs +++ b/crates/forge-core/src/store/wiki.rs @@ -44,7 +44,7 @@ mod tests { use crate::model::Visibility; #[tokio::test] - async fn wiki_is_on_by_default_and_can_be_switched() { + async fn wiki_is_on_by_default_can_be_switched_and_unknown_repo_is_not_found() { let dir = tempfile::tempdir().unwrap(); let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); let tahinli = store @@ -55,26 +55,28 @@ mod tests { .create_repo(&tahinli.id, "proj", "", Visibility::Public, "main") .await .unwrap(); - assert!(store.wiki_enabled(&repo.id).await.unwrap()); + assert!(store.wiki_enabled(&repo.id).await.unwrap(), "on by default"); store.set_wiki_enabled(&repo.id, false).await.unwrap(); - assert!(!store.wiki_enabled(&repo.id).await.unwrap()); + assert!(!store.wiki_enabled(&repo.id).await.unwrap(), "switched off"); store.set_wiki_enabled(&repo.id, true).await.unwrap(); - assert!(store.wiki_enabled(&repo.id).await.unwrap()); - } + assert!(store.wiki_enabled(&repo.id).await.unwrap(), "switched on"); - #[tokio::test] - async fn unknown_repo_is_not_found() { - let dir = tempfile::tempdir().unwrap(); - let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); - assert!(matches!( - store.wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV").await, - Err(Error::NotFound) - )); - assert!(matches!( - store - .set_wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV", false) - .await, - Err(Error::NotFound) - )); + // An id that is no repo is not found, for read and for write. + assert!( + matches!( + store.wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV").await, + Err(Error::NotFound) + ), + "unknown repo read" + ); + assert!( + matches!( + store + .set_wiki_enabled("01ARZ3NDEKTSV4RRFFQ69G5FAV", false) + .await, + Err(Error::NotFound) + ), + "unknown repo write" + ); } }