forge
Explore
Log in

Don't you dare go Hollow

Page tests share their fixtures and merge into tables

Browse files at this commit

3bcf844c602103be35c36cd9571d13b5c8b0637cAhmet Kaan GümüşParent: 5137546
  • test
    • page tests share their fixtures and merge into tables
Show full message
✅ test: page tests share their fixtures and merge into tables

ci_runs.rs: 22 -> 19 tests (ref/image shortening, scale/span text, axis/place merged).
compare.rs: 10 -> 4 tests (read-only views and pull posts share one fixture each; fork and private keep their own).
code_search.rs: 7 -> 2 tests (segment cases merged; all route scenarios share one fixture).
Total 39 -> 25. No assertions removed.
crates/forge-web/src/pages/ci_runs.rsmodified+28 -37
@@ -2412,32 +2412,26 @@ mod tests {
2412 assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]);2412 assert_eq!(branch_choices(&refs, "main"), vec!["main", "dev"]);
2413 }2413 }
2414 2414
2415+ /// Refs and images show their short names; refs outside heads and tags stay whole.
2415 #[test]2416 #[test]
2416- fn refs_shorten_to_branch_and_tag_names() {2417+ fn refs_and_images_shorten_to_their_last_part() {
2417- assert_eq!(short_ref("refs/heads/main"), "main");2418+ assert_eq!(short_ref("refs/heads/main"), "main", "branch ref");
2418- assert_eq!(short_ref("refs/tags/v1.2"), "v1.2");2419+ assert_eq!(short_ref("refs/tags/v1.2"), "v1.2", "tag ref");
2419- assert_eq!(short_ref("refs/pull/7/head"), "refs/pull/7/head");2420+ assert_eq!(short_ref("refs/pull/7/head"), "refs/pull/7/head", "pull ref");
2421+ assert_eq!(image_short("docker.io/library/rust:1"), "rust:1", "registry image");
2422+ assert_eq!(image_short("alpine"), "alpine", "bare image");
2420 }2423 }
2421 2424
2425+ /// The page scale is the longest run (never zero), and the scale line reads the real span.
2422 #[test]2426 #[test]
2423- fn images_show_their_last_part() {2427+ fn scale_is_the_longest_run_and_reads_the_real_span() {
2424- assert_eq!(image_short("docker.io/library/rust:1"), "rust:1");2428+ assert_eq!(bar_scale(&[30, 4692, 0]), 4692, "longest run");
2425- assert_eq!(image_short("alpine"), "alpine");2429+ assert_eq!(bar_scale(&[]), 1, "no runs");
2426- }2430+ assert_eq!(bar_scale(&[0, 0]), 1, "only zero runs");
2427-2431+ assert_eq!(span_text(5), "5s", "seconds");
2428- #[test]2432+ assert_eq!(span_text(90), "1m 30s", "minutes and seconds");
2429- fn bar_scale_is_the_longest_run_of_the_page() {2433+ assert_eq!(span_text(1200), "20m", "whole minutes");
2430- assert_eq!(bar_scale(&[30, 4692, 0]), 4692);2434+ assert_eq!(span_text(4692), "1h 19m", "hours and minutes");
2431- assert_eq!(bar_scale(&[]), 1);
2432- assert_eq!(bar_scale(&[0, 0]), 1);
2433- }
2434-
2435- #[test]
2436- fn scale_line_reads_the_real_span() {
2437- assert_eq!(span_text(5), "5s");
2438- assert_eq!(span_text(90), "1m 30s");
2439- assert_eq!(span_text(1200), "20m");
2440- assert_eq!(span_text(4692), "1h 19m");
2441 }2435 }
2442 2436
2443 #[test]2437 #[test]
@@ -2464,22 +2458,19 @@ mod tests {
2464 );2458 );
2465 }2459 }
2466 2460
2461+ /// The axis ticks are round and few, and places map times onto the track.
2467 #[test]2462 #[test]
2468- fn axis_ticks_are_round_and_few() {2463+ fn axis_ticks_are_round_and_places_follow_the_span() {
2469- assert_eq!(axis(4692), (1200, 4800));2464+ assert_eq!(axis(4692), (1200, 4800), "long axis");
2470- assert_eq!(axis(60), (60, 60));2465+ assert_eq!(axis(60), (60, 60), "short axis");
2471- assert_eq!(tick_label(0), "0");2466+ assert_eq!(tick_label(0), "0", "zero tick");
2472- assert_eq!(tick_label(1200), "20m");2467+ assert_eq!(tick_label(1200), "20m", "minutes tick");
2473- assert_eq!(tick_label(3600), "1h");2468+ assert_eq!(tick_label(3600), "1h", "hour tick");
2474- assert_eq!(tick_label(4800), "1h 20m");2469+ assert_eq!(tick_label(4800), "1h 20m", "hour and minutes tick");
2475- }2470+ assert_eq!(place(0, 4800, 530), 0, "start of the track");
2476-2471+ assert_eq!(place(2160, 4800, 530), 239, "middle of the track");
2477- #[test]2472+ assert_eq!(place(9999, 4800, 530), 530, "past the end is clamped");
2478- fn places_follow_the_span() {2473+ assert_eq!(place(5, 0, 530), 0, "empty span");
2479- assert_eq!(place(0, 4800, 530), 0);
2480- assert_eq!(place(2160, 4800, 530), 239);
2481- assert_eq!(place(9999, 4800, 530), 530);
2482- assert_eq!(place(5, 0, 530), 0);
2483 }2474 }
2484 2475
2485 #[test]2476 #[test]
crates/forge-web/src/pages/code_search.rsmodified+29 -35
@@ -273,7 +273,7 @@ mod tests {
273 }273 }
274 274
275 #[test]275 #[test]
276- fn segments_mark_each_occurrence() {276+ fn segments_split_around_each_hit() {
277 let segs = segments("Hello hello HELLO", "hello", false);277 let segs = segments("Hello hello HELLO", "hello", false);
278 assert_eq!(278 assert_eq!(
279 texts(&segs),279 texts(&segs),
@@ -283,21 +283,27 @@ mod tests {
283 ("hello".to_owned(), true),283 ("hello".to_owned(), true),
284 (" ".to_owned(), false),284 (" ".to_owned(), false),
285 ("HELLO".to_owned(), true),285 ("HELLO".to_owned(), true),
286- ]286+ ],
287+ "every case-insensitive hit is marked"
287 );288 );
288- }
289-
290- #[test]
291- fn segments_respect_case_and_unicode() {
292 let segs = segments("Hello hello", "hello", true);289 let segs = segments("Hello hello", "hello", true);
293 assert_eq!(290 assert_eq!(
294 texts(&segs),291 texts(&segs),
295- vec![("Hello ".to_owned(), false), ("hello".to_owned(), true)]292+ vec![("Hello ".to_owned(), false), ("hello".to_owned(), true)],
293+ "case-sensitive match skips other case"
296 );294 );
297 // Non-ASCII text is never split inside a character.295 // Non-ASCII text is never split inside a character.
298 let segs = segments("café CAFÉ", "é", false);296 let segs = segments("café CAFÉ", "é", false);
299- assert_eq!(segs.iter().filter(|s| s.hit).count(), 1);297+ assert_eq!(
300- assert_eq!(segments("no match here", "zzz", false).len(), 1);298+ segs.iter().filter(|s| s.hit).count(),
299+ 1,
300+ "unicode is not split inside a character"
301+ );
302+ assert_eq!(
303+ segments("no match here", "zzz", false).len(),
304+ 1,
305+ "no match is one plain segment"
306+ );
301 }307 }
302 }308 }
303 309
@@ -401,12 +407,16 @@ mod route_tests {
401 (status, String::from_utf8_lossy(&bytes).into_owned())407 (status, String::from_utf8_lossy(&bytes).into_owned())
402 }408 }
403 409
410+ /// Every search scenario reads one shared, read-only fixture: results and links, case,
411+ /// regex and path options, failure messages, a private repo, an unknown ref and the Turkish page.
404 #[tokio::test]412 #[tokio::test]
405- async fn results_group_by_file_with_line_links_and_marks() {413+ async fn search_pages_over_one_fixture() {
406 let dir = tempfile::tempdir().unwrap();414 let dir = tempfile::tempdir().unwrap();
407 let (router, _) = fixture(dir.path()).await;415 let (router, _) = fixture(dir.path()).await;
416+
417+ // Results group by file, link to the matching line and highlight the match.
408 let (status, body) = get(&router, "/tahinli/demo/search?q=hello").await;418 let (status, body) = get(&router, "/tahinli/demo/search?q=hello").await;
409- assert_eq!(status, 200);419+ assert_eq!(status, 200, "results");
410 assert!(body.contains("README.md"), "file listed");420 assert!(body.contains("README.md"), "file listed");
411 assert!(421 assert!(
412 body.contains("/tahinli/demo/blob/main/README.md#L3"),422 body.contains("/tahinli/demo/blob/main/README.md#L3"),
@@ -415,12 +425,8 @@ mod route_tests {
415 assert!(body.contains("<mark>Hello</mark>"), "match highlighted");425 assert!(body.contains("<mark>Hello</mark>"), "match highlighted");
416 assert!(body.contains("1 match in 1 file"), "summary shown");426 assert!(body.contains("1 match in 1 file"), "summary shown");
417 assert!(!body.contains("src/main.rs"), "non-matching file absent");427 assert!(!body.contains("src/main.rs"), "non-matching file absent");
418- }
419 428
420- #[tokio::test]429+ // Case-sensitive miss, regex hit without highlights, and a path glob that excludes src.
421- async fn match_case_and_regex_and_path_options() {
422- let dir = tempfile::tempdir().unwrap();
423- let (router, _) = fixture(dir.path()).await;
424 let (_, body) = get(&router, "/tahinli/demo/search?q=HELLO&case=1").await;430 let (_, body) = get(&router, "/tahinli/demo/search?q=HELLO&case=1").await;
425 assert!(body.contains("No matches."), "case-sensitive miss");431 assert!(body.contains("No matches."), "case-sensitive miss");
426 let (_, body) = get(&router, "/tahinli/demo/search?q=h.llo&regex=1").await;432 let (_, body) = get(&router, "/tahinli/demo/search?q=h.llo&regex=1").await;
@@ -428,35 +434,23 @@ mod route_tests {
428 assert!(!body.contains("<mark>"), "regex mode does not highlight");434 assert!(!body.contains("<mark>"), "regex mode does not highlight");
429 let (_, body) = get(&router, "/tahinli/demo/search?q=println&path=README.md").await;435 let (_, body) = get(&router, "/tahinli/demo/search?q=println&path=README.md").await;
430 assert!(body.contains("No matches."), "path glob excludes src");436 assert!(body.contains("No matches."), "path glob excludes src");
431- }
432 437
433- #[tokio::test]438+ // A bad regex, an empty repo and an unknown ref each show their message or status.
434- async fn bad_regex_and_empty_repo_show_messages() {
435- let dir = tempfile::tempdir().unwrap();
436- let (router, _) = fixture(dir.path()).await;
437 let (status, body) = get(&router, "/tahinli/demo/search?q=(&regex=1").await;439 let (status, body) = get(&router, "/tahinli/demo/search?q=(&regex=1").await;
438- assert_eq!(status, 200);440+ assert_eq!(status, 200, "bad regex");
439 assert!(body.contains("could not run"), "git failure shown");441 assert!(body.contains("could not run"), "git failure shown");
440 let (status, body) = get(&router, "/tahinli/empty/search?q=hello").await;442 let (status, body) = get(&router, "/tahinli/empty/search?q=hello").await;
441- assert_eq!(status, 200);443+ assert_eq!(status, 200, "empty repo");
442 assert!(body.contains("no commits"), "empty repo message");444 assert!(body.contains("no commits"), "empty repo message");
443 let (status, _) = get(&router, "/tahinli/demo/search?q=hello&ref=nope-branch").await;445 let (status, _) = get(&router, "/tahinli/demo/search?q=hello&ref=nope-branch").await;
444 assert_eq!(status, 404, "unknown ref");446 assert_eq!(status, 404, "unknown ref");
445- }
446 447
447- #[tokio::test]448+ // A private repo is 404 for anonymous viewers and leaks no content.
448- async fn private_repo_is_404_for_anonymous_viewers() {
449- let dir = tempfile::tempdir().unwrap();
450- let (router, _) = fixture(dir.path()).await;
451 let (status, body) = get(&router, "/tahinli/secret/search?q=hello").await;449 let (status, body) = get(&router, "/tahinli/secret/search?q=hello").await;
452- assert_eq!(status, 404);450+ assert_eq!(status, 404, "private repo");
453- assert!(!body.contains("README.md"));451+ assert!(!body.contains("README.md"), "private repo leaks no files");
454- }
455 452
456- #[tokio::test]453+ // A Turkish viewer gets the Turkish page.
457- async fn turkish_viewer_gets_turkish_search_page() {
458- let dir = tempfile::tempdir().unwrap();
459- let (router, _) = fixture(dir.path()).await;
460 let req = Request::builder()454 let req = Request::builder()
461 .method("GET")455 .method("GET")
462 .uri("/tahinli/demo/search?q=hello")456 .uri("/tahinli/demo/search?q=hello")
crates/forge-web/src/pages/compare.rsmodified+84 -109
@@ -841,20 +841,21 @@ mod tests {
841 format!("csrf={TAHINLI_CSRF}&base={base}&head={head}&title={title}&body=")841 format!("csrf={TAHINLI_CSRF}&base={base}&head={head}&title={title}&body=")
842 }842 }
843 843
844+ /// Read-only views over one public fixture: the same-repo comparison, the picker redirect,
845+ /// anonymous and signed-in views, and unknown heads.
844 #[tokio::test]846 #[tokio::test]
845- async fn same_repo_compare_lists_commit_and_file() {847+ async fn compare_pages_show_the_right_actions() {
846 let fx = Fixture::new(Visibility::Public).await;848 let fx = Fixture::new(Visibility::Public).await;
849+
850+ // Same-repo comparison lists the commit and the changed file.
847 let (status, _, body) = get(&fx.router, "/tahinli/demo/compare/main...feature").await;851 let (status, _, body) = get(&fx.router, "/tahinli/demo/compare/main...feature").await;
848- assert_eq!(status, 200);852+ assert_eq!(status, 200, "same repo");
849 let html = String::from_utf8(body).unwrap();853 let html = String::from_utf8(body).unwrap();
850- assert!(html.contains("Comparing"));854+ assert!(html.contains("Comparing"), "same repo");
851- assert!(html.contains("Add feature file"));855+ assert!(html.contains("Add feature file"), "same repo commit");
852- assert!(html.contains("feature.txt"));856+ assert!(html.contains("feature.txt"), "same repo file");
853- }
854 857
855- #[tokio::test]858+ // The picker redirects a filled-in form to the comparison; an empty picker is a page.
856- async fn picker_redirects_to_comparison() {
857- let fx = Fixture::new(Visibility::Public).await;
858 let (status, location, _) = send(859 let (status, location, _) = send(
859 &fx.router,860 &fx.router,
860 "GET",861 "GET",
@@ -863,19 +864,17 @@ mod tests {
863 None,864 None,
864 )865 )
865 .await;866 .await;
866- assert_eq!(status, 303);867+ assert_eq!(status, 303, "picker redirect");
867 assert_eq!(868 assert_eq!(
868 location.as_deref(),869 location.as_deref(),
869- Some("/tahinli/demo/compare/main...feature")870+ Some("/tahinli/demo/compare/main...feature"),
871+ "picker redirect"
870 );872 );
871 let (status, _, body) = send(&fx.router, "GET", "/tahinli/demo/compare", None, None).await;873 let (status, _, body) = send(&fx.router, "GET", "/tahinli/demo/compare", None, None).await;
872- assert_eq!(status, 200);874+ assert_eq!(status, 200, "empty picker");
873- assert!(body.contains("Compare branches"));875+ assert!(body.contains("Compare branches"), "empty picker");
874- }
875 876
876- #[tokio::test]877+ // Anonymous viewers see the comparison without the pull request form.
877- async fn anonymous_compare_has_no_form() {
878- let fx = Fixture::new(Visibility::Public).await;
879 let (status, _, body) = send(878 let (status, _, body) = send(
880 &fx.router,879 &fx.router,
881 "GET",880 "GET",
@@ -884,36 +883,73 @@ mod tests {
884 None,883 None,
885 )884 )
886 .await;885 .await;
887- assert_eq!(status, 200);886+ assert_eq!(status, 200, "anonymous");
888- assert!(!body.contains("action=\"/tahinli/demo/pulls\""));887+ assert!(!body.contains("action=\"/tahinli/demo/pulls\""), "anonymous has no form");
889- assert!(!body.contains("Open a pull request"));888+ assert!(!body.contains("Open a pull request"), "anonymous has no form");
889+
890+ // Signed-in viewers get the form, prefilled with the head commit's title.
891+ let (_, _, body) = send(
892+ &fx.router,
893+ "GET",
894+ "/tahinli/demo/compare/main...feature",
895+ Some("tahinli-token"),
896+ None,
897+ )
898+ .await;
899+ assert!(body.contains("action=\"/tahinli/demo/pulls\""), "signed in form");
900+ assert!(body.contains("value=\"Add feature file\""), "signed in form");
901+
902+ // A head from an unrelated owner, or a missing branch, is 404.
903+ let (status, _, _) = send(
904+ &fx.router,
905+ "GET",
906+ "/tahinli/demo/compare/main...mallory:feature",
907+ None,
908+ None,
909+ )
910+ .await;
911+ assert_eq!(status, 404, "unrelated head");
912+ let (status, _, _) = send(
913+ &fx.router,
914+ "GET",
915+ "/tahinli/demo/compare/main...missing",
916+ None,
917+ None,
918+ )
919+ .await;
920+ assert_eq!(status, 404, "missing head");
890 }921 }
891 922
923+ /// Opening pulls on one public fixture, in order: a bad CSRF token is refused and creates
924+ /// nothing; a good post creates pull 1; a repeat post goes to pull 1 instead of opening pull 2.
892 #[tokio::test]925 #[tokio::test]
893- async fn post_creates_pull_and_pull_ref() {926+ async fn pull_posts_create_one_pull_and_reject_bad_csrf() {
894 let fx = Fixture::new(Visibility::Public).await;927 let fx = Fixture::new(Visibility::Public).await;
895- let form = create_form("main", "feature", "Add+feature+file");928+
896- let (status, location, _) = send(929+ let no_csrf = "base=main&head=feature&title=Add+feature+file&body=";
930+ let (status, _, _) = send(
897 &fx.router,931 &fx.router,
898 "POST",932 "POST",
899 "/tahinli/demo/pulls",933 "/tahinli/demo/pulls",
900 Some("tahinli-token"),934 Some("tahinli-token"),
901- Some(form.as_str()),935+ Some(no_csrf),
902 )936 )
903 .await;937 .await;
904- assert_eq!(status, 303);938+ assert_eq!(status, 403, "missing csrf");
905- assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1"));939+ let wrong = "csrf=nope&base=main&head=feature&title=Add+feature+file&body=";
906- assert_eq!(940+ let (status, _, _) = send(
907- rev(&fx.bare, "refs/pull/1/head").as_deref(),941+ &fx.router,
908- Some(fx.feature_oid.as_str())942+ "POST",
909- );943+ "/tahinli/demo/pulls",
910- }944+ Some("tahinli-token"),
945+ Some(wrong),
946+ )
947+ .await;
948+ assert_eq!(status, 403, "wrong csrf");
949+ assert!(rev(&fx.bare, "refs/pull/1/head").is_none(), "rejected posts make no pull");
911 950
912- #[tokio::test]
913- async fn duplicate_post_redirects_to_existing_pull() {
914- let fx = Fixture::new(Visibility::Public).await;
915 let form = create_form("main", "feature", "Add+feature+file");951 let form = create_form("main", "feature", "Add+feature+file");
916- let (status, _, _) = send(952+ let (status, location, _) = send(
917 &fx.router,953 &fx.router,
918 "POST",954 "POST",
919 "/tahinli/demo/pulls",955 "/tahinli/demo/pulls",
@@ -921,7 +957,13 @@ mod tests {
921 Some(form.as_str()),957 Some(form.as_str()),
922 )958 )
923 .await;959 .await;
924- assert_eq!(status, 303);960+ assert_eq!(status, 303, "create");
961+ assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1"), "create");
962+ assert_eq!(
963+ rev(&fx.bare, "refs/pull/1/head").as_deref(),
964+ Some(fx.feature_oid.as_str()),
965+ "create points the pull ref at the head"
966+ );
925 967
926 let (status, location, _) = send(968 let (status, location, _) = send(
927 &fx.router,969 &fx.router,
@@ -931,9 +973,8 @@ mod tests {
931 Some(form.as_str()),973 Some(form.as_str()),
932 )974 )
933 .await;975 .await;
934- assert_eq!(status, 303);976+ assert_eq!(status, 303, "duplicate");
935- assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1"));977+ assert_eq!(location.as_deref(), Some("/tahinli/demo/pulls/1"), "duplicate");
936-
937 let repo = fx978 let repo = fx
938 .store979 .store
939 .repo_by_owner_and_name(OWNER, REPO)980 .repo_by_owner_and_name(OWNER, REPO)
@@ -945,7 +986,8 @@ mod tests {
945 .pull_by_number(&repo.id, 2)986 .pull_by_number(&repo.id, 2)
946 .await987 .await
947 .unwrap()988 .unwrap()
948- .is_none()989+ .is_none(),
990+ "duplicate opens no pull 2"
949 );991 );
950 992
951 // The comparison now links to the open pull instead of offering the form.993 // The comparison now links to the open pull instead of offering the form.
@@ -957,25 +999,8 @@ mod tests {
957 None,999 None,
958 )1000 )
959 .await;1001 .await;
960- let html = body;1002+ assert!(!body.contains("action=\"/tahinli/demo/pulls\""), "open pull hides the form");
961- assert!(!html.contains("action=\"/tahinli/demo/pulls\""));1003+ assert!(body.contains("/tahinli/demo/pulls/1"), "open pull is linked");
962- assert!(html.contains("/tahinli/demo/pulls/1"));
963- }
964-
965- #[tokio::test]
966- async fn signed_in_compare_shows_form() {
967- let fx = Fixture::new(Visibility::Public).await;
968- let (_, _, body) = send(
969- &fx.router,
970- "GET",
971- "/tahinli/demo/compare/main...feature",
972- Some("tahinli-token"),
973- None,
974- )
975- .await;
976- let html = body;
977- assert!(html.contains("action=\"/tahinli/demo/pulls\""));
978- assert!(html.contains("value=\"Add feature file\""));
979 }1004 }
980 1005
981 #[tokio::test]1006 #[tokio::test]
@@ -1032,54 +1057,4 @@ mod tests {
1032 .await;1057 .await;
1033 assert_eq!(status, 404);1058 assert_eq!(status, 404);
1034 }1059 }
1035-
1036- #[tokio::test]
1037- async fn missing_or_wrong_csrf_is_rejected() {
1038- let fx = Fixture::new(Visibility::Public).await;
1039- let no_csrf = "base=main&head=feature&title=Add+feature+file&body=";
1040- let (status, _, _) = send(
1041- &fx.router,
1042- "POST",
1043- "/tahinli/demo/pulls",
1044- Some("tahinli-token"),
1045- Some(no_csrf),
1046- )
1047- .await;
1048- assert_eq!(status, 403);
1049-
1050- let wrong = "csrf=nope&base=main&head=feature&title=Add+feature+file&body=";
1051- let (status, _, _) = send(
1052- &fx.router,
1053- "POST",
1054- "/tahinli/demo/pulls",
1055- Some("tahinli-token"),
1056- Some(wrong),
1057- )
1058- .await;
1059- assert_eq!(status, 403);
1060- assert!(rev(&fx.bare, "refs/pull/1/head").is_none());
1061- }
1062-
1063- #[tokio::test]
1064- async fn unrelated_head_is_404() {
1065- let fx = Fixture::new(Visibility::Public).await;
1066- let (status, _, _) = send(
1067- &fx.router,
1068- "GET",
1069- "/tahinli/demo/compare/main...mallory:feature",
1070- None,
1071- None,
1072- )
1073- .await;
1074- assert_eq!(status, 404);
1075- let (status, _, _) = send(
1076- &fx.router,
1077- "GET",
1078- "/tahinli/demo/compare/main...missing",
1079- None,
1080- None,
1081- )
1082- .await;
1083- assert_eq!(status, 404);
1084- }
1085 }1060 }