|
| 1 | +"""Tests for ``--no-readability-js`` (#4199): an operator opt-out from Node.js. |
| 2 | +
|
| 3 | +readabilipy runs Mozilla's Readability.js under Node whenever Node is on PATH |
| 4 | +(and readabilipy 0.3 may first run ``npm install``), in a subprocess with no |
| 5 | +timeout. A Node or npm that hangs, or a deployment that must not start them, |
| 6 | +had no remedy short of editing the installed package. With the flag the |
| 7 | +server asks readabilipy for its pure-Python extractor, which never probes for |
| 8 | +Node at all. |
| 9 | +
|
| 10 | +The opt-out tests replace readabilipy's ``subprocess`` module with one that |
| 11 | +fails on any use, so they prove no process is started rather than inferring |
| 12 | +it from the Markdown that comes back. The default-path test needs Node on PATH, |
| 13 | +like the repository's other Readability.js tests. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import sys |
| 19 | +from typing import Any |
| 20 | +from unittest.mock import AsyncMock |
| 21 | + |
| 22 | +import httpx |
| 23 | +import pytest |
| 24 | +import readabilipy.simple_json |
| 25 | +from mcp.types import CallToolResult |
| 26 | + |
| 27 | +import mcp_server_fetch |
| 28 | + |
| 29 | +from .harness import FakeWeb, connect |
| 30 | + |
| 31 | +PAGE = "https://example.com/article" |
| 32 | + |
| 33 | +ARTICLE = """<html><head><title>Article</title></head><body><article> |
| 34 | +<h1>Heading</h1> |
| 35 | +<p>This is a <a href="https://example.com/x">link</a> in a paragraph with |
| 36 | +enough words to count as the main content of the page.</p> |
| 37 | +</article></body></html>""" |
| 38 | + |
| 39 | + |
| 40 | +class NoProcesses: |
| 41 | + """Stands in for readabilipy's ``subprocess`` module: any use fails the test.""" |
| 42 | + |
| 43 | + def __getattr__(self, name: str) -> Any: |
| 44 | + raise AssertionError(f"readabilipy used subprocess.{name}") |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def no_processes(monkeypatch: pytest.MonkeyPatch) -> None: |
| 49 | + # Both readabilipy's Node probe (`have_node`) and its Readability.js call |
| 50 | + # go through this module attribute, as does 0.3's `npm install`. |
| 51 | + monkeypatch.setattr(readabilipy.simple_json, "subprocess", NoProcesses()) |
| 52 | + |
| 53 | + |
| 54 | +def wire(result: CallToolResult) -> dict[str, Any]: |
| 55 | + return result.model_dump( |
| 56 | + by_alias=True, mode="json", exclude_none=True, exclude_unset=True |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def html(body: str) -> httpx.Response: |
| 61 | + return httpx.Response( |
| 62 | + 200, content=body.encode(), headers={"content-type": "text/html"} |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def test_no_processes_fixture_catches_the_node_probe(no_processes: None) -> None: |
| 67 | + # The guard the opt-out tests rely on does fire on the default path. |
| 68 | + with pytest.raises(AssertionError, match="readabilipy used subprocess.run"): |
| 69 | + readabilipy.simple_json.simple_json_from_html_string( |
| 70 | + ARTICLE, use_readability=True |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def test_flag_is_passed_to_serve(monkeypatch: pytest.MonkeyPatch) -> None: |
| 75 | + serve = AsyncMock() |
| 76 | + monkeypatch.setattr(mcp_server_fetch, "serve", serve) |
| 77 | + monkeypatch.setattr(sys, "argv", ["mcp-server-fetch", "--no-readability-js"]) |
| 78 | + mcp_server_fetch.main() |
| 79 | + serve.assert_awaited_once_with(None, False, None, False, False) |
| 80 | + |
| 81 | + |
| 82 | +def test_flag_is_in_help( |
| 83 | + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] |
| 84 | +) -> None: |
| 85 | + monkeypatch.setattr(mcp_server_fetch, "serve", AsyncMock()) |
| 86 | + monkeypatch.setattr(sys, "argv", ["mcp-server-fetch", "--help"]) |
| 87 | + with pytest.raises(SystemExit) as excinfo: |
| 88 | + mcp_server_fetch.main() |
| 89 | + assert excinfo.value.code == 0 |
| 90 | + assert "--no-readability-js" in capsys.readouterr().out |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.usefixtures("no_processes") |
| 94 | +async def test_tool_call_without_readability_js_starts_no_process( |
| 95 | + web: FakeWeb, |
| 96 | +) -> None: |
| 97 | + web.add(PAGE, html(ARTICLE)) |
| 98 | + async with connect(ignore_robots_txt=True, use_readability_js=False) as ( |
| 99 | + session, |
| 100 | + _, |
| 101 | + ): |
| 102 | + result = await session.call_tool("fetch", {"url": PAGE}) |
| 103 | + data = wire(result) |
| 104 | + assert data["isError"] is False |
| 105 | + [content] = data["content"] |
| 106 | + # The pure-Python extractor's Markdown: the heading and the text survive, |
| 107 | + # the link target does not (Readability.js keeps it). |
| 108 | + assert content["text"].startswith(f"Contents of {PAGE}:\n") |
| 109 | + assert "# Heading" in content["text"] |
| 110 | + assert "This is a link in a paragraph" in content["text"] |
| 111 | + assert "https://example.com/x" not in content["text"] |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.usefixtures("no_processes") |
| 115 | +async def test_prompt_without_readability_js_starts_no_process( |
| 116 | + web: FakeWeb, |
| 117 | +) -> None: |
| 118 | + web.add(PAGE, html(ARTICLE)) |
| 119 | + async with connect(use_readability_js=False) as (session, _): |
| 120 | + result = await session.get_prompt("fetch", {"url": PAGE}) |
| 121 | + [message] = result.messages |
| 122 | + assert message.content.type == "text" |
| 123 | + assert "This is a link in a paragraph" in message.content.text |
| 124 | + assert "https://example.com/x" not in message.content.text |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.usefixtures("node_readability") |
| 128 | +async def test_default_still_uses_readability_js(web: FakeWeb) -> None: |
| 129 | + # Without the flag nothing changes: Node is used, and it keeps link targets. |
| 130 | + web.add(PAGE, html(ARTICLE)) |
| 131 | + async with connect(ignore_robots_txt=True) as (session, _): |
| 132 | + result = await session.call_tool("fetch", {"url": PAGE}) |
| 133 | + [content] = wire(result)["content"] |
| 134 | + assert "[link](https://example.com/x)" in content["text"] |
0 commit comments