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.

18.0 KB
Raw Blame
import importlib.util
import os
import re
import stat
import sys
from collections.abc import Mapping
from pathlib import Path
from types import ModuleType, SimpleNamespace

import pytest
import yaml

ROOT = Path(__file__).resolve().parents[2]
HOOK_PATH = ROOT / ".pre-commit-hooks" / "check-url-for-targets.py"
MODULE = "check_url_for_targets"
APP_PATH = Path("src/local_deep_research/web/fastapi_app.py")
TEMPLATES_DIR = Path("src/local_deep_research/web/templates")
KNOWN_TEMPLATE = {"x.html": "{{ url_for('known') }}"}
NO_LITERAL_TEMPLATE = {"x.html": "<main>ready</main>"}
NO_LITERAL_DIAGNOSTIC = "No literal url_for calls. Fix: restore the input."


@pytest.fixture
def hook() -> ModuleType:
    assert HOOK_PATH.is_file(), f"hook is missing: {HOOK_PATH}"
    spec = importlib.util.spec_from_file_location(MODULE, HOOK_PATH)
    assert spec is not None and spec.loader is not None
    module = importlib.util.module_from_spec(spec)
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


def source(body: str) -> str:
    return f"def _setup_template_globals():\n    {body.replace(chr(10), chr(10) + '    ')}\n"


def make_tree(
    tmp_path: Path, app_source: str, templates: Mapping[str, str]
) -> tuple[Path, Path]:
    app_path, templates_dir = (
        tmp_path / "fastapi_app.py",
        tmp_path / "templates",
    )
    app_path.write_text(app_source, encoding="utf-8")
    for relative_path, content in templates.items():
        path = templates_dir / relative_path
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(content, encoding="utf-8")
    return app_path, templates_dir


def check(hook, tmp_path: Path, app_source: str, templates: Mapping[str, str]):
    return hook.check_url_targets(*make_tree(tmp_path, app_source, templates))


VALID_SOURCE = source("_URL_MAP = {'known': '/', 'other': '/other'}")
SINGLE_MAP_SOURCE = "_URL_MAP = {'known': '/'}"
INVALID_MAPS = "_URL_MAP = []\x1f_URL_MAP = {'known': '/', 'known': '/again'}\x1f_URL_MAP = {**{'known': '/'}}\x1f_URL_MAP = {1: '/'}\x1f_URL_MAP = {'known': 1}".split(
    "\x1f"
)
MUTATIONS = "_URL_MAP = replacement\x1f_URL_MAP: dict[str, str] = replacement\x1f_URL_MAP |= replacement\x1f(_URL_MAP := replacement)\x1fdel _URL_MAP\x1f_URL_MAP['x'] = '/x'\x1fdel _URL_MAP['x']\x1f_URL_MAP.update({})\x1f_URL_MAP.setdefault('x', '/')\x1f_URL_MAP.pop('known')\x1f_URL_MAP.popitem()\x1f_URL_MAP.clear()\x1f_URL_MAP.__setitem__('x', '/')\x1f_URL_MAP.__delitem__('known')\x1falias = _URL_MAP\x1ffirst = second = _URL_MAP\x1falias: dict[str, str] = _URL_MAP\x1f(alias := _URL_MAP)".split(
    "\x1f"
)


def test_current_tree_has_22_mapped_literal_names(hook):
    result = hook.check_url_targets(APP_PATH, TEMPLATES_DIR)
    assert result.diagnostics == () and len(result.literal_names) == 22


def test_unmapped_literal_reports_location(hook, tmp_path):
    templates = {
        "z.html": "{{ url_for('missing') }}",
        "nested/a.html": '\n{{ url_for("missing") }}',
    }
    result = check(hook, tmp_path, VALID_SOURCE, templates)
    assert [item.render() for item in result.diagnostics] == [
        f"{tmp_path / 'templates/nested/a.html'}:2: url_for name 'missing' is absent from _URL_MAP. Fix: add it to _URL_MAP.",
        f"{tmp_path / 'templates/z.html'}:1: url_for name 'missing' is absent from _URL_MAP. Fix: add it to _URL_MAP.",
    ]


