forge
Explore
Log in

Don't you dare go Hollow

Forge-web unit tests merge into tables and drop checks that cannot fail

Browse files at this commit

aeec80cb599ded32268113b93ec0cc49af3c036fAhmet Kaan GümüşParent: 79f48c9
  • test
    • forge-web unit tests merge into tables and drop checks that cannot fail
Show full message
✅ test: forge-web unit tests merge into tables and drop checks that cannot fail

Test count in forge-web (excluding pages/): 580 before, 544 after.
Removed a redundant theme cycle test and folded several near-identical
tests (markdown links, cache scope, deploy decisions, lfs pointer
rejections, raw 404s and headers, webhook loopback rejections, accent
slugs, highlight None cases, template fallbacks) into table-driven tests
that keep every case and assertion, labelled per case.
crates/forge-web/src/accent.rsmodified+13 -21
@@ -247,29 +247,21 @@ mod tests {
247 fn ids_are_slugs_unique_and_the_default_comes_first() {247 fn ids_are_slugs_unique_and_the_default_comes_first() {
248 assert_eq!(PALETTES[0].id, DEFAULT);248 assert_eq!(PALETTES[0].id, DEFAULT);
249 assert_eq!(DEFAULT, forge_core::store::ui_prefs::DEFAULT_ACCENT);249 assert_eq!(DEFAULT, forge_core::store::ui_prefs::DEFAULT_ACCENT);
250- let mut seen = HashSet::new();
251- for p in PALETTES {
252- assert!(
253- forge_core::store::ui_prefs::valid_accent(p.id),
254- "{} is not a slug",
255- p.id
256- );
257- assert!(seen.insert(p.id), "{} is listed twice", p.id);
258- }
259- }
260-
261- #[test]
262- fn base_ids_are_slugs_unique_and_the_default_comes_first() {
263 assert_eq!(BASES[0].id, BASE_DEFAULT);250 assert_eq!(BASES[0].id, BASE_DEFAULT);
264 assert_eq!(BASE_DEFAULT, forge_core::store::ui_prefs::DEFAULT_BASE);251 assert_eq!(BASE_DEFAULT, forge_core::store::ui_prefs::DEFAULT_BASE);
265- let mut seen = HashSet::new();252+ let lists = [
266- for b in BASES {253+ ("accent", PALETTES.iter().map(|p| p.id).collect::<Vec<_>>()),
267- assert!(254+ ("base", BASES.iter().map(|b| b.id).collect::<Vec<_>>()),
268- forge_core::store::ui_prefs::valid_accent(b.id),255+ ];
269- "{} is not a slug",256+ for (kind, ids) in lists {
270- b.id257+ let mut seen = HashSet::new();
271- );258+ for id in ids {
272- assert!(seen.insert(b.id), "{} is listed twice", b.id);259+ assert!(
260+ forge_core::store::ui_prefs::valid_accent(id),
261+ "{kind} {id} is not a slug"
262+ );
263+ assert!(seen.insert(id), "{kind} {id} is listed twice");
264+ }
273 }265 }
274 }266 }
275 267
crates/forge-web/src/ci/api_tests.rsmodified+91 -80
@@ -1351,87 +1351,98 @@ async fn claim_advertises_the_binary_only_to_a_matching_target() {
1351 }1351 }
1352 1352
1353 #[test]1353 #[test]
1354-fn cache_scope_for_push_on_default_branch_has_no_restore_keys() {1354+fn cache_scope_keys_and_restore_order() {
1355- let scope = cache_scope("push", "refs/heads/main", None, None, "main").unwrap();1355+ // (name, trigger, ref, pull number, pull base, want key, want restore keys)
1356- assert_eq!(scope.key, "ref:refs/heads/main");1356+ type Case<'a> = (
1357- assert!(scope.restore_keys.is_empty());1357+ &'a str,
1358-}1358+ &'a str,
1359-1359+ &'a str,
1360-#[test]1360+ Option<i64>,
1361-fn cache_scope_for_push_on_branch_restores_default_branch() {1361+ Option<&'a str>,
1362- let scope = cache_scope("push", "refs/heads/feature/x", None, None, "main").unwrap();1362+ &'a str,
1363- assert_eq!(scope.key, "ref:refs/heads/feature/x");1363+ &'a [&'a str],
1364- assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]);
1365-}
1366-
1367-#[test]
1368-fn cache_scope_for_tag_writes_its_ref_and_restores_default_branch() {
1369- let scope = cache_scope("tag", "refs/tags/v1.0", None, None, "main").unwrap();
1370- assert_eq!(scope.key, "ref:refs/tags/v1.0");
1371- assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]);
1372-}
1373-
1374-#[test]
1375-fn cache_scope_for_manual_run_uses_the_ref() {
1376- let scope = cache_scope("manual", "refs/heads/main", None, None, "main").unwrap();
1377- assert_eq!(scope.key, "ref:refs/heads/main");
1378- assert!(scope.restore_keys.is_empty());
1379-}
1380-
1381-#[test]
1382-fn cache_scope_for_pull_request_writes_only_its_pr_key() {
1383- let scope = cache_scope(
1384- "pull_request",
1385- "refs/pull/12/head",
1386- Some(12),
1387- Some("release/1.0"),
1388- "main",
1389- )
1390- .unwrap();
1391- assert_eq!(scope.key, "pr:12");
1392- assert_eq!(
1393- scope.restore_keys,
1394- vec![
1395- "ref:refs/heads/release/1.0".to_string(),
1396- "ref:refs/heads/main".to_string()
1397- ]
1398 );1364 );
1399-}1365+ let cases: &[Case] = &[
1400-1366+ (
1401-#[test]1367+ "push default branch",
1402-fn cache_scope_for_pull_request_into_default_branch_restores_it_once() {1368+ "push",
1403- let scope = cache_scope(1369+ "refs/heads/main",
1404- "pull_request",1370+ None,
1405- "refs/pull/3/head",1371+ None,
1406- Some(3),1372+ "ref:refs/heads/main",
1407- Some("main"),1373+ &[],
1408- "main",1374+ ),
1409- )1375+ (
1410- .unwrap();1376+ "push branch",
1411- assert_eq!(scope.key, "pr:3");1377+ "push",
1412- assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]);1378+ "refs/heads/feature/x",
1413-}1379+ None,
1414-1380+ None,
1415-#[test]1381+ "ref:refs/heads/feature/x",
1416-fn cache_scope_for_pull_request_with_head_named_main_still_writes_pr_key() {1382+ &["ref:refs/heads/main"],
1417- // A fork or same-repo PR whose head branch is called `main` must never get a `ref:` write key.1383+ ),
1418- let scope = cache_scope(1384+ (
1419- "pull_request",1385+ "tag",
1420- "refs/pull/7/head",1386+ "tag",
1421- Some(7),1387+ "refs/tags/v1.0",
1422- Some("main"),1388+ None,
1423- "main",1389+ None,
1424- )1390+ "ref:refs/tags/v1.0",
1425- .unwrap();1391+ &["ref:refs/heads/main"],
1426- assert_eq!(scope.key, "pr:7");1392+ ),
1427- assert!(!scope.key.starts_with("ref:"));1393+ (
1428-}1394+ "manual",
1429-1395+ "manual",
1430-#[test]1396+ "refs/heads/main",
1431-fn cache_scope_for_pull_request_without_base_restores_default_only() {1397+ None,
1432- let scope = cache_scope("pull_request", "refs/pull/9/head", Some(9), None, "main").unwrap();1398+ None,
1433- assert_eq!(scope.key, "pr:9");1399+ "ref:refs/heads/main",
1434- assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]);1400+ &[],
1401+ ),
1402+ (
1403+ "pr into release",
1404+ "pull_request",
1405+ "refs/pull/12/head",
1406+ Some(12),
1407+ Some("release/1.0"),
1408+ "pr:12",
1409+ &["ref:refs/heads/release/1.0", "ref:refs/heads/main"],
1410+ ),
1411+ (
1412+ "pr into default",
1413+ "pull_request",
1414+ "refs/pull/3/head",
1415+ Some(3),
1416+ Some("main"),
1417+ "pr:3",
1418+ &["ref:refs/heads/main"],
1419+ ),
1420+ // A fork or same-repo PR whose head branch is called `main` must never get a `ref:` write key.
1421+ (
1422+ "pr head named main",
1423+ "pull_request",
1424+ "refs/pull/7/head",
1425+ Some(7),
1426+ Some("main"),
1427+ "pr:7",
1428+ &["ref:refs/heads/main"],
1429+ ),
1430+ (
1431+ "pr without base",
1432+ "pull_request",
1433+ "refs/pull/9/head",
1434+ Some(9),
1435+ None,
1436+ "pr:9",
1437+ &["ref:refs/heads/main"],
1438+ ),
1439+ ];
1440+ for (name, trigger, git_ref, pull, base, key, restore) in cases {
1441+ let scope = cache_scope(trigger, git_ref, *pull, *base, "main").unwrap();
1442+ assert_eq!(scope.key, *key, "case {name}");
1443+ let want: Vec<String> = restore.iter().map(|k| k.to_string()).collect();
1444+ assert_eq!(scope.restore_keys, want, "case {name}");
1445+ }
1435 }1446 }
1436 1447
1437 #[test]1448 #[test]
crates/forge-web/src/lfs_pointer.rsmodified+47 -48
@@ -74,6 +74,53 @@ mod tests {
74 format!("{VERSION_LINE}\noid sha256:{oid}\nsize {size}\n")74 format!("{VERSION_LINE}\noid sha256:{oid}\nsize {size}\n")
75 }75 }
76 76
77+ #[test]
78+ fn rejects_malformed_pointers() {
79+ let no_size = format!("{VERSION_LINE}\noid sha256:{OID}\n");
80+ let no_oid = format!("{VERSION_LINE}\nsize 1\n");
81+ let twice = format!("{VERSION_LINE}\noid sha256:{OID}\noid sha256:{OID}\nsize 1\n");
82+ let big = format!("{VERSION_LINE}\n{}", "x".repeat(MAX_POINTER_BYTES));
83+ let cases: Vec<(&str, Vec<u8>)> = vec![
84+ (
85+ "other version url",
86+ format!("version https://example.com/spec/v1\noid sha256:{OID}\nsize 1\n")
87+ .into_bytes(),
88+ ),
89+ (
90+ "version with trailing space",
91+ format!("{VERSION_LINE} \noid sha256:{OID}\nsize 1\n").into_bytes(),
92+ ),
93+ (
94+ "uppercase oid",
95+ pointer(&OID.to_uppercase(), "1").into_bytes(),
96+ ),
97+ ("short oid", pointer(&OID[..63], "1").into_bytes()),
98+ ("long oid", pointer(&format!("{OID}0"), "1").into_bytes()),
99+ (
100+ "md5 oid",
101+ format!("{VERSION_LINE}\noid md5:{OID}\nsize 1\n").into_bytes(),
102+ ),
103+ ("empty size", pointer(OID, "").into_bytes()),
104+ ("negative size", pointer(OID, "-1").into_bytes()),
105+ ("fractional size", pointer(OID, "1.5").into_bytes()),
106+ ("hex size", pointer(OID, "0x10").into_bytes()),
107+ (
108+ "size overflows u64",
109+ pointer(OID, "99999999999999999999999").into_bytes(),
110+ ),
111+ ("no size line", no_size.into_bytes()),
112+ ("no oid line", no_oid.into_bytes()),
113+ ("duplicate oid line", twice.into_bytes()),
114+ ("oversized", big.into_bytes()),
115+ ("non-utf8", vec![0xff, 0xfe, 0x00]),
116+ ("empty", Vec::new()),
117+ ("plain text", b"hello\nworld\n".to_vec()),
118+ ];
119+ for (name, bytes) in cases {
120+ assert_eq!(parse(&bytes), None, "case {name}");
121+ }
122+ }
123+
77 #[test]124 #[test]
78 fn parses_a_standard_pointer() {125 fn parses_a_standard_pointer() {
79 let got = parse(pointer(OID, "12345").as_bytes());126 let got = parse(pointer(OID, "12345").as_bytes());
@@ -97,52 +144,4 @@ mod tests {
97 })144 })
98 );145 );
99 }146 }
100-
101- #[test]
102- fn rejects_wrong_version_line() {
103- let text = format!("version https://example.com/spec/v1\noid sha256:{OID}\nsize 1\n");
104- assert_eq!(parse(text.as_bytes()), None);
105- let text = format!("{VERSION_LINE} \noid sha256:{OID}\nsize 1\n");
106- assert_eq!(parse(text.as_bytes()), None);
107- }
108-
109- #[test]
110- fn rejects_bad_oid() {
111- assert_eq!(parse(pointer(&OID.to_uppercase(), "1").as_bytes()), None);
112- assert_eq!(parse(pointer(&OID[..63], "1").as_bytes()), None);
113- assert_eq!(parse(pointer(&format!("{OID}0"), "1").as_bytes()), None);
114- let text = format!("{VERSION_LINE}\noid md5:{OID}\nsize 1\n");
115- assert_eq!(parse(text.as_bytes()), None);
116- }
117-
118- #[test]
119- fn rejects_bad_size() {
120- assert_eq!(parse(pointer(OID, "").as_bytes()), None);
121- assert_eq!(parse(pointer(OID, "-1").as_bytes()), None);
122- assert_eq!(parse(pointer(OID, "1.5").as_bytes()), None);
123- assert_eq!(parse(pointer(OID, "0x10").as_bytes()), None);
124- assert_eq!(
125- parse(pointer(OID, "99999999999999999999999").as_bytes()),
126- None
127- );
128- }
129-
130- #[test]
131- fn rejects_missing_or_duplicate_fields() {
132- let no_size = format!("{VERSION_LINE}\noid sha256:{OID}\n");
133- assert_eq!(parse(no_size.as_bytes()), None);
134- let no_oid = format!("{VERSION_LINE}\nsize 1\n");
135- assert_eq!(parse(no_oid.as_bytes()), None);
136- let twice = format!("{VERSION_LINE}\noid sha256:{OID}\noid sha256:{OID}\nsize 1\n");
137- assert_eq!(parse(twice.as_bytes()), None);
138- }
139-
140- #[test]
141- fn rejects_oversized_and_non_text_input() {
142- let big = format!("{VERSION_LINE}\n{}", "x".repeat(MAX_POINTER_BYTES));
143- assert_eq!(parse(big.as_bytes()), None);
144- assert_eq!(parse(&[0xff, 0xfe, 0x00]), None);
145- assert_eq!(parse(b""), None);
146- assert_eq!(parse(b"hello\nworld\n"), None);
147- }
148 }147 }
crates/forge-web/src/raw.rsmodified+25 -26
@@ -668,21 +668,23 @@ mod tests {
668 }668 }
669 669
670 #[tokio::test]670 #[tokio::test]
671- async fn html_is_forced_to_attachment() {671+ async fn html_is_forced_to_attachment_and_png_is_an_image() {
672 let fx = fixture().await;672 let fx = fixture().await;
673- let res = get(&fx.router, "GET", "/tahinli/pub/main/page.html", None).await;673+ // (path, want content-type, want content-disposition)
674- assert_eq!(res.status(), 200);674+ let cases = [
675- assert_eq!(header_str(&res, "content-type"), TEXT);675+ ("/tahinli/pub/main/page.html", TEXT, "attachment"),
676- assert_eq!(header_str(&res, "content-disposition"), "attachment");676+ ("/tahinli/pub/main/pic.png", "image/png", ""),
677- }677+ ];
678-678+ for (uri, content_type, disposition) in cases {
679- #[tokio::test]679+ let res = get(&fx.router, "GET", uri, None).await;
680- async fn png_is_served_as_image() {680+ assert_eq!(res.status(), 200, "case {uri}");
681- let fx = fixture().await;681+ assert_eq!(header_str(&res, "content-type"), content_type, "case {uri}");
682- let res = get(&fx.router, "GET", "/tahinli/pub/main/pic.png", None).await;682+ assert_eq!(
683- assert_eq!(res.status(), 200);683+ header_str(&res, "content-disposition"),
684- assert_eq!(header_str(&res, "content-type"), "image/png");684+ disposition,
685- assert_eq!(header_str(&res, "content-disposition"), "");685+ "case {uri}"
686+ );
687+ }
686 }688 }
687 689
688 #[tokio::test]690 #[tokio::test]
@@ -720,19 +722,16 @@ mod tests {
720 }722 }
721 723
722 #[tokio::test]724 #[tokio::test]
723- async fn missing_repo_is_404() {725+ async fn missing_repos_and_directories_are_404() {
724- let fx = fixture().await;
725- let res = get(&fx.router, "GET", "/tahinli/nope/main/hello.txt", None).await;
726- assert_eq!(res.status(), 404);
727- }
728-
729- #[tokio::test]
730- async fn directory_is_404() {
731 let fx = fixture().await;726 let fx = fixture().await;
732- let res = get(&fx.router, "GET", "/tahinli/pub/main/sub", None).await;727+ for uri in [
733- assert_eq!(res.status(), 404);728+ "/tahinli/nope/main/hello.txt",
734- let root = get(&fx.router, "GET", "/tahinli/pub/main", None).await;729+ "/tahinli/pub/main/sub",
735- assert_eq!(root.status(), 404);730+ "/tahinli/pub/main",
731+ ] {
732+ let res = get(&fx.router, "GET", uri, None).await;
733+ assert_eq!(res.status(), 404, "case {uri}");
734+ }
736 }735 }
737 736
738 #[tokio::test]737 #[tokio::test]
crates/forge-web/src/render/highlight.rsmodified+4 -7
@@ -202,13 +202,10 @@ mod tests {
202 }202 }
203 203
204 #[test]204 #[test]
205- fn unknown_extension_returns_none() {205+ fn unknown_or_plain_names_return_none() {
206- assert!(highlight_lines("data.unknownext", "hello\n").is_none());206+ for name in ["data.unknownext", "notes.txt"] {
207- }207+ assert!(highlight_lines(name, "hello\n").is_none(), "case {name}");
208-208+ }
209- #[test]
210- fn plain_text_returns_none() {
211- assert!(highlight_lines("notes.txt", "hello\n").is_none());
212 }209 }
213 210
214 #[test]211 #[test]
crates/forge-web/src/render/markdown.rsmodified+78 -108
@@ -837,6 +837,84 @@ mod tests {
837 }837 }
838 }838 }
839 839
840+ /// `(markdown, with_ctx, fragments that must appear in the rendered HTML)`.
841+ const CONTAINS_CASES: &[(&str, bool, &[&str])] = &[
842+ (
843+ "$x^2$ and ![a](/attachments/abc/a.png)",
844+ true,
845+ &[
846+ "data-math-style=\"inline\"",
847+ "src=\"/attachments/abc/a.png\"",
848+ ],
849+ ),
850+ (
851+ "| a | b |\n|:-|-:|\n| 1 | 2 |\n\n- [x] done\n- [ ] todo\n",
852+ false,
853+ &["<table>", "<td", "type=\"checkbox\"", "checked"],
854+ ),
855+ (
856+ "<details><summary>More</summary>\n\nHidden text\n\n</details>\n",
857+ false,
858+ &["<details>", "<summary>More</summary>"],
859+ ),
860+ ("# Hello World", false, &["id=\"user-content-hello-world\""]),
861+ (
862+ "[t](#hello-world)",
863+ true,
864+ &["href=\"#user-content-hello-world\""],
865+ ),
866+ (
867+ "[x](docs/a.md)",
868+ true,
869+ &["href=\"https://forge.test/o/r/blob/main/guide/docs/a.md\""],
870+ ),
871+ (
872+ "[d](../sub/)\n\n[e](../../../../x.md)",
873+ true,
874+ &[
875+ "href=\"https://forge.test/o/r/tree/main/sub\"",
876+ "href=\"https://forge.test/o/r/blob/main/x.md\"",
877+ ],
878+ ),
879+ (
880+ "![i](../img.png)",
881+ true,
882+ &["src=\"https://raw.test/o/r/main/img.png\""],
883+ ),
884+ // The fragment belongs to the target file (e.g. #L12 on code), so it is left as is.
885+ (
886+ "[r](/README.md#top) [s](/a/../b.md)",
887+ true,
888+ &[
889+ "href=\"https://forge.test/o/r/blob/main/README.md#top\"",
890+ "href=\"https://forge.test/o/r/blob/main/b.md\"",
891+ ],
892+ ),
893+ (
894+ "[g](https://example.com/a?b=1)",
895+ true,
896+ &[
897+ "href=\"https://example.com/a?b=1\"",
898+ "rel=\"nofollow noopener noreferrer\"",
899+ ],
900+ ),
901+ ("[x](docs/a.md)", false, &["href=\"docs/a.md\""]),
902+ ];
903+
904+ #[test]
905+ fn rendered_html_contains_expected_fragments() {
906+ for (src, with_ctx, needles) in CONTAINS_CASES {
907+ let lc = ctx();
908+ let html = render_markdown(src, with_ctx.then_some(&lc));
909+ for needle in needles.iter() {
910+ assert!(
911+ html.contains(needle),
912+ "case {src:?} missing {needle:?}: {html}"
913+ );
914+ }
915+ }
916+ }
917+
840 #[test]918 #[test]
841 fn tasks_are_counted_and_toggled() {919 fn tasks_are_counted_and_toggled() {
842 let src = "- [ ] one\n- [x] two\n - [ ] nested\n\n```\n- [ ] code\n```\n";920 let src = "- [ ] one\n- [x] two\n - [ ] nested\n\n```\n- [ ] code\n```\n";
@@ -850,13 +928,6 @@ mod tests {
850 assert!(set_task("no tasks", 0, true).is_none());928 assert!(set_task("no tasks", 0, true).is_none());
851 }929 }
852 930
853- #[test]
854- fn math_and_attachment_urls_survive() {
855- let html = render_markdown("$x^2$ and ![a](/attachments/abc/a.png)", Some(&ctx()));
856- assert!(html.contains("data-math-style=\"inline\""), "{html}");
857- assert!(html.contains("src=\"/attachments/abc/a.png\""), "{html}");
858- }
859-
860 #[test]931 #[test]
861 fn issue_refs_and_mentions_are_linked() {932 fn issue_refs_and_mentions_are_linked() {
862 let html = render_markdown("fixes #12, see @tahinli.\n\n- #3", Some(&issue_ctx()));933 let html = render_markdown("fixes #12, see @tahinli.\n\n- #3", Some(&issue_ctx()));
@@ -981,18 +1052,6 @@ mod tests {
981 assert_eq!(mention_spans("x @a, @b_c-d"), vec![2..4, 6..12]);1052 assert_eq!(mention_spans("x @a, @b_c-d"), vec![2..4, 6..12]);
982 }1053 }
983 1054
984- #[test]
985- fn gfm_tables_and_tasklists_render() {
986- let html = render_markdown(
987- "| a | b |\n|:-|-:|\n| 1 | 2 |\n\n- [x] done\n- [ ] todo\n",
988- None,
989- );
990- assert!(html.contains("<table>"), "{html}");
991- assert!(html.contains("<td"), "{html}");
992- assert!(html.contains("type=\"checkbox\""), "{html}");
993- assert!(html.contains("checked"), "{html}");
994- }
995-
996 #[test]1055 #[test]
997 fn dangerous_markup_is_stripped() {1056 fn dangerous_markup_is_stripped() {
998 let html = render_markdown(1057 let html = render_markdown(
@@ -1007,89 +1066,6 @@ mod tests {
1007 assert!(html.contains(">x</a>"), "{html}");1066 assert!(html.contains(">x</a>"), "{html}");
1008 }1067 }
1009 1068
1010- #[test]
1011- fn relative_page_link_goes_to_blob() {
1012- let html = render_markdown("[x](docs/a.md)", Some(&ctx()));
1013- assert!(
1014- html.contains("href=\"https://forge.test/o/r/blob/main/guide/docs/a.md\""),
1015- "{html}"
1016- );
1017- }
1018-
1019- #[test]
1020- fn trailing_slash_and_dotdot_resolve() {
1021- let html = render_markdown("[d](../sub/)\n\n[e](../../../../x.md)", Some(&ctx()));
1022- assert!(
1023- html.contains("href=\"https://forge.test/o/r/tree/main/sub\""),
1024- "{html}"
1025- );
1026- assert!(
1027- html.contains("href=\"https://forge.test/o/r/blob/main/x.md\""),
1028- "{html}"
1029- );
1030- }
1031-
1032- #[test]
1033- fn relative_image_resolves_against_raw_and_clamps() {
1034- let html = render_markdown("![i](../img.png)", Some(&ctx()));
1035- assert!(
1036- html.contains("src=\"https://raw.test/o/r/main/img.png\""),
1037- "{html}"
1038- );
1039- }
1040-
1041- #[test]
1042- fn root_relative_link_resolves_from_repo_root() {
1043- // The fragment belongs to the target file (e.g. #L12 on code), so it is left as is.
1044- let html = render_markdown("[r](/README.md#top) [s](/a/../b.md)", Some(&ctx()));
1045- assert!(
1046- html.contains("href=\"https://forge.test/o/r/blob/main/README.md#top\""),
1047- "{html}"
1048- );
1049- assert!(
1050- html.contains("href=\"https://forge.test/o/r/blob/main/b.md\""),
1051- "{html}"
1052- );
1053- }
1054-
1055- #[test]
1056- fn absolute_https_links_are_kept() {
1057- let html = render_markdown("[g](https://example.com/a?b=1)", Some(&ctx()));
1058- assert!(
1059- html.contains("href=\"https://example.com/a?b=1\""),
1060- "{html}"
1061- );
1062- assert!(
1063- html.contains("rel=\"nofollow noopener noreferrer\""),
1064- "{html}"
1065- );
1066- }
1067-
1068- #[test]
1069- fn heading_ids_are_prefixed() {
1070- let html = render_markdown("# Hello World", None);
1071- assert!(html.contains("id=\"user-content-hello-world\""), "{html}");
1072- }
1073-
1074- #[test]
1075- fn anchor_links_match_heading_ids() {
1076- let html = render_markdown("[t](#hello-world)", Some(&ctx()));
1077- assert!(
1078- html.contains("href=\"#user-content-hello-world\""),
1079- "{html}"
1080- );
1081- }
1082-
1083- #[test]
1084- fn details_and_summary_survive() {
1085- let html = render_markdown(
1086- "<details><summary>More</summary>\n\nHidden text\n\n</details>\n",
1087- None,
1088- );
1089- assert!(html.contains("<details>"), "{html}");
1090- assert!(html.contains("<summary>More</summary>"), "{html}");
1091- }
1092-
1093 #[test]1069 #[test]
1094 fn emoji_shortcodes_convert_outside_code() {1070 fn emoji_shortcodes_convert_outside_code() {
1095 let html = render_markdown(1071 let html = render_markdown(
@@ -1102,10 +1078,4 @@ mod tests {
1102 assert!(html.contains(":spark:"), "{html}");1078 assert!(html.contains(":spark:"), "{html}");
1103 assert!(!html.contains("<code>🎉</code>"), "{html}");1079 assert!(!html.contains("<code>🎉</code>"), "{html}");
1104 }1080 }
1105-
1106- #[test]
1107- fn no_context_means_no_rewriting() {
1108- let html = render_markdown("[x](docs/a.md)", None);
1109- assert!(html.contains("href=\"docs/a.md\""), "{html}");
1110- }
1111 }1081 }
crates/forge-web/src/self_update.rsmodified+145 -110
@@ -435,118 +435,153 @@ mod tests {
435 }435 }
436 436
437 #[test]437 #[test]
438- fn push_run_on_the_branch_of_the_repo_deploys() {438+ fn only_the_newest_successful_push_to_the_deploy_branch_deploys() {
439- assert!(should_deploy(439+ let on = cfg();
440- &cfg(),
441- "Tahinli/forge",
442- &good(),
443- Some("run-7")
444- ));
445- }
446-
447- #[test]
448- fn pull_request_runs_never_deploy() {
449- // A PR run carries a pull number and a refs/pull ref, and its trigger is not "push".
450- let pr = run(
451- "pull_request",
452- "refs/pull/3/head",
453- RunState::Success,
454- Some(3),
455- );
456- assert!(!should_deploy(&cfg(), "Tahinli/forge", &pr, Some("run-7")));
457- // Even if a PR run were stored on the branch ref, the trigger and pull number exclude it.
458- let pr_on_branch = run(
459- "pull_request",
460- "refs/heads/main",
461- RunState::Success,
462- Some(3),
463- );
464- assert!(!should_deploy(
465- &cfg(),
466- "Tahinli/forge",
467- &pr_on_branch,
468- Some("run-7")
469- ));
470- let push_with_pull = run("push", "refs/heads/main", RunState::Success, Some(3));
471- assert!(!should_deploy(
472- &cfg(),
473- "Tahinli/forge",
474- &push_with_pull,
475- Some("run-7")
476- ));
477- }
478-
479- #[test]
480- fn other_branch_tag_and_manual_runs_do_not_deploy() {
481- let feature = run("push", "refs/heads/feature", RunState::Success, None);
482- assert!(!should_deploy(
483- &cfg(),
484- "Tahinli/forge",
485- &feature,
486- Some("run-7")
487- ));
488- let tag = run("tag", "refs/tags/v1", RunState::Success, None);
489- assert!(!should_deploy(&cfg(), "Tahinli/forge", &tag, Some("run-7")));
490- let manual = run("manual", "refs/heads/main", RunState::Success, None);
491- assert!(!should_deploy(
492- &cfg(),
493- "Tahinli/forge",
494- &manual,
495- Some("run-7")
496- ));
497- }
498-
499- #[test]
500- fn other_repo_does_not_deploy() {
501- assert!(!should_deploy(
502- &cfg(),
503- "Tahinli/other",
504- &good(),
505- Some("run-7")
506- ));
507- // Case differences in the owner or name still match the configured repo.
508- assert!(should_deploy(
509- &cfg(),
510- "tahinli/FORGE",
511- &good(),
512- Some("run-7")
513- ));
514- }
515-
516- #[test]
517- fn only_a_successful_run_deploys() {
518- for state in [
519- RunState::Failure,
520- RunState::Cancelled,
521- RunState::Error,
522- RunState::Running,
523- ] {
524- let r = run("push", "refs/heads/main", state, None);
525- assert!(!should_deploy(&cfg(), "Tahinli/forge", &r, Some("run-7")));
526- }
527- }
528-
529- #[test]
530- fn an_older_run_on_the_branch_does_not_deploy() {
531- assert!(!should_deploy(
532- &cfg(),
533- "Tahinli/forge",
534- &good(),
535- Some("run-9")
536- ));
537- assert!(!should_deploy(&cfg(), "Tahinli/forge", &good(), None));
538- }
539-
540- #[test]
541- fn disabled_config_never_deploys() {
542 let mut off = cfg();440 let mut off = cfg();
543 off.enabled = false;441 off.enabled = false;
544- assert!(!should_deploy(442+ let failed = |state| run("push", "refs/heads/main", state, None);
545- &off,443+ // (case, config, repo, run, latest run id, want deploy)
546- "Tahinli/forge",444+ let cases = vec![
547- &good(),445+ (
548- Some("run-7")446+ "good push",
549- ));447+ &on,
448+ "Tahinli/forge",
449+ good(),
450+ Some("run-7"),
451+ true,
452+ ),
453+ (
454+ "repo name case",
455+ &on,
456+ "tahinli/FORGE",
457+ good(),
458+ Some("run-7"),
459+ true,
460+ ),
461+ (
462+ "other repo",
463+ &on,
464+ "Tahinli/other",
465+ good(),
466+ Some("run-7"),
467+ false,
468+ ),
469+ (
470+ "disabled",
471+ &off,
472+ "Tahinli/forge",
473+ good(),
474+ Some("run-7"),
475+ false,
476+ ),
477+ (
478+ "older run",
479+ &on,
480+ "Tahinli/forge",
481+ good(),
482+ Some("run-9"),
483+ false,
484+ ),
485+ ("no latest run", &on, "Tahinli/forge", good(), None, false),
486+ (
487+ "pull request run",
488+ &on,
489+ "Tahinli/forge",
490+ run(
491+ "pull_request",
492+ "refs/pull/3/head",
493+ RunState::Success,
494+ Some(3),
495+ ),
496+ Some("run-7"),
497+ false,
498+ ),
499+ // Even if a PR run were stored on the branch ref, the trigger and pull number exclude it.
500+ (
501+ "pull request on branch ref",
502+ &on,
503+ "Tahinli/forge",
504+ run(
505+ "pull_request",
506+ "refs/heads/main",
507+ RunState::Success,
508+ Some(3),
509+ ),
510+ Some("run-7"),
511+ false,
512+ ),
513+ (
514+ "push with a pull number",
515+ &on,
516+ "Tahinli/forge",
517+ run("push", "refs/heads/main", RunState::Success, Some(3)),
518+ Some("run-7"),
519+ false,
520+ ),
521+ (
522+ "feature branch",
523+ &on,
524+ "Tahinli/forge",
525+ run("push", "refs/heads/feature", RunState::Success, None),
526+ Some("run-7"),
527+ false,
528+ ),
529+ (
530+ "tag",
531+ &on,
532+ "Tahinli/forge",
533+ run("tag", "refs/tags/v1", RunState::Success, None),
534+ Some("run-7"),
535+ false,
536+ ),
537+ (
538+ "manual",
539+ &on,
540+ "Tahinli/forge",
541+ run("manual", "refs/heads/main", RunState::Success, None),
542+ Some("run-7"),
543+ false,
544+ ),
545+ (
546+ "failure",
547+ &on,
548+ "Tahinli/forge",
549+ failed(RunState::Failure),
550+ Some("run-7"),
551+ false,
552+ ),
553+ (
554+ "cancelled",
555+ &on,
556+ "Tahinli/forge",
557+ failed(RunState::Cancelled),
558+ Some("run-7"),
559+ false,
560+ ),
561+ (
562+ "error",
563+ &on,
564+ "Tahinli/forge",
565+ failed(RunState::Error),
566+ Some("run-7"),
567+ false,
568+ ),
569+ (
570+ "running",
571+ &on,
572+ "Tahinli/forge",
573+ failed(RunState::Running),
574+ Some("run-7"),
575+ false,
576+ ),
577+ ];
578+ for (case, config, repo, run, latest, want) in cases {
579+ assert_eq!(
580+ should_deploy(config, repo, &run, latest),
581+ want,
582+ "case {case}"
583+ );
584+ }
550 }585 }
551 586
552 #[test]587 #[test]
crates/forge-web/src/templates.rsmodified+23 -19
@@ -228,17 +228,29 @@ mod tests {
228 }228 }
229 229
230 #[test]230 #[test]
231- fn blank_name_falls_back_to_stem() {231+ fn blank_or_unclosed_front_matter_and_late_dashes_fall_back() {
232- let t = parse_template("docs", "---\nname: \n---\nx");232+ // (stem, text, want name, want body): a blank name uses the stem; an unclosed block and a
233- assert_eq!(t.name, "docs");233+ // dash line later in the body are not front matter, so the whole text is body.
234- }234+ let cases = [
235-235+ ("docs", "---\nname: \n---\nx", "docs", "x"),
236- #[test]236+ (
237- fn unclosed_front_matter_is_body() {237+ "oops",
238- let text = "---\nname: Oops\nno closing marker\n";238+ "---\nname: Oops\nno closing marker\n",
239- let t = parse_template("oops", text);239+ "oops",
240- assert_eq!(t.name, "oops");240+ "---\nname: Oops\nno closing marker\n",
241- assert_eq!(t.body, text);241+ ),
242+ (
243+ "x",
244+ "Intro\n---\nname: no\n---\n",
245+ "x",
246+ "Intro\n---\nname: no\n---\n",
247+ ),
248+ ];
249+ for (stem, text, name, body) in cases {
250+ let t = parse_template(stem, text);
251+ assert_eq!(t.name, name, "case {stem}");
252+ assert_eq!(t.body, body, "case {stem}");
253+ }
242 }254 }
243 255
244 #[test]256 #[test]
@@ -249,14 +261,6 @@ mod tests {
249 assert_eq!(t.labels, vec!["a", "b"]);261 assert_eq!(t.labels, vec!["a", "b"]);
250 assert_eq!(t.body, "Body\r\n");262 assert_eq!(t.body, "Body\r\n");
251 }263 }
252-
253- #[test]
254- fn a_dash_line_later_in_the_body_is_not_front_matter() {
255- let text = "Intro\n---\nname: no\n---\n";
256- let t = parse_template("x", text);
257- assert_eq!(t.name, "x");
258- assert_eq!(t.body, text);
259- }
260 }264 }
261 265
262 #[cfg(test)]266 #[cfg(test)]
crates/forge-web/src/theme.rsmodified+5 -21
@@ -86,15 +86,11 @@ mod tests {
86 }86 }
87 87
88 #[test]88 #[test]
89- fn unknown_preferences_move_on_to_light() {89+ fn unknown_or_missing_preferences_fall_back_to_light() {
90- assert_eq!(next_preference(""), "light");90+ for pref in ["", "neon"] {
91- assert_eq!(next_preference("neon"), "light");91+ assert_eq!(next_preference(pref), "light", "next {pref:?}");
92- }92+ assert_eq!(resolve_preference(pref), "light", "resolve {pref:?}");
93-93+ }
94- #[test]
95- fn no_choice_means_light() {
96- assert_eq!(resolve_preference(""), "light");
97- assert_eq!(resolve_preference("neon"), "light");
98 assert_eq!(resolve_preference("light"), "light");94 assert_eq!(resolve_preference("light"), "light");
99 }95 }
100 96
@@ -103,16 +99,4 @@ mod tests {
103 assert_eq!(resolve_preference("dark"), "dark");99 assert_eq!(resolve_preference("dark"), "dark");
104 assert_eq!(resolve_preference("auto"), "auto");100 assert_eq!(resolve_preference("auto"), "auto");
105 }101 }
106-
107- #[test]
108- fn the_default_cycle_starts_on_light_and_reaches_every_choice() {
109- let start = resolve_preference("");
110- assert_eq!(start, "light");
111- assert_eq!(next_preference(start), "dark");
112- assert_eq!(next_preference(next_preference(start)), "auto");
113- assert_eq!(
114- next_preference(next_preference(next_preference(start))),
115- "light"
116- );
117- }
118 }102 }
crates/forge-web/src/webhooks/client.rsmodified+14 -17
@@ -589,23 +589,20 @@ mod tests {
589 async fn send_rejects_loopback_by_default() {589 async fn send_rejects_loopback_by_default() {
590 let addr = serve(ok_response()).await;590 let addr = serve(ok_response()).await;
591 let client = HookClient::new(policy(true, &[])).expect("client");591 let client = HookClient::new(policy(true, &[])).expect("client");
592- let outcome = post(&client, &format!("http://127.0.0.1:{}/hook", addr.port())).await;592+ for host in ["127.0.0.1", "localhost"] {
593- assert_eq!(outcome.status, None);593+ let outcome = post(&client, &format!("http://{host}:{}/hook", addr.port())).await;
594- assert!(594+ assert_eq!(outcome.status, None, "case {host}");
595- outcome595+ assert!(outcome.error.is_some(), "case {host}");
596- .error596+ if host == "127.0.0.1" {
597- .as_deref()597+ assert!(
598- .is_some_and(|e| e.contains("destination not allowed"))598+ outcome
599- );599+ .error
600- }600+ .as_deref()
601-601+ .is_some_and(|e| e.contains("destination not allowed")),
602- #[tokio::test]602+ "case {host}"
603- async fn send_rejects_localhost_name_by_default() {603+ );
604- let addr = serve(ok_response()).await;604+ }
605- let client = HookClient::new(policy(true, &[])).expect("client");605+ }
606- let outcome = post(&client, &format!("http://localhost:{}/hook", addr.port())).await;
607- assert_eq!(outcome.status, None);
608- assert!(outcome.error.is_some());
609 }606 }
610 607
611 #[tokio::test]608 #[tokio::test]