BlueGit

timfa hfy-novels

Files

hfy-novels / indexer / tests / test_covers.py

test_covers.py

test_covers.py 4.9 KiB Download Raw
1import tempfile
2import unittest
3from pathlib import Path
4
5from indexer.covers import (
6 HEIGHT,
7 WIDTH,
8 choose_layout,
9 cover_relpath,
10 palette,
11 render_png,
12 render_svg,
13 text_bounds,
14 title_hash,
15 wrap_title,
16 write_cover,
17)
18
19
20class CoverTests(unittest.TestCase):
21 def test_same_title_same_hash_and_bytes(self):
22 a = "The Last Terran"
23 b = "The Last Terran"
24 self.assertEqual(title_hash(a), title_hash(b))
25 self.assertEqual(cover_relpath(a), cover_relpath(b))
26 self.assertEqual(render_svg(a), render_svg(b))
27 # Leading/trailing spatie telt niet mee, zodat dezelfde titel dezelfde cover houdt.
28 self.assertEqual(render_svg(" The Last Terran "), render_svg(a))
29
30 def test_different_titles_differ(self):
31 self.assertNotEqual(render_svg("The Last Terran"), render_svg("A Short Hello"))
32 self.assertNotEqual(palette("The Last Terran")["c1"], palette("A Short Hello")["c1"])
33
34 def test_write_is_deterministic_and_stable_path(self):
35 with tempfile.TemporaryDirectory() as tmp:
36 dest = Path(tmp)
37 rel1 = write_cover(dest, "The Last Terran")
38 data1 = (dest / rel1).read_bytes()
39 rel2 = write_cover(dest, "The Last Terran")
40 data2 = (dest / rel2).read_bytes()
41 self.assertEqual(rel1, rel2)
42 self.assertEqual(data1, data2)
43 self.assertTrue(rel1.startswith("covers/"))
44 self.assertTrue(rel1.endswith(".png") or rel1.endswith(".svg"))
45 data = (dest / rel1).read_bytes()
46 if rel1.endswith(".png"):
47 self.assertTrue(data.startswith(b"\x89PNG"))
48 else:
49 self.assertTrue(data.startswith(b"<svg"))
50
51 def test_svg_is_well_formed_and_sized(self):
52 svg = render_svg("The Last Terran")
53 self.assertIn('xmlns="http://www.w3.org/2000/svg"', svg)
54 self.assertIn(f'width="{WIDTH}"', svg)
55 self.assertIn(f'height="{HEIGHT}"', svg)
56 self.assertIn("The Last Terran", svg)
57 self.assertIn("<linearGradient", svg)
58 self.assertNotIn("<script", svg)
59
60 def test_xml_special_characters_are_escaped(self):
61 svg = render_svg('Cats & Dogs <OC> "Hello"')
62 self.assertIn("Cats &amp; Dogs", svg)
63 self.assertIn("&lt;OC&gt;", svg)
64 self.assertIn("&quot;Hello&quot;", svg)
65 self.assertNotIn("Cats & Dogs", svg)
66 self.assertNotIn("<OC>", svg)
67
68 def test_long_title_wraps_onto_multiple_lines(self):
69 title = (
70 "Wearing Power Armor to a Magic School After the Council Meeting "
71 "and the Unexpected Arrival of the Third Diplomatic Envoy"
72 )
73 font_size, lines = choose_layout(title)
74 self.assertGreaterEqual(len(lines), 3)
75 limit = text_bounds(title)["line_max_chars"]
76 for line in lines:
77 # Ellipsis mag één teken over de schatting gaan; verder niet.
78 self.assertLessEqual(len(line.rstrip("…")), limit)
79 bounds = text_bounds(title)
80 self.assertLessEqual(bounds["last_bottom"], HEIGHT - 8)
81 self.assertGreaterEqual(bounds["band_top"], 0)
82 svg = render_svg(title)
83 self.assertGreaterEqual(svg.count("<tspan"), 3)
84 # De titel staat als geheel in aria-label, maar niet als één tekstregel.
85 self.assertNotIn(f">{title}<", svg)
86
87 def test_very_long_word_breaks_and_ellipsis(self):
88 title = "Supercalifragilisticexpialidocious" * 6
89 lines = wrap_title(title, max_chars=12, max_lines=4)
90 self.assertEqual(len(lines), 4)
91 self.assertTrue(lines[-1].endswith("…"))
92 for line in lines[:-1]:
93 self.assertLessEqual(len(line), 12)
94 bounds = text_bounds(title)
95 self.assertLessEqual(bounds["last_bottom"], HEIGHT - 8)
96
97 def test_png_is_deterministic_and_sized(self):
98 a = render_png("The Last Terran")
99 b = render_png("The Last Terran")
100 self.assertEqual(a, b)
101 self.assertTrue(a.startswith(b"\x89PNG"))
102 from io import BytesIO
103 from PIL import Image
104 img = Image.open(BytesIO(a))
105 self.assertEqual(img.size, (WIDTH, HEIGHT))
106 self.assertLess(len(a), 80_000)
107
108 def test_png_long_title_stays_inside_the_frame(self):
109 title = (
110 "Wearing Power Armor to a Magic School After the Council Meeting "
111 "and the Unexpected Arrival of the Third Diplomatic Envoy"
112 )
113 bounds = text_bounds(title)
114 self.assertGreaterEqual(len(bounds["lines"]), 3)
115 self.assertLessEqual(bounds["last_bottom"], HEIGHT - 8)
116 png = render_png(title)
117 self.assertTrue(png.startswith(b"\x89PNG"))
118
119 def test_empty_title_uses_untitled(self):
120 self.assertEqual(wrap_title("", 16, 4), ["Untitled"])
121 self.assertIn("Untitled", render_svg(" "))
122 self.assertEqual(title_hash(""), title_hash("Untitled"))
123
124
125if __name__ == "__main__":
126 unittest.main()