mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-26 15:38:52 -03:00
fix: verify returned image ID matches requested ID in CivitAI API
Fix issue #870 where importing recipes from CivitAI image URLs would return the wrong image when the API response did not contain the requested image ID. The get_image_info() method now: - Iterates through all returned items to find matching ID - Returns None when no match is found and logs warning with returned IDs - Handles invalid (non-numeric) ID formats New test cases: - test_get_image_info_returns_matching_item - test_get_image_info_returns_none_when_id_mismatch - test_get_image_info_handles_invalid_id Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -484,9 +484,11 @@ async def test_get_model_version_info_success(monkeypatch, downloader):
|
||||
assert result["images"][0]["meta"]["other"] == "keep"
|
||||
|
||||
|
||||
async def test_get_image_info_returns_first_item(monkeypatch, downloader):
|
||||
async def test_get_image_info_returns_matching_item(monkeypatch, downloader):
|
||||
"""When API returns multiple items, return the one matching the requested ID."""
|
||||
async def fake_make_request(method, url, use_auth=True, **kwargs):
|
||||
return True, {"items": [{"id": 1}, {"id": 2}]}
|
||||
# Requested ID is 42, but it's the second item in the response
|
||||
return True, {"items": [{"id": 41}, {"id": 42, "name": "target"}, {"id": 43}]}
|
||||
|
||||
downloader.make_request = fake_make_request
|
||||
|
||||
@@ -494,7 +496,25 @@ async def test_get_image_info_returns_first_item(monkeypatch, downloader):
|
||||
|
||||
result = await client.get_image_info("42")
|
||||
|
||||
assert result == {"id": 1}
|
||||
assert result == {"id": 42, "name": "target"}
|
||||
|
||||
|
||||
async def test_get_image_info_returns_none_when_id_mismatch(monkeypatch, downloader, caplog):
|
||||
"""When API returns items but none match the requested ID, return None and log warning."""
|
||||
async def fake_make_request(method, url, use_auth=True, **kwargs):
|
||||
# Requested ID is 999, but API returns different IDs (simulating deleted/hidden image)
|
||||
return True, {"items": [{"id": 1}, {"id": 2}, {"id": 3}]}
|
||||
|
||||
downloader.make_request = fake_make_request
|
||||
|
||||
client = await CivitaiClient.get_instance()
|
||||
|
||||
result = await client.get_image_info("999")
|
||||
|
||||
assert result is None
|
||||
# Verify warning was logged
|
||||
assert "CivitAI API returned no matching image for requested ID 999" in caplog.text
|
||||
assert "Returned 3 item(s) with IDs: [1, 2, 3]" in caplog.text
|
||||
|
||||
|
||||
async def test_get_image_info_handles_missing(monkeypatch, downloader):
|
||||
@@ -508,3 +528,13 @@ async def test_get_image_info_handles_missing(monkeypatch, downloader):
|
||||
result = await client.get_image_info("42")
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
async def test_get_image_info_handles_invalid_id(monkeypatch, downloader, caplog):
|
||||
"""When given a non-numeric image ID, return None and log error."""
|
||||
client = await CivitaiClient.get_instance()
|
||||
|
||||
result = await client.get_image_info("not-a-number")
|
||||
|
||||
assert result is None
|
||||
assert "Invalid image ID format" in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user