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.

5.6 KB
Raw Blame
"""Guardian tests for the focused FastAPI migration workflow.

The quality job is useful only when both sides of its contract stay wired:
every selected test file must trigger the workflow when changed, and changes
to the implementations those tests protect must trigger it as well.
"""

from __future__ import annotations

import re
from pathlib import Path

import pytest
import yaml

REPO_ROOT = Path(__file__).resolve().parents[2]
WORKFLOW_PATH = (
    REPO_ROOT / ".github" / "workflows" / "fastapi-migration-quality.yml"
)

REQUIRED_SOURCE_TRIGGERS = {
    "src/local_deep_research/database/backup/backup_service.py",
    "src/local_deep_research/database/library_init.py",
    "src/local_deep_research/scheduler/background.py",
    "src/local_deep_research/utilities/request_context.py",
    "src/local_deep_research/utilities/resource_utils.py",
    "src/local_deep_research/web/app.py",
    "src/local_deep_research/web/auth/**",
    "src/local_deep_research/web/dependencies/**",
    "src/local_deep_research/web/exceptions.py",
    "src/local_deep_research/web/fastapi_app.py",
    "src/local_deep_research/web/queue/**",
    "src/local_deep_research/web/research_state.py",
    "src/local_deep_research/web/routes/research_validation.py",
    "src/local_deep_research/web/routers/**",
    "src/local_deep_research/web/server_config.py",
    "src/local_deep_research/web/services/research_service.py",
    "src/local_deep_research/web/services/settings_service.py",
    "src/local_deep_research/web/services/socketio_asgi.py",
    "src/local_deep_research/web/template_config.py",
}

# Some selected integration tests deliberately reuse deterministic fixtures
# and wire helpers from broader suites. Changes to those support modules can
# break the focused gate just as directly as changes to a conftest.py.
REQUIRED_TEST_SUPPORT_TRIGGERS = {
    "tests/security/test_realtime_channel_isolation.py",
    "tests/test_end_to_end_journeys.py",
}

REQUIRED_VITEST_TRIGGERS = {
    "package.json",
    "package-lock.json",
    "vite.config.js",
    "src/local_deep_research/web/static/js/**",
    "src/local_deep_research/web/templates/**",
    "tests/js/**",
}


@pytest.fixture(scope="module")
def workflow():
    assert WORKFLOW_PATH.is_file(), WORKFLOW_PATH
    parsed = yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8"))
    # PyYAML follows YAML 1.1 and can coerce GitHub's top-level `on` key to
    # boolean True.  Normalize it so the assertions describe GitHub's model.
    if True in parsed and "on" not in parsed:
        parsed["on"] = parsed.pop(True)
    return parsed


def _pull_request_paths(workflow) -> set[str]:
    return set(workflow["on"]["pull_request"]["paths"])


def _focused_pytest_script(workflow) -> str:
    steps = workflow["jobs"]["fastapi-migration-quality"]["steps"]
    matching = [
        step.get("run", "")
        for step in steps
        if step.get("name") == "Run focused migration tests"
    ]
    assert len(matching) == 1, (
        "expected exactly one focused migration test step, "
        f"found {len(matching)}"
    )
    return matching[0]


def test_every_selected_test_file_also_triggers_the_workflow(workflow):
    script = _focused_pytest_script(workflow)
    selected = set(
        re.findall(r"(?m)^\s+(tests/\S+?\.py)(?:::\S+)?\s*\\?$", script)
    )
    assert selected, (
        "focused pytest command contains no discoverable test paths"
    )

    missing = selected - _pull_request_paths(workflow)
    assert not missing, (
        "tests run by the FastAPI quality job do not trigger that job when "
        f"changed: {sorted(missing)}"
    )


def test_implementation_only_changes_trigger_the_quality_workflow(workflow):
    triggers = _pull_request_paths(workflow)
    missing = REQUIRED_SOURCE_TRIGGERS - triggers
    assert not missing, (
        "FastAPI implementation changes can bypass the focused migration "
        f"gate; missing pull_request.paths entries: {sorted(missing)}"
    )


def test_cross_module_test_support_changes_trigger_the_quality_workflow(
    workflow,
):
    triggers = _pull_request_paths(workflow)
    missing = REQUIRED_TEST_SUPPORT_TRIGGERS - triggers
    assert not missing, (
        "support modules imported by selected migration tests can bypass the "
        f"focused gate; missing pull_request.paths entries: {sorted(missing)}"
    )


def test_frontend_changes_trigger_the_vitest_job(workflow):
    triggers = _pull_request_paths(workflow)
    missing = REQUIRED_VITEST_TRIGGERS - triggers
    assert not missing, (
        "frontend migration changes can bypass Vitest; missing "
        f"pull_request.paths entries: {sorted(missing)}"
    )


def test_vitest_job_runs_the_full_seeded_suite(workflow):
    steps = {step["name"]: step for step in workflow["jobs"]["vitest"]["steps"]}
    assert steps["Set up Node.js"]["with"]["node-version"] == "24"
    assert steps["Install frontend dependencies"]["run"] == "npm ci"
    assert steps["Run full Vitest suite (shuffle seed 3299)"]["run"] == (
        "npm test -- --sequence.shuffle --sequence.seed=3299"
    )


def test_workflow_guards_its_definition_and_both_target_branches(workflow):
    """The focused gate must run when its own contract or either target moves."""
    pull_request = workflow["on"]["pull_request"]
    assert set(pull_request["branches"]) == {
        "main",
        "refactor/fastapi-migration-phase1",
    }
    assert ".github/workflows/fastapi-migration-quality.yml" in set(
        pull_request["paths"]
    )


def test_focused_gate_rejects_skipped_tests(workflow):
    script = _focused_pytest_script(workflow)
    assert 'if [ "$skipped" -gt 0 ]' in script
    assert "pytest_status=1" in script