|
- import unittest
- from unittest.mock import patch, MagicMock
-
- from services.ssr import extract_headlines
- from services.headlines import prepare_headlines
-
-
- HTML = """<html><head>
- <script type="application/ld+json">
- {
- "@context": "https://schema.org",
- "@graph": [
- {"@type": "NewsArticle", "headline": "Court rules on landmark case", "datePublished": "2026-09-14T12:00:00Z"},
- {"@type": "Organization", "name": "Example News"}
- ]
- }
- </script>
- </head><body>
- <script id="__NEXT_DATA__" type="application/json">
- {"props": {"pageProps": {"channel": {"items": [
- {"title": "Senate passes infrastructure bill", "url": "https://example.com/a/1"},
- {"title": "Markets", "url": "https://example.com/markets"}
- ]}}}}
- </script>
- </body></html>"""
-
-
- class TestExtractHeadlines(unittest.TestCase):
- def test_json_ld_and_next_data(self):
- items = extract_headlines(HTML)
- headlines = [h for h, _ in items]
-
- # schema.org headline and Next.js title+url are both recovered.
- self.assertIn("Court rules on landmark case", headlines)
- self.assertIn("Senate passes infrastructure bill", headlines)
-
- # Organization `name` without an article signal is not treated as a headline.
- self.assertNotIn("Example News", headlines)
-
- # Date is parsed from JSON-LD datePublished.
- by_title = {h: d for h, d in items}
- self.assertIsNotNone(by_title["Court rules on landmark case"])
-
-
- class TestPrepareHeadlinesSSR(unittest.TestCase):
- def setUp(self):
- self.stopwords = {"the", "a", "an", "in", "on", "and", "of", "to"}
-
- @patch("services.headlines.requests.get")
- def test_ssr_source_is_parsed(self, mock_get):
- mock_response = MagicMock()
- mock_response.status_code = 200
- mock_response.content = HTML.encode("utf-8")
- mock_response.text = HTML
- mock_get.return_value = mock_response
-
- headlines = prepare_headlines(["https://www.example.com/business/"], self.stopwords)
-
- titles = [h.display_text for h in headlines]
- # "Markets" is filtered out by the minimum-word headline check downstream.
- self.assertIn("Court rules on landmark case", titles)
- self.assertIn("Senate passes infrastructure bill", titles)
- self.assertNotIn("Markets", titles)
-
-
- if __name__ == "__main__":
- unittest.main()
|