78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import unittest
|
|
from html.parser import HTMLParser
|
|
from pathlib import Path
|
|
|
|
|
|
STATIC_DIR = Path(__file__).resolve().parents[1] / "static"
|
|
|
|
|
|
class IdCollector(HTMLParser):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.ids: list[str] = []
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
self.ids.extend(value for key, value in attrs if key == "id" and value)
|
|
|
|
|
|
class FrontendContractTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
cls.html = (STATIC_DIR / "index.html").read_text(encoding="utf-8")
|
|
cls.script = (STATIC_DIR / "app.js").read_text(encoding="utf-8")
|
|
cls.ui_core = (STATIC_DIR / "ui-core.js").read_text(encoding="utf-8")
|
|
collector = IdCollector()
|
|
collector.feed(cls.html)
|
|
cls.ids = collector.ids
|
|
|
|
def test_html_ids_are_unique(self):
|
|
duplicates = sorted({item for item in self.ids if self.ids.count(item) > 1})
|
|
self.assertEqual(duplicates, [])
|
|
|
|
def test_literal_id_selectors_exist_in_html(self):
|
|
selectors = set(re.findall(r'querySelector\("#([A-Za-z][A-Za-z0-9_-]*)"\)', self.script))
|
|
selectors.update(re.findall(r'getElementById\("([A-Za-z][A-Za-z0-9_-]*)"\)', self.script))
|
|
selectors.update(re.findall(r'setText\("([A-Za-z][A-Za-z0-9_-]*)"', self.script))
|
|
missing = sorted(selectors - set(self.ids))
|
|
self.assertEqual(missing, [])
|
|
|
|
def test_all_primary_views_have_navigation_entries(self):
|
|
views = set(re.findall(r'id="([A-Za-z][A-Za-z0-9_-]*View|limitPool)" class="workspace-view', self.html))
|
|
navigation = set(re.findall(r'data-view="([A-Za-z][A-Za-z0-9_-]*)"', self.html))
|
|
self.assertEqual(views, navigation)
|
|
self.assertEqual(len(views), 13)
|
|
|
|
def test_public_knowledge_editors_are_hidden_for_non_admins(self):
|
|
self.assertIn('document.querySelector("#reasonForm").hidden = !isAdmin;', self.script)
|
|
self.assertIn('document.querySelector("#sectorPhaseManager").hidden = !isAdmin;', self.script)
|
|
self.assertIn('const canManage = state.user?.role === "admin";', self.script)
|
|
|
|
def test_shared_ui_core_loads_before_application(self):
|
|
self.assertLess(
|
|
self.html.index('<script src="/ui-core.js"'),
|
|
self.html.index('<script src="/app.js"'),
|
|
)
|
|
for function_name in (
|
|
"number", "clamp", "escapeHtml", "formatNumber", "formatTimestamp",
|
|
"displayCompactDate", "todayString", "localDateString", "parseLocalDate",
|
|
):
|
|
self.assertIn(f"function {function_name}(", self.ui_core)
|
|
|
|
def test_business_views_do_not_expose_engineering_source_labels(self):
|
|
prohibited = (
|
|
"Tushare 实时行情",
|
|
"Tushare 日K",
|
|
"SQLite 缓存",
|
|
"演示日K",
|
|
"rt_k 实时截面",
|
|
)
|
|
for label in prohibited:
|
|
self.assertNotIn(label, self.script)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|