@pytest.mark.parametrize(
    "app_source",
    [
        "def unrelated():\n    pass\n",
        source("_URL_MAP = {'known': '/'}")
        + source("_URL_MAP = {'other': '/'}"),
        "async def _setup_template_globals():\n    pass\n",
        source("def nested():\n    _URL_MAP = {'known': '/'}"),
        source("class Holder:\n    _URL_MAP = {'known': '/'}"),
        source("if True:\n    _URL_MAP = {'known': '/'}"),
        source("left = _URL_MAP = {'known': '/'}"),
    ],
)
def test_invalid_setup_map_scope_fails_closed(hook, tmp_path, app_source):
    assert check(hook, tmp_path, app_source, KNOWN_TEMPLATE).diagnostics


@pytest.mark.parametrize("map_body", INVALID_MAPS)
def test_invalid_literal_map_contract_fails_closed(hook, tmp_path, map_body):
    assert check(hook, tmp_path, source(map_body), KNOWN_TEMPLATE).diagnostics


@pytest.mark.parametrize("mutation", MUTATIONS)
def test_direct_map_mutations_and_aliases_fail_closed(hook, tmp_path, mutation):
    body = source(f"{SINGLE_MAP_SOURCE}\n{mutation}")
    assert check(hook, tmp_path, body, KNOWN_TEMPLATE).diagnostics


def test_same_line_post_map_mutation_fails_closed(hook, tmp_path):
    body = source(f"{SINGLE_MAP_SOURCE}; _URL_MAP.update({{}})")
    assert check(hook, tmp_path, body, KNOWN_TEMPLATE).diagnostics


def test_excluded_aliases_calls_and_nested_scopes_pass(hook, tmp_path):
    body = "_URL_MAP = {'known': '/'}\nfirst, second = _URL_MAP\nholder.value = _URL_MAP\nholder[0] = _URL_MAP\nconsume(_URL_MAP)\ndef nested():\n    _URL_MAP.update({})\n    alias = _URL_MAP"
    assert check(hook, tmp_path, source(body), KNOWN_TEMPLATE).diagnostics == ()


@pytest.mark.parametrize(
    "template",
    [
        "{{ url_for('known') }}",
        '{{ url_for("known") }}',
        "{{\turl_for (\n  'known'\n) }}",
        "{{ url_for(name = 'known') }}",
        "{{ url_for('known', section='x') }}",
    ],
)
def test_supported_literal_call_forms_pass(hook, tmp_path, template):
    assert (
        check(
            hook, tmp_path, VALID_SOURCE, {"nested/x.html": template}
        ).diagnostics
        == ()
    )


@pytest.mark.parametrize(
    "template",
    [
        "{{ url_for(target) }}",
        "{{ url_for('missing' + suffix) }}",
        "{{ url_for('missing' ~ suffix) }}",
        "{{ url_for('missing' if ready else 'other') }}",
        "url_for('missing')",
        "<script>url_for('missing')</script>",
        "{{ my_url_for('missing') }} {{ helper.url_for('missing') }}",
        "{{ \"url_for('missing')\" }}",
        "{% set value = 'url_for(\"missing\")' %}",
        '{{ "escaped \\" url_for(\'missing\')" }}',
        "{% raw %}{{ url_for('missing') }}{% endraw %}",
        "{%- raw -%}{{ url_for('missing') }}{%- endraw -%}",
    ],
)
def test_nonliteral_calls_are_ignored(hook, tmp_path, template):
    content = template + "{{ url_for('known') }}"
    assert (
        check(hook, tmp_path, VALID_SOURCE, {"x.html": content}).diagnostics
        == ()
    )


def test_html_comment_expression_is_executable_and_scanned(hook, tmp_path):
    template = {"x.html": "<!-- {{ url_for('missing') }} -->"}
    assert check(hook, tmp_path, VALID_SOURCE, template).diagnostics


@pytest.mark.parametrize(
    "template",
    [
        "{# comment",
        "{{ url_for('known')",
        "{% set value = 1",
        "{% raw %}hidden",
        "{% endraw %}",
        "{% raw %}{% raw %}{% endraw %}{% endraw %}",
        "{{ url_for('bad\\\\name') }}",
    ],
)
def test_invalid_jinja_lexical_contract_fails_closed(hook, tmp_path, template):
    assert check(hook, tmp_path, VALID_SOURCE, {"x.html": template}).diagnostics


