Spaces:
Running

syntheogenesis / tests /_frontend.py
github-actions[bot]
Deploy e68c206
cf826f7
Raw History Blame Contribute Delete
3.56 kB
"""The front end as the tests mean it: one script, however many files it is.
For years `dee/static/app.js` WAS the front end, so a test that wanted to
assert something about the front end read that path, and 74 test files across
this suite still name it — 129 times. That was accurate and is now a trap. The
file reached 24,449 lines, which is more than fits in one sitting or one
context window, and a file nobody reads whole is a file where edits get made
blind; the `.mc-find[hidden]` bug in #413 was exactly that. It has to come
apart. But every extraction moves text out of app.js, and 129 assertions that
name app.js by path would fail on text that had simply moved — so the file
could not be split without breaking dozens of tests that have no interest in
which file anything lives in.
So the assertions follow the CODE rather than the filename. `app_js()` returns
app.js concatenated with every file lifted out of it, in the order index.html
loads them, and a test asserting "this string is in the front end" keeps
meaning that through any future extraction.
Scope is deliberate. This is NOT every script the page loads: cockpit.js,
trace.js and the rest are separate programs that were never part of app.js,
and folding them in would quietly change the many tests here that count
occurrences rather than merely look for one.
The membership rule is self-describing, like the coupled asset set in
dee/server.py: a lifted file says so in its own header, and this reads that
rather than keeping a list beside it. A list is a second thing to update, and
the way it fails is a file dropping silently out of everything asserted here.
"""
from __future__ import annotations
import pathlib
import re
STATIC = pathlib.Path(__file__).resolve().parent.parent / "dee" / "static"
ROOT = "app.js"
#: What a file lifted out of app.js writes in its own header to say so.
LIFTED_MARKER = "lifted out of app.js"
def app_js_files() -> list[pathlib.Path]:
"""app.js and everything lifted out of it, in index.html's load order.
Load order, not alphabetical: these files share one global scope and the
whole reason the split is safe is that document order is execution order.
A test that reasons about what is defined before what needs to see them in
the order the browser does.
"""
html = (STATIC / "index.html").read_text(encoding="utf-8")
loaded = re.findall(r'<script src="/static/([A-Za-z0-9_.-]+\.js)\?', html)
out = []
for name in loaded:
if name in out:
continue
path = STATIC / name
if name == ROOT:
out.append(name)
elif path.is_file() and LIFTED_MARKER in path.read_text(encoding="utf-8")[:4000]:
out.append(name)
assert ROOT in out, (
f"index.html does not load {ROOT}. Either the front end was renamed "
f"and this helper needs to know, or the include was dropped — in which "
f"case the app is broken and every assertion here is meaningless.")
return [STATIC / n for n in out]
def app_js() -> str:
"""The front-end script as one string."""
return "\n".join(p.read_text(encoding="utf-8") for p in app_js_files())
def which_file(needle: str) -> list[str]:
"""Which of the front-end files contain `needle` — for failure messages.
A test that fails on a missing string is much easier to read when it can
say the string is in plasmid-studio.js rather than that it is absent.
"""
return [p.name for p in app_js_files()
if needle in p.read_text(encoding="utf-8")]