|
| 1 | +"""Tests for the page title on the fetch tool's simplified result (#2472). |
| 2 | +
|
| 3 | +Readability.js drops an article heading that repeats the document title, so a |
| 4 | +page such as pandas' "What's new in 2.1.0" release note came back without the |
| 5 | +line naming it. ``with_page_title`` now puts the title on top as a heading |
| 6 | +unless the Markdown already opens with it. The protocol-level tests drive the |
| 7 | +real server through an MCP client with HTTP served by ``harness.FakeWeb``, on |
| 8 | +both extractor paths, and pin that the raw result is untouched; the unit tests |
| 9 | +below pin each rule for choosing the title and spotting a duplicate. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +import httpx |
| 17 | +import pytest |
| 18 | +from mcp import ClientSession |
| 19 | +from mcp.types import CallToolResult |
| 20 | + |
| 21 | +from mcp_server_fetch.title import page_title, with_page_title |
| 22 | + |
| 23 | +from .harness import FakeWeb, connect |
| 24 | + |
| 25 | +PAGE = "https://example.com/page" |
| 26 | +ROBOTS = "https://example.com/robots.txt" |
| 27 | +PREFIX = f"Contents of {PAGE}:\n" |
| 28 | + |
| 29 | +# The shape of a Sphinx (pydata theme) page: `<title>` adds the site name to |
| 30 | +# the `<h1>`, which carries a "#" permalink. |
| 31 | +BODY = "<p>These are the changes in pandas 2.1.0, with a long enough body. " * 8 |
| 32 | +SPHINX_PAGE = f"""<!DOCTYPE html> |
| 33 | +<html><head><title>What’s new in 2.1.0 (Aug 30, 2023) — pandas documentation</title></head> |
| 34 | +<body><article> |
| 35 | +<h1>What’s new in 2.1.0 (Aug 30, 2023)<a class="headerlink" href="#x">#</a></h1> |
| 36 | +{BODY}</p> |
| 37 | +</article></body></html>""" |
| 38 | + |
| 39 | + |
| 40 | +def text_of(result: CallToolResult) -> str: |
| 41 | + assert not result.is_error, result |
| 42 | + data = result.model_dump(by_alias=True, mode="json", exclude_none=True) |
| 43 | + assert len(data["content"]) == 1 |
| 44 | + return data["content"][0]["text"] |
| 45 | + |
| 46 | + |
| 47 | +async def fetch(web: FakeWeb, html: str, **arguments: Any) -> str: |
| 48 | + web.add(ROBOTS, httpx.Response(404)) |
| 49 | + web.add( |
| 50 | + PAGE, |
| 51 | + httpx.Response(200, text=html, headers={"content-type": "text/html"}), |
| 52 | + ) |
| 53 | + async with connect() as (session, _): |
| 54 | + assert isinstance(session, ClientSession) |
| 55 | + result = await session.call_tool("fetch", {"url": PAGE, **arguments}) |
| 56 | + return text_of(result) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.usefixtures("node_readability", "private_tempdir") |
| 60 | +async def test_readability_js_result_gains_the_dropped_title(web: FakeWeb) -> None: |
| 61 | + text = await fetch(web, SPHINX_PAGE) |
| 62 | + assert text.startswith(f"{PREFIX}# What’s new in 2.1.0 (Aug 30, 2023)\n\n") |
| 63 | + assert text.count("What’s new in 2.1.0") == 1 |
| 64 | + assert "These are the changes in pandas 2.1.0" in text |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.usefixtures("python_readability") |
| 68 | +async def test_python_result_is_not_given_a_second_title(web: FakeWeb) -> None: |
| 69 | + # The pure-Python extractor keeps the `<h1>` (and opens with the |
| 70 | + # `<title>`), so the title is already there and nothing is added. |
| 71 | + text = await fetch(web, SPHINX_PAGE) |
| 72 | + assert not text.startswith(f"{PREFIX}# ") |
| 73 | + assert "What’s new in 2.1.0" in text |
| 74 | + assert "These are the changes in pandas 2.1.0" in text |
| 75 | + |
| 76 | + |
| 77 | +async def test_raw_result_is_untouched(web: FakeWeb) -> None: |
| 78 | + text = await fetch(web, SPHINX_PAGE, raw=True) |
| 79 | + assert text.endswith(SPHINX_PAGE) |
| 80 | + assert "\n# What’s new" not in text |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.usefixtures("node_readability", "private_tempdir") |
| 84 | +async def test_failed_simplification_is_not_titled(web: FakeWeb) -> None: |
| 85 | + text = await fetch( |
| 86 | + web, "<html><head><title>Shell</title></head><body></body></html>" |
| 87 | + ) |
| 88 | + assert text == f"{PREFIX}<error>Page failed to be simplified from HTML</error>" |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize( |
| 92 | + ("html", "expected"), |
| 93 | + [ |
| 94 | + # og:title wins over everything else. |
| 95 | + ( |
| 96 | + '<head><meta property="og:title" content=" Social title "><title>T</title></head><h1>T</h1>', |
| 97 | + "Social title", |
| 98 | + ), |
| 99 | + # The `<h1>` wins when `<title>` holds it. |
| 100 | + ("<title>Heading | Site</title><h1>Heading</h1>", "Heading"), |
| 101 | + # Otherwise `<title>`. |
| 102 | + ("<title>Document title</title><h1>Something else</h1>", "Document title"), |
| 103 | + # A trailing "¶" permalink that is plain text is dropped. |
| 104 | + ("<title>Doc ¶</title>", "Doc"), |
| 105 | + # No `<title>`: the `<h1>`. |
| 106 | + ("<body><h1>Only heading</h1></body>", "Only heading"), |
| 107 | + # An empty og:title and title fall through to the `<h1>`. |
| 108 | + ( |
| 109 | + '<meta property="og:title" content=""><title> </title><h1>H</h1>', |
| 110 | + "H", |
| 111 | + ), |
| 112 | + # Nothing names the page. |
| 113 | + ("<body><p>No title</p></body>", None), |
| 114 | + ("<title></title><h1><a class='headerlink'>#</a></h1>", None), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_page_title(html: str, expected: str | None) -> None: |
| 118 | + assert page_title(html) == expected |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.parametrize( |
| 122 | + ("content", "expected"), |
| 123 | + [ |
| 124 | + # No heading: the title goes on top. |
| 125 | + ("Body text.\n", "# Title Here\n\nBody text.\n"), |
| 126 | + # Leading blank lines are folded into the separator. |
| 127 | + ("\n\nBody text.\n", "# Title Here\n\nBody text.\n"), |
| 128 | + # Already opens with the title as a heading, at any level. |
| 129 | + ("# Title Here\n\nBody.", "# Title Here\n\nBody."), |
| 130 | + ("\n## title here ¶\n\nBody.", "\n## title here ¶\n\nBody."), |
| 131 | + # A first line that holds the whole title, heading or not. |
| 132 | + ("<p>Title Here — Site</p>Skip\n", "<p>Title Here — Site</p>Skip\n"), |
| 133 | + # A first heading the title holds (the title adds a site name). |
| 134 | + ("# Title\n\nBody.", "# Title\n\nBody."), |
| 135 | + # A first paragraph the title holds is not a heading: title added. |
| 136 | + ("Title\n\nBody.", "# Title Here\n\nTitle\n\nBody."), |
| 137 | + # A first line of punctuation only cannot match. |
| 138 | + ("---\n\nBody.", "# Title Here\n\n---\n\nBody."), |
| 139 | + # Empty content still gains the title. |
| 140 | + ("", "# Title Here\n\n"), |
| 141 | + ], |
| 142 | +) |
| 143 | +def test_with_page_title(content: str, expected: str) -> None: |
| 144 | + assert with_page_title("<title>Title Here</title>", content) == expected |
| 145 | + |
| 146 | + |
| 147 | +def test_with_page_title_leaves_untitled_page_and_failure_alone() -> None: |
| 148 | + assert with_page_title("<p>x</p>", "Body.") == "Body." |
| 149 | + failure = "<error>Page failed to be simplified from HTML</error>" |
| 150 | + assert with_page_title("<title>T</title>", failure) == failure |
| 151 | + |
| 152 | + |
| 153 | +def test_title_with_no_word_characters_is_not_matched() -> None: |
| 154 | + assert with_page_title("<title>—</title>", "Body.") == "# —\n\nBody." |
0 commit comments