@pytest.mark.parametrize("kind", ["missing", "file", "empty", "invalid_utf8"])
def test_template_tree_input_contract_fails_closed(hook, tmp_path, kind):
    app_path, templates_dir = (
        tmp_path / "fastapi_app.py",
        tmp_path / "templates",
    )
    app_path.write_text(VALID_SOURCE, encoding="utf-8")
    if kind == "file":
        templates_dir.write_text("not a directory", encoding="utf-8")
    elif kind == "empty":
        templates_dir.mkdir()
    elif kind == "invalid_utf8":
        templates_dir.mkdir()
        (templates_dir / "x.html").write_bytes(b"\xff")
    assert hook.check_url_targets(app_path, templates_dir).diagnostics


def test_html_tree_without_literal_calls_fails_closed(hook, tmp_path):
    result = check(hook, tmp_path, VALID_SOURCE, NO_LITERAL_TEMPLATE)
    assert result.diagnostics[0].message == NO_LITERAL_DIAGNOSTIC


@pytest.mark.parametrize("suffix", [".html", ".py"])
def test_unreadable_input_fails_closed(hook, tmp_path, monkeypatch, suffix):
    app_path, templates_dir = make_tree(tmp_path, VALID_SOURCE, KNOWN_TEMPLATE)
    open_file = os.open

    def deny(path, flags, *args, **kwargs):
        if Path(path).suffix == suffix:
            raise PermissionError("denied")
        return open_file(path, flags, *args, **kwargs)

    monkeypatch.setattr(os, "open", deny)
    assert hook.check_url_targets(app_path, templates_dir).diagnostics


def test_missing_app_source_fails_closed(hook, tmp_path):
    app_path, templates_dir = make_tree(tmp_path, VALID_SOURCE, KNOWN_TEMPLATE)
    app_path.unlink()
    assert hook.check_url_targets(app_path, templates_dir).diagnostics


def test_syntax_invalid_url_map_fails_closed(hook, tmp_path):
    result = check(
        hook, tmp_path, "def _setup_template_globals(:\n", KNOWN_TEMPLATE
    )
    assert "syntax" in result.diagnostics[0].message.lower()


@pytest.mark.parametrize(
    "template",
    [
        '{{ {{ url_for("known") }}',
        '{% set x = 1 {% set y = url_for("known") %}',
        '<script>const marker = "{{";</script>{{ url_for("known") }}',
        '{% --raw-- %}{{ url_for("missing") }}{% endraw %}{{ url_for("known") }}',
        '{% - raw - %}{{ url_for("missing") }}{% endraw %}{{ url_for("known") }}',
    ],
)
def test_final_review_malformed_jinja_fails_closed(hook, tmp_path, template):
    assert check(hook, tmp_path, VALID_SOURCE, {"x.html": template}).diagnostics


def test_whitespace_separated_attribute_call_is_ignored(hook, tmp_path):
    template = '{{ helper . url_for("missing") }}{{ url_for("known") }}'
    assert not check(
        hook, tmp_path, VALID_SOURCE, {"x.html": template}
    ).diagnostics


def test_nested_non_jinja_braces_do_not_form_jinja_closers(hook, tmp_path):
    template = "<script>run({nested: {}});</script>{{ url_for('known') }}"
    assert not check(
        hook, tmp_path, VALID_SOURCE, {"x.html": template}
    ).diagnostics


def test_registered_hook_selects_only_url_map_targets():
    config = yaml.safe_load((ROOT / ".pre-commit-config.yaml").read_text())
    hooks = [
        hook
        for repository in config["repos"]
        for hook in repository["hooks"]
        if hook["id"] == "check-url-for-targets"
    ]
    assert len(hooks) == 1
    (hook,) = hooks
    assert hook["entry"] == ".pre-commit-hooks/check-url-for-targets.py"
    assert hook["language"] == "script" and hook["pass_filenames"] is False
    assert "always_run" not in hook
    matcher, web_path = (
        re.compile(hook["files"]),
        "src/local_deep_research/web/",
    )
    targets = (
        "fastapi_app.py templates/index.html templates/nested/item.html".split()
    )
    assert all(matcher.search(f"{web_path}{path}") for path in targets)
    non_targets = "routers/items.py template/index.html templates_backup/index.html".split()
    assert not any(matcher.search(f"{web_path}{path}") for path in non_targets)
    leaks = (
        f"prefix/{web_path}fastapi_app.py",
        f"{web_path}fastapi_app.py.bak",
        f"{web_path}templates/item.html.bak",
        f"{web_path}static/app.js",
        "tests/hooks/test_check_url_for_targets.py",
        "docs/unrelated.html",
    )
    assert not any(matcher.search(path) for path in leaks)


