File size: 5,754 Bytes
c4a298e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""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></html>", "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></html>", "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('<div><p class="title">Hello</p></div>', "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("<div></div>", "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('<div><img class="pic" src="old.png"></div>', "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('<div><span class="label">old</span></div>', "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 = '<div><p class="item">A</p><p class="item">B</p><p class="item">C</p></div>'
        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 = """
        <table role="presentation"><tr><td>
            <img alt="Polygraph by Polymarket" src="header.png">
        </td></tr></table>
        <table role="presentation"><tr><td>
            <img alt="Polymarket" src="footer.png">
        </td></tr></table>
        """
        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"]