refactor: establish frontend request and state boundaries

This commit is contained in:
leefer
2026-07-29 20:16:21 +08:00
parent a368174a75
commit e177f21d1c
7 changed files with 421 additions and 242 deletions
+37
View File
@@ -0,0 +1,37 @@
from __future__ import annotations
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")
state_position = html.index('/shared/state.js')
api_position = html.index('/shared/api.js')
app_position = html.index('/app.js')
self.assertLess(state_position, api_position)
self.assertLess(api_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)
if __name__ == "__main__":
unittest.main()