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.

9.5 KB
Raw Blame
"""An opted-in compatibility run must execute against a published predecessor."""

# allow: no-sut-import — exercises the real compatibility probe and workflow commands.

import importlib.util
import io
import json
import os
from contextlib import contextmanager
from pathlib import Path
import subprocess
import sys
from xml.etree import ElementTree

import pytest
import yaml


ROOT = Path(__file__).resolve().parents[2]


@contextmanager
def _setup_must_fail(message):
    # An uncaught pytest.skip would also skip this regression test. Convert it
    # to a failure so restoring the old behavior cannot make the test green.
    try:
        with pytest.raises(pytest.fail.Exception, match=message):
            yield
    except pytest.skip.Exception as exc:
        pytest.fail(f"Opted-in compatibility probe skipped: {exc}")


@pytest.fixture
def probe_module():
    spec = importlib.util.spec_from_file_location(
        "pypi_compatibility_probe",
        ROOT / "tests/performance/database/test_backwards_compatibility.py",
    )
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def test_predecessor_is_latest_non_yanked_stable_release_without_pip(
    probe_module, monkeypatch
):
    payload = {
        "releases": {
            "1.10.6": [{"yanked": False}],
            "1.9.99": [{"yanked": False}],
            "1.10.7": [{"yanked": True}, {"yanked": False}],
            "1.10.8": [{"yanked": True}],
            "2.0.0rc1": [{"yanked": False}],
            "3.0.0.dev1": [{"yanked": False}],
            "9.0.0": [],
        }
    }

    def request(url, *, timeout):
        assert url == "https://pypi.org/pypi/local-deep-research/json"
        assert 0 < timeout <= 30
        return io.BytesIO(json.dumps(payload).encode())

    def no_pip(*args, **kwargs):
        pytest.fail("Version discovery must work without pip in the PDM venv")

    monkeypatch.setattr(probe_module, "urlopen", request)
    monkeypatch.setattr(probe_module.subprocess, "run", no_pip)
    assert (
        probe_module.TestBackwardsCompatibility().get_previous_version()
        == "1.10.7"
    )


def _serve(probe_module, monkeypatch, releases):
    monkeypatch.setattr(
        probe_module,
        "urlopen",
        lambda *a, **kw: io.BytesIO(
            json.dumps({"releases": releases}).encode()
        ),
    )


def test_known_uninstallable_release_is_replaced_by_next_predecessor(
    probe_module, monkeypatch
):
    # 1.4.0's published metadata cannot be resolved (#3331). It must not
    # block the gate, and the probe must still run against a real release.
    _serve(
        probe_module,
        monkeypatch,
        {
            "1.3.9": [{"yanked": False}],
            "1.4.0": [{"yanked": False}],
        },
    )
    assert "1.4.0" in probe_module.KNOWN_UNINSTALLABLE_RELEASES
    assert (
        probe_module.TestBackwardsCompatibility().get_previous_version()
        == "1.3.9"
    )


def test_uninstallable_allowance_is_exact_and_documented(probe_module):
    from packaging.version import Version

    allowed = probe_module.KNOWN_UNINSTALLABLE_RELEASES
    # A narrow, pinned list: exact stable versions with a reason, never a
    # range or a blanket switch.
    assert allowed == {
        "1.4.0": "requests>=2.33 conflicts with arxiv~=2.4 (requests~=2.32.0)"
    }
    for version, reason in allowed.items():
        assert str(Version(version)) == version
        assert reason.strip()


def test_allowance_does_not_exclude_neighbouring_releases(
    probe_module, monkeypatch
):
    # Only the listed version is excluded: a neighbouring release (1.4.1) is
    # still chosen, so its install failure fails the probe.
    _serve(
        probe_module,
        monkeypatch,
        {"1.4.0": [{"yanked": False}], "1.4.1": [{"yanked": False}]},
    )
    assert (
        probe_module.TestBackwardsCompatibility().get_previous_version()
        == "1.4.1"
    )


def test_only_uninstallable_releases_is_an_error(probe_module, monkeypatch):
    _serve(probe_module, monkeypatch, {"1.4.0": [{"yanked": False}]})
    with pytest.raises(ValueError, match="KNOWN_UNINSTALLABLE_RELEASES"):
        probe_module.TestBackwardsCompatibility().get_previous_version()


def test_no_published_predecessor_is_an_error(probe_module, monkeypatch):
    monkeypatch.setattr(
        probe_module,
        "urlopen",
        lambda *a, **kw: io.BytesIO(b'{"releases": {}}'),
    )
    with pytest.raises(ValueError, match="no non-yanked stable release"):
        probe_module.TestBackwardsCompatibility().get_previous_version()


def test_version_discovery_failure_cannot_skip(
    probe_module, monkeypatch, tmp_path
):
    def unavailable(*args, **kwargs):
        raise OSError("PyPI unavailable")

    monkeypatch.setattr(probe_module, "urlopen", unavailable)
    with _setup_must_fail("PyPI unavailable"):
        probe_module.TestBackwardsCompatibility().test_open_database_from_previous_version(
            tmp_path, monkeypatch
        )


