File size: 6,549 Bytes
ec94fc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""Tests for the Jinja2 template rendering pipeline."""

from __future__ import annotations

import pytest
from pathlib import Path

from app.services.utils import h, handbook_anchor
from app.services.renderers import sort_toc


# ── Templates exist ──

def test_templates_directory_exists():
    templates_dir = Path(__file__).resolve().parent.parent / "app" / "templates"
    assert templates_dir.is_dir(), f"Templates directory not found: {templates_dir}"


def test_main_template_exists():
    template_path = Path(__file__).resolve().parent.parent / "app" / "templates" / "handbook.html"
    assert template_path.is_file(), f"Main template not found: {template_path}"


def test_partials_exist():
    partials_dir = Path(__file__).resolve().parent.parent / "app" / "templates" / "partials"
    assert partials_dir.is_dir()
    required_partials = ["cover.html", "toc.html", "university.html", "section.html"]
    for name in required_partials:
        p = partials_dir / name
        assert p.is_file(), f"Partial template not found: {p}"


# ── Print CSS exists ──

def test_print_css_exists():
    css_path = Path(__file__).resolve().parent.parent / "app" / "static" / "css" / "print.css"
    assert css_path.is_file(), f"Print CSS not found: {css_path}"


def test_print_css_has_page_rules():
    css_path = Path(__file__).resolve().parent.parent / "app" / "static" / "css" / "print.css"
    css = css_path.read_text(encoding="utf-8")
    assert "@page" in css
    assert "size: A4" in css
    assert "page-break" in css


def test_print_css_has_core_classes():
    css_path = Path(__file__).resolve().parent.parent / "app" / "static" / "css" / "print.css"
    css = css_path.read_text(encoding="utf-8")
    expected_classes = [
        ".h2", ".h3", ".p", ".tbl", ".programs",
        ".cover-page", ".toc-page", ".uni-name",
        ".benefits-bar", ".career-list",
    ]
    for cls in expected_classes:
        assert cls in css, f"Expected class {cls} missing from print.css"


# ── Jinja2 environment ──

def test_jinja2_env_loads():
    from app.services.html_builder import _get_jinja_env
    env = _get_jinja_env()
    template = env.get_template("handbook.html")
    assert template is not None


def test_jinja2_renders_minimal_context():
    """Test that the main template renders with minimal data (no sections)."""
    from app.services.html_builder import _get_jinja_env
    from markupsafe import Markup

    env = _get_jinja_env()
    template = env.get_template("handbook.html")

    html = template.render(
        font_css=Markup("/* no fonts */"),
        base_url="file:///tmp",
        extra_css="",
        cover_image="",
        toc_image="",
        toc_items=[],
        toc_items_sorted=[],
        toc_title="Table of Contents",
        toc_sort_order=None,
        general_sections=[],
        summary_block=None,
        universities=[],
        bottom_pages=[],
        debug=False,
        stats={},
    )

    assert "<!doctype html>" in html.lower() or "<!DOCTYPE html>" in html
    assert "<body>" in html
    assert "</body>" in html
    assert "ISP Handbook" in html


def test_jinja2_renders_with_cover():
    """Test cover image inclusion in template."""
    from app.services.html_builder import _get_jinja_env
    from markupsafe import Markup

    env = _get_jinja_env()
    template = env.get_template("handbook.html")

    html = template.render(
        font_css=Markup(""),
        base_url="file:///tmp",
        extra_css="",
        cover_image="file:///tmp/cover.jpg",
        toc_image="",
        toc_items=[],
        toc_items_sorted=[],
        toc_title="Table of Contents",
        toc_sort_order=None,
        general_sections=[],
        summary_block=None,
        universities=[],
        bottom_pages=[],
        debug=False,
        stats={},
    )

    assert "cover-page" in html
    assert "cover.jpg" in html


def test_jinja2_renders_toc():
    """Test TOC rendering in template."""
    from app.services.html_builder import _get_jinja_env
    from markupsafe import Markup

    env = _get_jinja_env()
    template = env.get_template("handbook.html")

    toc_items_sorted = [
        {"title": "Overview", "display_title": "OVERVIEW", "target": "#overview",
         "level": 0, "bold": True, "upper": True, "page": "3"},
        {"title": "Drew University", "display_title": "Drew University", "target": "#uni-drew",
         "level": 1, "bold": False, "upper": False, "page": ""},
    ]

    html = template.render(
        font_css=Markup(""),
        base_url="file:///tmp",
        extra_css="",
        cover_image="",
        toc_image="",
        toc_items=toc_items_sorted,  # non-empty triggers TOC
        toc_items_sorted=toc_items_sorted,
        toc_title="Table of Contents",
        toc_sort_order=1,
        general_sections=[],
        summary_block=None,
        universities=[],
        bottom_pages=[],
        debug=False,
        stats={},
    )

    assert "toc-page" in html
    assert "OVERVIEW" in html
    assert "Drew University" in html
    assert "toc-table" in html


def test_jinja2_renders_sections():
    """Test global section rendering."""
    from app.services.html_builder import _get_jinja_env
    from markupsafe import Markup

    env = _get_jinja_env()
    template = env.get_template("handbook.html")

    sections = [{
        "anchor": "g-overview-1",
        "data": {"section_key": "overview", "section_title": "Overview", "sort_order": 1},
        "page_break": True,
        "sec_class": "sec-overview",
        "rendered_html": Markup('<h2 class="h2">OVERVIEW</h2><p class="p">Welcome to the handbook.</p>'),
    }]

    html = template.render(
        font_css=Markup(""),
        base_url="file:///tmp",
        extra_css="",
        cover_image="",
        toc_image="",
        toc_items=[],
        toc_items_sorted=[],
        toc_title="Table of Contents",
        toc_sort_order=None,
        general_sections=sections,
        summary_block=None,
        universities=[],
        bottom_pages=[],
        debug=False,
        stats={},
    )

    assert "sec-overview" in html
    assert "OVERVIEW" in html
    assert "Welcome to the handbook." in html
    assert "page-break" in html


# ── PDF renderer module ──

def test_pdf_renderer_module_imports():
    """Test that the PDF renderer module can be imported."""
    from app.services.pdf_renderer import render_pdf_from_html, shutdown_browser
    assert callable(render_pdf_from_html)
    assert callable(shutdown_browser)