|
| 1 | +"""Protocol-level tests for pages whose text has no cp1252 encoding (#5303). |
| 2 | +
|
| 3 | +readabilipy 0.2.0's Readability.js path wrote the page to a temp file opened |
| 4 | +in the locale encoding, so on a Western-locale Windows (cp1252) any page |
| 5 | +holding a character outside that code page (CJK text, a leading byte-order |
| 6 | +mark, U+2010 HYPHEN) crashed extraction with ``UnicodeEncodeError``. readabilipy |
| 7 | +0.3.0 writes and reads its temp files as UTF-8, and pyproject.toml now requires |
| 8 | +it. On Linux and macOS the locale is already UTF-8, so these tests pass there |
| 9 | +either way; the "Test fetch (Windows)" CI leg is where they prove the fix. |
| 10 | +
|
| 11 | +Each test drives ``serve()`` through an MCP client, with the page served by |
| 12 | +``harness.FakeWeb``, on the Readability.js path (the one that crashed). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import httpx |
| 18 | +import pytest |
| 19 | +from mcp.types import CallToolResult |
| 20 | + |
| 21 | +from .harness import FakeWeb, connect |
| 22 | + |
| 23 | +PAGE = "https://example.jp/article" |
| 24 | +BOM = "" |
| 25 | + |
| 26 | +# None of these characters exists in cp1252. |
| 27 | +HEADING = "日本語の見出し" |
| 28 | +PARAGRAPH = ( |
| 29 | + "これは本文の段落です。読みやすさの抽出が主な内容として選ぶのに" |
| 30 | + "十分な長さの文章をここに書いています。" |
| 31 | +) |
| 32 | +HYPHENATED = "non‐breaking" |
| 33 | + |
| 34 | +ARTICLE = f"""<!doctype html> |
| 35 | +<html lang="ja"><head><meta charset="utf-8"><title>{HEADING}</title></head> |
| 36 | +<body><article> |
| 37 | +<h1>{HEADING}</h1> |
| 38 | +<p>{PARAGRAPH}</p> |
| 39 | +<p>A <a href="https://example.jp/x">link</a> and a {HYPHENATED} word.</p> |
| 40 | +</article></body></html>""" |
| 41 | + |
| 42 | + |
| 43 | +def text_of(result: CallToolResult) -> str: |
| 44 | + data = result.model_dump(by_alias=True, mode="json", exclude_none=True) |
| 45 | + assert data["isError"] is False |
| 46 | + assert len(data["content"]) == 1 |
| 47 | + assert data["content"][0]["type"] == "text" |
| 48 | + return data["content"][0]["text"] |
| 49 | + |
| 50 | + |
| 51 | +async def fetch(web: FakeWeb, response: httpx.Response) -> str: |
| 52 | + web.add(PAGE, response) |
| 53 | + async with connect(ignore_robots_txt=True) as (session, _): |
| 54 | + result = await session.call_tool("fetch", {"url": PAGE}) |
| 55 | + return text_of(result) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.usefixtures("node_readability") |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "body", |
| 61 | + [ |
| 62 | + pytest.param(ARTICLE, id="japanese"), |
| 63 | + pytest.param(f"{BOM}{ARTICLE}", id="japanese-with-bom"), |
| 64 | + ], |
| 65 | +) |
| 66 | +async def test_non_cp1252_page_is_simplified(web: FakeWeb, body: str) -> None: |
| 67 | + response = httpx.Response( |
| 68 | + 200, |
| 69 | + content=body.encode("utf-8"), |
| 70 | + headers={"content-type": "text/html; charset=utf-8"}, |
| 71 | + ) |
| 72 | + text = await fetch(web, response) |
| 73 | + |
| 74 | + assert "Page failed to be simplified" not in text |
| 75 | + assert text.startswith(f"Contents of {PAGE}:\n") |
| 76 | + # The non-cp1252 text came through Readability.js intact, as Markdown. |
| 77 | + assert PARAGRAPH in text |
| 78 | + assert HYPHENATED in text |
| 79 | + assert "[link](https://example.jp/x)" in text |
| 80 | + assert "<article>" not in text |
0 commit comments