From aeec80cb599ded32268113b93ec0cc49af3c036f 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:09:00 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20forge-web=20unit=20tests=20?= =?UTF-8?q?merge=20into=20tables=20and=20drop=20checks=20that=20cannot=20f?= =?UTF-8?q?ail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.rs | 34 ++- crates/forge-web/src/ci/api_tests.rs | 171 ++++++++------- crates/forge-web/src/lfs_pointer.rs | 95 +++++---- crates/forge-web/src/raw.rs | 51 +++-- crates/forge-web/src/render/highlight.rs | 11 +- crates/forge-web/src/render/markdown.rs | 186 +++++++---------- crates/forge-web/src/self_update.rs | 255 +++++++++++++---------- crates/forge-web/src/templates.rs | 42 ++-- crates/forge-web/src/theme.rs | 26 +-- crates/forge-web/src/webhooks/client.rs | 31 ++- 10 files changed, 445 insertions(+), 457 deletions(-) diff --git a/crates/forge-web/src/accent.rs b/crates/forge-web/src/accent.rs index 4f8a2e5..d8f4244 100644 --- a/crates/forge-web/src/accent.rs +++ b/crates/forge-web/src/accent.rs @@ -247,29 +247,21 @@ mod tests { fn ids_are_slugs_unique_and_the_default_comes_first() { assert_eq!(PALETTES[0].id, DEFAULT); assert_eq!(DEFAULT, forge_core::store::ui_prefs::DEFAULT_ACCENT); - let mut seen = HashSet::new(); - for p in PALETTES { - assert!( - forge_core::store::ui_prefs::valid_accent(p.id), - "{} is not a slug", - p.id - ); - assert!(seen.insert(p.id), "{} is listed twice", p.id); - } - } - - #[test] - fn base_ids_are_slugs_unique_and_the_default_comes_first() { assert_eq!(BASES[0].id, BASE_DEFAULT); assert_eq!(BASE_DEFAULT, forge_core::store::ui_prefs::DEFAULT_BASE); - let mut seen = HashSet::new(); - for b in BASES { - assert!( - forge_core::store::ui_prefs::valid_accent(b.id), - "{} is not a slug", - b.id - ); - assert!(seen.insert(b.id), "{} is listed twice", b.id); + let lists = [ + ("accent", PALETTES.iter().map(|p| p.id).collect::>()), + ("base", BASES.iter().map(|b| b.id).collect::>()), + ]; + for (kind, ids) in lists { + let mut seen = HashSet::new(); + for id in ids { + assert!( + forge_core::store::ui_prefs::valid_accent(id), + "{kind} {id} is not a slug" + ); + assert!(seen.insert(id), "{kind} {id} is listed twice"); + } } } diff --git a/crates/forge-web/src/ci/api_tests.rs b/crates/forge-web/src/ci/api_tests.rs index 7cbf4eb..d95b1b1 100644 --- a/crates/forge-web/src/ci/api_tests.rs +++ b/crates/forge-web/src/ci/api_tests.rs @@ -1351,87 +1351,98 @@ async fn claim_advertises_the_binary_only_to_a_matching_target() { } #[test] -fn cache_scope_for_push_on_default_branch_has_no_restore_keys() { - let scope = cache_scope("push", "refs/heads/main", None, None, "main").unwrap(); - assert_eq!(scope.key, "ref:refs/heads/main"); - assert!(scope.restore_keys.is_empty()); -} - -#[test] -fn cache_scope_for_push_on_branch_restores_default_branch() { - let scope = cache_scope("push", "refs/heads/feature/x", None, None, "main").unwrap(); - assert_eq!(scope.key, "ref:refs/heads/feature/x"); - assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]); -} - -#[test] -fn cache_scope_for_tag_writes_its_ref_and_restores_default_branch() { - let scope = cache_scope("tag", "refs/tags/v1.0", None, None, "main").unwrap(); - assert_eq!(scope.key, "ref:refs/tags/v1.0"); - assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]); -} - -#[test] -fn cache_scope_for_manual_run_uses_the_ref() { - let scope = cache_scope("manual", "refs/heads/main", None, None, "main").unwrap(); - assert_eq!(scope.key, "ref:refs/heads/main"); - assert!(scope.restore_keys.is_empty()); -} - -#[test] -fn cache_scope_for_pull_request_writes_only_its_pr_key() { - let scope = cache_scope( - "pull_request", - "refs/pull/12/head", - Some(12), - Some("release/1.0"), - "main", - ) - .unwrap(); - assert_eq!(scope.key, "pr:12"); - assert_eq!( - scope.restore_keys, - vec![ - "ref:refs/heads/release/1.0".to_string(), - "ref:refs/heads/main".to_string() - ] +fn cache_scope_keys_and_restore_order() { + // (name, trigger, ref, pull number, pull base, want key, want restore keys) + type Case<'a> = ( + &'a str, + &'a str, + &'a str, + Option, + Option<&'a str>, + &'a str, + &'a [&'a str], ); -} - -#[test] -fn cache_scope_for_pull_request_into_default_branch_restores_it_once() { - let scope = cache_scope( - "pull_request", - "refs/pull/3/head", - Some(3), - Some("main"), - "main", - ) - .unwrap(); - assert_eq!(scope.key, "pr:3"); - assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]); -} - -#[test] -fn cache_scope_for_pull_request_with_head_named_main_still_writes_pr_key() { - // A fork or same-repo PR whose head branch is called `main` must never get a `ref:` write key. - let scope = cache_scope( - "pull_request", - "refs/pull/7/head", - Some(7), - Some("main"), - "main", - ) - .unwrap(); - assert_eq!(scope.key, "pr:7"); - assert!(!scope.key.starts_with("ref:")); -} - -#[test] -fn cache_scope_for_pull_request_without_base_restores_default_only() { - let scope = cache_scope("pull_request", "refs/pull/9/head", Some(9), None, "main").unwrap(); - assert_eq!(scope.key, "pr:9"); - assert_eq!(scope.restore_keys, vec!["ref:refs/heads/main".to_string()]); + let cases: &[Case] = &[ + ( + "push default branch", + "push", + "refs/heads/main", + None, + None, + "ref:refs/heads/main", + &[], + ), + ( + "push branch", + "push", + "refs/heads/feature/x", + None, + None, + "ref:refs/heads/feature/x", + &["ref:refs/heads/main"], + ), + ( + "tag", + "tag", + "refs/tags/v1.0", + None, + None, + "ref:refs/tags/v1.0", + &["ref:refs/heads/main"], + ), + ( + "manual", + "manual", + "refs/heads/main", + None, + None, + "ref:refs/heads/main", + &[], + ), + ( + "pr into release", + "pull_request", + "refs/pull/12/head", + Some(12), + Some("release/1.0"), + "pr:12", + &["ref:refs/heads/release/1.0", "ref:refs/heads/main"], + ), + ( + "pr into default", + "pull_request", + "refs/pull/3/head", + Some(3), + Some("main"), + "pr:3", + &["ref:refs/heads/main"], + ), + // A fork or same-repo PR whose head branch is called `main` must never get a `ref:` write key. + ( + "pr head named main", + "pull_request", + "refs/pull/7/head", + Some(7), + Some("main"), + "pr:7", + &["ref:refs/heads/main"], + ), + ( + "pr without base", + "pull_request", + "refs/pull/9/head", + Some(9), + None, + "pr:9", + &["ref:refs/heads/main"], + ), + ]; + for (name, trigger, git_ref, pull, base, key, restore) in cases { + let scope = cache_scope(trigger, git_ref, *pull, *base, "main").unwrap(); + assert_eq!(scope.key, *key, "case {name}"); + let want: Vec = restore.iter().map(|k| k.to_string()).collect(); + assert_eq!(scope.restore_keys, want, "case {name}"); + } } #[test] diff --git a/crates/forge-web/src/lfs_pointer.rs b/crates/forge-web/src/lfs_pointer.rs index b8c675c..d284a2e 100644 --- a/crates/forge-web/src/lfs_pointer.rs +++ b/crates/forge-web/src/lfs_pointer.rs @@ -74,6 +74,53 @@ mod tests { format!("{VERSION_LINE}\noid sha256:{oid}\nsize {size}\n") } + #[test] + fn rejects_malformed_pointers() { + let no_size = format!("{VERSION_LINE}\noid sha256:{OID}\n"); + let no_oid = format!("{VERSION_LINE}\nsize 1\n"); + let twice = format!("{VERSION_LINE}\noid sha256:{OID}\noid sha256:{OID}\nsize 1\n"); + let big = format!("{VERSION_LINE}\n{}", "x".repeat(MAX_POINTER_BYTES)); + let cases: Vec<(&str, Vec)> = vec![ + ( + "other version url", + format!("version https://example.com/spec/v1\noid sha256:{OID}\nsize 1\n") + .into_bytes(), + ), + ( + "version with trailing space", + format!("{VERSION_LINE} \noid sha256:{OID}\nsize 1\n").into_bytes(), + ), + ( + "uppercase oid", + pointer(&OID.to_uppercase(), "1").into_bytes(), + ), + ("short oid", pointer(&OID[..63], "1").into_bytes()), + ("long oid", pointer(&format!("{OID}0"), "1").into_bytes()), + ( + "md5 oid", + format!("{VERSION_LINE}\noid md5:{OID}\nsize 1\n").into_bytes(), + ), + ("empty size", pointer(OID, "").into_bytes()), + ("negative size", pointer(OID, "-1").into_bytes()), + ("fractional size", pointer(OID, "1.5").into_bytes()), + ("hex size", pointer(OID, "0x10").into_bytes()), + ( + "size overflows u64", + pointer(OID, "99999999999999999999999").into_bytes(), + ), + ("no size line", no_size.into_bytes()), + ("no oid line", no_oid.into_bytes()), + ("duplicate oid line", twice.into_bytes()), + ("oversized", big.into_bytes()), + ("non-utf8", vec![0xff, 0xfe, 0x00]), + ("empty", Vec::new()), + ("plain text", b"hello\nworld\n".to_vec()), + ]; + for (name, bytes) in cases { + assert_eq!(parse(&bytes), None, "case {name}"); + } + } + #[test] fn parses_a_standard_pointer() { let got = parse(pointer(OID, "12345").as_bytes()); @@ -97,52 +144,4 @@ mod tests { }) ); } - - #[test] - fn rejects_wrong_version_line() { - let text = format!("version https://example.com/spec/v1\noid sha256:{OID}\nsize 1\n"); - assert_eq!(parse(text.as_bytes()), None); - let text = format!("{VERSION_LINE} \noid sha256:{OID}\nsize 1\n"); - assert_eq!(parse(text.as_bytes()), None); - } - - #[test] - fn rejects_bad_oid() { - assert_eq!(parse(pointer(&OID.to_uppercase(), "1").as_bytes()), None); - assert_eq!(parse(pointer(&OID[..63], "1").as_bytes()), None); - assert_eq!(parse(pointer(&format!("{OID}0"), "1").as_bytes()), None); - let text = format!("{VERSION_LINE}\noid md5:{OID}\nsize 1\n"); - assert_eq!(parse(text.as_bytes()), None); - } - - #[test] - fn rejects_bad_size() { - assert_eq!(parse(pointer(OID, "").as_bytes()), None); - assert_eq!(parse(pointer(OID, "-1").as_bytes()), None); - assert_eq!(parse(pointer(OID, "1.5").as_bytes()), None); - assert_eq!(parse(pointer(OID, "0x10").as_bytes()), None); - assert_eq!( - parse(pointer(OID, "99999999999999999999999").as_bytes()), - None - ); - } - - #[test] - fn rejects_missing_or_duplicate_fields() { - let no_size = format!("{VERSION_LINE}\noid sha256:{OID}\n"); - assert_eq!(parse(no_size.as_bytes()), None); - let no_oid = format!("{VERSION_LINE}\nsize 1\n"); - assert_eq!(parse(no_oid.as_bytes()), None); - let twice = format!("{VERSION_LINE}\noid sha256:{OID}\noid sha256:{OID}\nsize 1\n"); - assert_eq!(parse(twice.as_bytes()), None); - } - - #[test] - fn rejects_oversized_and_non_text_input() { - let big = format!("{VERSION_LINE}\n{}", "x".repeat(MAX_POINTER_BYTES)); - assert_eq!(parse(big.as_bytes()), None); - assert_eq!(parse(&[0xff, 0xfe, 0x00]), None); - assert_eq!(parse(b""), None); - assert_eq!(parse(b"hello\nworld\n"), None); - } } diff --git a/crates/forge-web/src/raw.rs b/crates/forge-web/src/raw.rs index 02a4677..3447fc2 100644 --- a/crates/forge-web/src/raw.rs +++ b/crates/forge-web/src/raw.rs @@ -668,21 +668,23 @@ mod tests { } #[tokio::test] - async fn html_is_forced_to_attachment() { + async fn html_is_forced_to_attachment_and_png_is_an_image() { let fx = fixture().await; - let res = get(&fx.router, "GET", "/tahinli/pub/main/page.html", None).await; - assert_eq!(res.status(), 200); - assert_eq!(header_str(&res, "content-type"), TEXT); - assert_eq!(header_str(&res, "content-disposition"), "attachment"); - } - - #[tokio::test] - async fn png_is_served_as_image() { - let fx = fixture().await; - let res = get(&fx.router, "GET", "/tahinli/pub/main/pic.png", None).await; - assert_eq!(res.status(), 200); - assert_eq!(header_str(&res, "content-type"), "image/png"); - assert_eq!(header_str(&res, "content-disposition"), ""); + // (path, want content-type, want content-disposition) + let cases = [ + ("/tahinli/pub/main/page.html", TEXT, "attachment"), + ("/tahinli/pub/main/pic.png", "image/png", ""), + ]; + for (uri, content_type, disposition) in cases { + let res = get(&fx.router, "GET", uri, None).await; + assert_eq!(res.status(), 200, "case {uri}"); + assert_eq!(header_str(&res, "content-type"), content_type, "case {uri}"); + assert_eq!( + header_str(&res, "content-disposition"), + disposition, + "case {uri}" + ); + } } #[tokio::test] @@ -720,19 +722,16 @@ mod tests { } #[tokio::test] - async fn missing_repo_is_404() { - let fx = fixture().await; - let res = get(&fx.router, "GET", "/tahinli/nope/main/hello.txt", None).await; - assert_eq!(res.status(), 404); - } - - #[tokio::test] - async fn directory_is_404() { + async fn missing_repos_and_directories_are_404() { let fx = fixture().await; - let res = get(&fx.router, "GET", "/tahinli/pub/main/sub", None).await; - assert_eq!(res.status(), 404); - let root = get(&fx.router, "GET", "/tahinli/pub/main", None).await; - assert_eq!(root.status(), 404); + for uri in [ + "/tahinli/nope/main/hello.txt", + "/tahinli/pub/main/sub", + "/tahinli/pub/main", + ] { + let res = get(&fx.router, "GET", uri, None).await; + assert_eq!(res.status(), 404, "case {uri}"); + } } #[tokio::test] diff --git a/crates/forge-web/src/render/highlight.rs b/crates/forge-web/src/render/highlight.rs index 0c9d518..9e559b1 100644 --- a/crates/forge-web/src/render/highlight.rs +++ b/crates/forge-web/src/render/highlight.rs @@ -202,13 +202,10 @@ mod tests { } #[test] - fn unknown_extension_returns_none() { - assert!(highlight_lines("data.unknownext", "hello\n").is_none()); - } - - #[test] - fn plain_text_returns_none() { - assert!(highlight_lines("notes.txt", "hello\n").is_none()); + fn unknown_or_plain_names_return_none() { + for name in ["data.unknownext", "notes.txt"] { + assert!(highlight_lines(name, "hello\n").is_none(), "case {name}"); + } } #[test] diff --git a/crates/forge-web/src/render/markdown.rs b/crates/forge-web/src/render/markdown.rs index 41b7268..2b51d73 100644 --- a/crates/forge-web/src/render/markdown.rs +++ b/crates/forge-web/src/render/markdown.rs @@ -837,6 +837,84 @@ mod tests { } } + /// `(markdown, with_ctx, fragments that must appear in the rendered HTML)`. + const CONTAINS_CASES: &[(&str, bool, &[&str])] = &[ + ( + "$x^2$ and ![a](/attachments/abc/a.png)", + true, + &[ + "data-math-style=\"inline\"", + "src=\"/attachments/abc/a.png\"", + ], + ), + ( + "| a | b |\n|:-|-:|\n| 1 | 2 |\n\n- [x] done\n- [ ] todo\n", + false, + &["", "More\n\nHidden text\n\n\n", + false, + &["
", "More"], + ), + ("# Hello World", false, &["id=\"user-content-hello-world\""]), + ( + "[t](#hello-world)", + true, + &["href=\"#user-content-hello-world\""], + ), + ( + "[x](docs/a.md)", + true, + &["href=\"https://forge.test/o/r/blob/main/guide/docs/a.md\""], + ), + ( + "[d](../sub/)\n\n[e](../../../../x.md)", + true, + &[ + "href=\"https://forge.test/o/r/tree/main/sub\"", + "href=\"https://forge.test/o/r/blob/main/x.md\"", + ], + ), + ( + "![i](../img.png)", + true, + &["src=\"https://raw.test/o/r/main/img.png\""], + ), + // The fragment belongs to the target file (e.g. #L12 on code), so it is left as is. + ( + "[r](/README.md#top) [s](/a/../b.md)", + true, + &[ + "href=\"https://forge.test/o/r/blob/main/README.md#top\"", + "href=\"https://forge.test/o/r/blob/main/b.md\"", + ], + ), + ( + "[g](https://example.com/a?b=1)", + true, + &[ + "href=\"https://example.com/a?b=1\"", + "rel=\"nofollow noopener noreferrer\"", + ], + ), + ("[x](docs/a.md)", false, &["href=\"docs/a.md\""]), + ]; + + #[test] + fn rendered_html_contains_expected_fragments() { + for (src, with_ctx, needles) in CONTAINS_CASES { + let lc = ctx(); + let html = render_markdown(src, with_ctx.then_some(&lc)); + for needle in needles.iter() { + assert!( + html.contains(needle), + "case {src:?} missing {needle:?}: {html}" + ); + } + } + } + #[test] fn tasks_are_counted_and_toggled() { let src = "- [ ] one\n- [x] two\n - [ ] nested\n\n```\n- [ ] code\n```\n"; @@ -850,13 +928,6 @@ mod tests { assert!(set_task("no tasks", 0, true).is_none()); } - #[test] - fn math_and_attachment_urls_survive() { - let html = render_markdown("$x^2$ and ![a](/attachments/abc/a.png)", Some(&ctx())); - assert!(html.contains("data-math-style=\"inline\""), "{html}"); - assert!(html.contains("src=\"/attachments/abc/a.png\""), "{html}"); - } - #[test] fn issue_refs_and_mentions_are_linked() { let html = render_markdown("fixes #12, see @tahinli.\n\n- #3", Some(&issue_ctx())); @@ -981,18 +1052,6 @@ mod tests { assert_eq!(mention_spans("x @a, @b_c-d"), vec![2..4, 6..12]); } - #[test] - fn gfm_tables_and_tasklists_render() { - let html = render_markdown( - "| a | b |\n|:-|-:|\n| 1 | 2 |\n\n- [x] done\n- [ ] todo\n", - None, - ); - assert!(html.contains("
"), "{html}"); - assert!(html.contains("x"), "{html}"); } - #[test] - fn relative_page_link_goes_to_blob() { - let html = render_markdown("[x](docs/a.md)", Some(&ctx())); - assert!( - html.contains("href=\"https://forge.test/o/r/blob/main/guide/docs/a.md\""), - "{html}" - ); - } - - #[test] - fn trailing_slash_and_dotdot_resolve() { - let html = render_markdown("[d](../sub/)\n\n[e](../../../../x.md)", Some(&ctx())); - assert!( - html.contains("href=\"https://forge.test/o/r/tree/main/sub\""), - "{html}" - ); - assert!( - html.contains("href=\"https://forge.test/o/r/blob/main/x.md\""), - "{html}" - ); - } - - #[test] - fn relative_image_resolves_against_raw_and_clamps() { - let html = render_markdown("![i](../img.png)", Some(&ctx())); - assert!( - html.contains("src=\"https://raw.test/o/r/main/img.png\""), - "{html}" - ); - } - - #[test] - fn root_relative_link_resolves_from_repo_root() { - // The fragment belongs to the target file (e.g. #L12 on code), so it is left as is. - let html = render_markdown("[r](/README.md#top) [s](/a/../b.md)", Some(&ctx())); - assert!( - html.contains("href=\"https://forge.test/o/r/blob/main/README.md#top\""), - "{html}" - ); - assert!( - html.contains("href=\"https://forge.test/o/r/blob/main/b.md\""), - "{html}" - ); - } - - #[test] - fn absolute_https_links_are_kept() { - let html = render_markdown("[g](https://example.com/a?b=1)", Some(&ctx())); - assert!( - html.contains("href=\"https://example.com/a?b=1\""), - "{html}" - ); - assert!( - html.contains("rel=\"nofollow noopener noreferrer\""), - "{html}" - ); - } - - #[test] - fn heading_ids_are_prefixed() { - let html = render_markdown("# Hello World", None); - assert!(html.contains("id=\"user-content-hello-world\""), "{html}"); - } - - #[test] - fn anchor_links_match_heading_ids() { - let html = render_markdown("[t](#hello-world)", Some(&ctx())); - assert!( - html.contains("href=\"#user-content-hello-world\""), - "{html}" - ); - } - - #[test] - fn details_and_summary_survive() { - let html = render_markdown( - "
More\n\nHidden text\n\n
\n", - None, - ); - assert!(html.contains("
"), "{html}"); - assert!(html.contains("More"), "{html}"); - } - #[test] fn emoji_shortcodes_convert_outside_code() { let html = render_markdown( @@ -1102,10 +1078,4 @@ mod tests { assert!(html.contains(":spark:"), "{html}"); assert!(!html.contains("🎉"), "{html}"); } - - #[test] - fn no_context_means_no_rewriting() { - let html = render_markdown("[x](docs/a.md)", None); - assert!(html.contains("href=\"docs/a.md\""), "{html}"); - } } diff --git a/crates/forge-web/src/self_update.rs b/crates/forge-web/src/self_update.rs index f6a212f..2b0b95a 100644 --- a/crates/forge-web/src/self_update.rs +++ b/crates/forge-web/src/self_update.rs @@ -435,118 +435,153 @@ mod tests { } #[test] - fn push_run_on_the_branch_of_the_repo_deploys() { - assert!(should_deploy( - &cfg(), - "Tahinli/forge", - &good(), - Some("run-7") - )); - } - - #[test] - fn pull_request_runs_never_deploy() { - // A PR run carries a pull number and a refs/pull ref, and its trigger is not "push". - let pr = run( - "pull_request", - "refs/pull/3/head", - RunState::Success, - Some(3), - ); - assert!(!should_deploy(&cfg(), "Tahinli/forge", &pr, Some("run-7"))); - // Even if a PR run were stored on the branch ref, the trigger and pull number exclude it. - let pr_on_branch = run( - "pull_request", - "refs/heads/main", - RunState::Success, - Some(3), - ); - assert!(!should_deploy( - &cfg(), - "Tahinli/forge", - &pr_on_branch, - Some("run-7") - )); - let push_with_pull = run("push", "refs/heads/main", RunState::Success, Some(3)); - assert!(!should_deploy( - &cfg(), - "Tahinli/forge", - &push_with_pull, - Some("run-7") - )); - } - - #[test] - fn other_branch_tag_and_manual_runs_do_not_deploy() { - let feature = run("push", "refs/heads/feature", RunState::Success, None); - assert!(!should_deploy( - &cfg(), - "Tahinli/forge", - &feature, - Some("run-7") - )); - let tag = run("tag", "refs/tags/v1", RunState::Success, None); - assert!(!should_deploy(&cfg(), "Tahinli/forge", &tag, Some("run-7"))); - let manual = run("manual", "refs/heads/main", RunState::Success, None); - assert!(!should_deploy( - &cfg(), - "Tahinli/forge", - &manual, - Some("run-7") - )); - } - - #[test] - fn other_repo_does_not_deploy() { - assert!(!should_deploy( - &cfg(), - "Tahinli/other", - &good(), - Some("run-7") - )); - // Case differences in the owner or name still match the configured repo. - assert!(should_deploy( - &cfg(), - "tahinli/FORGE", - &good(), - Some("run-7") - )); - } - - #[test] - fn only_a_successful_run_deploys() { - for state in [ - RunState::Failure, - RunState::Cancelled, - RunState::Error, - RunState::Running, - ] { - let r = run("push", "refs/heads/main", state, None); - assert!(!should_deploy(&cfg(), "Tahinli/forge", &r, Some("run-7"))); - } - } - - #[test] - fn an_older_run_on_the_branch_does_not_deploy() { - assert!(!should_deploy( - &cfg(), - "Tahinli/forge", - &good(), - Some("run-9") - )); - assert!(!should_deploy(&cfg(), "Tahinli/forge", &good(), None)); - } - - #[test] - fn disabled_config_never_deploys() { + fn only_the_newest_successful_push_to_the_deploy_branch_deploys() { + let on = cfg(); let mut off = cfg(); off.enabled = false; - assert!(!should_deploy( - &off, - "Tahinli/forge", - &good(), - Some("run-7") - )); + let failed = |state| run("push", "refs/heads/main", state, None); + // (case, config, repo, run, latest run id, want deploy) + let cases = vec![ + ( + "good push", + &on, + "Tahinli/forge", + good(), + Some("run-7"), + true, + ), + ( + "repo name case", + &on, + "tahinli/FORGE", + good(), + Some("run-7"), + true, + ), + ( + "other repo", + &on, + "Tahinli/other", + good(), + Some("run-7"), + false, + ), + ( + "disabled", + &off, + "Tahinli/forge", + good(), + Some("run-7"), + false, + ), + ( + "older run", + &on, + "Tahinli/forge", + good(), + Some("run-9"), + false, + ), + ("no latest run", &on, "Tahinli/forge", good(), None, false), + ( + "pull request run", + &on, + "Tahinli/forge", + run( + "pull_request", + "refs/pull/3/head", + RunState::Success, + Some(3), + ), + Some("run-7"), + false, + ), + // Even if a PR run were stored on the branch ref, the trigger and pull number exclude it. + ( + "pull request on branch ref", + &on, + "Tahinli/forge", + run( + "pull_request", + "refs/heads/main", + RunState::Success, + Some(3), + ), + Some("run-7"), + false, + ), + ( + "push with a pull number", + &on, + "Tahinli/forge", + run("push", "refs/heads/main", RunState::Success, Some(3)), + Some("run-7"), + false, + ), + ( + "feature branch", + &on, + "Tahinli/forge", + run("push", "refs/heads/feature", RunState::Success, None), + Some("run-7"), + false, + ), + ( + "tag", + &on, + "Tahinli/forge", + run("tag", "refs/tags/v1", RunState::Success, None), + Some("run-7"), + false, + ), + ( + "manual", + &on, + "Tahinli/forge", + run("manual", "refs/heads/main", RunState::Success, None), + Some("run-7"), + false, + ), + ( + "failure", + &on, + "Tahinli/forge", + failed(RunState::Failure), + Some("run-7"), + false, + ), + ( + "cancelled", + &on, + "Tahinli/forge", + failed(RunState::Cancelled), + Some("run-7"), + false, + ), + ( + "error", + &on, + "Tahinli/forge", + failed(RunState::Error), + Some("run-7"), + false, + ), + ( + "running", + &on, + "Tahinli/forge", + failed(RunState::Running), + Some("run-7"), + false, + ), + ]; + for (case, config, repo, run, latest, want) in cases { + assert_eq!( + should_deploy(config, repo, &run, latest), + want, + "case {case}" + ); + } } #[test] diff --git a/crates/forge-web/src/templates.rs b/crates/forge-web/src/templates.rs index e0ae60b..4c78150 100644 --- a/crates/forge-web/src/templates.rs +++ b/crates/forge-web/src/templates.rs @@ -228,17 +228,29 @@ mod tests { } #[test] - fn blank_name_falls_back_to_stem() { - let t = parse_template("docs", "---\nname: \n---\nx"); - assert_eq!(t.name, "docs"); - } - - #[test] - fn unclosed_front_matter_is_body() { - let text = "---\nname: Oops\nno closing marker\n"; - let t = parse_template("oops", text); - assert_eq!(t.name, "oops"); - assert_eq!(t.body, text); + fn blank_or_unclosed_front_matter_and_late_dashes_fall_back() { + // (stem, text, want name, want body): a blank name uses the stem; an unclosed block and a + // dash line later in the body are not front matter, so the whole text is body. + let cases = [ + ("docs", "---\nname: \n---\nx", "docs", "x"), + ( + "oops", + "---\nname: Oops\nno closing marker\n", + "oops", + "---\nname: Oops\nno closing marker\n", + ), + ( + "x", + "Intro\n---\nname: no\n---\n", + "x", + "Intro\n---\nname: no\n---\n", + ), + ]; + for (stem, text, name, body) in cases { + let t = parse_template(stem, text); + assert_eq!(t.name, name, "case {stem}"); + assert_eq!(t.body, body, "case {stem}"); + } } #[test] @@ -249,14 +261,6 @@ mod tests { assert_eq!(t.labels, vec!["a", "b"]); assert_eq!(t.body, "Body\r\n"); } - - #[test] - fn a_dash_line_later_in_the_body_is_not_front_matter() { - let text = "Intro\n---\nname: no\n---\n"; - let t = parse_template("x", text); - assert_eq!(t.name, "x"); - assert_eq!(t.body, text); - } } #[cfg(test)] diff --git a/crates/forge-web/src/theme.rs b/crates/forge-web/src/theme.rs index 1af081b..3982356 100644 --- a/crates/forge-web/src/theme.rs +++ b/crates/forge-web/src/theme.rs @@ -86,15 +86,11 @@ mod tests { } #[test] - fn unknown_preferences_move_on_to_light() { - assert_eq!(next_preference(""), "light"); - assert_eq!(next_preference("neon"), "light"); - } - - #[test] - fn no_choice_means_light() { - assert_eq!(resolve_preference(""), "light"); - assert_eq!(resolve_preference("neon"), "light"); + fn unknown_or_missing_preferences_fall_back_to_light() { + for pref in ["", "neon"] { + assert_eq!(next_preference(pref), "light", "next {pref:?}"); + assert_eq!(resolve_preference(pref), "light", "resolve {pref:?}"); + } assert_eq!(resolve_preference("light"), "light"); } @@ -103,16 +99,4 @@ mod tests { assert_eq!(resolve_preference("dark"), "dark"); assert_eq!(resolve_preference("auto"), "auto"); } - - #[test] - fn the_default_cycle_starts_on_light_and_reaches_every_choice() { - let start = resolve_preference(""); - assert_eq!(start, "light"); - assert_eq!(next_preference(start), "dark"); - assert_eq!(next_preference(next_preference(start)), "auto"); - assert_eq!( - next_preference(next_preference(next_preference(start))), - "light" - ); - } } diff --git a/crates/forge-web/src/webhooks/client.rs b/crates/forge-web/src/webhooks/client.rs index f3f54b5..3bc3011 100644 --- a/crates/forge-web/src/webhooks/client.rs +++ b/crates/forge-web/src/webhooks/client.rs @@ -589,23 +589,20 @@ mod tests { async fn send_rejects_loopback_by_default() { let addr = serve(ok_response()).await; let client = HookClient::new(policy(true, &[])).expect("client"); - let outcome = post(&client, &format!("http://127.0.0.1:{}/hook", addr.port())).await; - assert_eq!(outcome.status, None); - assert!( - outcome - .error - .as_deref() - .is_some_and(|e| e.contains("destination not allowed")) - ); - } - - #[tokio::test] - async fn send_rejects_localhost_name_by_default() { - let addr = serve(ok_response()).await; - let client = HookClient::new(policy(true, &[])).expect("client"); - let outcome = post(&client, &format!("http://localhost:{}/hook", addr.port())).await; - assert_eq!(outcome.status, None); - assert!(outcome.error.is_some()); + for host in ["127.0.0.1", "localhost"] { + let outcome = post(&client, &format!("http://{host}:{}/hook", addr.port())).await; + assert_eq!(outcome.status, None, "case {host}"); + assert!(outcome.error.is_some(), "case {host}"); + if host == "127.0.0.1" { + assert!( + outcome + .error + .as_deref() + .is_some_and(|e| e.contains("destination not allowed")), + "case {host}" + ); + } + } } #[tokio::test]