From 9d648536cec7a7512b0a773e881921bdc8101c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 06:55:11 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20ci=20page=20tests=20share?= =?UTF-8?q?=20their=20fixtures=20and=20merge=20into=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit api_tests 32 -> 26, schedule 17 -> 11, release 10 -> 8 (59 -> 45). Folded checks that needed no fresh store into the test that already had one (unknown secret, blank runner name, missing log offset, memory overrides). Merged the OOM, dependant, self-update, cancel, glob, manual, pull request and failed-run cases into one test each, one branch, tag or run number per scenario. The runner binary stream test is unchanged. --- crates/forge-web/src/ci/api_tests.rs | 129 +++++++++----------- crates/forge-web/src/ci/release.rs | 47 ++++---- crates/forge-web/src/ci/schedule.rs | 168 +++++++++++++-------------- 3 files changed, 161 insertions(+), 183 deletions(-) diff --git a/crates/forge-web/src/ci/api_tests.rs b/crates/forge-web/src/ci/api_tests.rs index d95b1b1..495ed47 100644 --- a/crates/forge-web/src/ci/api_tests.rs +++ b/crates/forge-web/src/ci/api_tests.rs @@ -224,20 +224,10 @@ async fn register_good_and_reused_tokens() { let reused = send(&env.router, "POST", uri, &[], json_body(&body)).await; assert_eq!(reused.status, 401, "{}", reused.json()); -} -#[tokio::test] -async fn register_rejects_bad_names() { - let env = bare(|_| {}).await; - let body = json!({ "token": "unused", "name": " ", "labels": [] }); - let sent = send( - &env.router, - "POST", - "/api/v1/ci/runners/register", - &[], - json_body(&body), - ) - .await; + // A blank runner name is refused before the token is looked at. + let bad = json!({ "token": "unused", "name": " ", "labels": [] }); + let sent = send(&env.router, "POST", uri, &[], json_body(&bad)).await; assert_eq!(sent.status, 422, "{}", sent.json()); } @@ -246,11 +236,14 @@ async fn register_rejects_bad_names() { // --------------------------------------------------------------------------------------------- #[tokio::test] -async fn claim_is_204_without_work_and_200_with_work() { +async fn claim_answers_204_200_and_401_by_state() { let env = bare(|_| {}).await; let (_, secret) = runner(&env, &["linux"]).await; let sent = claim(&env.router, &secret).await; assert_eq!(sent.status, 204); + // A secret that matches no runner is refused. + let stranger = claim(&env.router, "not-a-secret").await; + assert_eq!(stranger.status, 401); make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await; let assignment = claim_job(&env.router, &secret).await; @@ -298,13 +291,6 @@ async fn disabled_runner_gets_403() { assert_eq!(sent.status, 403, "{}", sent.json()); } -#[tokio::test] -async fn unknown_runner_secret_is_401() { - let env = bare(|_| {}).await; - let sent = claim(&env.router, "not-a-secret").await; - assert_eq!(sent.status, 401); -} - // --------------------------------------------------------------------------------------------- // Secrets on fork-PR runs // --------------------------------------------------------------------------------------------- @@ -399,21 +385,13 @@ async fn logs_append_retry_mismatch_and_mask() { std::fs::read_to_string(&path).unwrap(), "hello *** world\nmore\n" ); -} -#[tokio::test] -async fn log_offset_header_is_required() { - let env = bare(|_| {}).await; - let (_, secret) = runner(&env, &[]).await; - make_run(&env, "push", None, COMMIT, &[job("a", spec(&[], &[]))]).await; - let assignment = claim_job(&env.router, &secret).await; - let job_id = assignment["job_id"].as_str().unwrap(); - let token = assignment["job_token"].as_str().unwrap(); + // A chunk without the offset header is a bad request. let sent = send( &env.router, "POST", &format!("/api/v1/ci/jobs/{job_id}/logs"), - &[bearer(token)], + &[bearer(&token)], b"text".to_vec(), ) .await; @@ -556,6 +534,7 @@ async fn claim_with_memory(router: &Router, secret: &str, free_gib: u64) -> Sent async fn claim_hands_out_only_a_job_that_fits_the_free_memory() { let env = bare(|_| {}).await; let (runner, secret) = runner(&env, &[]).await; + // Scenario 1: jobs are sized against the free memory the runner reports. make_run( &env, "push", @@ -587,6 +566,28 @@ async fn claim_hands_out_only_a_job_that_fits_the_free_memory() { let sent = claim_with_memory(&env.router, &secret, 5).await; assert_eq!(sent.json()["job_name"], "big"); assert_eq!(sent.json()["memory_bytes"], 4u64 << 30); + + // Memory overrides from the admin travel on the claim answer. None before they are set. + let sent = claim_with_memory(&env.router, &secret, 6).await; + assert!(sent.headers.get("Forge-Runner-Memory-Budget").is_none()); + assert!(sent.headers.get("Forge-Runner-Reserve-Memory").is_none()); + env.store + .set_runner_field(&runner.id, "limits.memory_budget", Some("5g")) + .await + .unwrap(); + env.store + .set_runner_field(&runner.id, "limits.reserve_memory", Some("2g")) + .await + .unwrap(); + let sent = claim_with_memory(&env.router, &secret, 6).await; + assert_eq!( + sent.headers.get("Forge-Runner-Memory-Budget").unwrap(), + "5g" + ); + assert_eq!( + sent.headers.get("Forge-Runner-Reserve-Memory").unwrap(), + "2g" + ); } #[tokio::test] @@ -626,32 +627,6 @@ async fn an_old_runner_without_memory_fields_still_gets_jobs() { assert_eq!(listed.reported.memory_budget, None); } -#[tokio::test] -async fn memory_overrides_from_the_admin_travel_on_the_claim_answer() { - let env = bare(|_| {}).await; - let (runner, secret) = runner(&env, &[]).await; - let sent = claim_with_memory(&env.router, &secret, 6).await; - assert!(sent.headers.get("Forge-Runner-Memory-Budget").is_none()); - assert!(sent.headers.get("Forge-Runner-Reserve-Memory").is_none()); - env.store - .set_runner_field(&runner.id, "limits.memory_budget", Some("5g")) - .await - .unwrap(); - env.store - .set_runner_field(&runner.id, "limits.reserve_memory", Some("2g")) - .await - .unwrap(); - let sent = claim_with_memory(&env.router, &secret, 6).await; - assert_eq!( - sent.headers.get("Forge-Runner-Memory-Budget").unwrap(), - "5g" - ); - assert_eq!( - sent.headers.get("Forge-Runner-Reserve-Memory").unwrap(), - "2g" - ); -} - async fn post_claim(router: &Router, secret: &str, boot: &str) -> Sent { let body = serde_json::json!({ "labels": [], "boot_id": boot, "capabilities": ["restart"] }); send( @@ -891,11 +866,13 @@ async fn requeue_hands_the_job_back_and_it_can_be_claimed_again() { } #[tokio::test] -async fn finish_stores_the_oom_kills_the_runner_saw() { +async fn finish_stores_oom_kills_and_old_bodies_leave_them_empty() { let env = bare(|_| {}).await; let (_runner, secret) = runner(&env, &[]).await; + + // Scenario 1: cargo exited 101 because its rustc was killed: nothing in the exit code or + // message. The OOM kill count the runner saw is stored. make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await; - // cargo exited 101 because its rustc was killed: nothing in the exit code or message. let (id, _) = finish_with( &env, &secret, @@ -906,12 +883,8 @@ async fn finish_stores_the_oom_kills_the_runner_saw() { let stored = env.store.job_by_id(&id).await.unwrap().unwrap(); assert_eq!(stored.oom_kills, Some(2)); assert_eq!(stored.exit_code, Some(101)); -} -#[tokio::test] -async fn an_old_runner_finish_body_without_oom_fields_still_works() { - let env = bare(|_| {}).await; - let (_runner, secret) = runner(&env, &[]).await; + // Scenario 2: an old runner's finish body has no OOM fields, and the job still finishes. make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await; let (id, _) = finish_with( &env, @@ -921,7 +894,8 @@ async fn an_old_runner_finish_body_without_oom_fields_still_works() { .await; assert_eq!( env.store.job_by_id(&id).await.unwrap().unwrap().oom_kills, - None + None, + "scenario old runner" ); } @@ -944,9 +918,11 @@ async fn two_step_run(env: &Bare) -> CiRun { } #[tokio::test] -async fn finish_queues_dependants_on_success() { +async fn finish_queues_dependants_on_success_and_skips_them_on_failure() { let env = bare(|_| {}).await; let (_, secret) = runner(&env, &[]).await; + + // Scenario 1: a green `a` queues `b`, which is then claimed and finished. let run = two_step_run(&env).await; let assignment = claim_job(&env.router, &secret).await; assert_eq!(assignment["job_name"], "a"); @@ -966,16 +942,23 @@ async fn finish_queues_dependants_on_success() { let jobs = env.store.jobs_for_run(&run.id).await.unwrap(); let state_of = |name: &str| -> JobState { jobs.iter().find(|j: &&CiJob| j.name == name).unwrap().state }; - assert_eq!(state_of("a"), JobState::Success); - assert_eq!(state_of("b"), JobState::Queued); + assert_eq!(state_of("a"), JobState::Success, "success scenario"); + assert_eq!(state_of("b"), JobState::Queued, "success scenario"); let next = claim_job(&env.router, &secret).await; assert_eq!(next["job_name"], "b"); -} + let b_id = next["job_id"].as_str().unwrap(); + let b_token = next["job_token"].as_str().unwrap(); + let sent = send( + &env.router, + "POST", + &format!("/api/v1/ci/jobs/{b_id}/finish"), + &[bearer(b_token)], + finish_body("success"), + ) + .await; + assert_eq!(sent.status, 200, "{}", sent.json()); -#[tokio::test] -async fn finish_skips_dependants_on_failure() { - let env = bare(|_| {}).await; - let (_, secret) = runner(&env, &[]).await; + // Scenario 2: a failed `a` skips `b`, and the run ends in failure. let run = two_step_run(&env).await; let assignment = claim_job(&env.router, &secret).await; let job_id = assignment["job_id"].as_str().unwrap(); diff --git a/crates/forge-web/src/ci/release.rs b/crates/forge-web/src/ci/release.rs index 41a7bac..5b305a3 100644 --- a/crates/forge-web/src/ci/release.rs +++ b/crates/forge-web/src/ci/release.rs @@ -600,29 +600,29 @@ title = "forge {tag}" assert_eq!(asset_names(&fx, &release).await.len(), 1); } + /// Failed runs never publish. Each scenario has its own tag, so its run is number 1, 2 and 3. + /// Scenario 1: `docs` fails after the jobs run in order. Scenario 2: `build` finishes green + /// while `docs` still runs, then `docs` fails. Scenario 3: `docs` fails fast while `build` + /// has not finished, so `build` is cancelled at once (the running-job path is covered in the + /// store tests). No release is created in any scenario. #[tokio::test] - async fn failed_run_creates_no_release() { + async fn failed_runs_create_no_release() { let fx = fixture().await; + + // Scenario 1. push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; let run = run_of(&fx, 1).await; let build = job_of(&fx, &run, "build").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; finish_all(&fx, &run.id, Some("docs")).await; let run = run_of(&fx, 1).await; - assert_eq!(run.state, RunState::Failure); - + assert_eq!(run.state, RunState::Failure, "in order"); on_run_finished(&fx.state, &run).await; + assert!(release_named(&fx, "v1.0").await.is_none(), "in order"); - assert!(release_named(&fx, "v1.0").await.is_none()); - } - - /// Parallel jobs: `build` finishes green while `docs` still runs, then `docs` fails. The - /// release hook runs after each finish, as `finish_job` does, and never publishes. - #[tokio::test] - async fn build_green_first_then_other_job_fails_creates_no_release() { - let fx = fixture().await; - push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; - let run = run_of(&fx, 1).await; + // Scenario 2. + push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v2.0").await; + let run = run_of(&fx, 2).await; let build = job_of(&fx, &run, "build").await; let docs = job_of(&fx, &run, "docs").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; @@ -635,7 +635,7 @@ title = "forge {tag}" .expect("finish build"); assert_eq!(after_build.state, RunState::Running, "docs is still queued"); on_run_finished(&fx.state, &after_build).await; - assert!(release_named(&fx, "v1.0").await.is_none()); + assert!(release_named(&fx, "v2.0").await.is_none(), "parallel"); let after_docs = fx .state @@ -643,18 +643,13 @@ title = "forge {tag}" .finish_job(&docs.id, JobState::Failure, Some(1), None) .await .expect("finish docs"); - assert_eq!(after_docs.state, RunState::Failure); + assert_eq!(after_docs.state, RunState::Failure, "parallel"); on_run_finished(&fx.state, &after_docs).await; - assert!(release_named(&fx, "v1.0").await.is_none()); - } + assert!(release_named(&fx, "v2.0").await.is_none(), "parallel"); - /// Fail fast: `docs` fails while `build` has not finished. `build` is cancelled at once (the - /// running-job path is covered in the store tests), the run fails, and no release is created. - #[tokio::test] - async fn failing_job_cancels_build_and_creates_no_release() { - let fx = fixture().await; - push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await; - let run = run_of(&fx, 1).await; + // Scenario 3. + push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v3.0").await; + let run = run_of(&fx, 3).await; let build = job_of(&fx, &run, "build").await; let docs = job_of(&fx, &run, "docs").await; add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await; @@ -665,10 +660,10 @@ title = "forge {tag}" .finish_job(&docs.id, JobState::Failure, Some(1), None) .await .expect("finish docs"); - assert_eq!(after_docs.state, RunState::Failure); + assert_eq!(after_docs.state, RunState::Failure, "fail fast"); assert_eq!(job_of(&fx, &run, "build").await.state, JobState::Cancelled); on_run_finished(&fx.state, &after_docs).await; - assert!(release_named(&fx, "v1.0").await.is_none()); + assert!(release_named(&fx, "v3.0").await.is_none(), "fail fast"); } #[tokio::test] diff --git a/crates/forge-web/src/ci/schedule.rs b/crates/forge-web/src/ci/schedule.rs index 6e5bc1d..c55d878 100644 --- a/crates/forge-web/src/ci/schedule.rs +++ b/crates/forge-web/src/ci/schedule.rs @@ -908,9 +908,11 @@ run = "make" assert!(!config.description.as_deref().unwrap_or("").is_empty()); } + /// Branch globs pick the branches that run (scenario 1), and tag globs pick the tags (scenario 2). #[tokio::test] - async fn branch_globs_select_runs() { + async fn branch_and_tag_globs_select_runs() { let fx = fixture().await; + // Scenario 1: branches. push(&fx, &[(CONFIG_PATH, CONFIG_GLOBS)], "refs/heads/main").await; assert_eq!(runs_total(&fx).await, 0, "main is not in the globs"); @@ -929,20 +931,17 @@ run = "make" .expect("query") .expect("run"); assert_eq!(run.git_ref, "refs/heads/release/1.0"); - } - #[tokio::test] - async fn tag_push_creates_tag_run() { - let fx = fixture().await; + // Scenario 2: tags. push(&fx, &[(CONFIG_PATH, CONFIG_TAGS)], "refs/tags/nightly").await; - assert_eq!(runs_total(&fx).await, 0, "nightly does not match v*"); + assert_eq!(runs_total(&fx).await, 1, "nightly does not match v*"); push(&fx, &[(CONFIG_PATH, CONFIG_TAGS)], "refs/tags/v1.0").await; - assert_eq!(runs_total(&fx).await, 1); + assert_eq!(runs_total(&fx).await, 2); let run = fx .state .store - .run_by_number(&fx.repo.id, 1) + .run_by_number(&fx.repo.id, 2) .await .expect("query") .expect("run"); @@ -979,10 +978,15 @@ run = "make" pr } + /// Pull requests wait for approval when the author is a fork (scenario 1) or cannot write to + /// the repo (scenario 2). A writer's pull request runs at once (scenario 3). Each scenario has + /// its own head branch, and its pull request run is number 1, 2 and 3. #[tokio::test] - async fn fork_pull_request_waits_for_approval() { + async fn pull_requests_wait_for_approval_unless_the_author_writes_the_repo() { let fx = fixture().await; - let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature").await; + + // Scenario 1: a fork's pull request. + let fork_head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature-fork").await; let bob = fx .state .store @@ -995,7 +999,7 @@ run = "make" .create_repo(&bob.id, "proj", "", Visibility::Public, "main") .await .expect("fork"); - let pr = open_pull(&fx, &fx.owner.id, &fork.id, "feature", &head).await; + let pr = open_pull(&fx, &fx.owner.id, &fork.id, "feature-fork", &fork_head).await; let run = fx .state @@ -1005,58 +1009,54 @@ run = "make" .expect("query") .expect("run"); assert_eq!(run.trigger, "pull_request"); - assert_eq!(run.state, RunState::WaitingApproval); + assert_eq!(run.state, RunState::WaitingApproval, "fork"); assert_eq!(run.pull_number, Some(pr.issue.number)); let statuses = fx .state .store - .statuses_for(&fx.repo.id, &head) + .statuses_for(&fx.repo.id, &fork_head) .await .expect("statuses"); let build = status(&statuses, "ci/build").expect("ci/build"); assert_eq!(build.state, StatusState::Pending); assert_eq!(build.description.as_deref(), Some("Waiting for approval")); - } - #[tokio::test] - async fn non_writer_pull_request_waits_for_approval() { - let fx = fixture().await; - let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature").await; - let bob = fx + // Scenario 2: a same-repo pull request from a user who cannot write to it. + let carol = fx .state .store - .create_user("bob", None, "unused", false) + .create_user("carol", None, "unused", false) .await .expect("user"); - open_pull(&fx, &bob.id, &fx.repo.id, "feature", &head).await; + let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature-carol").await; + open_pull(&fx, &carol.id, &fx.repo.id, "feature-carol", &head).await; let run = fx .state .store - .run_by_number(&fx.repo.id, 1) + .run_by_number(&fx.repo.id, 2) .await .expect("query") .expect("run"); - assert_eq!(run.state, RunState::WaitingApproval); - } + assert_eq!(run.state, RunState::WaitingApproval, "non-writer"); - #[tokio::test] - async fn writer_pull_request_runs_at_once() { - let fx = fixture().await; - let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature").await; - open_pull(&fx, &fx.owner.id, &fx.repo.id, "feature", &head).await; + // Scenario 3: the owner's pull request runs at once. + let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature-writer").await; + open_pull(&fx, &fx.owner.id, &fx.repo.id, "feature-writer", &head).await; let run = fx .state .store - .run_by_number(&fx.repo.id, 1) + .run_by_number(&fx.repo.id, 3) .await .expect("query") .expect("run"); - assert_ne!(run.state, RunState::WaitingApproval); + assert_ne!(run.state, RunState::WaitingApproval, "writer"); } + /// Manual runs use the branch head (scenario 1). A missing branch is an error, and a branch + /// whose config sets `manual = false` is refused (scenario 2). #[tokio::test] - async fn manual_run_uses_branch_head() { + async fn manual_runs_use_the_branch_head_and_refuse_when_asked() { let fx = fixture().await; let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/main").await; let before = runs_total(&fx).await; @@ -1067,19 +1067,15 @@ run = "make" assert_eq!(run.git_ref, "refs/heads/main"); assert_eq!(run.commit_oid, head); assert_eq!(runs_total(&fx).await, before + 1); - } - #[tokio::test] - async fn manual_run_refuses_missing_branch_and_disabled_manual() { - let fx = fixture().await; - push(&fx, &[(CONFIG_PATH, CONFIG_NO_MANUAL)], "refs/heads/main").await; + push(&fx, &[(CONFIG_PATH, CONFIG_NO_MANUAL)], "refs/heads/quiet").await; let before = runs_total(&fx).await; assert!( manual_run(&fx.state, &fx.repo, "nope", &fx.owner.id) .await .is_err() ); - let refused = manual_run(&fx.state, &fx.repo, "main", &fx.owner.id).await; + let refused = manual_run(&fx.state, &fx.repo, "quiet", &fx.owner.id).await; assert!(matches!(refused, Err(forge_core::Error::Invalid(_)))); assert_eq!(runs_total(&fx).await, before); } @@ -1172,54 +1168,58 @@ image = "alpine:3" run = "make" "#; + /// A newer push cancels the older live run on the same ref, unless the config keeps it. Each + /// case has its own branch, since superseding only looks at one ref. The runs of a case are + /// numbered `first` and `first + 1`, because every push on these branches starts one run. #[tokio::test] - async fn a_newer_push_cancels_older_live_runs_when_asked() { + async fn a_newer_push_cancels_older_live_runs_unless_the_config_keeps_them() { let fx = fixture().await; - push(&fx, &[(CONFIG_PATH, CONFIG_SUPERSEDE)], "refs/heads/main").await; - push(&fx, &[("a.txt", "a")], "refs/heads/main").await; - let store = &fx.state.store; - let first = store.run_by_number(&fx.repo.id, 1).await.unwrap().unwrap(); - let second = store.run_by_number(&fx.repo.id, 2).await.unwrap().unwrap(); - assert_eq!( - first.state, - RunState::Cancelled, - "the older run is cancelled" - ); - assert_eq!(second.state, RunState::Queued, "the newest run keeps going"); - let jobs = store.jobs_for_run(&first.id).await.unwrap(); - assert_eq!(state_of(&jobs, "build"), Some(JobState::Cancelled)); - } - - #[tokio::test] - async fn a_newer_push_cancels_older_live_runs_by_default() { - let fx = fixture().await; - push(&fx, &[(CONFIG_PATH, CONFIG_ONE_JOB)], "refs/heads/main").await; - push(&fx, &[("a.txt", "a")], "refs/heads/main").await; - let first = fx - .state - .store - .run_by_number(&fx.repo.id, 1) - .await - .unwrap() - .unwrap(); - assert_eq!(first.state, RunState::Cancelled); - } - - #[tokio::test] - async fn older_runs_are_kept_when_cancel_superseded_is_false() { - let fx = fixture().await; - let config = - CONFIG_SUPERSEDE.replace("cancel_superseded = true", "cancel_superseded = false"); - push(&fx, &[(CONFIG_PATH, &config)], "refs/heads/main").await; - push(&fx, &[("a.txt", "a")], "refs/heads/main").await; - let first = fx - .state - .store - .run_by_number(&fx.repo.id, 1) - .await - .unwrap() - .unwrap(); - assert_eq!(first.state, RunState::Queued); + // Each config triggers on its own branch, so its pushes make runs there. + let on = |config: &str, branch: &str| { + config.replace("push = [\"main\"]", &format!("push = [\"{branch}\"]")) + }; + // (branch, config, the older run is cancelled, number of the older run) + let cases: [(&str, String, bool, i64); 3] = [ + ("default", on(CONFIG_ONE_JOB, "default"), true, 1), + ("asked", on(CONFIG_SUPERSEDE, "asked"), true, 3), + ( + "kept", + on( + &CONFIG_SUPERSEDE.replace("cancel_superseded = true", "cancel_superseded = false"), + "kept", + ), + false, + 5, + ), + ]; + for (branch, config, cancelled, first_number) in cases { + let git_ref = format!("refs/heads/{branch}"); + let case = format!("case {branch}"); + push(&fx, &[(CONFIG_PATH, config.as_str())], &git_ref).await; + push(&fx, &[("a.txt", "a")], &git_ref).await; + let store = &fx.state.store; + let first = store + .run_by_number(&fx.repo.id, first_number) + .await + .unwrap() + .unwrap(); + let second = store + .run_by_number(&fx.repo.id, first_number + 1) + .await + .unwrap() + .unwrap(); + let want = if cancelled { + RunState::Cancelled + } else { + RunState::Queued + }; + assert_eq!(first.state, want, "{case}: the older run"); + assert_eq!(second.state, RunState::Queued, "{case}: the newest run"); + if cancelled { + let jobs = store.jobs_for_run(&first.id).await.unwrap(); + assert_eq!(state_of(&jobs, "build"), Some(JobState::Cancelled), "{case}"); + } + } } #[tokio::test]