mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 23:38:37 +00:00
109 lines
4.5 KiB
Python
109 lines
4.5 KiB
Python
"""Unit tests for tools/gallery.py standalone 3D Studio & Asset Gallery server."""
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
import unittest
|
|
import urllib.request
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
ROOT = os.path.dirname(HERE)
|
|
TOOLS_DIR = os.path.join(ROOT, "tools")
|
|
if TOOLS_DIR not in sys.path:
|
|
sys.path.insert(0, TOOLS_DIR)
|
|
|
|
import gallery
|
|
|
|
|
|
class TestGalleryCategorization(unittest.TestCase):
|
|
def test_categories(self) -> None:
|
|
self.assertEqual(gallery.categorize_model("ped_lucia_body.glb"), "Персонажи (Characters)")
|
|
self.assertEqual(gallery.categorize_model("veh_infernus.glb"), "Транспорт (Vehicles)")
|
|
self.assertEqual(gallery.categorize_model("w_rifle_m4.glb"), "Оружие (Weapons)")
|
|
self.assertEqual(gallery.categorize_model("build_wall_01.glb"), "Карта и Здания (World)")
|
|
self.assertEqual(gallery.categorize_model("prop_chair_office.glb"), "Объекты и Пропы (Props)")
|
|
self.assertEqual(gallery.categorize_model("tree_palm_02.glb"), "Растительность (Nature)")
|
|
self.assertEqual(gallery.categorize_model("misc_something.glb"), "Прочее (Miscellaneous)")
|
|
|
|
def test_folder_inference(self) -> None:
|
|
self.assertEqual(gallery.categorize_model("asset_01.glb", "clean_vehicles"), "Транспорт (Vehicles)")
|
|
self.assertEqual(gallery.categorize_model("model.glb", "unpacked_peds"), "Персонажи (Characters)")
|
|
|
|
|
|
class TestGalleryServer(unittest.TestCase):
|
|
def setUp(self) -> None:
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.models_dir = os.path.join(self.tmp.name, "models")
|
|
self.textures_dir = os.path.join(self.tmp.name, "textures")
|
|
self.viewer_dir = os.path.join(self.tmp.name, "viewer")
|
|
os.makedirs(self.models_dir, exist_ok=True)
|
|
os.makedirs(self.textures_dir, exist_ok=True)
|
|
os.makedirs(self.viewer_dir, exist_ok=True)
|
|
|
|
# Create dummy assets
|
|
with open(os.path.join(self.models_dir, "test_car.glb"), "wb") as f:
|
|
f.write(b"glTF\x02\x00\x00\x00\x0c\x00\x00\x00")
|
|
|
|
with open(os.path.join(self.textures_dir, "test_tex.png"), "wb") as f:
|
|
f.write(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR")
|
|
|
|
with open(os.path.join(self.viewer_dir, "studio.html"), "w", encoding="utf-8") as f:
|
|
f.write("<!DOCTYPE html><html><body><h1>Studio</h1></body></html>")
|
|
|
|
# Configure handler
|
|
gallery.StudioHandler.models_dir = self.models_dir
|
|
gallery.StudioHandler.textures_dir = self.textures_dir
|
|
gallery.StudioHandler.viewer_dir = self.viewer_dir
|
|
|
|
# Start server on an open port
|
|
addr = ("127.0.0.1", 0)
|
|
self.httpd = gallery.socketserver.TCPServer(addr, gallery.StudioHandler)
|
|
self.port = self.httpd.server_address[1]
|
|
self.thread = threading.Thread(target=self.httpd.serve_forever, daemon=True)
|
|
self.thread.start()
|
|
time.sleep(0.1)
|
|
|
|
def tearDown(self) -> None:
|
|
self.httpd.shutdown()
|
|
self.httpd.server_close()
|
|
self.tmp.cleanup()
|
|
|
|
def test_endpoints(self) -> None:
|
|
base = f"http://127.0.0.1:{self.port}"
|
|
|
|
# 1. Studio HTML root
|
|
with urllib.request.urlopen(f"{base}/", timeout=3) as r:
|
|
self.assertEqual(r.status, 200)
|
|
body = r.read().decode("utf-8")
|
|
self.assertIn("<h1>Studio</h1>", body)
|
|
|
|
# 2. Models API
|
|
with urllib.request.urlopen(f"{base}/api/models", timeout=3) as r:
|
|
self.assertEqual(r.status, 200)
|
|
data = json.loads(r.read())
|
|
self.assertEqual(data["count"], 1)
|
|
self.assertEqual(data["models"][0]["name"], "test_car.glb")
|
|
self.assertEqual(data["models"][0]["category"], "Транспорт (Vehicles)")
|
|
|
|
# 3. Textures API
|
|
with urllib.request.urlopen(f"{base}/api/textures", timeout=3) as r:
|
|
self.assertEqual(r.status, 200)
|
|
tdata = json.loads(r.read())
|
|
self.assertEqual(tdata["count"], 1)
|
|
self.assertEqual(tdata["textures"][0]["name"], "test_tex.png")
|
|
|
|
# 4. Binary serving
|
|
with urllib.request.urlopen(f"{base}/models/test_car.glb", timeout=3) as r:
|
|
self.assertEqual(r.status, 200)
|
|
self.assertTrue(r.read().startswith(b"glTF"))
|
|
|
|
with urllib.request.urlopen(f"{base}/textures/test_tex.png", timeout=3) as r:
|
|
self.assertEqual(r.status, 200)
|
|
self.assertTrue(r.read().startswith(b"\x89PNG"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|