"""Tests for templateify base helpers and both service convert() methods.""" from bs4 import BeautifulSoup from app.templateify_base import TemplateifyHelpers from app.templateify_new_service import TemplateifyNewService from app.templateify_special_events_service import TemplateifySpecialEventsService # --------------------------------------------------------------------------- # TemplateifyHelpers unit tests # --------------------------------------------------------------------------- class TestTemplateifyHelpers: def test_register_adds_token(self): soup = BeautifulSoup("", "html.parser") h = TemplateifyHelpers(soup) h.register("{{FOO}}", "A foo token") assert len(h.tokens) == 1 assert h.tokens[0] == {"name": "{{FOO}}", "description": "A foo token"} def test_register_deduplicates(self): soup = BeautifulSoup("", "html.parser") h = TemplateifyHelpers(soup) h.register("{{FOO}}", "first") h.register("{{FOO}}", "second") assert len(h.tokens) == 1 assert h.tokens[0]["description"] == "first" def test_set_text_replaces_content(self): soup = BeautifulSoup('

Hello

', "html.parser") h = TemplateifyHelpers(soup) h.set_text(".title", "{{TITLE}}", "A title token") assert soup.select_one(".title").string == "{{TITLE}}" assert len(h.tokens) == 1 def test_set_text_missing_selector_is_noop(self): soup = BeautifulSoup("
", "html.parser") h = TemplateifyHelpers(soup) h.set_text(".missing", "{{X}}", "desc") assert len(h.tokens) == 0 def test_set_attr_updates_attribute(self): soup = BeautifulSoup('
', "html.parser") h = TemplateifyHelpers(soup) parent = soup.select_one("div") h.set_attr(parent, ".pic", "src", "{{IMG}}", "Image URL") assert soup.select_one(".pic")["src"] == "{{IMG}}" assert len(h.tokens) == 1 def test_set_child_text(self): soup = BeautifulSoup('
old
', "html.parser") h = TemplateifyHelpers(soup) parent = soup.select_one("div") h.set_child_text(parent, ".label", "{{LABEL}}", "Label token") assert soup.select_one(".label").string == "{{LABEL}}" def test_loopify_keeps_first_removes_rest(self): html = '

A

B

C

' soup = BeautifulSoup(html, "html.parser") h = TemplateifyHelpers(soup) def transform(node): node.clear() node.append("{{TEXT}}") h.register("{{TEXT}}", "Item text") h.loopify(".item", "ITEMS", transform, "Repeated items.") items = soup.select(".item") assert len(items) == 1 assert items[0].string == "{{TEXT}}" assert "{{#ITEMS}}" in str(soup) assert "{{/ITEMS}}" in str(soup) def test_wrap_images_in_anchors(self): html = """
Polygraph by Polymarket
Polymarket
""" soup = BeautifulSoup(html, "html.parser") h = TemplateifyHelpers(soup) h.wrap_images_in_anchors() header_img = soup.find("img", alt="Polygraph by Polymarket") assert header_img.find_parent("a") is not None assert header_img.find_parent("a")["href"] == "https://poly.market/0d7VvzI" footer_img = soup.find("img", alt="Polymarket") assert footer_img.find_parent("a") is not None # --------------------------------------------------------------------------- # Service-level integration tests # --------------------------------------------------------------------------- class TestTemplateifyNewService: def test_convert_returns_html_and_tokens(self, sample_daily_html): service = TemplateifyNewService() result = service.convert(sample_daily_html) assert "html" in result assert "tokens" in result assert isinstance(result["html"], str) assert isinstance(result["tokens"], list) def test_convert_tokenizes_intro(self, sample_daily_html): service = TemplateifyNewService() result = service.convert(sample_daily_html) assert "{{{INTRO_TEXT}}}" in result["html"] token_names = [t["name"] for t in result["tokens"]] assert "{{{INTRO_TEXT}}}" in token_names class TestTemplateifySpecialEventsService: def test_convert_returns_html_and_tokens(self, sample_special_events_html): service = TemplateifySpecialEventsService() result = service.convert(sample_special_events_html) assert "html" in result assert "tokens" in result assert isinstance(result["html"], str) assert isinstance(result["tokens"], list) def test_convert_tokenizes_event_title(self, sample_special_events_html): service = TemplateifySpecialEventsService() result = service.convert(sample_special_events_html) assert "{{EVENT_TITLE}}" in result["html"] token_names = [t["name"] for t in result["tokens"]] assert "{{EVENT_TITLE}}" in token_names def test_convert_tokenizes_intro_and_outro(self, sample_special_events_html): service = TemplateifySpecialEventsService() result = service.convert(sample_special_events_html) assert "{{{INTRO_TEXT}}}" in result["html"] assert "{{{OUTRO_TEXT}}}" in result["html"]