feat(jlcpcb): warn when downloaded catalog is stale (#199)

Per review feedback: CDFER's upstream scraper pipeline has stalled for weeks
(broken cart API + a bug in their scraper), so a "fresh download" can still be
old data. Compute the catalog age from the source Last-Modified header, expose
catalog_age_days, and emit a stale=True + warning when older than 14 days that
points users to source='yaqwsx' (fresh, needs 7z) or source='official'. Surface
the warning in the MCP tool output.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
mixelpixx
2026-05-24 13:49:40 -04:00
parent b21b1410eb
commit cb5f744754
3 changed files with 81 additions and 3 deletions

View File

@@ -170,3 +170,40 @@ def test_download_database_prefer_cdfer_converts(tmp_path, monkeypatch):
assert result["basic_parts"] == 1
assert result["catalog_last_modified"].startswith("Thu, 02 Apr 2026")
assert (tmp_path / "jlcpcb_parts.db").exists()
def _http_date(days_ago: int) -> str:
from datetime import datetime, timedelta, timezone
from email.utils import format_datetime
return format_datetime(datetime.now(timezone.utc) - timedelta(days=days_ago))
def test_result_flags_stale_catalog_and_leaves_fresh_alone(tmp_path, monkeypatch):
"""A catalog older than STALE_AFTER_DAYS must be flagged; a fresh one must not."""
monkeypatch.setattr(
jlcpcb_downloader.PlatformHelper, "get_data_dir", staticmethod(lambda: tmp_path)
)
def _fake_cdfer_dated(http_date):
def _impl(cache_dir, progress=None):
cache_dir.mkdir(parents=True, exist_ok=True)
src = cache_dir / "cdfer.sqlite3"
_make_cdfer_like_source(src)
return src, http_date
return _impl
# Stale: 60 days old
monkeypatch.setattr(jlcpcb_downloader, "download_cdfer", _fake_cdfer_dated(_http_date(60)))
stale = jlcpcb_downloader.download_database(prefer_source="cdfer")
assert stale.get("stale") is True
assert "warning" in stale
assert stale["catalog_age_days"] >= jlcpcb_downloader.STALE_AFTER_DAYS
# Fresh: 1 day old
monkeypatch.setattr(jlcpcb_downloader, "download_cdfer", _fake_cdfer_dated(_http_date(1)))
fresh = jlcpcb_downloader.download_database(prefer_source="cdfer")
assert fresh.get("stale") is not True
assert "warning" not in fresh
assert fresh["catalog_age_days"] <= 1