a73x

e600496f

test(server): integration tests for readme rendering on overview

alex emery   2026-04-13 05:52

Also fix load_readme to resolve the branch ref directly rather than
calling repo.head(), which fails on bare repos where HEAD points to an
unborn default branch (master) when only main has been pushed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

diff --git a/src/server/http/repo/readme.rs b/src/server/http/repo/readme.rs
index a7bab9e..0f4db71 100644
--- a/src/server/http/repo/readme.rs
+++ b/src/server/http/repo/readme.rs
@@ -23,8 +23,13 @@ pub fn load_readme(
    repo_name: &str,
    branch: &str,
) -> Option<RenderedReadme> {
    let head = repo.head().ok()?;
    let commit = head.peel_to_commit().ok()?;
    // Resolve the branch ref directly so this works on bare repos where HEAD
    // may point to an unborn branch (e.g. `master` when only `main` is pushed).
    let obj = repo
        .revparse_single(&format!("refs/heads/{branch}"))
        .or_else(|_| repo.revparse_single(branch))
        .ok()?;
    let commit = obj.peel_to_commit().ok()?;
    let tree = commit.tree().ok()?;
    let entry = find_readme_entry(&tree)?;

diff --git a/tests/server_behavior_test.rs b/tests/server_behavior_test.rs
index 6254074..4e52158 100644
--- a/tests/server_behavior_test.rs
+++ b/tests/server_behavior_test.rs
@@ -100,3 +100,38 @@ fn missing_repository_and_missing_objects_return_not_found() {
    assert!(missing_blob.body.contains("src/missing.rs"));
    assert!(missing_blob.body.contains("not found"));
}

#[test]
fn readme_md_renders_on_repo_overview_page() {
    let harness = ServerHarness::new("behavior-readme");

    harness.work_repo().commit_file(
        "README.md",
        "# Hello World\n\nA short description with a [link](https://example.com).\n",
        "add README",
    );
    harness.push_head();

    let overview = harness.get_ok(&format!("/{}", harness.repo_name()));
    assert!(overview.body.contains("<h1>Hello World</h1>"), "missing h1: {}", overview.body);
    assert!(overview.body.contains("href=\"https://example.com\""));
    // Sanity: the new card wrapper exists.
    assert!(overview.body.contains("class=\"readme-body\""));
}

#[test]
fn missing_readme_does_not_break_overview_page() {
    let harness = ServerHarness::new("behavior-no-readme");

    harness.work_repo().commit_file(
        "src/lib.rs",
        "pub fn x() {}\n",
        "add lib",
    );
    harness.push_head();

    let overview = harness.get_ok(&format!("/{}", harness.repo_name()));
    assert!(!overview.body.contains("class=\"readme-body\""));
    // The rest of the page still renders.
    assert!(overview.body.contains("Open Patches") || overview.body.contains("Recent Commits"));
}