from __future__ import annotations import json import re import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] STATIC = ROOT / "static" class FrontendBoundaryTests(unittest.TestCase): def test_shared_api_is_the_only_application_fetch_exit(self) -> None: fetch_files = [] for path in STATIC.rglob("*.js"): if "vendor" in path.parts: continue if re.search(r"\bfetch\s*\(", path.read_text(encoding="utf-8")): fetch_files.append(path.relative_to(STATIC).as_posix()) self.assertEqual(fetch_files, ["shared/api.js"]) def test_shared_dependencies_load_before_application(self) -> None: html = (STATIC / "index.html").read_text(encoding="utf-8") pages_position = html.index('/pages.config.js') state_position = html.index('/shared/state.js') api_position = html.index('/shared/api.js') shell_position = html.index('/shared/shell.js') app_position = html.index('/app.js') self.assertLess(pages_position, state_position) self.assertLess(state_position, api_position) self.assertLess(api_position, shell_position) self.assertLess(shell_position, app_position) def test_application_state_is_created_through_shared_boundary(self) -> None: app = (STATIC / "app.js").read_text(encoding="utf-8") self.assertIn("const state = window.XiaobaiState.create({", app) self.assertNotIn("const state = {", app) def test_runtime_page_registry_matches_governance_registry(self) -> None: expected = json.loads( (ROOT / "config" / "pages.config.json").read_text(encoding="utf-8") )["pages"] runtime = (STATIC / "pages.config.js").read_text(encoding="utf-8") rows = re.findall( r'^\s*\["([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", (true|false)\],$', runtime, re.MULTILINE, ) actual = [ { "id": row[0], "title": row[1], "feature": row[2], "group": row[3], "access": row[4], "default": row[5] == "true", "desktop_scroll": "page", "mobile_layout": "dedicated", } for row in rows ] self.assertEqual(actual, expected) def test_shell_owns_navigation_and_page_mounting(self) -> None: app = (STATIC / "app.js").read_text(encoding="utf-8") shell = (STATIC / "shared" / "shell.js").read_text(encoding="utf-8") self.assertNotIn("function syncNavigationState", app) self.assertNotIn("function initializeApplicationShell", app) self.assertIn("function syncNavigation(viewId)", shell) self.assertIn("function mount(viewId, mountOptions = {})", shell) self.assertIn("function openModalDialog(dialog)", shell) self.assertNotIn('document.querySelectorAll(".module-tab").forEach', app) if __name__ == "__main__": unittest.main()