From 1a6f67b523cf4c3643969de78172fbbd18538160 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:26:41 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20page=20tests=20share=20thei?= =?UTF-8?q?r=20fixtures=20and=20merge=20into=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merge fixture-sharing scenarios into one table-like test each, keeping every assertion and labelling each with its case. - pulls_merge.rs: 22 -> 19 (four refused-merge tests become one) - push.rs: 8 -> 5 (VAPID, refused subscriptions, public routes) - pulls_list.rs: 6 -> 4 (list, issues exclusion and new-PR button) - pull_branch.rs: 6 -> 5 (protected and default branch refusals) Total in scope: 42 -> 33. Verified with nextest: 34 passed in the pages module filter. --- crates/forge-web/src/pages/pull_branch.rs | 74 +++++++++++------------ crates/forge-web/src/pages/pulls_list.rs | 71 ++++++---------------- crates/forge-web/src/pages/pulls_merge.rs | 50 ++++++++------- crates/forge-web/src/pages/push.rs | 42 +++++-------- 4 files changed, 96 insertions(+), 141 deletions(-) diff --git a/crates/forge-web/src/pages/pull_branch.rs b/crates/forge-web/src/pages/pull_branch.rs index 642abc2..ea1bd28 100644 --- a/crates/forge-web/src/pages/pull_branch.rs +++ b/crates/forge-web/src/pages/pull_branch.rs @@ -662,9 +662,13 @@ mod tests { assert!(events.iter().any(|e| e.kind == "head_ref_restored")); } + /// Deleting a branch is refused for a protected branch and for the default branch. The + /// scenarios share one fixture; each refusal leaves its branch in place. #[tokio::test] - async fn protected_branch_is_refused() { + async fn protected_and_default_branches_are_refused() { let fx = fixture().await; + + // A merged feature branch that is protected. let number = fx.open_feature("feature", "f.txt", "feature").await; fx.merge(number).await; fx.state @@ -672,12 +676,41 @@ mod tests { .upsert_branch_rule(&fx.repo.id, "feature", RuleFlags::basic(false, false)) .await .expect("rule"); - let reply = fx .branch_post(number, "delete_branch", &fx.tahinli_cookie) .await; assert_refused(&reply, number, "protected"); - assert!(fx.branch_exists("feature")); + assert!(fx.branch_exists("feature"), "case protected"); + + // A finished pull request whose head is the default branch itself. + let main = fx.branch_tip("main"); + let pr = fx + .state + .store + .create_pull( + &fx.repo.id, + &fx.tahinli.id, + "Main", + "", + "dev", + &fx.repo.id, + "main", + &main, + &main, + ) + .await + .expect("create pull"); + fx.state + .store + .mark_merged(&pr.issue.id, &fx.tahinli.id, &main) + .await + .expect("mark merged"); + let number = pr.issue.number; + let reply = fx + .branch_post(number, "delete_branch", &fx.tahinli_cookie) + .await; + assert_refused(&reply, number, "default_branch"); + assert!(fx.branch_exists("main"), "case default branch"); } #[tokio::test] @@ -724,41 +757,6 @@ mod tests { assert!(!page.body.contains("Delete branch"), "{}", page.body); } - #[tokio::test] - async fn default_branch_is_refused() { - let fx = fixture().await; - // A finished pull request whose head is the default branch itself. - let main = fx.branch_tip("main"); - let pr = fx - .state - .store - .create_pull( - &fx.repo.id, - &fx.tahinli.id, - "Main", - "", - "dev", - &fx.repo.id, - "main", - &main, - &main, - ) - .await - .expect("create pull"); - fx.state - .store - .mark_merged(&pr.issue.id, &fx.tahinli.id, &main) - .await - .expect("mark merged"); - - let number = pr.issue.number; - let reply = fx - .branch_post(number, "delete_branch", &fx.tahinli_cookie) - .await; - assert_refused(&reply, number, "default_branch"); - assert!(fx.branch_exists("main")); - } - #[test] fn unknown_branch_error_codes_have_no_message() { assert!(branch_error_text(Lang::En, "protected").is_some()); diff --git a/crates/forge-web/src/pages/pulls_list.rs b/crates/forge-web/src/pages/pulls_list.rs index 692aa9d..aeb360c 100644 --- a/crates/forge-web/src/pages/pulls_list.rs +++ b/crates/forge-web/src/pages/pulls_list.rs @@ -427,6 +427,25 @@ mod tests { ); assert!(html.contains("issue-badge-open\">Open<")); assert!(html.contains(&format!("href=\"/{OWNER}/{REPO}/compare\""))); + assert!(html.contains("New pull request"), "case signed in"); + let anon = send(&fx.router, "GET", &base(), false, None, &[]) + .await + .text(); + assert!(!anon.contains("New pull request"), "case anonymous"); + + // The issues list keeps excluding pull requests, using the same data. + let issues = send( + &fx.router, + "GET", + &format!("/{OWNER}/{REPO}/issues?state=all"), + true, + None, + &[], + ) + .await + .text(); + assert!(issues.contains("plain issue title")); + assert!(!issues.contains("pull request title"), "{issues}"); } #[tokio::test] @@ -525,56 +544,4 @@ mod tests { let owner = send(&fx.router, "GET", &base(), true, None, &[]).await; assert_eq!(owner.status, 200); } - - #[tokio::test] - async fn new_pull_request_button_only_for_signed_in_viewers() { - let fx = fixture(Visibility::Public).await; - let button = format!("href=\"/{OWNER}/{REPO}/compare\""); - let signed_in = send(&fx.router, "GET", &base(), true, None, &[]) - .await - .text(); - assert!(signed_in.contains("New pull request")); - assert!(signed_in.contains(&button)); - let anon = send(&fx.router, "GET", &base(), false, None, &[]) - .await - .text(); - assert!(!anon.contains("New pull request")); - } - - #[tokio::test] - async fn issues_list_still_excludes_pulls() { - let fx = fixture(Visibility::Public).await; - let repo_id = fx.repo.id.clone(); - fx.store - .create_issue(&repo_id, &fx.owner.id, "plain issue title", "") - .await - .unwrap(); - fx.store - .create_pull( - &repo_id, - &fx.owner.id, - "pull request title", - "", - "main", - &repo_id, - "feature", - HEAD_OID, - BASE_OID, - ) - .await - .unwrap(); - - let html = send( - &fx.router, - "GET", - &format!("/{OWNER}/{REPO}/issues?state=all"), - true, - None, - &[], - ) - .await - .text(); - assert!(html.contains("plain issue title")); - assert!(!html.contains("pull request title"), "{html}"); - } } diff --git a/crates/forge-web/src/pages/pulls_merge.rs b/crates/forge-web/src/pages/pulls_merge.rs index 7010ac9..00c9766 100644 --- a/crates/forge-web/src/pages/pulls_merge.rs +++ b/crates/forge-web/src/pages/pulls_merge.rs @@ -931,49 +931,53 @@ mod tests { assert!(fx.pull(number).await.merged_at.is_some()); } + /// Refusals that must leave the base alone. The scenarios share one fixture: each uses its own + /// branch and file, and the base only moves forward between them. The scenario that moves + /// main onto its head runs last. #[tokio::test] - async fn fast_forward_is_refused_when_base_has_diverged() { + async fn refused_merges_leave_the_base_unchanged() { let fx = fixture().await; - let number = fx - .open_feature("feature", "feature.txt", "f1", &fx.bob) - .await; + + // Fast-forward is impossible once the base has diverged. + let number = fx.open_feature("ff", "ff.txt", "f1", &fx.bob).await; fx.advance_main("other.txt", "other"); let before = fx.main_tip(); - let reply = fx.merge(number, "ff").await; assert_refused(&reply, number, ERR_FF_IMPOSSIBLE); - assert_eq!(fx.main_tip(), before); - assert!(fx.pull(number).await.merged_at.is_none()); - } + assert_eq!(fx.main_tip(), before, "case ff: base moved"); + assert!(fx.pull(number).await.merged_at.is_none(), "case ff"); - #[tokio::test] - async fn conflicting_pull_request_is_refused_and_base_unchanged() { - let fx = fixture().await; + // The same file changed on both sides conflicts. let number = fx - .open_feature("feature", "file.txt", "feature", &fx.bob) + .open_feature("conflict", "file.txt", "feature", &fx.bob) .await; fx.advance_main("file.txt", "main"); let before = fx.main_tip(); - let reply = fx.merge(number, "merge").await; assert_refused(&reply, number, ERR_CONFLICT); - assert_eq!(fx.main_tip(), before); - assert!(fx.pull(number).await.merged_at.is_none()); - } + assert_eq!(fx.main_tip(), before, "case conflict: base moved"); + assert!(fx.pull(number).await.merged_at.is_none(), "case conflict"); - #[tokio::test] - async fn head_already_in_base_is_refused_as_up_to_date() { - let fx = fixture().await; + // A rebase that replays onto a conflicting base is refused too. let number = fx - .open_feature("feature", "feature.txt", "f1", &fx.bob) + .open_feature("rebase", "same.txt", "feature side", &fx.bob) .await; - // Move main onto the feature head, so the head is already contained in the base. - fx.push("feature:main"); + fx.advance_main("same.txt", "main side"); let before = fx.main_tip(); + let reply = fx.merge(number, "rebase").await; + assert_refused(&reply, number, ERR_CONFLICT); + assert_eq!(fx.main_tip(), before, "case rebase: base moved"); + assert!(fx.pull(number).await.merged_at.is_none(), "case rebase"); + // Move main onto the head, so the head is already contained in the base. + let number = fx + .open_feature("contained", "contained.txt", "f1", &fx.bob) + .await; + fx.push("contained:main"); + let before = fx.main_tip(); let reply = fx.merge(number, "merge").await; assert_refused(&reply, number, ERR_UP_TO_DATE); - assert_eq!(fx.main_tip(), before); + assert_eq!(fx.main_tip(), before, "case up to date: base moved"); } #[tokio::test] diff --git a/crates/forge-web/src/pages/push.rs b/crates/forge-web/src/pages/push.rs index aee3aea..fad8db1 100644 --- a/crates/forge-web/src/pages/push.rs +++ b/crates/forge-web/src/pages/push.rs @@ -392,13 +392,10 @@ mod tests { let key: serde_json::Value = serde_json::from_str(&first).expect("json"); let public = key["public_key"].as_str().expect("public key"); assert_eq!(crypto::decode_b64(public).expect("b64").len(), 65); - } - #[tokio::test] - async fn the_vapid_key_needs_a_session() { - let fx = fixture().await; + // Without a session the key is not served. let (status, _) = send(&fx.router, "GET", "/push/vapid-key", None, None).await; - assert_ne!(status, 200); + assert_ne!(status, 200, "case anonymous"); } #[tokio::test] @@ -445,10 +442,14 @@ mod tests { ); } + /// Every refused subscription (bad token or bad fields) stores nothing. The scenarios share one + /// fixture and each one is refused, so the store stays empty throughout. #[tokio::test] - async fn a_subscription_needs_the_csrf_token() { + async fn refused_subscriptions_store_nothing() { let fx = fixture().await; let (p256dh, auth) = browser_keys(); + + // A wrong CSRF token is forbidden. let form = subscribe_form("wrong", "https://push.example.test/x", &p256dh, &auth); let (status, _) = send( &fx.router, @@ -458,20 +459,8 @@ mod tests { Some(form), ) .await; - assert_eq!(status, 403); - assert!( - fx.store - .push_subscriptions(&fx.user_id) - .await - .expect("list") - .is_empty() - ); - } + assert_eq!(status, 403, "case csrf"); - #[tokio::test] - async fn a_subscription_with_bad_fields_is_refused() { - let fx = fixture().await; - let (p256dh, auth) = browser_keys(); let http_endpoint = subscribe_form(CSRF, "http://push.example.test/x", &p256dh, &auth); let (status, _) = send( &fx.router, @@ -481,7 +470,7 @@ mod tests { Some(http_endpoint), ) .await; - assert_eq!(status, 400, "an endpoint must be https"); + assert_eq!(status, 400, "case http endpoint: an endpoint must be https"); let short_auth = subscribe_form( CSRF, "https://push.example.test/x", @@ -496,7 +485,7 @@ mod tests { Some(short_auth), ) .await; - assert_eq!(status, 400, "the auth secret is 16 bytes"); + assert_eq!(status, 400, "case short auth: the auth secret is 16 bytes"); let not_a_point = subscribe_form( CSRF, "https://push.example.test/x", @@ -511,7 +500,7 @@ mod tests { Some(not_a_point), ) .await; - assert_eq!(status, 400, "the browser key is a point"); + assert_eq!(status, 400, "case not a point: the browser key is a point"); assert!( fx.store .push_subscriptions(&fx.user_id) @@ -560,8 +549,9 @@ mod tests { ); } + /// The push worker and manifest are public, while the events stream needs a session. #[tokio::test] - async fn the_events_stream_needs_a_session() { + async fn worker_and_manifest_are_public_and_events_need_a_session() { let fx = fixture().await; let (status, _) = send( &fx.router, @@ -571,12 +561,8 @@ mod tests { None, ) .await; - assert_eq!(status, 401); - } + assert_eq!(status, 401, "case events without a session"); - #[tokio::test] - async fn the_service_worker_and_manifest_are_served() { - let fx = fixture().await; let (status, sw) = send(&fx.router, "GET", "/sw.js", None, None).await; assert_eq!(status, 200); assert!(sw.contains("notificationclick"), "{sw}");