diff --git a/crates/forge-web/src/pages/ci_runs.rs b/crates/forge-web/src/pages/ci_runs.rs index 8030a49..b11b2ac 100644 --- a/crates/forge-web/src/pages/ci_runs.rs +++ b/crates/forge-web/src/pages/ci_runs.rs @@ -2412,32 +2412,26 @@ mod tests { assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]); } + /// Refs and images show their short names; refs outside heads and tags stay whole. #[test] - fn refs_shorten_to_branch_and_tag_names() { - assert_eq!(short_ref("refs/heads/main"), "main"); - assert_eq!(short_ref("refs/tags/v1.2"), "v1.2"); - assert_eq!(short_ref("refs/pull/7/head"), "refs/pull/7/head"); + fn refs_and_images_shorten_to_their_last_part() { + assert_eq!(short_ref("refs/heads/main"), "main", "branch ref"); + assert_eq!(short_ref("refs/tags/v1.2"), "v1.2", "tag ref"); + assert_eq!(short_ref("refs/pull/7/head"), "refs/pull/7/head", "pull ref"); + assert_eq!(image_short("docker.io/library/rust:1"), "rust:1", "registry image"); + assert_eq!(image_short("alpine"), "alpine", "bare image"); } + /// The page scale is the longest run (never zero), and the scale line reads the real span. #[test] - fn images_show_their_last_part() { - assert_eq!(image_short("docker.io/library/rust:1"), "rust:1"); - assert_eq!(image_short("alpine"), "alpine"); - } - - #[test] - fn bar_scale_is_the_longest_run_of_the_page() { - assert_eq!(bar_scale(&[30, 4692, 0]), 4692); - assert_eq!(bar_scale(&[]), 1); - assert_eq!(bar_scale(&[0, 0]), 1); - } - - #[test] - fn scale_line_reads_the_real_span() { - assert_eq!(span_text(5), "5s"); - assert_eq!(span_text(90), "1m 30s"); - assert_eq!(span_text(1200), "20m"); - assert_eq!(span_text(4692), "1h 19m"); + fn scale_is_the_longest_run_and_reads_the_real_span() { + assert_eq!(bar_scale(&[30, 4692, 0]), 4692, "longest run"); + assert_eq!(bar_scale(&[]), 1, "no runs"); + assert_eq!(bar_scale(&[0, 0]), 1, "only zero runs"); + assert_eq!(span_text(5), "5s", "seconds"); + assert_eq!(span_text(90), "1m 30s", "minutes and seconds"); + assert_eq!(span_text(1200), "20m", "whole minutes"); + assert_eq!(span_text(4692), "1h 19m", "hours and minutes"); } #[test] @@ -2464,22 +2458,19 @@ mod tests { ); } + /// The axis ticks are round and few, and places map times onto the track. #[test] - fn axis_ticks_are_round_and_few() { - assert_eq!(axis(4692), (1200, 4800)); - assert_eq!(axis(60), (60, 60)); - assert_eq!(tick_label(0), "0"); - assert_eq!(tick_label(1200), "20m"); - assert_eq!(tick_label(3600), "1h"); - assert_eq!(tick_label(4800), "1h 20m"); - } - - #[test] - fn places_follow_the_span() { - assert_eq!(place(0, 4800, 530), 0); - assert_eq!(place(2160, 4800, 530), 239); - assert_eq!(place(9999, 4800, 530), 530); - assert_eq!(place(5, 0, 530), 0); + fn axis_ticks_are_round_and_places_follow_the_span() { + assert_eq!(axis(4692), (1200, 4800), "long axis"); + assert_eq!(axis(60), (60, 60), "short axis"); + assert_eq!(tick_label(0), "0", "zero tick"); + assert_eq!(tick_label(1200), "20m", "minutes tick"); + assert_eq!(tick_label(3600), "1h", "hour tick"); + assert_eq!(tick_label(4800), "1h 20m", "hour and minutes tick"); + assert_eq!(place(0, 4800, 530), 0, "start of the track"); + assert_eq!(place(2160, 4800, 530), 239, "middle of the track"); + assert_eq!(place(9999, 4800, 530), 530, "past the end is clamped"); + assert_eq!(place(5, 0, 530), 0, "empty span"); } #[test] diff --git a/crates/forge-web/src/pages/code_search.rs b/crates/forge-web/src/pages/code_search.rs index df3a149..3401273 100644 --- a/crates/forge-web/src/pages/code_search.rs +++ b/crates/forge-web/src/pages/code_search.rs @@ -273,7 +273,7 @@ mod tests { } #[test] - fn segments_mark_each_occurrence() { + fn segments_split_around_each_hit() { let segs = segments("Hello hello HELLO", "hello", false); assert_eq!( texts(&segs), @@ -283,21 +283,27 @@ mod tests { ("hello".to_owned(), true), (" ".to_owned(), false), ("HELLO".to_owned(), true), - ] + ], + "every case-insensitive hit is marked" ); - } - - #[test] - fn segments_respect_case_and_unicode() { let segs = segments("Hello hello", "hello", true); assert_eq!( texts(&segs), - vec![("Hello ".to_owned(), false), ("hello".to_owned(), true)] + vec![("Hello ".to_owned(), false), ("hello".to_owned(), true)], + "case-sensitive match skips other case" ); // Non-ASCII text is never split inside a character. let segs = segments("café CAFÉ", "é", false); - assert_eq!(segs.iter().filter(|s| s.hit).count(), 1); - assert_eq!(segments("no match here", "zzz", false).len(), 1); + assert_eq!( + segs.iter().filter(|s| s.hit).count(), + 1, + "unicode is not split inside a character" + ); + assert_eq!( + segments("no match here", "zzz", false).len(), + 1, + "no match is one plain segment" + ); } } @@ -401,12 +407,16 @@ mod route_tests { (status, String::from_utf8_lossy(&bytes).into_owned()) } + /// Every search scenario reads one shared, read-only fixture: results and links, case, + /// regex and path options, failure messages, a private repo, an unknown ref and the Turkish page. #[tokio::test] - async fn results_group_by_file_with_line_links_and_marks() { + async fn search_pages_over_one_fixture() { let dir = tempfile::tempdir().unwrap(); let (router, _) = fixture(dir.path()).await; + + // Results group by file, link to the matching line and highlight the match. let (status, body) = get(&router, "/tahinli/demo/search?q=hello").await; - assert_eq!(status, 200); + assert_eq!(status, 200, "results"); assert!(body.contains("README.md"), "file listed"); assert!( body.contains("/tahinli/demo/blob/main/README.md#L3"), @@ -415,12 +425,8 @@ mod route_tests { assert!(body.contains("Hello"), "match highlighted"); assert!(body.contains("1 match in 1 file"), "summary shown"); assert!(!body.contains("src/main.rs"), "non-matching file absent"); - } - #[tokio::test] - async fn match_case_and_regex_and_path_options() { - let dir = tempfile::tempdir().unwrap(); - let (router, _) = fixture(dir.path()).await; + // Case-sensitive miss, regex hit without highlights, and a path glob that excludes src. let (_, body) = get(&router, "/tahinli/demo/search?q=HELLO&case=1").await; assert!(body.contains("No matches."), "case-sensitive miss"); let (_, body) = get(&router, "/tahinli/demo/search?q=h.llo®ex=1").await; @@ -428,35 +434,23 @@ mod route_tests { assert!(!body.contains(""), "regex mode does not highlight"); let (_, body) = get(&router, "/tahinli/demo/search?q=println&path=README.md").await; assert!(body.contains("No matches."), "path glob excludes src"); - } - #[tokio::test] - async fn bad_regex_and_empty_repo_show_messages() { - let dir = tempfile::tempdir().unwrap(); - let (router, _) = fixture(dir.path()).await; + // A bad regex, an empty repo and an unknown ref each show their message or status. let (status, body) = get(&router, "/tahinli/demo/search?q=(®ex=1").await; - assert_eq!(status, 200); + assert_eq!(status, 200, "bad regex"); assert!(body.contains("could not run"), "git failure shown"); let (status, body) = get(&router, "/tahinli/empty/search?q=hello").await; - assert_eq!(status, 200); + assert_eq!(status, 200, "empty repo"); assert!(body.contains("no commits"), "empty repo message"); let (status, _) = get(&router, "/tahinli/demo/search?q=hello&ref=nope-branch").await; assert_eq!(status, 404, "unknown ref"); - } - #[tokio::test] - async fn private_repo_is_404_for_anonymous_viewers() { - let dir = tempfile::tempdir().unwrap(); - let (router, _) = fixture(dir.path()).await; + // A private repo is 404 for anonymous viewers and leaks no content. let (status, body) = get(&router, "/tahinli/secret/search?q=hello").await; - assert_eq!(status, 404); - assert!(!body.contains("README.md")); - } + assert_eq!(status, 404, "private repo"); + assert!(!body.contains("README.md"), "private repo leaks no files"); - #[tokio::test] - async fn turkish_viewer_gets_turkish_search_page() { - let dir = tempfile::tempdir().unwrap(); - let (router, _) = fixture(dir.path()).await; + // A Turkish viewer gets the Turkish page. let req = Request::builder() .method("GET") .uri("/tahinli/demo/search?q=hello") diff --git a/crates/forge-web/src/pages/compare.rs b/crates/forge-web/src/pages/compare.rs index 258f2df..86ee1e0 100644 --- a/crates/forge-web/src/pages/compare.rs +++ b/crates/forge-web/src/pages/compare.rs @@ -841,20 +841,21 @@ mod tests { format!("csrf={TAHINLI_CSRF}&base={base}&head={head}&title={title}&body=") } + /// Read-only views over one public fixture: the same-repo comparison, the picker redirect, + /// anonymous and signed-in views, and unknown heads. #[tokio::test] - async fn same_repo_compare_lists_commit_and_file() { + async fn compare_pages_show_the_right_actions() { let fx = Fixture::new(Visibility::Public).await; + + // Same-repo comparison lists the commit and the changed file. let (status, _, body) = get(&fx.router, "/tahinli/demo/compare/main...feature").await; - assert_eq!(status, 200); + assert_eq!(status, 200, "same repo"); let html = String::from_utf8(body).unwrap(); - assert!(html.contains("Comparing")); - assert!(html.contains("Add feature file")); - assert!(html.contains("feature.txt")); - } + assert!(html.contains("Comparing"), "same repo"); + assert!(html.contains("Add feature file"), "same repo commit"); + assert!(html.contains("feature.txt"), "same repo file"); - #[tokio::test] - async fn picker_redirects_to_comparison() { - let fx = Fixture::new(Visibility::Public).await; + // The picker redirects a filled-in form to the comparison; an empty picker is a page. let (status, location, _) = send( &fx.router, "GET", @@ -863,19 +864,17 @@ mod tests { None, ) .await; - assert_eq!(status, 303); + assert_eq!(status, 303, "picker redirect"); assert_eq!( location.as_deref(), - Some("/tahinli/demo/compare/main...feature") + Some("/tahinli/demo/compare/main...feature"), + "picker redirect" ); let (status, _, body) = send(&fx.router, "GET", "/tahinli/demo/compare", None, None).await; - assert_eq!(status, 200); - assert!(body.contains("Compare branches")); - } + assert_eq!(status, 200, "empty picker"); + assert!(body.contains("Compare branches"), "empty picker"); - #[tokio::test] - async fn anonymous_compare_has_no_form() { - let fx = Fixture::new(Visibility::Public).await; + // Anonymous viewers see the comparison without the pull request form. let (status, _, body) = send( &fx.router, "GET", @@ -884,36 +883,73 @@ mod tests { None, ) .await; - assert_eq!(status, 200); - assert!(!body.contains("action=\"/tahinli/demo/pulls\"")); - assert!(!body.contains("Open a pull request")); + assert_eq!(status, 200, "anonymous"); + assert!(!body.contains("action=\"/tahinli/demo/pulls\""), "anonymous has no form"); + assert!(!body.contains("Open a pull request"), "anonymous has no form"); + + // Signed-in viewers get the form, prefilled with the head commit's title. + let (_, _, body) = send( + &fx.router, + "GET", + "/tahinli/demo/compare/main...feature", + Some("tahinli-token"), + None, + ) + .await; + assert!(body.contains("action=\"/tahinli/demo/pulls\""), "signed in form"); + assert!(body.contains("value=\"Add feature file\""), "signed in form"); + + // A head from an unrelated owner, or a missing branch, is 404. + let (status, _, _) = send( + &fx.router, + "GET", + "/tahinli/demo/compare/main...mallory:feature", + None, + None, + ) + .await; + assert_eq!(status, 404, "unrelated head"); + let (status, _, _) = send( + &fx.router, + "GET", + "/tahinli/demo/compare/main...missing", + None, + None, + ) + .await; + assert_eq!(status, 404, "missing head"); } + /// Opening pulls on one public fixture, in order: a bad CSRF token is refused and creates + /// nothing; a good post creates pull 1; a repeat post goes to pull 1 instead of opening pull 2. #[tokio::test] - async fn post_creates_pull_and_pull_ref() { + async fn pull_posts_create_one_pull_and_reject_bad_csrf() { let fx = Fixture::new(Visibility::Public).await; - let form = create_form("main", "feature", "Add+feature+file"); - let (status, location, _) = send( + + let no_csrf = "base=main&head=feature&title=Add+feature+file&body="; + let (status, _, _) = send( &fx.router, "POST", "/tahinli/demo/pulls", Some("tahinli-token"), - Some(form.as_str()), + Some(no_csrf), ) .await; - assert_eq!(status, 303); - assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1")); - assert_eq!( - rev(&fx.bare, "refs/pull/1/head").as_deref(), - Some(fx.feature_oid.as_str()) - ); - } + assert_eq!(status, 403, "missing csrf"); + let wrong = "csrf=nope&base=main&head=feature&title=Add+feature+file&body="; + let (status, _, _) = send( + &fx.router, + "POST", + "/tahinli/demo/pulls", + Some("tahinli-token"), + Some(wrong), + ) + .await; + assert_eq!(status, 403, "wrong csrf"); + assert!(rev(&fx.bare, "refs/pull/1/head").is_none(), "rejected posts make no pull"); - #[tokio::test] - async fn duplicate_post_redirects_to_existing_pull() { - let fx = Fixture::new(Visibility::Public).await; let form = create_form("main", "feature", "Add+feature+file"); - let (status, _, _) = send( + let (status, location, _) = send( &fx.router, "POST", "/tahinli/demo/pulls", @@ -921,7 +957,13 @@ mod tests { Some(form.as_str()), ) .await; - assert_eq!(status, 303); + assert_eq!(status, 303, "create"); + assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1"), "create"); + assert_eq!( + rev(&fx.bare, "refs/pull/1/head").as_deref(), + Some(fx.feature_oid.as_str()), + "create points the pull ref at the head" + ); let (status, location, _) = send( &fx.router, @@ -931,9 +973,8 @@ mod tests { Some(form.as_str()), ) .await; - assert_eq!(status, 303); - assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1")); - + assert_eq!(status, 303, "duplicate"); + assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1"), "duplicate"); let repo = fx .store .repo_by_owner_and_name(OWNER, REPO) @@ -945,7 +986,8 @@ mod tests { .pull_by_number(&repo.id, 2) .await .unwrap() - .is_none() + .is_none(), + "duplicate opens no pull 2" ); // The comparison now links to the open pull instead of offering the form. @@ -957,25 +999,8 @@ mod tests { None, ) .await; - let html = body; - assert!(!html.contains("action=\"/tahinli/demo/pulls\"")); - assert!(html.contains("/tahinli/demo/pulls/1")); - } - - #[tokio::test] - async fn signed_in_compare_shows_form() { - let fx = Fixture::new(Visibility::Public).await; - let (_, _, body) = send( - &fx.router, - "GET", - "/tahinli/demo/compare/main...feature", - Some("tahinli-token"), - None, - ) - .await; - let html = body; - assert!(html.contains("action=\"/tahinli/demo/pulls\"")); - assert!(html.contains("value=\"Add feature file\"")); + assert!(!body.contains("action=\"/tahinli/demo/pulls\""), "open pull hides the form"); + assert!(body.contains("/tahinli/demo/pulls/1"), "open pull is linked"); } #[tokio::test] @@ -1032,54 +1057,4 @@ mod tests { .await; assert_eq!(status, 404); } - - #[tokio::test] - async fn missing_or_wrong_csrf_is_rejected() { - let fx = Fixture::new(Visibility::Public).await; - let no_csrf = "base=main&head=feature&title=Add+feature+file&body="; - let (status, _, _) = send( - &fx.router, - "POST", - "/tahinli/demo/pulls", - Some("tahinli-token"), - Some(no_csrf), - ) - .await; - assert_eq!(status, 403); - - let wrong = "csrf=nope&base=main&head=feature&title=Add+feature+file&body="; - let (status, _, _) = send( - &fx.router, - "POST", - "/tahinli/demo/pulls", - Some("tahinli-token"), - Some(wrong), - ) - .await; - assert_eq!(status, 403); - assert!(rev(&fx.bare, "refs/pull/1/head").is_none()); - } - - #[tokio::test] - async fn unrelated_head_is_404() { - let fx = Fixture::new(Visibility::Public).await; - let (status, _, _) = send( - &fx.router, - "GET", - "/tahinli/demo/compare/main...mallory:feature", - None, - None, - ) - .await; - assert_eq!(status, 404); - let (status, _, _) = send( - &fx.router, - "GET", - "/tahinli/demo/compare/main...missing", - None, - None, - ) - .await; - assert_eq!(status, 404); - } }