papertrail/backend/engines/Valsci/tests/test_downloader_timeout_progress.py
caelum0x 174222bcaa feat: Builder-Track named-user tools, bio + evidence-intelligence platform, Apache-2.0
Builds PaperTrail from the base skeleton into a full evidence-verification platform
for the "Built with Claude: Life Sciences" hackathon (Builder Track).

New named-user features (Build Beyond the Bench):
- Lab Notebook Companion — rough bench notes -> structured, searchable, grounded
  experiment records (lib/labNotebook, /console/lab-notebook, migration 0059)
- Clinical Trial Matcher — de-identified patient notes -> ranked eligible
  ClinicalTrials.gov trials with per-criterion inclusion/exclusion reasoning
  (lib/trialMatcher, /console/trial-matcher, migrations 0060)

Platform:
- Deterministic verification moat (biostats recompute, exact-span grounding)
- 11 biomedical evidence engines (FAERS, GWAS, Open Targets, ChEMBL, ClinVar,
  PharmGKB, PubTator, DDI, repurposing, biomarker, unified verifier)
- Evidence Intelligence (KG, dossier, provenance/export, RWE) + enterprise
  governance (RBAC, /api/v1 gateway, metering, audit chain, retention)
- 17 OSS projects assimilated into native TypeScript on our stack (backend/engines
  retained + credited in NOTICE)
- Multi-format upload (PDF/DOCX/XLSX/CSV/MD/TXT), logo, marketing site
- Next.js 16 upgrade, benchmark harness (honest smoke-test caveat)

Licensing: Apache-2.0 (LICENSE + NOTICE).
2026-07-10 16:23:13 +03:00

103 lines
3.2 KiB
Python

import sys
import types
sys.modules.setdefault("ijson", types.SimpleNamespace())
sys.modules.setdefault("openai", types.SimpleNamespace(OpenAI=object))
from semantic_scholar.utils import downloader as downloader_module
from semantic_scholar.utils.downloader import S2DatasetDownloader
class _Resp:
status_code = 200
def raise_for_status(self):
pass
def _bare_downloader():
d = S2DatasetDownloader.__new__(S2DatasetDownloader)
d.session = types.SimpleNamespace(headers={})
d.rate_limiter = types.SimpleNamespace(wait=lambda: None)
return d
def test_make_request_applies_default_timeout(monkeypatch):
captured = {}
def fake_get(url, **kwargs):
captured.update(kwargs)
return _Resp()
monkeypatch.setattr(downloader_module.requests, "get", fake_get)
_bare_downloader().make_request("https://example.org/shard.gz", stream=True)
assert captured["timeout"] == downloader_module.DEFAULT_REQUEST_TIMEOUT
def test_make_request_preserves_explicit_timeout(monkeypatch):
captured = {}
def fake_get(url, **kwargs):
captured.update(kwargs)
return _Resp()
monkeypatch.setattr(downloader_module.requests, "get", fake_get)
_bare_downloader().make_request("https://example.org/shard.gz", timeout=(5, 5))
assert captured["timeout"] == (5, 5)
def test_default_read_timeout_is_finite_and_in_transient_errors():
# A read timeout only helps if it actually fires and is recoverable: the
# resume machinery keys off urllib3 ReadTimeoutError being transient.
import urllib3
connect, read = downloader_module.DEFAULT_REQUEST_TIMEOUT
assert read is not None and read > 0
assert urllib3.exceptions.ReadTimeoutError in downloader_module.TRANSIENT_STREAM_ERRORS
def test_shard_progress_logger_throttles_by_lines_and_time(monkeypatch):
prints = []
monkeypatch.setattr(downloader_module.console, "print", lambda msg: prints.append(msg))
clock = {"t": 1000.0}
monkeypatch.setattr(downloader_module.time, "time", lambda: clock["t"])
d = S2DatasetDownloader.__new__(S2DatasetDownloader)
tick = d._shard_progress_logger(
"papers 13/60",
lambda: "5 matched, 2 still missing",
every_lines=10,
every_seconds=15.0,
)
# Below the line gate: silent.
for _ in range(9):
tick()
assert prints == []
# Hits the line gate at row 10, but no time has elapsed yet: still silent.
tick()
assert prints == []
# Enough wall-clock has passed; the next line-gate hit (row 20) prints once.
clock["t"] += 20
for _ in range(10):
tick()
assert len(prints) == 1
message = prints[0]
assert "papers 13/60" in message
assert "20 rows scanned" in message
assert "5 matched, 2 still missing" in message
def test_shard_progress_logger_quiet_for_small_shards(monkeypatch):
prints = []
monkeypatch.setattr(downloader_module.console, "print", lambda msg: prints.append(msg))
monkeypatch.setattr(downloader_module.time, "time", lambda: 0.0)
d = S2DatasetDownloader.__new__(S2DatasetDownloader)
tick = d._shard_progress_logger("authors 1/2", every_lines=100_000)
for _ in range(5_000):
tick()
assert prints == []