| 1 | """Tests for scripts/lib/web_fetch_keyless.py — keyless URL-to-markdown fetch.""" |
| 2 | |
| 3 | from unittest import mock |
| 4 | |
| 5 | from lib import web_fetch_keyless |
| 6 | |
| 7 | |
| 8 | class TestFetchMarkdown: |
| 9 | """fetch_markdown turns a URL into clean markdown via the keyless reader.""" |
| 10 | |
| 11 | def test_happy_path(self): |
| 12 | body = "Title: Example\nURL Source: https://example.com\n\n# Example\n\nReal content." |
| 13 | with mock.patch.object(web_fetch_keyless.http, "get_text", return_value=body) as gt: |
| 14 | result = web_fetch_keyless.fetch_markdown("https://example.com") |
| 15 | assert result.ok is True |
| 16 | assert "Real content." in result.markdown |
| 17 | assert result.cached_snapshot is False |
| 18 | # Requests the reader-prefixed URL with a text accept header. |
| 19 | called_url = gt.call_args.args[0] |
| 20 | assert called_url == "https://r.jina.ai/https://example.com" |
| 21 | assert gt.call_args.kwargs.get("accept") == "text/plain" |
| 22 | |
| 23 | def test_cached_snapshot_flagged(self): |
| 24 | body = "Warning: showing a cached snapshot of this page.\n\n# Title\n\nbody" |
| 25 | with mock.patch.object(web_fetch_keyless.http, "get_text", return_value=body): |
| 26 | result = web_fetch_keyless.fetch_markdown("https://example.com/post") |
| 27 | assert result.ok is True |
| 28 | assert result.cached_snapshot is True |
| 29 | |
| 30 | def test_fetch_failure_returns_typed_empty(self): |
| 31 | with mock.patch.object(web_fetch_keyless.http, "get_text", return_value=None): |
| 32 | result = web_fetch_keyless.fetch_markdown("https://example.com") |
| 33 | assert result.ok is False |
| 34 | assert result.markdown == "" |
| 35 | assert result.reason == "fetch-failed" |
| 36 | |
| 37 | def test_empty_body_returns_typed_empty(self): |
| 38 | with mock.patch.object(web_fetch_keyless.http, "get_text", return_value=" \n "): |
| 39 | result = web_fetch_keyless.fetch_markdown("https://example.com") |
| 40 | assert result.ok is False |
| 41 | assert result.reason == "empty-body" |
| 42 | |
| 43 | def test_invalid_url_makes_no_request(self): |
| 44 | with mock.patch.object(web_fetch_keyless.http, "get_text") as gt: |
| 45 | result = web_fetch_keyless.fetch_markdown("not a url") |
| 46 | assert result.ok is False |
| 47 | assert result.reason == "invalid-url" |
| 48 | gt.assert_not_called() |
| 49 | |
| 50 | def test_empty_url_makes_no_request(self): |
| 51 | with mock.patch.object(web_fetch_keyless.http, "get_text") as gt: |
| 52 | result = web_fetch_keyless.fetch_markdown("") |
| 53 | assert result.ok is False |
| 54 | assert result.reason == "invalid-url" |
| 55 | gt.assert_not_called() |
| 56 |