Files
flexly-cms/tests/test_fragments.py
T
phelex 88ac95dd19 Integrate Antirion admin with FastAPI and htmx
Add the shared document API, constructor and expert editors, isolated preview, and fragment catalog metadata with typed block IDs.

Include design references, specification 001, QA scripts, project guidance, and cached Docker dependency installation. Leave example sites for a separate commit.
2026-11-12 21:55:36 +03:00

135 lines
4.4 KiB
Python

import shutil
import unittest
from helpers import NOW, ROOT, SiteTestCase
from site_engine.core.registry import load_site
from site_engine.core.render import Renderer
from site_engine.core.routes import build_route_table
from site_engine.errors import Report
CORE_FRAGMENTS = {
"site_header", "site_footer", "hero", "page_header", "section", "columns", "richtext",
"image", "card_grid", "collection_list", "entry_header", "cta_banner", "form", "faq", "disclaimer",
}
class BuiltinFragmentsTest(SiteTestCase):
def test_builtin_set_matches_adr_006(self):
reg = load_site(self.site)
builtin = {f.id for f in reg.fragments.values() if f.builtin}
self.assertEqual(builtin, CORE_FRAGMENTS)
def test_all_templates_compile_in_sandbox(self):
reg = load_site(self.site)
renderer = Renderer(reg, build_route_table(reg, Report()), Report(), now=NOW)
for fid in CORE_FRAGMENTS:
renderer.env.get_template(f"fragment/{fid}")
def test_every_builtin_has_style_and_flat_unique_id(self):
reg = load_site(self.site)
self.assertEqual(reg.report.errors, [])
for fid in CORE_FRAGMENTS:
self.assertTrue(reg.fragments[fid].style, fid)
def test_steps_support_more_than_three_items_and_escape_titles(self):
shutil.copytree(
ROOT / "examples/ai-studio/fragments/steps",
self.site / "fragments/steps",
dirs_exist_ok=True,
)
self.write("pages/process.md", """---
schema: 1
title: Процесс
status: published
regions:
main:
- id: process_steps
fragment: steps
props:
title: Этапы
items:
- {title: Исследование, text: '**Проверяем** задачу.'}
- {title: Разработка}
- {title: Внедрение}
- {title: '<script>unsafe()</script>'}
---
""")
from site_engine.build.pipeline import build_site
build_site(self.site, self.out, now=NOW)
html = (self.out / "current/process/index.html").read_text(encoding="utf-8")
self.assertEqual(html.count("class='f-steps__item'"), 4)
self.assertIn("aria-hidden='true'>04</span>", html)
self.assertIn("<strong>Проверяем</strong>", html)
self.assertIn("&lt;script&gt;unsafe()&lt;/script&gt;", html)
self.assertNotIn("<script>unsafe()", html)
def test_legacy_card_grid_steps_remains_supported(self):
self.write("pages/legacy-steps.md", """---
schema: 1
title: Старые этапы
status: published
regions:
main:
- id: legacy_steps
fragment: card_grid
props:
variant: steps
items:
- {title: Прежний этап}
---
""")
from site_engine.build.pipeline import build_site
build_site(self.site, self.out, now=NOW)
html = (self.out / "current/legacy-steps/index.html").read_text(encoding="utf-8")
self.assertIn("f-card_grid--steps", html)
self.assertIn("Прежний этап", html)
def test_nested_slots_render(self):
self.write("pages/layout-test.md", """---
title: Колонки
status: published
regions:
main:
- id: s1
fragment: section
props: {title: Секция, text: 'Вводный **текст** секции'}
slots:
content:
- id: c1
fragment: columns
props: {count: 2}
slots:
col_1:
- {id: t1, fragment: richtext, props: {content: Левая колонка}}
col_2:
- {id: t2, fragment: richtext, props: {content: Правая колонка}}
---
""")
from site_engine.build.pipeline import build_site
build_site(self.site, self.out, now=NOW)
html = (self.out / "current" / "layout-test" / "index.html").read_text(encoding="utf-8")
self.assertIn("Левая колонка", html)
self.assertIn("f-columns--2", html)
self.assertIn("Вводный <strong>текст</strong> секции", html)
def test_slot_depth_limit(self):
from site_engine.core.models import parse_block
raw = {"id": "a1", "fragment": "section"}
node = raw
for i in range(2, 7):
child = {"id": f"a{i}", "fragment": "section"}
node["slots"] = {"content": [child]}
node = child
report = Report()
parse_block(raw, "t", report)
self.assertIn("E_BLOCK_DEPTH", report.codes())
if __name__ == "__main__":
unittest.main()