Publish first RF4 fish quality upgrades
This commit is contained in:
@@ -6,7 +6,7 @@ import io
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from rf4_research.media_assets import approve_stored_assets, audit_media_catalog, extract_media_candidates, inspect_image, media_coverage, media_quality_report, merge_manifest, reconcile_queued_duplicates, review_asset, store_asset
|
||||
from rf4_research.media_assets import approve_stored_assets, audit_media_catalog, compare_quality_upgrades, extract_media_candidates, inspect_image, media_coverage, media_quality_report, merge_manifest, publish_quality_upgrades, queue_quality_upgrades, reconcile_queued_duplicates, review_asset, store_asset
|
||||
|
||||
|
||||
def test_extracts_and_classifies_unique_https_media() -> None:
|
||||
@@ -193,3 +193,58 @@ def test_quality_report_finds_low_resolution_asset_and_known_alternative(tmp_pat
|
||||
assert (report["published"], report["below_minimum"], report["upsampled_in_cards"]) == (2, 1, 1)
|
||||
assert report["known_alternative_urls"] == 1
|
||||
assert report["by_source"]["small.example"]["below_minimum"] == 1
|
||||
|
||||
|
||||
def test_quality_upgrade_queue_preserves_published_fallback(tmp_path: Path) -> None:
|
||||
path = tmp_path / "manifest.json"
|
||||
path.write_text(json.dumps({"assets": [
|
||||
{"entity_type": "fish", "label": "Щука", "status": "approved", "asset_url": "https://small.example/pike.png", "width": 48, "height": 48},
|
||||
{"entity_type": "fish", "label": "Щука", "status": "duplicate", "asset_url": "https://large.example/pike.webp", "duplicate_of": "https://small.example/pike.png"},
|
||||
{"entity_type": "fish", "label": "Окунь", "status": "approved", "asset_url": "https://large.example/perch.webp", "width": 1024, "height": 1024},
|
||||
]}), encoding="utf-8")
|
||||
|
||||
report = queue_quality_upgrades(path)
|
||||
assets = json.loads(path.read_text(encoding="utf-8"))["assets"]
|
||||
|
||||
assert report == {"minimum_dimension": 256, "low_resolution_published": 1, "upgrade_queued": 1}
|
||||
assert assets[0]["status"] == "approved"
|
||||
assert assets[1]["status"] == "upgrade_queued"
|
||||
|
||||
|
||||
def test_compare_quality_upgrades_checks_candidate_and_fallback(tmp_path: Path) -> None:
|
||||
small = Image.new("RGBA", (48, 48), (1, 2, 3, 0))
|
||||
large = Image.new("RGBA", (512, 512), (1, 2, 3, 0))
|
||||
small_body = io.BytesIO()
|
||||
large_body = io.BytesIO()
|
||||
small.save(small_body, format="PNG")
|
||||
large.save(large_body, format="WEBP")
|
||||
small_digest, small_path, *_ = store_asset(tmp_path, small_body.getvalue(), content_type="image/png", source_url="https://small/pike.png")
|
||||
large_digest, large_path, *_ = store_asset(tmp_path, large_body.getvalue(), content_type="image/webp", source_url="https://large/pike.webp")
|
||||
(tmp_path / "manifest.json").write_text(json.dumps({"assets": [
|
||||
{"entity_type": "fish", "label": "Щука", "status": "approved", "asset_url": "https://small/pike.png", "local_path": small_path, "sha256": small_digest, "width": 48, "height": 48, "bytes": len(small_body.getvalue()), "content_type": "image/png"},
|
||||
{"entity_type": "fish", "label": "Щука", "status": "upgrade_stored", "asset_url": "https://large/pike.webp", "duplicate_of": "https://small/pike.png", "local_path": large_path, "sha256": large_digest, "width": 512, "height": 512, "bytes": len(large_body.getvalue()), "content_type": "image/webp"},
|
||||
]}), encoding="utf-8")
|
||||
|
||||
report = compare_quality_upgrades(tmp_path)
|
||||
|
||||
assert (report["compared"], report["meets_minimum"], report["aspect_ratio_matches"]) == (1, 1, 1)
|
||||
assert report["issues"] == []
|
||||
assert report["comparisons"][0]["candidate"]["alpha"] is True
|
||||
|
||||
|
||||
def test_publish_quality_upgrades_switches_mapping_and_retains_fallback(tmp_path: Path) -> None:
|
||||
path = tmp_path / "manifest.json"
|
||||
path.write_text(json.dumps({"assets": [
|
||||
{"entity_type": "fish", "entity_key": "fish:pike", "status": "approved", "asset_url": "https://small/pike.png", "local_path": "files/small.png", "sha256": "a" * 64},
|
||||
{"entity_type": "fish", "status": "upgrade_stored", "asset_url": "https://large/pike.webp", "duplicate_of": "https://small/pike.png", "local_path": "files/large.webp", "sha256": "b" * 64, "width": 512, "height": 512},
|
||||
]}), encoding="utf-8")
|
||||
|
||||
report = publish_quality_upgrades(path, note="owner approved quality upgrades")
|
||||
assets = json.loads(path.read_text(encoding="utf-8"))["assets"]
|
||||
|
||||
assert report == {"published": 1, "retained_fallbacks": 1}
|
||||
assert assets[0]["status"] == "superseded"
|
||||
assert assets[0]["replaced_by"] == assets[1]["asset_url"]
|
||||
assert assets[1]["status"] == "approved"
|
||||
assert assets[1]["entity_key"] == "fish:pike"
|
||||
assert assets[1]["supersedes"] == assets[0]["asset_url"]
|
||||
|
||||
@@ -82,3 +82,22 @@ def test_batch_stops_domain_after_three_consecutive_invalid_assets(tmp_path: Pat
|
||||
assert report["attempted_total"] == 3
|
||||
assert report["failed_total"] == 3
|
||||
assert "3 consecutive failures" in report["domains"]["rf4map.ru"]["stopped_reason"]
|
||||
|
||||
|
||||
def test_batch_downloads_quality_upgrade_without_unpublishing_fallback(tmp_path: Path, monkeypatch) -> None:
|
||||
root = tmp_path / "media"
|
||||
root.mkdir()
|
||||
candidate = {"status": "upgrade_queued", "asset_url": "https://rf4db.com/pike.webp", "entity_type": "fish", "duplicate_of": "https://rf4map.ru/pike.png"}
|
||||
(root / "manifest.json").write_text(json.dumps({"version": 1, "assets": [candidate]}), encoding="utf-8")
|
||||
|
||||
def store_upgrade(_root, path, manifest, item):
|
||||
item["status"] = "upgrade_stored"
|
||||
media_cli._save_manifest(path, manifest)
|
||||
return "stored", None
|
||||
|
||||
monkeypatch.setattr(media_cli, "_attempt_asset", store_upgrade)
|
||||
report = _download_batch(root, tmp_path / "state.json", limit=1)
|
||||
|
||||
assert report["stored_total"] == 1
|
||||
assets = json.loads((root / "manifest.json").read_text(encoding="utf-8"))["assets"]
|
||||
assert assets[0]["status"] == "upgrade_stored"
|
||||
|
||||
Reference in New Issue
Block a user