"""Tests for #3878: content that server-side rendering hides until hydration.
A progressively rendered page streams a visible loading shell, then the real
page in a container hidden until the client hydrates it. Readability.js drops
hidden nodes, so fetch returned only the shell. The server now re-runs
Readability.js with such a container unhidden, but only when a hidden element
(that is not navigation or other page furniture) holds more text than the whole
first extraction, and keeps the second result only when it has more text.
What counts as hidden depends on the Readability.js that readabilipy runs: the
copy bundled with readabilipy 0.2 (the lockfile's) drops ``display: none`` and
``hidden``; the ``@mozilla/readability`` that readabilipy 0.3 installs also
drops ``visibility: hidden`` and ``aria-hidden="true"``. The protocol tests
that prove the second run happens use the two hidings both versions drop. They
count extractor calls, so "a normal page is unchanged" is asserted as "it
was extracted once, as before", not inferred from the Markdown.
"""
from __future__ import annotations
from typing import Any
import httpx
import pytest
import readabilipy.simple_json
from bs4 import BeautifulSoup
from mcp.types import CallToolResult
import mcp_server_fetch.server as server_module
from mcp_server_fetch.server import _is_hidden
from .harness import FakeWeb, connect
PAGE = "https://example.com/docs"
ARTICLE = """
Developer Concepts
Everything you need to integrate the framework: guides, concepts and
references for developers and architects, rendered on the server.
Install
Install the package and initialize your first project. The command line
handles project setup and configuration automatically.
"""
def ssr_page(hiding: str) -> str:
# The shape from the issue: a visible shell, then the content, hidden.
return f"""
Docs
Unified framework for full-stack applications
{ARTICLE}
"""
@pytest.fixture
def calls(monkeypatch: pytest.MonkeyPatch) -> list[str]:
"""Record every HTML string an extractor is asked to simplify.
Both extractors: the server's Readability.js runner and readabilipy's
pure-Python extractor, which it falls back to.
"""
seen: list[str] = []
original_js = server_module.extract_article
original_py = readabilipy.simple_json.simple_json_from_html_string
def spy_js(html: str) -> Any:
result = original_js(html)
# None: Readability.js did not run (no Node), so nothing was extracted.
if result is not None:
seen.append(html)
return result
def spy_py(html: str, **kwargs: Any) -> Any:
seen.append(html)
return original_py(html, **kwargs)
monkeypatch.setattr(server_module, "extract_article", spy_js)
monkeypatch.setattr(readabilipy.simple_json, "simple_json_from_html_string", spy_py)
return seen
def html(body: str) -> httpx.Response:
return httpx.Response(
200, content=body.encode(), headers={"content-type": "text/html"}
)
def text_of(result: CallToolResult) -> str:
data = result.model_dump(
by_alias=True, mode="json", exclude_none=True, exclude_unset=True
)
assert data["isError"] is False
[content] = data["content"]
assert content["type"] == "text"
return content["text"]
async def fetch(web: FakeWeb, page: str, **serve_kwargs: Any) -> str:
web.add(PAGE, html(page))
async with connect(ignore_robots_txt=True, **serve_kwargs) as (session, _):
return text_of(await session.call_tool("fetch", {"url": PAGE}))
@pytest.mark.usefixtures("node_readability")
@pytest.mark.parametrize(
"hiding",
[
'style="position:absolute;top:-9999px;display:none"',
"hidden",
'style="display: NONE !important"',
'style="display:none !IMPORTANT"',
],
)
async def test_hidden_ssr_content_is_recovered(
web: FakeWeb, calls: list[str], hiding: str
) -> None:
text = await fetch(web, ssr_page(hiding))
assert "## Install" in text
assert "rendered on the server" in text
assert "configuration automatically" in text
# Extracted once as is, then once with the container unhidden.
assert len(calls) == 2
@pytest.mark.usefixtures("node_readability")
@pytest.mark.parametrize("hiding", ['style="visibility: hidden"', 'aria-hidden="true"'])
async def test_newer_readability_hidings_come_back_too(
web: FakeWeb, hiding: str
) -> None:
# Dropped by readabilipy 0.3's Readability.js, kept by 0.2's: either way
# the content reaches the model.
text = await fetch(web, ssr_page(hiding))
assert "rendered on the server" in text
@pytest.mark.usefixtures("node_readability")
async def test_page_with_small_hidden_element_is_extracted_once(
web: FakeWeb, calls: list[str]
) -> None:
page = f"""{ARTICLE}
Copied to clipboard
"""
text = await fetch(web, page)
assert "rendered on the server" in text
assert "Copied to clipboard" not in text
assert len(calls) == 1
@pytest.mark.usefixtures("node_readability")
@pytest.mark.parametrize(
("opening", "closing"),
[
(""),
('
', "
"),
(""),
('
', "
"),
('
', "
"),
],
)
async def test_hidden_navigation_is_left_hidden(
web: FakeWeb, calls: list[str], opening: str, closing: str
) -> None:
# A collapsed menu larger than a short page is not a hydration container.
links = "".join(
f'