@pytest.mark.parametrize(
    ("name", "expected"),
    [
        ("known", []),
        (
            "missing",
            [
                (
                    2,
                    "url_for name 'missing' is absent from _URL_MAP. Fix: add it to _URL_MAP.",
                )
            ],
        ),
    ],
)
def test_nested_jinja_arguments_close_only_at_depth_zero(
    hook, tmp_path, name, expected
):
    template = (
        "\n{{ url_for('"
        + name
        + "', values={'outer': {'inner': [1, (2,)]}}) }}"
    )
    result = check(hook, tmp_path, VALID_SOURCE, {"x.html": template})
    assert [
        (item.line, item.message) for item in result.diagnostics
    ] == expected


@pytest.mark.parametrize("closer", ("}}", "%}", "#}"))
def test_jinja_data_state_closing_text_is_ordinary(hook, tmp_path, closer):
    template = (
        f'<script>const marker = "{closer}";</script>{{{{ url_for("known") }}}}'
    )
    assert (
        check(hook, tmp_path, VALID_SOURCE, {"x.html": template}).diagnostics
        == ()
    )


@pytest.mark.parametrize(
    "template",
    (
        "{%+ raw %}{{ url_for('missing') }}{% endraw %}{{ url_for('known') }}",
        "{% raw %}{{ url_for('missing') }}{%+ endraw +%}{{ url_for('known') }}",
        "{% raw %}{% incomplete {{ url_for('missing') }}{% endraw %}{{ url_for('known') }}",
    ),
)
def test_jinja_valid_raw_forms_mask_missing_calls(hook, tmp_path, template):
    assert (
        check(hook, tmp_path, VALID_SOURCE, {"x.html": template}).diagnostics
        == ()
    )


def test_invalid_raw_opener_plus_is_not_silently_masked(hook, tmp_path):
    template = "{% raw +%}{{ url_for('missing') }}{% endraw %}"
    assert check(hook, tmp_path, VALID_SOURCE, {"x.html": template}).diagnostics


def test_backslash_heavy_unclosed_literal_fails_deterministically(
    hook, tmp_path
):
    template = "{{ url_for('" + "\\" * 4096
    assert check(hook, tmp_path, VALID_SOURCE, {"x.html": template}).diagnostics


def test_many_references_keep_exact_final_line(hook, tmp_path):
    template = "{{ url_for('known') }}\n" * 1000 + "{{ url_for('missing') }}"
    assert (
        check(hook, tmp_path, VALID_SOURCE, {"x.html": template})
        .diagnostics[0]
        .line
        == 1001
    )


EAGER_MUTATIONS = (
    "@_URL_MAP.pop('known')\ndef nested(): pass",
    "@_URL_MAP.pop('known')\nasync def nested(): pass",
    "def nested(value=_URL_MAP.pop('known')): pass",
    "def nested(*, value=_URL_MAP.pop('known')): pass",
    "def nested(value: _URL_MAP.pop('known')): pass",
    "def nested() -> _URL_MAP.pop('known'): pass",
    "callback = lambda value=_URL_MAP.pop('known'): value",
    "@_URL_MAP.pop('known')\nclass Nested: pass",
    "class Nested(_URL_MAP.pop('known')): pass",
    "class Nested(metaclass=_URL_MAP.pop('known')): pass",
    "def nested[T: _URL_MAP.pop('known')](): pass",
    "class Nested[T: _URL_MAP.pop('known')]: pass",
)


@pytest.mark.parametrize("expression", EAGER_MUTATIONS)
def test_eager_definition_expressions_that_mutate_map_fail_closed(
    hook, tmp_path, expression
):
    body = source(f"{SINGLE_MAP_SOURCE}\n{expression}")
    assert check(hook, tmp_path, body, KNOWN_TEMPLATE).diagnostics


@pytest.mark.parametrize(
    "body",
    (
        "def nested():\n    _URL_MAP.pop('known')\n    alias = _URL_MAP",
        "async def nested():\n    _URL_MAP.pop('known')\n    alias = _URL_MAP",
        "callback = lambda: _URL_MAP.pop('known')",
        "class Nested:\n    _URL_MAP.pop('known')\n    alias = _URL_MAP",
    ),
)
def test_nested_definition_bodies_remain_excluded(hook, tmp_path, body):
    code = source(f"{SINGLE_MAP_SOURCE}\n{body}")
    assert check(hook, tmp_path, code, KNOWN_TEMPLATE).diagnostics == ()


