#!/usr/bin/env python3 # /// script # requires-python = ">=3.11" # /// import json import re import unittest from pathlib import Path import stamp_static ROOT = Path(__file__).resolve().parent.parent PEEK_JS = (ROOT / "static" / "bufo-peek.js").read_text() def peek_sprites() -> dict[str, dict]: block = re.search(r"/\* sprites:start \*/\s*const SPRITES = (.*?);\s*/\* sprites:end \*/", PEEK_JS, re.S) assert block, "sprites block markers missing from static/bufo-peek.js" return json.loads(block.group(1)) class PeekSpriteTests(unittest.TestCase): def test_every_sprite_exists_in_the_catalog(self): catalog = set(json.loads((ROOT / "scripts" / "captions.json").read_text())) sprites = peek_sprites() self.assertGreater(len(sprites), 50) self.assertEqual([n for n in sprites if n not in catalog], []) def test_every_sprite_has_a_placeable_cut_and_a_sane_box(self): placements = set(re.findall(r"^ '?([a-z-]+)'?: \{", PEEK_JS.split("const placements = {")[1].split("\n };")[0], re.M)) for name, entry in peek_sprites().items(): with self.subTest(name): self.assertIn(entry["cut"], placements) left, top, right, bottom = entry["box"] self.assertTrue(0 <= left < right <= 1 and 0 <= top < bottom <= 1) def test_names_the_script_special_cases_are_in_the_block(self): special = set(re.findall(r"'(bufo-[a-z-]+\.(?:png|gif))'", PEEK_JS.split("/* sprites:end */")[1])) self.assertTrue(special) self.assertEqual(special - set(peek_sprites()), set()) def test_self_hosted_gif_is_present(self): name = re.search(r"GIF_URL = '/static/([^']+)'", PEEK_JS).group(1) self.assertTrue((ROOT / "static" / name).is_file()) class StampTests(unittest.TestCase): def test_index_stamps_match_file_contents(self): html = (ROOT / "static" / "index.html").read_text() self.assertIn("/static/bufo-peek.js?v=", html) self.assertEqual(stamp_static.stamped(html), html, "run: uv run scripts/stamp_static.py") if __name__ == "__main__": unittest.main()