diff --git a/crates/forge-core/src/store/ci.rs b/crates/forge-core/src/store/ci.rs index 1460536..a90e995 100644 --- a/crates/forge-core/src/store/ci.rs +++ b/crates/forge-core/src/store/ci.rs @@ -2296,17 +2296,16 @@ mod tests { )); } - #[tokio::test] - async fn create_run_numbers_per_repo() { - let fx = fixture().await; + async fn numbering_is_per_repo(fx: &Fx) { + let repo = repo_named(fx, "numbers").await; let other = fx .store .create_repo(&fx.tahinli.id, "other", "", Visibility::Public, "main") .await .unwrap(); let jobs = [job("build", &[], &[]), job("test", &["build"], &[])]; - let r1 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; - let r2 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let r1 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; + let r2 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; let o1 = create(&fx.store, &other.id, &fx.tahinli.id, &jobs, false).await; assert_eq!((r1.number, r2.number, o1.number), (1, 2, 1)); assert_eq!(r1.state, RunState::Queued); @@ -2320,7 +2319,7 @@ mod tests { ); assert_eq!( fx.store - .run_by_number(&fx.repo.id, 2) + .run_by_number(&repo.id, 2) .await .unwrap() .unwrap() @@ -2331,7 +2330,7 @@ mod tests { let broken = fx .store .create_run( - &fx.repo.id, + &repo.id, "push", "refs/heads/main", "abc123", @@ -2348,32 +2347,31 @@ mod tests { assert!(broken.finished_at.is_some()); assert!(fx.store.jobs_for_run(&broken.id).await.unwrap().is_empty()); - let (page, total) = fx.store.list_runs(&fx.repo.id, 1, 2).await.unwrap(); + let (page, total) = fx.store.list_runs(&repo.id, 1, 2).await.unwrap(); assert_eq!(total, 3); assert_eq!( page.iter().map(|r| r.number).collect::>(), vec![3, 2] ); - let (page2, _) = fx.store.list_runs(&fx.repo.id, 2, 2).await.unwrap(); + let (page2, _) = fx.store.list_runs(&repo.id, 2, 2).await.unwrap(); assert_eq!(page2.iter().map(|r| r.number).collect::>(), vec![1]); } - #[tokio::test] - async fn latest_run_on_ref_picks_newest_per_branch() { - let fx = fixture().await; + async fn newest_run_per_ref(fx: &Fx) { + let repo = repo_named(fx, "latest").await; let jobs = [job("build", &[], &[])]; assert!( fx.store - .latest_run_on_ref(&fx.repo.id, "refs/heads/main") + .latest_run_on_ref(&repo.id, "refs/heads/main") .await .unwrap() .is_none() ); - let main1 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let main1 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; let dev = fx .store .create_run( - &fx.repo.id, + &repo.id, "push", "refs/heads/dev", "abc123", @@ -2385,10 +2383,10 @@ mod tests { ) .await .unwrap(); - let main2 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let main2 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; let latest = fx .store - .latest_run_on_ref(&fx.repo.id, "refs/heads/main") + .latest_run_on_ref(&repo.id, "refs/heads/main") .await .unwrap() .unwrap(); @@ -2396,25 +2394,24 @@ mod tests { assert_ne!(latest.id, main1.id); let on_dev = fx .store - .latest_run_on_ref(&fx.repo.id, "refs/heads/dev") + .latest_run_on_ref(&repo.id, "refs/heads/dev") .await .unwrap() .unwrap(); assert_eq!(on_dev.id, dev.id); assert!( fx.store - .latest_run_on_ref(&fx.repo.id, "refs/heads/nope") + .latest_run_on_ref(&repo.id, "refs/heads/nope") .await .unwrap() .is_none() ); } - #[tokio::test] - async fn approval_flow() { - let fx = fixture().await; + async fn approval_holds_the_run(fx: &Fx) { + let repo = repo_named(fx, "approval").await; let jobs = [job("build", &[], &[]), job("test", &["build"], &[])]; - let run = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, true).await; + let run = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, true).await; assert_eq!(run.state, RunState::WaitingApproval); assert_eq!( job_named(&fx.store, &run.id, "build").await.state, @@ -3011,11 +3008,10 @@ mod tests { )); } - #[tokio::test] - async fn rerun_copies_commit_and_specs() { - let fx = fixture().await; + async fn rerun_copies_commit_and_specs(fx: &Fx) { + let repo = repo_named(fx, "rerun").await; let jobs = [job("build", &[], &["linux"]), job("test", &["build"], &[])]; - let first = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let first = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; let again = fx.store.rerun(&first.id, &fx.tahinli.id).await.unwrap(); assert_eq!(again.number, 2); assert_eq!(again.commit_oid, first.commit_oid); @@ -3038,16 +3034,15 @@ mod tests { )); } - #[tokio::test] - async fn partial_reruns_take_failed_jobs_and_their_dependants() { - let fx = fixture().await; + async fn partial_reruns_take_failed_jobs_and_their_dependants(fx: &Fx) { + let repo = repo_named(fx, "partial").await; let jobs = [ job("a", &[], &[]), job("b", &["a"], &[]), job("c", &["b"], &[]), job("d", &[], &[]), ]; - let first = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let first = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; assert!(matches!( fx.store.rerun_failed(&first.id, &fx.tahinli.id).await, Err(Error::Conflict(_)) @@ -3108,11 +3103,10 @@ mod tests { )); } - #[tokio::test] - async fn warnings_are_stored_and_carried_into_reruns() { - let fx = fixture().await; + async fn warnings_are_stored_and_carried_into_reruns(fx: &Fx) { + let repo = repo_named(fx, "warnings").await; let jobs = [job("a", &[], &[])]; - let run = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let run = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; assert!(fx.store.run_warnings(&run.id).await.unwrap().is_empty()); let warnings = vec!["unknown key `on.schedule`".to_owned(), "second".to_owned()]; fx.store.set_run_warnings(&run.id, &warnings).await.unwrap(); @@ -3121,27 +3115,26 @@ mod tests { assert_eq!(fx.store.run_warnings(&again.id).await.unwrap(), warnings); } - #[tokio::test] - async fn superseded_candidates_are_older_live_push_runs_on_the_ref() { - let fx = fixture().await; + async fn superseded_candidates_are_older_live_push_runs_on_the_ref(fx: &Fx) { + let repo = repo_named(fx, "superseded").await; let jobs = [job("a", &[], &[])]; - let first = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; + let first = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; let found = fx .store - .superseded_candidates(&fx.repo.id, "refs/heads/main", "def456") + .superseded_candidates(&repo.id, "refs/heads/main", "def456") .await .unwrap(); assert_eq!(found.len(), 1); assert_eq!(found[0].id, first.id); let same = fx .store - .superseded_candidates(&fx.repo.id, "refs/heads/main", "abc123") + .superseded_candidates(&repo.id, "refs/heads/main", "abc123") .await .unwrap(); assert!(same.is_empty()); let elsewhere = fx .store - .superseded_candidates(&fx.repo.id, "refs/heads/other", "def456") + .superseded_candidates(&repo.id, "refs/heads/other", "def456") .await .unwrap(); assert!(elsewhere.is_empty()); @@ -3228,12 +3221,11 @@ mod tests { assert!(logs2.is_empty() && artifacts2.is_empty()); } - #[tokio::test] - async fn artifacts_are_unique_per_job_and_listed() { - let fx = fixture().await; + async fn artifacts_are_unique_per_job_and_listed(fx: &Fx) { + let repo = repo_named(fx, "artifacts").await; let run = create( &fx.store, - &fx.repo.id, + &repo.id, &fx.tahinli.id, &[job("a", &[], &[])], false, @@ -3260,16 +3252,15 @@ mod tests { ); } - #[tokio::test] - async fn secrets_seal_round_trip_and_list_never_returns_values() { - let fx = fixture().await; + async fn secrets_seal_round_trip_and_list_never_returns_values(fx: &Fx) { + let repo = repo_named(fx, "secrets").await; let plain = "hunter2-very-secret-value"; fx.store - .set_ci_secret(&fx.repo.id, "DEPLOY_KEY", plain.as_bytes()) + .set_ci_secret(&repo.id, "DEPLOY_KEY", plain.as_bytes()) .await .unwrap(); fx.store - .set_ci_secret(&fx.repo.id, "TOKEN", b"second") + .set_ci_secret(&repo.id, "TOKEN", b"second") .await .unwrap(); @@ -3279,7 +3270,7 @@ mod tests { let raw = fetch_opt( &conn, "SELECT sealed FROM ci_secrets WHERE repo_id = ?1 AND name = 'DEPLOY_KEY'", - (fx.repo.id.clone(),), + (repo.id.clone(),), |row| col_blob(row, 0), ) .await @@ -3288,7 +3279,7 @@ mod tests { assert!(!raw.windows(plain.len()).any(|w| w == plain.as_bytes())); } - let names = fx.store.list_ci_secret_names(&fx.repo.id).await.unwrap(); + let names = fx.store.list_ci_secret_names(&repo.id).await.unwrap(); assert_eq!( names.iter().map(|(n, _)| n.as_str()).collect::>(), vec!["DEPLOY_KEY", "TOKEN"] @@ -3299,10 +3290,7 @@ mod tests { let values = fx .store - .ci_secret_values( - &fx.repo.id, - &["DEPLOY_KEY".to_string(), "MISSING".to_string()], - ) + .ci_secret_values(&repo.id, &["DEPLOY_KEY".to_string(), "MISSING".to_string()]) .await .unwrap(); assert_eq!(values.len(), 1); @@ -3310,20 +3298,16 @@ mod tests { // Replacing keeps one row. fx.store - .set_ci_secret(&fx.repo.id, "DEPLOY_KEY", b"rotated") + .set_ci_secret(&repo.id, "DEPLOY_KEY", b"rotated") .await .unwrap(); assert_eq!( - fx.store - .list_ci_secret_names(&fx.repo.id) - .await - .unwrap() - .len(), + fx.store.list_ci_secret_names(&repo.id).await.unwrap().len(), 2 ); let rotated = fx .store - .ci_secret_values(&fx.repo.id, &["DEPLOY_KEY".to_string()]) + .ci_secret_values(&repo.id, &["DEPLOY_KEY".to_string()]) .await .unwrap(); assert_eq!( @@ -3331,30 +3315,40 @@ mod tests { Some("rotated") ); - assert!( - fx.store - .delete_ci_secret(&fx.repo.id, "TOKEN") - .await - .unwrap() - ); - assert!( - !fx.store - .delete_ci_secret(&fx.repo.id, "TOKEN") - .await - .unwrap() - ); + assert!(fx.store.delete_ci_secret(&repo.id, "TOKEN").await.unwrap()); + assert!(!fx.store.delete_ci_secret(&repo.id, "TOKEN").await.unwrap()); assert!(matches!( - fx.store.set_ci_secret(&fx.repo.id, "lower", b"x").await, + fx.store.set_ci_secret(&repo.id, "lower", b"x").await, Err(Error::Invalid(_)) )); assert!(matches!( - fx.store - .set_ci_secret(&fx.repo.id, "BAD", &[0xff, 0xfe]) - .await, + fx.store.set_ci_secret(&repo.id, "BAD", &[0xff, 0xfe]).await, Err(Error::Invalid(_)) )); } + async fn repo_named(fx: &Fx, name: &str) -> Repo { + fx.store + .create_repo(&fx.tahinli.id, name, "", Visibility::Public, "main") + .await + .unwrap() + } + + /// Each scenario works in its own repo and leaves its jobs unclaimed, so they share one store. + #[tokio::test] + async fn repo_scenarios_share_one_store() { + let fx = fixture().await; + numbering_is_per_repo(&fx).await; + newest_run_per_ref(&fx).await; + approval_holds_the_run(&fx).await; + rerun_copies_commit_and_specs(&fx).await; + partial_reruns_take_failed_jobs_and_their_dependants(&fx).await; + warnings_are_stored_and_carried_into_reruns(&fx).await; + superseded_candidates_are_older_live_push_runs_on_the_ref(&fx).await; + artifacts_are_unique_per_job_and_listed(&fx).await; + secrets_seal_round_trip_and_list_never_returns_values(&fx).await; + } + #[tokio::test] async fn secrets_need_the_key() { let dir = tempfile::tempdir().unwrap();