# -*- coding: utf-8 -*- """V21 图像渲染器测试:4 渲染器 + 加载协议 + --img CLI。""" from __future__ import annotations import json import os import subprocess import sys import tempfile import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from holoaeon.concept_anatomy import ConceptAnatomizer # noqa: E402 from holoaeon import concept_anatomy_render as rmod # noqa: E402 def _anatomy(): return ConceptAnatomizer().anatomize("经络") class TestRenderers(unittest.TestCase): @classmethod def setUpClass(cls): cls.result = _anatomy() cls.tokens = {**rmod.DEFAULT_TOKENS} def test_extract(self): d = rmod._extract(self.result) self.assertEqual(d["concept"], "经络") self.assertGreaterEqual(len(d["components"]), 6) def test_explosion_svg(self): svg = rmod.render_explosion(self.result, self.tokens) self.assertTrue(svg.startswith("custom", "t") fn = rmod.load_render("custom_test") self.assertEqual(fn({}, {}), "custom") def test_render_protocol_entry(self): svg = rmod.render(self.result, {"img_style": "explosion"}) self.assertIn("概念爆炸图", svg) class TestRenderCli(unittest.TestCase): @classmethod def setUpClass(cls): cls.repo = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def test_module_main_save(self): with tempfile.TemporaryDirectory() as td: out = os.path.join(td, "pen.svg") r = subprocess.run( [sys.executable, "-X", "utf8", "-m", "holoaeon.concept_anatomy", "经络", "--img", "explosion", "--save", out], cwd=self.repo, capture_output=True, text=True, encoding="utf-8", timeout=60) self.assertEqual(r.returncode, 0, r.stderr) d = json.loads(r.stdout) self.assertEqual(d["saved"], out) self.assertEqual(d["style"], "explosion") self.assertTrue(os.path.isfile(out)) with open(out, encoding="utf-8") as f: head = f.read(60) self.assertTrue(head.startswith("