@pytest.mark.parametrize(
    ("failing_call", "message"),
    [
        (1, "Could not create venv"),
        (2, "Could not install previous version"),
        (3, "Could not create database with previous version"),
    ],
)
def test_predecessor_setup_failures_cannot_skip_or_import_checkout(
    probe_module, monkeypatch, tmp_path, failing_call, message
):
    probe = probe_module.TestBackwardsCompatibility()
    monkeypatch.setattr(probe, "get_previous_version", lambda: "1.10.7")
    monkeypatch.setenv("PYTHONPATH", str(ROOT / "src"))
    calls = []

    def run(args, **kwargs):
        calls.append(args)
        assert kwargs["cwd"] == tmp_path
        assert "PYTHONPATH" not in kwargs["env"]
        assert 0 < kwargs["timeout"] <= 600
        return subprocess.CompletedProcess(
            args,
            int(len(calls) == failing_call),
            stdout="",
            stderr="setup failed",
        )

    monkeypatch.setattr(probe_module.subprocess, "run", run)
    with _setup_must_fail(message):
        probe.test_open_database_from_previous_version(tmp_path, monkeypatch)
    assert len(calls) == failing_call
    if failing_call >= 2:
        assert "local-deep-research==1.10.7" in calls[1]
        assert str(tmp_path / "prev_venv") in calls[1][0]
    if failing_call == 3:
        assert Path(calls[2][1]).parent == tmp_path


@pytest.fixture(scope="module")
def compatibility_job():
    return yaml.safe_load(
        (ROOT / ".github/workflows/backwards-compatibility.yml").read_text()
    )["jobs"]["pypi-compatibility"]


def _step(job, step_id):
    return next(s for s in job["steps"] if s.get("id") == step_id)


def _execute(script, directory, extra_env=None):
    return subprocess.run(
        ["bash", "-e", "-o", "pipefail", "-c", script],
        cwd=directory,
        env={
            **os.environ,
            "PATH": f"{Path(sys.executable).parent}:{os.environ.get('PATH', '')}",
            **(extra_env or {}),
        },
        capture_output=True,
        text=True,
        timeout=20,
        check=False,
    )


def test_workflow_opts_in_and_retains_diagnostics(compatibility_job):
    test = _step(compatibility_job, "compatibility-test")
    assert test["env"]["RUN_SLOW_TESTS"] == "true"
    assert test["shell"] == "bash"
    assert "if" not in test and not test.get("continue-on-error")
    assert "--junitxml=test-results/pypi-compatibility.xml" in test["run"]
    validator = _step(compatibility_job, "validate-results")
    assert validator["if"] == "always()"
    assert not validator.get("continue-on-error")
    upload = next(
        s
        for s in compatibility_job["steps"]
        if s.get("uses", "").startswith("actions/upload-artifact@")
    )
    assert upload["if"] == "always()"
    assert upload["with"]["path"] == "test-results/pypi-compatibility.*"


@pytest.mark.parametrize(
    "outcome",
    [
        "passed",
        "skipped",
        "failure",
        "error",
        "wrong-test",
        "empty",
        "missing",
        "malformed",
    ],
)
def test_real_workflow_rejects_unexecuted_probe(
    compatibility_job, tmp_path, outcome
):
    report = tmp_path / "test-results/pypi-compatibility.xml"
    if outcome != "missing":
        report.parent.mkdir()
        if outcome == "malformed":
            report.write_text("<testsuites>")
        else:
            suite = ElementTree.Element("testsuite")
            if outcome != "empty":
                case = ElementTree.SubElement(
                    suite,
                    "testcase",
                    name="unrelated_test"
                    if outcome == "wrong-test"
                    else "test_open_database_from_previous_version",
                )
                if outcome in {"skipped", "failure", "error"}:
                    ElementTree.SubElement(case, outcome)
            ElementTree.ElementTree(suite).write(report)
    result = _execute(
        _step(compatibility_job, "validate-results")["run"], tmp_path
    )
    assert (result.returncode == 0) is (outcome == "passed"), (
        result.stdout + result.stderr
    )


@pytest.mark.parametrize("pytest_status", [0, 1, 5])
def test_pytest_status_survives_log_pipeline(
    compatibility_job, tmp_path, pytest_status
):
    binary = tmp_path / "bin"
    binary.mkdir()
    pdm = binary / "pdm"
    pdm.write_text(f"#!/bin/sh\nexit {pytest_status}\n")
    pdm.chmod(0o755)
    result = _execute(
        _step(compatibility_job, "compatibility-test")["run"],
        tmp_path,
        {"PATH": f"{binary}:{os.environ.get('PATH', '')}"},
    )
    assert result.returncode == pytest_status, result.stdout + result.stderr