"""Tests for the page title on the fetch tool's simplified result (#2472).
Readability.js drops an article heading that repeats the document title, so a
page such as pandas' "What's new in 2.1.0" release note came back without the
line naming it. ``with_page_title`` now puts the title on top as a heading
unless the Markdown already opens with it. The protocol-level tests drive the
real server through an MCP client with HTTP served by ``harness.FakeWeb``, on
both extractor paths, and pin that the raw result is untouched; the unit tests
below pin each rule for choosing the title and spotting a duplicate.
"""
from __future__ import annotations
from typing import Any
import httpx
import pytest
from mcp import ClientSession
from mcp.types import CallToolResult
from mcp_server_fetch.title import page_title, with_page_title
from .harness import FakeWeb, connect
PAGE = "https://example.com/page"
ROBOTS = "https://example.com/robots.txt"
PREFIX = f"Contents of {PAGE}:\n"
# The shape of a Sphinx (pydata theme) page: `
` adds the site name to
# the `
`, which carries a "#" permalink.
BODY = "
These are the changes in pandas 2.1.0, with a long enough body. " * 8
SPHINX_PAGE = f"""
What’s new in 2.1.0 (Aug 30, 2023) — pandas documentation
{BODY}
"""
def text_of(result: CallToolResult) -> str:
assert not result.is_error, result
data = result.model_dump(by_alias=True, mode="json", exclude_none=True)
assert len(data["content"]) == 1
return data["content"][0]["text"]
async def fetch(web: FakeWeb, html: str, **arguments: Any) -> str:
web.add(ROBOTS, httpx.Response(404))
web.add(
PAGE,
httpx.Response(200, text=html, headers={"content-type": "text/html"}),
)
async with connect() as (session, _):
assert isinstance(session, ClientSession)
result = await session.call_tool("fetch", {"url": PAGE, **arguments})
return text_of(result)
@pytest.mark.usefixtures("node_readability", "private_tempdir")
async def test_readability_js_result_gains_the_dropped_title(web: FakeWeb) -> None:
text = await fetch(web, SPHINX_PAGE)
assert text.startswith(f"{PREFIX}# What’s new in 2.1.0 (Aug 30, 2023)\n\n")
assert text.count("What’s new in 2.1.0") == 1
assert "These are the changes in pandas 2.1.0" in text
@pytest.mark.usefixtures("python_readability")
async def test_python_result_is_not_given_a_second_title(web: FakeWeb) -> None:
# The pure-Python extractor keeps the `
` (and opens with the
# ``), so the title is already there and nothing is added.
text = await fetch(web, SPHINX_PAGE)
assert not text.startswith(f"{PREFIX}# ")
assert "What’s new in 2.1.0" in text
assert "These are the changes in pandas 2.1.0" in text
async def test_raw_result_is_untouched(web: FakeWeb) -> None:
text = await fetch(web, SPHINX_PAGE, raw=True)
assert text.endswith(SPHINX_PAGE)
assert "\n# What’s new" not in text
@pytest.mark.usefixtures("node_readability", "private_tempdir")
async def test_failed_simplification_is_not_titled(web: FakeWeb) -> None:
text = await fetch(
web, "Shell"
)
assert text == f"{PREFIX}Page failed to be simplified from HTML"
@pytest.mark.parametrize(
("html", "expected"),
[
# og:title wins over everything else.
(
'T
T
',
"Social title",
),
# The `
` wins when `` holds it.
("Heading | Site
Heading
", "Heading"),
# Otherwise ``.
("Document title
Something else
", "Document title"),
# A trailing "¶" permalink that is plain text is dropped.
("Doc ¶", "Doc"),
# No ``: the `
`.
("
Only heading
", "Only heading"),
# An empty og:title and title fall through to the `
", None),
],
)
def test_page_title(html: str, expected: str | None) -> None:
assert page_title(html) == expected
@pytest.mark.parametrize(
("content", "expected"),
[
# No heading: the title goes on top.
("Body text.\n", "# Title Here\n\nBody text.\n"),
# Leading blank lines are folded into the separator.
("\n\nBody text.\n", "# Title Here\n\nBody text.\n"),
# Already opens with the title as a heading, at any level.
("# Title Here\n\nBody.", "# Title Here\n\nBody."),
("\n## title here ¶\n\nBody.", "\n## title here ¶\n\nBody."),
# A first line that holds the whole title, heading or not.
("
Title Here — Site
Skip\n", "
Title Here — Site
Skip\n"),
# A first heading the title holds (the title adds a site name).
("# Title\n\nBody.", "# Title\n\nBody."),
# A first paragraph the title holds is not a heading: title added.
("Title\n\nBody.", "# Title Here\n\nTitle\n\nBody."),
# A first line of punctuation only cannot match.
("---\n\nBody.", "# Title Here\n\n---\n\nBody."),
# The title only in a link destination or tag is not shown: added.
(
"[Source](https://example.com/title-here)\n",
"# Title Here\n\n[Source](https://example.com/title-here)\n",
),
(
"x\n",
"# Title Here\n\nx\n",
),
# The title as a link's visible text is shown: not added.
("[Title Here](https://example.com)\n", "[Title Here](https://example.com)\n"),
# Empty content still gains the title.
("", "# Title Here\n\n"),
],
)
def test_with_page_title(content: str, expected: str) -> None:
assert with_page_title("Title Here", content) == expected
def test_with_page_title_leaves_untitled_page_and_failure_alone() -> None:
assert with_page_title("