diff --git a/services/comfyui_history.py b/services/comfyui_history.py index 2e93eb5..7252e36 100644 --- a/services/comfyui_history.py +++ b/services/comfyui_history.py @@ -34,13 +34,13 @@ def _pick_string(payload: Dict[str, Any], *keys: str) -> str: def _pick_asset_hash(image_ref: Dict[str, Any]) -> str: - asset_hash = _pick_string(image_ref, "asset_hash") + asset_hash = _pick_string(image_ref, "asset_hash", "hash") if asset_hash: return asset_hash nested = image_ref.get("asset") if isinstance(nested, dict): - return _pick_string(nested, "asset_hash") + return _pick_string(nested, "asset_hash", "hash") return "" diff --git a/tests/test_comfyui_history_parsing.py b/tests/test_comfyui_history_parsing.py index 7a8f063..22480b0 100644 --- a/tests/test_comfyui_history_parsing.py +++ b/tests/test_comfyui_history_parsing.py @@ -104,6 +104,80 @@ class TestComfyUIHistoryParsing(unittest.TestCase): self.assertFalse(images[0]["asset_api_required"]) self.assertEqual(images[0]["resolution"], "view") + def test_extract_images_accepts_top_level_hash_alias(self): + from services.comfyui_history import extract_images + + history_item = { + "outputs": { + "2": { + "images": [ + { + "filename": "hash-alias.png", + "type": "output", + "hash": "blake3:alias123", + } + ] + } + } + } + + images = extract_images(history_item) + self.assertEqual(len(images), 1) + self.assertEqual(images[0]["filename"], "hash-alias.png") + self.assertEqual(images[0]["asset_hash"], "blake3:alias123") + self.assertIn("filename=blake3%3Aalias123", images[0]["view_url"]) + self.assertFalse(images[0]["asset_api_required"]) + self.assertEqual(images[0]["resolution"], "view") + + def test_extract_images_accepts_nested_hash_alias(self): + from services.comfyui_history import extract_images + + history_item = { + "outputs": { + "2": { + "images": [ + { + "name": "nested-hash-alias.png", + "asset": { + "hash": "blake3:nested-alias", + }, + } + ] + } + } + } + + images = extract_images(history_item) + self.assertEqual(len(images), 1) + self.assertEqual(images[0]["filename"], "nested-hash-alias.png") + self.assertEqual(images[0]["asset_hash"], "blake3:nested-alias") + self.assertIn("filename=blake3%3Anested-alias", images[0]["view_url"]) + self.assertFalse(images[0]["asset_api_required"]) + self.assertEqual(images[0]["resolution"], "view") + + def test_extract_images_prefers_asset_hash_over_hash_alias(self): + from services.comfyui_history import extract_images + + history_item = { + "outputs": { + "2": { + "images": [ + { + "filename": "preferred.png", + "asset_hash": "blake3:preferred", + "hash": "blake3:alias", + } + ] + } + } + } + + images = extract_images(history_item) + self.assertEqual(len(images), 1) + self.assertEqual(images[0]["asset_hash"], "blake3:preferred") + self.assertIn("filename=blake3%3Apreferred", images[0]["view_url"]) + self.assertNotIn("filename=blake3%3Aalias", images[0]["view_url"]) + def test_extract_images_preserves_asset_api_only_refs_as_explicit_no_go_contract( self, ):