44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
import unittest
|
|
from pathlib import Path
|
|
|
|
from scripts.optimize_assets import (
|
|
build_webp_path,
|
|
classify_large_image,
|
|
should_keep_conversion,
|
|
should_prune_original,
|
|
)
|
|
|
|
|
|
class OptimizeAssetsRulesTest(unittest.TestCase):
|
|
def test_rejects_webp_when_not_smaller_than_source(self) -> None:
|
|
self.assertFalse(should_keep_conversion(120_000, 123_000))
|
|
|
|
def test_accepts_materially_smaller_webp_for_large_file(self) -> None:
|
|
self.assertTrue(should_keep_conversion(500_000, 430_000))
|
|
|
|
def test_rejects_small_gain_below_threshold(self) -> None:
|
|
self.assertFalse(should_keep_conversion(200_000, 191_000))
|
|
|
|
def test_flags_large_by_filesize(self) -> None:
|
|
self.assertTrue(classify_large_image(301_000, 1200, 900))
|
|
|
|
def test_flags_large_by_dimensions(self) -> None:
|
|
self.assertTrue(classify_large_image(120_000, 3000, 1800))
|
|
|
|
def test_uses_extension_qualified_webp_name_when_stem_collides(self) -> None:
|
|
target = build_webp_path(Path("assets/images/sj.jpg"), {"assets/images/sj"})
|
|
self.assertEqual(target.as_posix(), "assets/images/sj.jpg.webp")
|
|
|
|
def test_prunes_original_when_webp_exists_and_original_is_unreferenced(self) -> None:
|
|
self.assertTrue(
|
|
should_prune_original(
|
|
Path("assets/images/banner.jpg"),
|
|
{"index.html", "assets/images/banner.webp"},
|
|
{"assets/images/banner.webp"},
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|