@pytest.mark.parametrize("kind", ("app", "root", "directory", "file"))
def test_symlinked_inputs_fail_closed(hook, tmp_path, kind):
    app_path, templates_dir = make_tree(tmp_path, VALID_SOURCE, KNOWN_TEMPLATE)
    if kind == "app":
        linked = tmp_path / "app-link.py"
        linked.symlink_to(app_path)
        app_path = linked
    elif kind == "root":
        actual = tmp_path / "actual"
        actual.mkdir()
        (actual / "x.html").write_text(KNOWN_TEMPLATE["x.html"])
        linked = tmp_path / "templates-link"
        linked.symlink_to(actual, target_is_directory=True)
        templates_dir = linked
    elif kind == "directory":
        actual = tmp_path / "nested"
        actual.mkdir()
        (actual / "x.html").write_text(KNOWN_TEMPLATE["x.html"])
        (templates_dir / "nested").symlink_to(actual, target_is_directory=True)
    else:
        actual = tmp_path / "target.html"
        actual.write_text(KNOWN_TEMPLATE["x.html"])
        (templates_dir / "linked.html").symlink_to(actual)
    assert hook.check_url_targets(app_path, templates_dir).diagnostics


@pytest.mark.parametrize("target", ("app", "template"))
def test_non_regular_inputs_fail_closed_without_opening_them(
    hook, tmp_path, monkeypatch, target
):
    app_path, templates_dir = make_tree(tmp_path, VALID_SOURCE, KNOWN_TEMPLATE)
    marked = app_path if target == "app" else templates_dir / "x.html"
    lstat = Path.lstat

    def fake_lstat(path):
        return (
            SimpleNamespace(st_mode=stat.S_IFIFO, st_size=0)
            if path == marked
            else lstat(path)
        )

    monkeypatch.setattr(Path, "lstat", fake_lstat)
    assert hook.check_url_targets(app_path, templates_dir).diagnostics


@pytest.mark.parametrize("delta", (0, 1))
def test_application_size_boundary_is_enforced(
    hook, tmp_path, monkeypatch, delta
):
    monkeypatch.setattr(
        hook, "MAX_APP_BYTES", len(VALID_SOURCE.encode()) - delta, raising=False
    )
    assert bool(
        check(hook, tmp_path, VALID_SOURCE, KNOWN_TEMPLATE).diagnostics
    ) is bool(delta)


@pytest.mark.parametrize(
    ("constant", "templates", "boundary"),
    (
        ("MAX_TEMPLATE_BYTES", KNOWN_TEMPLATE, len(KNOWN_TEMPLATE["x.html"])),
        (
            "MAX_TEMPLATE_TOTAL_BYTES",
            {
                "x.html": KNOWN_TEMPLATE["x.html"],
                "y.html": KNOWN_TEMPLATE["x.html"],
            },
            2 * len(KNOWN_TEMPLATE["x.html"]),
        ),
        (
            "MAX_TEMPLATE_FILES",
            {
                "x.html": KNOWN_TEMPLATE["x.html"],
                "y.html": KNOWN_TEMPLATE["x.html"],
            },
            2,
        ),
    ),
)
@pytest.mark.parametrize("delta", (0, 1))
def test_template_size_total_and_count_boundaries_are_enforced(
    hook, tmp_path, monkeypatch, constant, templates, boundary, delta
):
    monkeypatch.setattr(hook, constant, boundary - delta, raising=False)
    assert bool(
        check(hook, tmp_path, VALID_SOURCE, templates).diagnostics
    ) is bool(delta)


def test_invalid_utf8_fails_after_bounded_binary_read(
    hook, tmp_path, monkeypatch
):
    app_path, templates_dir = make_tree(tmp_path, VALID_SOURCE, KNOWN_TEMPLATE)
    (templates_dir / "x.html").write_bytes(b"\xff")
    monkeypatch.setattr(hook, "MAX_TEMPLATE_BYTES", 1, raising=False)
    assert hook.check_url_targets(app_path, templates_dir).diagnostics