Skip to content

~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.

4.0 KB
Raw Blame
"""Guard against loading safe-fetch-dependent components too early.

base.html renders ``{% block content %}`` BEFORE its shared deferred
scripts (ui.js, api.js, url-validator.js, safe-fetch.js) and renders
``{% block page_scripts %}`` after them. Deferred scripts execute in
document order, so a ``<script defer>`` emitted inside the content block
runs before ``safeFetchWithAuth`` exists.

The annotation/notes components (annotation_surface.js, document_notes.js,
research_notes.js) call ``safeFetchWithAuth`` during their synchronous
init (they run at readyState 'interactive', where the
``readyState === 'loading'`` DOMContentLoaded branch is never taken).
Loading them from the content block therefore fails deterministically on
every page view — this broke notes/annotations on the library document
pages while results.html (page_scripts block) worked.

This test pins the invariant: any page template loading one of these
components must do so inside ``{% block page_scripts %}``.
"""

import re
from pathlib import Path

import local_deep_research

_PAGES = (
    Path(local_deep_research.__file__).parent / "web" / "templates" / "pages"
)

# Components whose synchronous init depends on base.html's deferred shared
# scripts (safeFetchWithAuth from safe-fetch.js).
_SAFE_FETCH_COMPONENTS = (
    "embedding_performance_hint.js",
    "components/annotation_surface.js",
    "components/document_notes.js",
    "components/research_notes.js",
)

_PAGE_SCRIPTS_BLOCK_RE = re.compile(
    r"{%\s*block\s+page_scripts\s*%}(.*?){%\s*endblock\s*%}", re.DOTALL
)


def _component_script_offsets(html: str) -> dict[str, int]:
    offsets = {}
    for component in _SAFE_FETCH_COMPONENTS:
        m = re.search(
            r'<script\b[^>]*\bsrc="/static/js/' + re.escape(component) + '"',
            html,
        )
        if m:
            offsets[component] = m.start()
    return offsets


def test_safe_fetch_components_load_from_page_scripts_block():
    violations = []
    seen_any = False
    for template in sorted(_PAGES.glob("*.html")):
        html = template.read_text(encoding="utf-8")
        offsets = _component_script_offsets(html)
        if not offsets:
            continue
        seen_any = True
        block = _PAGE_SCRIPTS_BLOCK_RE.search(html)
        for component, offset in offsets.items():
            inside = block is not None and (
                block.start() <= offset < block.end()
            )
            if not inside:
                violations.append(
                    f"{template.name} loads {component} outside "
                    "{% block page_scripts %} — it would execute before "
                    "safe-fetch.js and fail at init on every page load"
                )

    # Sanity: the scan must find the known consumers, or a rename would
    # make this test silently pass on nothing.
    assert seen_any, (
        "no page template loads the safe-fetch-dependent components any "
        "more — update _SAFE_FETCH_COMPONENTS if they were renamed"
    )
    assert not violations, "\n".join(violations)


def test_known_consumers_still_covered():
    # The templates that load these components today. If one stops
    # loading them, this list (not the invariant) needs updating.
    expected = {
        "collections.html": {"embedding_performance_hint.js"},
        "collection_details.html": {"embedding_performance_hint.js"},
        "embedding_settings.html": {"embedding_performance_hint.js"},
        "document_details.html": {"components/document_notes.js"},
        "document_text.html": {
            "components/annotation_surface.js",
            "components/document_notes.js",
        },
        "results.html": {
            "components/annotation_surface.js",
            "components/research_notes.js",
        },
    }
    for name, components in expected.items():
        html = (_PAGES / name).read_text(encoding="utf-8")
        found = set(_component_script_offsets(html))
        assert components <= found, (
            f"{name}: expected {sorted(components)} to be loaded, "
            f"found {sorted(found)}"
        )