~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Every fully-literal frontend ``fetch()``/XHR URL must resolve to a real
backend route.
Why this guard exists
---------------------
``test_urls_js.py`` validates the central ``URLS`` table in
``static/js/config/urls.js`` against the live app — but the frontend also
contains ~75 hardcoded ``fetch('/...')`` calls across templates and component
scripts (news-subscription-form.html alone has a dozen), plus a comparable
number of calls through the app's hardened fetch wrappers (below). Those
literals have no guard at all today, and the browser-level Puppeteer suites
run only in the release pipeline (``strict-mode``), so a renamed or removed
route breaks the UI silently and is discovered weeks later at release time.
PR #3299's own changelog shows the failure class is real: five legacy
``/api/news/*`` endpoints were removed and every hardcoded client reference
had to be found by hand.
Scope, deliberately narrow
--------------------------
Only *fully literal* first arguments are checked — ``fetch('/news/api/subscribe')``.
URLs built by interpolation (``fetch(`/api/research/${id}/status`)``), from
variables, or via the ``URLS`` table are skipped: they are not resolvable
statically, and the ``URLS`` table is already covered by ``test_urls_js.py``.
String concatenation is skipped only when it extends the *path* itself
(``fetch('/api/research/' + id + '/status')``) — the captured
``/api/research/`` fragment is not itself a URL and matches no route by
construction, so flagging it would be a false positive, not a caught bug.
Query-string concatenation is *not* skipped, in either of its two idioms:
appended to the path literal itself (``fetch('/news/api/feed?limit=' + n)``
— the path portion before the ``?`` is already complete, since the check
ignores everything from ``?`` on regardless) or concatenated as its own
separate literal (``fetch('/news/api/feed' + '?limit=' + n)`` — the
captured ``/news/api/feed`` is already the complete path, even though it
contains no ``?`` itself; see ``_is_concatenated`` for the precise rule).
Both are still resolvable and still checked. Fragment concatenation
behaves identically, since the check also ignores everything from ``#``
on: an embedded fragment (``fetch('/news/api/feed#row-' + n)``) and a
separate-literal fragment (``fetch('/news/api/feed' + '#row')``) are
both still checked too.
Generated bundles (``dist/``, ``*.min.js``) are excluded — they are build
output, not sources.
Beyond bare ``fetch()``/``XMLHttpRequest.open()``, the guard also recognises
the codebase's hardened fetch wrappers — ``safeFetch``/``safeFetchWithAuth``/
``safeFetchJson`` (static/js/security/safe-fetch.js; ``safeFetchWithAuth`` is
the wrapper the codebase now prefers for new code), ``fetchWithErrorHandling``/
``window.api.fetchWithErrorHandling`` (static/js/services/api.js), and the
smaller per-page helpers ``getJSON``/``postJSON``/``apiGet``/``apiPost``. All
of them take the URL as their first argument, exactly like ``fetch()``, so
the same literal/concatenation/interpolation rules apply unchanged — only
the set of recognised call names is wider. Skipping these wrappers would
leave a large and growing share of the app's actual HTTP surface unguarded,
since the guard's job is to catch every *resolvable* literal URL, not just
those reached via one specific call spelling.
This is the *covered* set, not a complete inventory of every URL-first
wrapper shape in the tree: it currently misses the lowercase ``postJson``
(``notes_shared.js``, case-sensitive regex), ``deleteRequest``/
``getRequest``/``postRequest`` (``delete_manager.js``), and
``_callSynthesize`` (``notes.js``) with its own literal endpoints. None of
these is unresolved today, so nothing is broken, but the guard does not see
them. #6227 tracks widening the recognised set to close this gap.
The route table is derived by AST/regex from the mounted routers rather
than by importing the app, mirroring ``test_route_table_parity.py``'s
approach: no import side effects, no dependency on a bootable environment,
and it runs everywhere. A dynamically-registered route would be missed —
the app registers none today (the parity test's snapshot pins the full
table and would flag any that appears).
A URL passes if it equals a route path (trailing-slash tolerant), matches a
parameterized route (``{id}`` → one segment, ``{path:path}`` → any suffix),
or falls under a mount prefix (``/ws``). Query strings and fragments are
ignored, as are external and non-root-relative URLs.
"""
# allow: no-sut-import — a documentation-reference guardian over
# static files, not behaviour; importing the app would add side effects
# this guard deliberately avoids (see module docstring).
import re
import sys
from functools import lru_cache
from pathlib import Path
import pytest
SRC_ROOT = Path(__file__).resolve().parents[2] / "src" / "local_deep_research"
WEB_DIR = SRC_ROOT / "web"
FASTAPI_APP_FILE = WEB_DIR / "fastapi_app.py"
ROUTERS_DIR = WEB_DIR / "routers"
#: Generated build output — never scanned.
_EXCLUDED_DIR_NAMES = {"dist", "node_modules"}
_HTTP_METHODS = r"get|post|put|patch|delete|head|options"
# ``fetch('...')`` / ``fetch("...")`` / ``fetch(`...`)`` — first argument a
# string literal. Backtick captures are kept only when they contain no
# interpolation.
_FETCH_RE = re.compile(r"\bfetch\(\s*([\"'`])([^\"'`]*)\1")
# ``xhr.open('POST', '...')`` — method then URL, both quoted.
_XHR_OPEN_RE = re.compile(
r"\.open\(\s*[\"'][A-Za-z]+[\"']\s*,\s*([\"'])([^\"']+)\1"
)
# The codebase's hardened fetch wrappers — all of which take the URL as
# their first argument, exactly like ``fetch()`` itself:
# - ``safeFetch`` / ``safeFetchWithAuth`` / ``safeFetchJson``, defined in
# static/js/security/safe-fetch.js and exposed as globals. This is the
# *preferred* wrapper going forward (it validates the URL and, for the
# auth-aware variant, handles session-expiry redirects), but bare
# ``fetch()`` is still the more common call spelling in the tree today
# (192 bare ``fetch(`` call sites vs. 135 across all these wrappers
# combined) — these wrappers are worth guarding because they are a
# large and growing share of the app's HTTP surface, not because they
# already dominate it.
# - ``window.api.fetchWithErrorHandling``, the older wrapper defined in
# static/js/services/api.js (also callable unqualified as
# ``fetchWithErrorHandling`` from within that same file/closure).
# - ``getJSON`` / ``postJSON`` / ``apiGet`` / ``apiPost`` — smaller local
# fetch helpers with the same "URL first" shape, defined per-page/
# component (e.g. templates/pages/zotero.html, static/js/components/
# chat.js). ``postJSON`` is the exception: it is *also* a shared helper
# in static/js/services/api.js (``api.js:142``), not only per-page.
# Without this, every hardcoded URL routed through one of these — the
# common case, not the exception — is invisible to the guard.
_FETCH_WRAPPER_RE = re.compile(
r"\b(?:"
r"safeFetchWithAuth|safeFetchJson|safeFetch"
r"|window\.api\.fetchWithErrorHandling|fetchWithErrorHandling"
r"|getJSON|postJSON|apiGet|apiPost"
r")\(\s*([\"'`])([^\"'`]*)\1"
)
#: Backtick URL containing interpolation — dynamic, skipped by design.
_INTERPOLATION_MARKERS = ("${", "{{", "{%")
def _line_no(text: str, offset: int) -> int:
return text.count("\n", 0, offset) + 1
#: A quoted literal immediately following a ``+`` — used to look one token
#: past the ``+`` to decide what the concatenation is actually building.
_NEXT_LITERAL_RE = re.compile(r"""(['"`])([^'"`]*)\1""")
def _is_concatenated(text: str, match: re.Match[str]) -> bool:
"""True if the matched literal is a truncated *path* fragment continued
by string concatenation, e.g. the ``'/api/research/'`` in
``fetch('/api/research/' + id + '/status')``.
Only a trailing ``+`` matters: the regexes above only ever capture the
*first* quoted fragment of the call's arguments, so the text
immediately before the captured quote is always ``(`` or ``,`` (the
call's opening paren, or the comma after the XHR method) — never
``+``. A leading-``+`` check would therefore be dead code; it is
omitted rather than kept as a check that can never fire.
A trailing ``+`` alone is not enough to call the fragment incomplete,
though. Two independent ways the rest of the path can already be
complete before the ``+``:
1. The captured literal itself contains a ``?`` or a ``#``:
everything before that marker is already a complete path
(``_normalise`` strips the query string / fragment), e.g.
``'<path>?param=' + value`` or ``'<path>#row-' + value``.
2. The captured literal has *no* ``?`` or ``#``, but the very next
token is itself a quoted literal that *starts* with ``?`` or ``#``,
e.g. ``'/api/news/feed' + '?limit=' + n``. Here the query string is
concatenated as its own separate literal rather than appended to
the path literal, so the captured ``/api/news/feed`` is already the
complete, resolvable path — even though it contains no ``?`` and is
followed by a ``+``. Checking only the captured literal's own
contents (case 1) misses this idiom entirely, which is exactly what
made the guard skip (and thus never check) fragments like this one.
Only when neither holds — no ``?``/``#`` in the captured literal,
and the next token after the ``+`` is not a literal beginning with
``?``/``#`` (typically an identifier, e.g. ``... + id + '/status'``) — does the
``+`` genuinely extend the path itself, making the fragment
unresolvable on its own and the case to skip.
"""
after = text[match.end() :].lstrip()
if not after.startswith("+"):
return False
if "?" in match.group(2) or "#" in match.group(2):
return False
next_token = after[1:].lstrip()
next_literal = _NEXT_LITERAL_RE.match(next_token)
if next_literal and next_literal.group(2).startswith(("?", "#")):
return False
return True
@lru_cache(maxsize=None)
def _route_paths() -> set[str]:
"""All route paths the app serves, derived statically.
Mirrors what ``fastapi_app._mount_all`` registers: every
``@router.<method>("...")`` decorator in the mounted router modules
(prefixed by each router's ``APIRouter(prefix=...)``), plus the routes
declared directly on the app (``/``, ``/favicon.ico``,
``/static/{path:path}``).
"""
app_source = FASTAPI_APP_FILE.read_text(encoding="utf-8")
paths: set[str] = set()
# Routes declared on the app object itself.
for match in re.finditer(
rf"@app\.({_HTTP_METHODS})\(\s*[\"']([^\"']+)[\"']", app_source
):
paths.add(match.group(2))
# The mounted router modules: the _router_modules list literal plus the
# api_v1 import that _mount_all seeds the dict with.
router_names = set(
re.findall(
r"\(\s*[\"'][a-z_0-9]+[\"']\s*,\s*\"\.routers\.([a-z_0-9]+)\"\s*\)",
app_source,
)
)
api_v1 = re.search(r"from \.routers\.api_v1 import router", app_source)
if api_v1:
router_names.add("api_v1")
assert router_names, (
"no mounted routers found in fastapi_app.py — the extractor is "
"broken, not the frontend"
)
for name in sorted(router_names):
router_file = ROUTERS_DIR / f"{name}.py"
source = router_file.read_text(encoding="utf-8")
prefix_match = re.search(
r"APIRouter\(\s*[^)]*?prefix\s*=\s*[\"']([^\"']*)[\"']",
source,
flags=re.DOTALL,
)
prefix = prefix_match.group(1) if prefix_match else ""
for match in re.finditer(
rf"@router\.({_HTTP_METHODS})\(\s*[\"']([^\"']+)[\"']", source
):
paths.add(prefix + match.group(2))
return paths
def _route_pattern(route: str) -> re.Pattern[str]:
"""Compile a route path into a matcher for concrete URLs.
``{id}`` matches exactly one segment; ``{path:path}`` matches any
remainder including slashes. Everything else is literal.
"""
parts = []
for segment in route.split("/"):
if not segment:
continue
path_param = re.fullmatch(r"\{([^:}]+):path\}", segment)
plain_param = re.fullmatch(r"\{[^}]+\}", segment)
if path_param:
parts.append("(?:/.+)?")
elif plain_param:
parts.append("/[^/]+")
else:
parts.append("/" + re.escape(segment))
body = "".join(parts) or "/"
# Trailing-slash tolerant.
return re.compile("^" + body + "/?$")
@lru_cache(maxsize=None)
def _route_matchers() -> tuple[re.Pattern[str], ...]:
return tuple(_route_pattern(route) for route in _route_paths())
#: Mount prefixes that swallow arbitrary sub-paths (the Socket.IO ASGI app).
_MOUNT_PREFIXES = ("/ws",)
def _url_resolves(url: str) -> bool:
if any(
url == prefix or url.startswith(prefix + "/")
for prefix in _MOUNT_PREFIXES
):
return True
return any(matcher.match(url) for matcher in _route_matchers())
def _normalise(raw: str) -> str | None:
"""Return the comparable path for a literal URL, or None to skip."""
url = raw.strip()
if not url.startswith("/"):
return None # external, relative, data:, etc.
url = url.split("#", 1)[0].split("?", 1)[0]
if not url or url == "/":
return url or None
return url
def _iter_frontend_files() -> list[Path]:
files: list[Path] = []
for base in (WEB_DIR / "templates", WEB_DIR / "static" / "js"):
if not base.is_dir():
continue
for path in base.rglob("*"):
if not path.is_file():
continue
if path.suffix not in {".html", ".js"}:
continue
if any(part in _EXCLUDED_DIR_NAMES for part in path.parts):
continue
if path.name.endswith(".min.js"):
continue
files.append(path)
return sorted(files)
def _collect_literals() -> dict[str, list[str]]:
"""{normalised_url: [file:line, ...]} for every literal fetch/XHR URL."""
found: dict[str, list[str]] = {}
for path in _iter_frontend_files():
text = path.read_text(encoding="utf-8", errors="replace")
for regex in (_FETCH_RE, _XHR_OPEN_RE, _FETCH_WRAPPER_RE):
for match in regex.finditer(text):
raw = match.group(2)
if any(marker in raw for marker in _INTERPOLATION_MARKERS):
continue
if _is_concatenated(text, match):
continue
normalised = _normalise(raw)
if normalised is None:
continue
rel = path.relative_to(SRC_ROOT.parent.parent)
found.setdefault(normalised, []).append(
f"{rel}:{_line_no(text, match.start())}"
)
return found
def test_frontend_literal_urls_resolve_to_routes():
literals = _collect_literals()
# Anti-vacuity: the extractor must find a substantial body of literals —
# a regex regression that matches nothing would otherwise pass silently.
# Bare fetch()/XHR alone currently yield 47; adding the fetch-wrapper
# coverage (safeFetchWithAuth & co., see module docstring) brings the
# real count to 69. The floor is set well below that observed count —
# room for routine churn — but well above the old 40, so it is *more*
# likely to catch a regression that drops wrapper coverage than the old
# bare-fetch-only floor was. It is not a guarantee for every wrapper
# name on its own: losing `safeFetchWithAuth` alone (the largest single
# contributor) does trip this floor, but losing a smaller wrapper name
# — e.g. `safeFetchJson`, which contributes no distinct literal today —
# would not move this integer at all. `_FETCH_WRAPPER_RE` being wired
# into `_collect_literals` in the first place, and each individual name
# staying matched, is instead asserted by name in
# `test_each_wrapper_name_is_wired_into_the_collector` below.
assert len(literals) >= 60, (
f"only {len(literals)} distinct literal URLs found across "
"templates/static-js — the extractor is likely broken"
)
unresolved = {
url: locations
for url, locations in literals.items()
if not _url_resolves(url)
}
assert not unresolved, (
"Frontend literal fetch()/XHR URLs that no backend route "
"serves:\n"
+ "\n".join(
f" {url} (used at {', '.join(locs)})"
for url, locs in sorted(unresolved.items())
)
+ "\nEither fix the frontend reference or restore/rename the route."
)
@pytest.mark.parametrize(
"text,regex",
[
("fetch('/api/research/' + id + '/status')", _FETCH_RE),
('xhr.open("POST", "/api/upload/" + id)', _XHR_OPEN_RE),
("fetch(`/library/api/document/` + id)", _FETCH_RE),
],
)
def test_concatenated_url_fragment_is_skipped_not_flagged(text, regex):
"""A ``+``-concatenated call must not be treated as if its first quoted
fragment were the whole URL: ``/api/research/`` alone matches no route
(``^/api/research/[^/]+/?$`` requires a following segment), so asserting
it resolves would be a false positive against perfectly valid frontend
code. It must be skipped instead."""
match = next(regex.finditer(text))
assert _is_concatenated(text, match), (
f"expected {match.group(2)!r} in {text!r} to be recognised as a "
"concatenation fragment"
)
def test_standalone_bogus_literal_is_still_caught():
"""The concatenation skip must not neuter the guard: a genuinely wrong,
non-concatenated literal still has to fail ``_url_resolves`` so the
guard can catch it."""
text = "fetch('/api/does-not-exist')"
match = next(_FETCH_RE.finditer(text))
assert not _is_concatenated(text, match)
assert not _url_resolves(match.group(2))
def test_query_string_concatenation_is_not_skipped():
"""Query-string concatenation (``'<path>?param=' + value``) must NOT be
treated as an unresolvable path fragment: ``_normalise`` already strips
everything from ``?`` onward, so the path portion of the literal is
complete and genuinely checkable. A bogus path hidden behind this idiom
(e.g. a removed ``/api/news/*`` route) must still be caught."""
text = "fetch('/api/news/feed?limit=' + n)"
match = next(_FETCH_RE.finditer(text))
assert not _is_concatenated(text, match), (
"query-string concatenation must still be checked, not skipped"
)
normalised = _normalise(match.group(2))
assert normalised == "/api/news/feed"
assert not _url_resolves(normalised)
def test_query_string_as_separate_literal_is_not_skipped():
"""The other query-string-concatenation idiom: the ``?...`` portion is
its own separate literal rather than appended to the path literal, e.g.
``fetch('/api/news/feed' + '?limit=' + n)``. The captured
``/api/news/feed`` fragment contains no ``?`` and is followed by a
``+``, which superficially looks like the genuine path-continuation
idiom (``'/api/research/' + id + '/status'``) — but it is not: the path
is already complete, and the ``+`` is only concatenating on the query
string. It must still be checked, not skipped."""
text = "fetch('/api/news/feed' + '?limit=' + n)"
match = next(_FETCH_RE.finditer(text))
assert match.group(2) == "/api/news/feed"
assert not _is_concatenated(text, match), (
"a captured literal followed by '+ <literal starting with ?>' must "
"still be checked — the path itself is already complete"
)
# A bogus path hidden behind the same idiom must still be caught.
bogus_text = "fetch('/api/does-not-exist' + '?limit=' + n)"
bogus_match = next(_FETCH_RE.finditer(bogus_text))
assert not _is_concatenated(bogus_text, bogus_match)
assert not _url_resolves(bogus_match.group(2))
def test_fragment_concatenated_as_separate_literal_is_not_skipped():
"""Same idiom as above, with a ``#fragment`` instead of a ``?query`` —
the rule also checks for a leading ``#`` on the following literal."""
text = "fetch('/api/research/history' + '#section')"
match = next(_FETCH_RE.finditer(text))
assert not _is_concatenated(text, match)
def test_fragment_embedded_in_concatenated_literal_is_not_skipped():
"""The embedded-fragment idiom: the ``#...`` portion sits inside the
captured literal itself, e.g. ``fetch('/api/x#row-' + id)``. Like an
embedded ``?`` (see ``test_query_string_concatenation_is_not_skipped``),
the path portion before the ``#`` is already complete — ``_normalise``
strips the fragment — so the call must be checked, not skipped."""
text = "fetch('/api/research/history#row-' + id)"
match = next(_FETCH_RE.finditer(text))
assert not _is_concatenated(text, match), (
"fragment-bearing concatenation must still be checked, not skipped"
)
# A bogus path hidden behind the same idiom must still be caught.
bogus_text = "fetch('/api/does-not-exist#row-' + id)"
bogus_match = next(_FETCH_RE.finditer(bogus_text))
assert not _is_concatenated(bogus_text, bogus_match)
normalised = _normalise(bogus_match.group(2))
assert normalised == "/api/does-not-exist"
assert not _url_resolves(normalised)
def test_genuine_path_concatenation_still_skipped():
"""The fix for the query-as-separate-literal idiom must not regress the
genuine path-continuation case: when the token after ``+`` is an
identifier (not a quoted literal), the captured fragment is still an
unresolvable partial path and must still be skipped."""
text = "fetch('/api/research/' + id + '/status')"
match = next(_FETCH_RE.finditer(text))
assert _is_concatenated(text, match)
def test_fetch_wrapper_call_sites_are_recognised():
"""The guard must not be blind to the codebase's hardened fetch
wrappers: a literal URL passed to ``safeFetchWithAuth`` (the wrapper the
codebase now prefers over bare ``fetch()``) must be extracted and
checked exactly like a bare ``fetch()`` call.
This only exercises ``_FETCH_WRAPPER_RE`` in isolation, not whether it
is actually wired into ``_collect_literals`` (see
``test_each_wrapper_name_is_wired_into_the_collector`` for that). A
``_is_concatenated`` check is deliberately not repeated here: the
character right after this capture is always ``,`` (the call's second
argument), never ``+``, so that assertion would always trivially pass
regardless of ``_is_concatenated``'s behaviour — see
``test_fetch_wrapper_genuine_concatenation_still_skipped`` for the real
concatenation coverage on wrapper calls."""
text = "const r = await safeFetchWithAuth('/api/does-not-exist', {method: 'GET'});"
match = next(_FETCH_WRAPPER_RE.finditer(text))
assert match.group(2) == "/api/does-not-exist"
assert not _url_resolves(match.group(2))
@pytest.mark.parametrize(
"text",
[
"safeFetchWithAuth('/settings/api/' + key, {method: 'GET'})",
"safeFetch('/settings/api/' + key)",
"getJSON('/api/research/' + id)",
"postJSON('/api/research/' + id + '/status')",
"apiGet('/api/research/' + id)",
"apiPost('/api/research/' + id, body)",
"window.api.fetchWithErrorHandling('/api/research/' + id)",
],
)
def test_fetch_wrapper_genuine_concatenation_still_skipped(text):
"""The wrapper call shapes reuse ``_is_concatenated`` unchanged, so the
genuine path-continuation skip must still apply to them, not just to
bare ``fetch()``. The ``window.api.fetchWithErrorHandling`` parameter
cannot fail under any single-name mutation of ``_FETCH_WRAPPER_RE``,
for the same ``\\b`` fallback reason disclosed on
``test_each_wrapper_name_is_wired_into_the_collector``."""
match = next(_FETCH_WRAPPER_RE.finditer(text))
assert _is_concatenated(text, match)
#: One bogus, non-resolving literal call per alternative in
#: ``_FETCH_WRAPPER_RE`` — every name the regex recognises, not a subset.
_WRAPPER_FIXTURE_CALLS = {
"safeFetchWithAuth": "safeFetchWithAuth('/bogus/safeFetchWithAuth')",
"safeFetchJson": "safeFetchJson('/bogus/safeFetchJson')",
"safeFetch": "safeFetch('/bogus/safeFetch')",
"window.api.fetchWithErrorHandling": (
"window.api.fetchWithErrorHandling('/bogus/windowApiFetchWithErrorHandling')"
),
"fetchWithErrorHandling": "fetchWithErrorHandling('/bogus/fetchWithErrorHandling')",
"getJSON": "getJSON('/bogus/getJSON')",
"postJSON": "postJSON('/bogus/postJSON')",
"apiGet": "apiGet('/bogus/apiGet')",
"apiPost": "apiPost('/bogus/apiPost')",
}
@pytest.mark.parametrize("wrapper_name", sorted(_WRAPPER_FIXTURE_CALLS))
def test_each_wrapper_name_is_wired_into_the_collector(
wrapper_name, tmp_path, monkeypatch
):
"""Named, per-wrapper proof that a literal URL passed to *this specific*
wrapper name reaches ``_collect_literals()`` — not just that
``_FETCH_WRAPPER_RE`` can match it in isolation
(``test_fetch_wrapper_call_sites_are_recognised`` doesn't touch the
collector at all), and not just that some aggregate integer stays above
a floor (the anti-vacuity floor in
``test_frontend_literal_urls_resolve_to_routes`` only reacts to losing
``safeFetchWithAuth`` — the largest single contributor — or to the
regex being unwired from ``_collect_literals`` at its call site
entirely, both of which drop the count far enough to trip it; every
other wrapper name can silently fall out of ``_FETCH_WRAPPER_RE``
without moving that floor at all).
Dropping any one name from ``_FETCH_WRAPPER_RE`` fails that name's case
here, with one caveat: dropping only the ``window.api.`` qualified
alternative still passes, because ``\\b`` also matches immediately after
the ``.`` in ``window.api.fetchWithErrorHandling(``, so the plain
``fetchWithErrorHandling`` alternative independently matches the same
call site at a later position in the alternation. Only dropping *both*
fails both cases. Unwiring ``_FETCH_WRAPPER_RE`` from
``_collect_literals`` entirely fails every case.
"""
fixture_dir = tmp_path / "static" / "js"
fixture_dir.mkdir(parents=True)
fixture_file = fixture_dir / "wrapper_fixture.js"
fixture_file.write_text(
"\n".join(_WRAPPER_FIXTURE_CALLS.values()), encoding="utf-8"
)
this_module = sys.modules[__name__]
monkeypatch.setattr(
this_module, "_iter_frontend_files", lambda: [fixture_file]
)
# `_collect_literals` also does `path.relative_to(SRC_ROOT.parent.parent)`
# to build its location strings — `tmp_path` lives outside the repo, so
# without this it raises ValueError rather than exercising the wrapper
# logic. Repoint `SRC_ROOT` so `tmp_path` (the real ancestor of
# `fixture_file`) is where that walk-up lands; nothing else reads
# `SRC_ROOT` through this monkeypatched call, since `_iter_frontend_files`
# is replaced above rather than driven by `WEB_DIR`.
monkeypatch.setattr(
this_module, "SRC_ROOT", tmp_path / "src" / "local_deep_research"
)
literals = _collect_literals()
expected_url = re.search(
r"""\(\s*(['"`])([^'"`]*)\1""", _WRAPPER_FIXTURE_CALLS[wrapper_name]
).group(2)
assert expected_url.startswith("/bogus/"), (
f"fixture call text is malformed — extracted {expected_url!r}"
)
assert expected_url in literals, (
f"{wrapper_name!r} call site not surfaced by _collect_literals() — "
"either this name no longer matches in _FETCH_WRAPPER_RE, or the "
"regex is unwired from the collector"
)
assert not _url_resolves(expected_url), (
f"fixture URL {expected_url!r} unexpectedly resolves to a real "
"route — this test relies on it being unresolvable"
)
def test_route_extractor_finds_the_app_level_and_router_routes():
"""The static route table must contain representatives of every shape it
is matched against — app-level, prefixed-router, and parameterized."""
routes = _route_paths()
assert "/" in routes, "app-level root route missing from extraction"
assert any(r.startswith("/api/v1") for r in routes), (
"prefixed router routes missing from extraction"
)
assert any("{" in r for r in routes), (
"parameterized routes missing from extraction"
)
assert "/static/{path:path}" in routes
if __name__ == "__main__": # pragma: no cover
pytest.main([__file__, "-v"])