Files
stack/tests/dev/test_lex_probe.py
kert f6c7b63a76 fix(infra): serve lex dossier modules without the page rewrite
The lex catch-all rewrote every path to index.html, so the browser received HTML for app.js and the page stayed on Loading. Module requests now use a higher-priority router that leaves the path alone.
2026-11-14 17:13:23 -05:00

363 lines
12 KiB
Python

"""dev/scripts/lex_probe.py and lex_dump_api.py: the lex headless probe and
the API dump its fixture mode serves (lex S1 Task 5, D13)."""
from __future__ import annotations
import importlib.util
import json
import re
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[2]
def _load(name: str, path: Path):
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
return mod
probe = _load("_lex_probe", ROOT / "dev/scripts/lex_probe.py")
dump = _load("_lex_dump_api", ROOT / "dev/scripts/lex_dump_api.py")
def test_route_target_mirrors_traefik() -> None:
rt = probe.route_target
assert rt("/api/policy/contents") == ("api", "/api/policy/contents")
assert rt("/api/policy/dossier/header") == ("api", "/api/policy/dossier/header")
assert rt("/assets/dossier/view-payment.js") == ("asset", "view-payment.js")
assert rt("/assets/dossier/app.js") == ("asset", "app.js")
for path in (
"/assets/dossier/%2e%2e/img/logo.svg",
"/assets/dossier/sub/x.js",
"/assets/dossier/X.js",
"/assets/dossier/index.html",
"/assets/dossier/1x.js",
"/api/policyx",
"/api/policy",
"/chat",
"/",
"/dossier",
):
assert rt(path) == ("page", ""), path
def test_services_yml_serves_lex_modules_without_the_page_rewrite() -> None:
"""The catch-all rewrites every lex path to index.html. Module requests
have to hit a higher-priority router that does not use that middleware,
or the browser receives HTML for app.js and the page stays on Loading."""
src = (ROOT / "infra/traefik/dynamic/services.yml").read_text()
rule = "PathRegexp(`^/assets/dossier/[a-z][a-z0-9-]*[.]js$`)"
blocks = re.findall(
r"\n (lex-assets(?:-tls)?):\n(.*?)(?=\n [a-z0-9-]+:\n)",
src,
re.S,
)
assert [name for name, _ in blocks] == ["lex-assets", "lex-assets-tls"]
for name, body in blocks:
assert rule in body, name
assert "priority: 1000" in body, name
assert "service: git" in body, name
assert "lex-page" not in body, name
assert "git-sso" in body and "dossier-nocache" in body, name
def test_probe_imports_without_playwright() -> None:
src = (ROOT / "dev/scripts/lex_probe.py").read_text(encoding="utf-8")
top = src.split("\ndef main(", 1)[0]
assert "playwright" not in re.sub(r'""".*?"""', "", top, flags=re.S)
def test_views_mirror_state_js() -> None:
js = (ROOT / "infra/gitea/custom/public/assets/dossier/state.js").read_text()
m = re.search(r"export const VIEWS = \[(.*?)\];", js, re.S)
assert m
assert list(probe.VIEWS) == re.findall(r'"([a-z]+)"', m.group(1))
def test_view_url() -> None:
assert probe.view_url("http://lex.test/", "timeline", "A/D") == (
"http://lex.test/timeline?family=A%2FD"
)
assert probe.view_url("http://lex.test", "contents", "HOME-VISITS") == (
"http://lex.test/contents?family=HOME-VISITS"
)
def test_host_rules() -> None:
assert probe.host_rules([]) is None
assert probe.host_rules(["lex.test=traefik"]) == "MAP lex.test traefik"
assert (
probe.host_rules(["a.test=b", "c.test=d:8080"])
== "MAP a.test b, MAP c.test d:8080"
)
for bad in ("nohost", "=x", "x=", "a b=c"):
with pytest.raises(SystemExit):
probe.host_rules([bad])
def test_console_noise() -> None:
assert probe.ignorable("https://fonts.googleapis.com/css2?family=x")
assert probe.ignorable("https://fonts.gstatic.com/s/a.woff2")
assert not probe.ignorable("http://lex.test/assets/dossier/app.js")
assert not probe.ignorable("")
def _api_dir(tmp_path: Path) -> Path:
d = tmp_path / "api"
(d / "dossier").mkdir(parents=True)
(d / "fact").mkdir()
(d / "contents.json").write_text("{}")
(d / "dossier/header.json").write_text("{}")
(d / "fact/F1.json").write_text("{}")
return d
def test_fixture_file(tmp_path: Path) -> None:
d = _api_dir(tmp_path)
ff = probe.fixture_file
assert ff(d, "/api/policy/contents", "", "HV") == (200, d / "contents.json")
assert ff(d, "/api/policy/dossier/header", "family=HV", "HV") == (
200,
d / "dossier/header.json",
)
assert ff(d, "/api/policy/dossier/header", "family=hv", "HV")[0] == 200
assert ff(d, "/api/policy/fact", "family=HV&id=F1", "HV") == (
200,
d / "fact/F1.json",
)
# a slash key travels encoded
assert ff(d, "/api/policy/dossier/header", "family=A%2FD", "A/D")[0] == 200
for path, query in (
("/api/policy/dossier/timeline", "family=HV"), # not dumped
("/api/policy/dossier/header", "family=CCM"), # another family
("/api/policy/dossier/header", ""),
("/api/policy/dossier/..", "family=HV"),
("/api/policy/dossier/a/b", "family=HV"),
("/api/policy/fact", "family=HV&id=F2"),
("/api/policy/fact", "family=HV&id=../x"),
("/api/policy/other", "family=HV"),
):
status, body = ff(d, path, query, "HV")
assert status == 404 and isinstance(body, dict), (path, query)
assert body["detail"]["message"]
assert ff(d, "/api/policy/dossier/header", "family=CCM", "HV")[1]["detail"] == {
"message": "No dossier is built for CCM yet.",
"family": "CCM",
"status": "unknown",
}
def _also(url: str = "", locator: str = "", label: str = "") -> dict:
return {"url": url, "locator": locator, "label": label}
def test_fact_misses() -> None:
e = {"id": "F1", "url": "https://x.test/a", "locator": "", "label": "A", "also": []}
assert probe.fact_misses(e, ["https://x.test/a"], []) == []
assert probe.fact_misses(e, [], []) == ["F1: link https://x.test/a not rendered"]
b = {"id": "F2", "url": "", "locator": "CPT 2019 p. 20", "label": "x", "also": []}
assert probe.fact_misses(b, [], ["CPT 2019 p. 20"]) == []
assert probe.fact_misses(b, ["CPT 2019 p. 20"], []) == [
"F2: locator 'CPT 2019 p. 20' not rendered"
]
n = {"id": "F3", "url": "", "locator": "", "label": "Comment 1", "also": []}
assert probe.fact_misses(n, [], ["Comment 1"]) == []
a = {**e, "also": [_also("https://x.test/s"), _also("https://x.test/t")]}
assert probe.fact_misses(a, ["https://x.test/a", "https://x.test/s"], []) == [
"F1: also link https://x.test/t not rendered"
]
def test_fact_misses_book_also_cites() -> None:
"""S1-P10: a licensed-book also-cite (no url) is matched by its locator."""
e = {
"id": "F9",
"url": "https://x.test/a",
"locator": "",
"label": "A",
"also": [
_also(locator="CPT Changes 2023 p. 48"),
_also(locator="CPT 2024 p. 29"),
],
}
links = ["https://x.test/a"]
assert (
probe.fact_misses(e, links, ["CPT Changes 2023 p. 48", "CPT 2024 p. 29"]) == []
)
assert probe.fact_misses(
e, links, ["CPT Changes 2023 p. 48", "CPT 2024 p. 30"]
) == ["F9: also locator 'CPT 2024 p. 29' not rendered"]
# the count alone is not enough: the text must match
assert probe.fact_misses(e, links, ["x", "y"]) == [
"F9: also locator 'CPT Changes 2023 p. 48' not rendered",
"F9: also locator 'CPT 2024 p. 29' not rendered",
]
# no locator: the label stands in, as citeHtml renders it
b = {**e, "also": [_also(label="Comment 9")]}
assert probe.fact_misses(b, links, ["Comment 9"]) == []
def test_facts_line() -> None:
assert probe.facts_line(546, 546, 540, 6) == (
"facts 546 rendered 546 links ok 540 locator-only 6"
)
def test_dump_layout_feeds_fixture_mode(tmp_path: Path) -> None:
calls = []
def load_slice(family: str, section: str) -> dict:
calls.append(section)
if section == "facts":
return {"facts": [{"id": "F1"}, {"id": "F2"}]}
return {"section": section, "family": family}
out = tmp_path / "api"
n = dump.dump(
out,
"HOME-VISITS",
load_slice=load_slice,
load_contents=lambda: {"families": []},
load_fact=lambda family, fid: {"id": fid, "family": family},
)
assert n == {"sections": 7, "facts": 2}
assert calls == list(dump.SECTIONS)
assert json.loads((out / "dossier/header.json").read_text()) == {
"section": "header",
"family": "HOME-VISITS",
}
assert json.loads((out / "fact/F2.json").read_text())["id"] == "F2"
ff = probe.fixture_file
assert ff(out, "/api/policy/contents", "", "HOME-VISITS")[0] == 200
for s in dump.SECTIONS:
assert (
ff(out, f"/api/policy/dossier/{s}", "family=HOME-VISITS", "HOME-VISITS")[0]
== 200
)
for fid in ("F1", "F2"):
assert (
ff(out, "/api/policy/fact", f"family=HOME-VISITS&id={fid}", "HOME-VISITS")[
0
]
== 200
)
def test_dump_sections_match_the_api() -> None:
from llm.policy_slices import SECTIONS
assert tuple(dump.SECTIONS) == tuple(SECTIONS)
class _Route:
def __init__(self, url: str) -> None:
self.request = type("R", (), {"url": url})()
self.done: dict | None = None
def continue_(self) -> None:
self.done = {"continued": True}
def fulfill(self, **kw) -> None:
self.done = kw
def test_fixture_routes(tmp_path: Path) -> None:
api = _api_dir(tmp_path)
(api / "dossier/header.json").write_text('{"name": "H"}')
assets = tmp_path / "assets"
assets.mkdir()
(assets / "app.js").write_text("export {};")
page = tmp_path / "index.html"
page.write_text("<p>page</p>")
args = probe.parse_args(
[
"--base",
"http://lex.test",
"--family",
"HV",
"--check",
"smoke",
"--fixture",
str(api),
"--assets",
str(assets),
"--page",
str(page),
]
)
seen = {}
class Ctx:
def route(self, pattern: str, handler) -> None:
seen[pattern] = handler
probe._install_fixture(Ctx(), args)
handle = seen["**/*"]
def hit(url: str) -> dict:
r = _Route(url)
handle(r)
return r.done
assert hit("https://fonts.googleapis.com/css2?x") == {"continued": True}
d = hit("http://lex.test/api/policy/dossier/header?family=HV")
assert d["status"] == 200 and d["body"] == b'{"name": "H"}'
assert d["content_type"] == "application/json"
d = hit("http://lex.test/api/policy/dossier/payment?family=HV")
assert d["status"] == 404 and json.loads(d["body"])["detail"]["message"]
d = hit("http://lex.test/assets/dossier/app.js?v=abc")
assert (d["status"], d["content_type"], d["body"]) == (
200,
"text/javascript",
b"export {};",
)
assert hit("http://lex.test/assets/dossier/state.js")["status"] == 404
for url in (
"http://lex.test/timeline?family=HV",
"http://lex.test/assets/dossier/X.js",
):
d = hit(url)
assert d["status"] == 200 and d["body"] == b"<p>page</p>", url
assert d["content_type"].startswith("text/html")
def test_parse_args_fixture_trio() -> None:
base = ["--base", "http://lex.test", "--family", "HV", "--check", "facts"]
a = probe.parse_args([*base, "--map", "a.test=b", "--map", "c.test=d"])
assert a.map == ["a.test=b", "c.test=d"] and a.fixture is None
with pytest.raises(SystemExit):
probe.parse_args([*base, "--fixture", "/x"])
with pytest.raises(SystemExit):
probe.parse_args([*base[:4], "--check", "other"])
def test_dump_main(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys) -> None:
from llm import config, policy
cfg = object()
monkeypatch.setattr(config, "load", lambda: cfg)
monkeypatch.setattr(
policy,
"load_slice",
lambda c, family, section: (
{"facts": [{"id": "F1"}]} if section == "facts" else {"s": section}
),
)
monkeypatch.setattr(policy, "load_contents", lambda c: {"families": []})
monkeypatch.setattr(policy, "load_fact", lambda c, family, fid: {"id": fid})
out = tmp_path / "api"
assert dump.main(["--family", "HV", "--out", str(out)]) == 0
assert json.loads((out / "fact/F1.json").read_text()) == {"id": "F1"}
assert json.loads((out / "dossier/valuation.json").read_text()) == {
"s": "valuation"
}
assert capsys.readouterr().out.strip() == (
f"wrote {out}: contents, 7 sections, 1 facts"
)