"""TDD contract for the direct gov.pl (press releases) ingestion path.""" import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from discover_govpl import listing_articles, news_candidates, max_page from fetch_govpl import article_body, normalize, BASE LISTING = """
14.07.2026Paryż
Polska gospodarzem ćwiczeń
Lead...
13.07.2026
Druga wiadomość
nav
""" ARTICLE = """

Tytuł

Rada Ministrów przyjęła rozporządzenie. """ + ("Treść komunikatu. " * 20) + """

""" class GovplContractTest(unittest.TestCase): def test_listing_binds_date_title_url_and_filters_foreign(self): arts = listing_articles(LISTING, "premier") self.assertEqual(len(arts), 2) # nav link (foreign slug) dropped self.assertEqual(arts[0]["url"], "/web/premier/polska-gospodarzem-cwiczen") self.assertEqual(arts[0]["date"], "14.07.2026") self.assertIn("ćwiczeń", arts[0]["title"]) # entities decoded self.assertEqual(arts[1]["date"], "13.07.2026") def test_article_body_picks_richest_region_and_drops_gallery(self): body = article_body(ARTICLE) self.assertIn("Rada Ministrów", body) self.assertNotIn("<", body) # tags stripped self.assertNotIn("Pokaż zdjęcie", body) # gallery caption cut self.assertNotIn("Załącznik do pobrania", body) # attachments cut def test_normalize_schema_prepends_title_and_prefixes_url(self): art = {"url": "/web/premier/x", "title": "Nagłówek", "date": "14.07.2026"} row = normalize(art, ARTICLE, "premier") self.assertEqual(set(row), {"text", "meta"}) self.assertEqual(set(row["meta"]), {"url", "subsite", "title", "date"}) self.assertTrue(row["text"].startswith("Nagłówek")) self.assertEqual(row["meta"]["url"], BASE + "/web/premier/x") self.assertEqual(row["meta"]["subsite"], "premier") def test_thin_body_is_rejected(self): thin = '

Krótko.

' self.assertIsNone(normalize({"url": "/web/premier/x", "title": "T"}, thin, "premier")) self.assertIsNone(normalize({"url": "/web/premier/x", "title": "T"}, "", "premier")) def test_news_candidate_detection(self): landing = ('n' 'n' 'n' 'x' 'x') cands = set(news_candidates(landing, "arimr")) self.assertEqual(cands, {"aktualnosci10", "wszystkie-aktualnosci", "serwis-prasowy-arimr2"}) def test_max_page(self): self.assertEqual(max_page('2294'), 294) self.assertEqual(max_page("no pager"), 1) # single-page listing if __name__ == "__main__": unittest.main()