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
"""Journal-data refreshes must replace an already-installed snapshot.

`Path.rename` is `os.rename`, which on Windows raises `FileExistsError` when
the destination exists - the normal case when refreshing data that was
downloaded before. POSIX `rename` overwrites silently, so Linux/macOS CI never
saw the failure, but on Windows every source aborted at the promotion step
(`✗ ... FileExistsError` on `/metrics/journals`) and the update could never
complete.

`windows_rename` below gives `os.rename` that Windows behaviour, so these tests
pin the promotion to an overwriting primitive (`os.replace`) rather than to
whatever `os.rename` happens to do on the host.
"""

from __future__ import annotations

import json
import os
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.journal_quality import data_sources as data_sources_pkg
from local_deep_research.journal_quality.data_sources import (
    predatory as pred_mod,
)
from local_deep_research.journal_quality.data_sources.base import (
    atomic_replace,
)
from local_deep_research.journal_quality.data_sources.predatory import (
    PredatorySource,
)


@pytest.fixture
def windows_rename(monkeypatch):
    real_rename = os.rename

    def _rename(src, dst, *args, **kwargs):
        if Path(dst).exists():
            raise FileExistsError(17, "File exists", str(dst))
        return real_rename(src, dst, *args, **kwargs)

    monkeypatch.setattr(os, "rename", _rename)


def _csv_response(text: str) -> MagicMock:
    resp = MagicMock()
    resp.raise_for_status = MagicMock()
    resp.text = text
    return resp


def test_atomic_replace_overwrites_existing_destination(
    tmp_path, windows_rename
):
    """The destination is replaced even where rename would refuse."""
    tmp = tmp_path / "snapshot.json.tmp"
    output = tmp_path / "snapshot.json"
    tmp.write_bytes(b'{"version": "new"}')
    output.write_bytes(b'{"version": "installed"}')

    atomic_replace(tmp, output)

    assert json.loads(output.read_text(encoding="utf-8"))["version"] == "new"
    assert not tmp.exists()


def test_predatory_refresh_replaces_installed_snapshot(
    tmp_path, monkeypatch, windows_rename
):
    """A fetch over an existing predatory.json must overwrite it."""
    monkeypatch.setattr(pred_mod, "_MIN_PREDATORY_TOTAL", 5)

    installed = tmp_path / "predatory.json"
    installed.write_bytes(b'{"sentinel": "previous build"}')

    publishers_csv = "name,url\n" + "\n".join(
        f"Pub {i},http://p{i}.example" for i in range(10)
    )

    with patch(
        "local_deep_research.security.safe_requests.safe_get_with_retries",
    ) as mock_get:
        mock_get.side_effect = [
            _csv_response(publishers_csv),
            _csv_response("name,url\n"),
            _csv_response("hijacked,hijackedurl,authentic,authenticurl\n"),
        ]
        result = PredatorySource().fetch(tmp_path)

    assert result == 10
    assert (
        json.loads(installed.read_text(encoding="utf-8"))["metadata"][
            "publisher_count"
        ]
        == 10
    )
    assert not (tmp_path / "predatory.json.tmp").exists()


def test_every_source_promotes_through_atomic_replace():
    """No source may regress to ``tmp.rename`` (Windows-refusing) promotion.

    Only the predatory call site is exercised behaviourally above; the other
    four share the same bug but cannot be reached from a Linux CI host (POSIX
    ``rename`` overwrites silently), so pin the whole set structurally here.
    """
    sources_dir = Path(data_sources_pkg.__file__).parent
    modules = sorted(sources_dir.glob("*.py"))

    offenders = [
        p.name for p in modules if ".rename(" in p.read_text(encoding="utf-8")
    ]
    assert offenders == [], f"use atomic_replace, not rename: {offenders}"

    for name in ("doaj", "institutions", "jabref", "openalex", "predatory"):
        source = (sources_dir / f"{name}.py").read_text(encoding="utf-8")
        assert "atomic_replace(" in source, (
            f"{name}.py must promote its snapshot via atomic_replace"
        )