Files
LihonT/tests/test_frames.py
cheb0chik d40c17b083 LihonT 2 Serena beta
Avatar frames that follow the outline of the avatar (ring and parts hung on it), packs of frames that arrive from a catalog on the
site by themselves, the frame shown to other members through the members service, save an avatar by user name, the diary of the
mod, the seek bar on every video, the beta version line and its what is new, plus tests, the catalog builder, the sprites and the
scripts that draw them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-03 10:30:22 +03:00

94 lines
4.2 KiB
Python

"""Avatar frames: the catalog the mod downloads and the outline geometry, on a real JVM, and the catalog builder.
The catalog reader (FrameIndex) and the outline table (Outline) are plain Java, so they are compiled with javac and each scenario
runs in its own process. The parts that draw or download need a device; the runtime compile test covers that they build.
"""
import json
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SOURCES = [ROOT / "src" / "lihont" / "two" / "frame" / "FrameIndex.java",
ROOT / "src" / "lihont" / "two" / "frame" / "Outline.java",
ROOT / "src" / "lihont" / "two" / "plugin" / "Json.java",
ROOT / "src" / "lihont" / "two" / "plugin" / "LtpException.java"]
HARNESS = ROOT / "tests" / "java" / "lihont" / "two" / "frame" / "FrameTest.java"
class FrameJavaTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
javac, cls.java = shutil.which("javac"), shutil.which("java")
if not javac or not cls.java:
raise RuntimeError("Frame tests require a JDK with java and javac")
cls.temporary = tempfile.TemporaryDirectory(prefix="lihont-frame-tests-")
cls.addClassCleanup(cls.temporary.cleanup)
cls.classes = Path(cls.temporary.name)
env = dict(os.environ)
env.pop("JAVA_TOOL_OPTIONS", None)
cls.env = env
result = subprocess.run([javac, "--release", "17", "-encoding", "UTF-8", "-Xlint:all", "-Werror", "-d", str(cls.classes)]
+ [str(s) for s in SOURCES] + [str(HARNESS)],
capture_output=True, text=True, encoding="utf-8", env=env, timeout=120)
if result.returncode:
raise AssertionError(result.stdout + result.stderr)
def scenario(self, name):
result = subprocess.run([self.java, "-Dfile.encoding=UTF-8", "-cp", str(self.classes), "lihont.two.frame.FrameTest", name],
capture_output=True, text=True, encoding="utf-8", env=self.env, timeout=60, cwd=str(ROOT))
self.assertEqual(result.stdout.strip(), "PASS " + name, result.stdout + result.stderr)
def test_the_real_catalog_is_read(self):
self.scenario("real")
def test_bad_catalogs_are_refused(self):
self.scenario("refusals")
def test_bad_entries_are_dropped_and_good_ones_kept(self):
self.scenario("dropped")
def test_ids_names_and_mirrors(self):
self.scenario("ids")
def test_the_outline_geometry(self):
self.scenario("outline")
class CatalogBuilderTest(unittest.TestCase):
def test_the_catalog_is_up_to_date(self):
result = subprocess.run([sys.executable, "-X", "utf8", "-B", str(ROOT / "scripts" / "build_frames_catalog.py"), "--check"],
capture_output=True, text=True, encoding="utf-8", timeout=60)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_every_sprite_matches_its_checksum_and_stays_small(self):
import hashlib
catalog = json.loads((ROOT / "frames" / "index.json").read_text(encoding="utf-8"))
total = 0
for pack in catalog["packs"]:
for sprite in pack["sprites"]:
data = (ROOT / "frames" / sprite["file"]).read_bytes()
self.assertEqual(hashlib.sha256(data).hexdigest(), sprite["sha256"], sprite["file"])
self.assertEqual(len(data), sprite["bytes"], sprite["file"])
self.assertLessEqual(len(data), 150 * 1024)
total += len(data)
self.assertLess(total, 400 * 1024, "the whole basic pack should stay a small download")
def test_every_frame_uses_sprites_of_its_pack_only(self):
catalog = json.loads((ROOT / "frames" / "index.json").read_text(encoding="utf-8"))
for pack in catalog["packs"]:
known = {s["file"] for s in pack["sprites"]}
for frame in pack["frames"]:
for part in frame["parts"]:
self.assertIn(part["file"], known)
self.assertTrue(part["file"].startswith(pack["id"] + "/"))
if __name__ == "__main__":
unittest.main()