mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-08-21 21:11:26 -03:00
feat(recipe): send embedded recipe workflow to ComfyUI canvas
This commit is contained in:
@@ -393,6 +393,214 @@ async def test_load_recipe_upgrades_string_checkpoint(tmp_path: Path, recipe_sca
|
||||
assert loaded["checkpoint"]["file_name"] == "sd15"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# has_workflow detection (plan 3.1)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _recipe_json(recipes_dir: Path, recipe_id: str, image_path: Path, **extra: Any) -> Path:
|
||||
recipe_path = recipes_dir / f"{recipe_id}.recipe.json"
|
||||
data: Dict[str, Any] = {
|
||||
"id": recipe_id,
|
||||
"file_path": str(image_path),
|
||||
"title": recipe_id,
|
||||
"modified": 0.0,
|
||||
"created_date": 0.0,
|
||||
"loras": [],
|
||||
}
|
||||
data.update(extra)
|
||||
recipe_path.write_text(json.dumps(data))
|
||||
return recipe_path
|
||||
|
||||
|
||||
def _mock_metadata(monkeypatch, workflow=None, raises=False):
|
||||
def _load_structured_metadata(_image_path):
|
||||
if raises:
|
||||
raise RuntimeError("metadata parse failure")
|
||||
return {
|
||||
"parameters": None,
|
||||
"prompt": "a test prompt",
|
||||
"workflow": workflow,
|
||||
"comment": None,
|
||||
}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"py.services.recipe_scanner.ExifUtils._load_structured_metadata",
|
||||
_load_structured_metadata,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_recipe_detects_embedded_workflow(tmp_path: Path, recipe_scanner, monkeypatch):
|
||||
scanner, _ = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "with-workflow.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "with-workflow", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, workflow='{"nodes": []}')
|
||||
|
||||
loaded = await scanner._load_recipe_file(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is True
|
||||
persisted = json.loads(recipe_path.read_text())
|
||||
assert persisted["has_workflow"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_recipe_detects_no_workflow(tmp_path: Path, recipe_scanner, monkeypatch):
|
||||
scanner, _ = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "without-workflow.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "without-workflow", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, workflow=None)
|
||||
|
||||
loaded = await scanner._load_recipe_file(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is False
|
||||
persisted = json.loads(recipe_path.read_text())
|
||||
assert persisted["has_workflow"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_recipe_metadata_exception_yields_false(
|
||||
tmp_path: Path, recipe_scanner, monkeypatch
|
||||
):
|
||||
scanner, _ = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "broken-meta.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "broken-meta", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, raises=True)
|
||||
|
||||
loaded = await scanner._load_recipe_file(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_recipe_missing_image_yields_false(
|
||||
tmp_path: Path, recipe_scanner, monkeypatch
|
||||
):
|
||||
scanner, _ = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "missing-image.webp"
|
||||
recipe_path = _recipe_json(recipes_dir, "missing-image", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, workflow='{"nodes": []}')
|
||||
|
||||
loaded = await scanner._load_recipe_file(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_recipe_keeps_existing_has_workflow(
|
||||
tmp_path: Path, recipe_scanner, monkeypatch
|
||||
):
|
||||
scanner, _ = recipe_scanner
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "pre-recorded.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "pre-recorded", image_path, has_workflow=True)
|
||||
|
||||
_mock_metadata(monkeypatch, workflow=None)
|
||||
|
||||
loaded = await scanner._load_recipe_file(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is True
|
||||
persisted = json.loads(recipe_path.read_text())
|
||||
assert persisted["has_workflow"] is True
|
||||
|
||||
|
||||
def test_load_recipe_file_sync_detects_embedded_workflow(tmp_path: Path, monkeypatch):
|
||||
RecipeScanner._instance = None
|
||||
settings_manager_module.reset_settings_manager()
|
||||
monkeypatch.setattr(config, "loras_roots", [str(tmp_path)])
|
||||
scanner = RecipeScanner(lora_scanner=StubLoraScanner()) # pyright: ignore[reportArgumentType]
|
||||
try:
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "sync-workflow.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "sync-workflow", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, workflow='{"nodes": []}')
|
||||
|
||||
loaded = scanner._load_recipe_file_sync(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is True
|
||||
persisted = json.loads(recipe_path.read_text())
|
||||
assert persisted["has_workflow"] is True
|
||||
finally:
|
||||
RecipeScanner._instance = None
|
||||
settings_manager_module.reset_settings_manager()
|
||||
|
||||
|
||||
def test_load_recipe_file_sync_detects_no_workflow(tmp_path: Path, monkeypatch):
|
||||
RecipeScanner._instance = None
|
||||
settings_manager_module.reset_settings_manager()
|
||||
monkeypatch.setattr(config, "loras_roots", [str(tmp_path)])
|
||||
scanner = RecipeScanner(lora_scanner=StubLoraScanner()) # pyright: ignore[reportArgumentType]
|
||||
try:
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "sync-no-workflow.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "sync-no-workflow", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, workflow=None)
|
||||
|
||||
loaded = scanner._load_recipe_file_sync(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is False
|
||||
persisted = json.loads(recipe_path.read_text())
|
||||
assert persisted["has_workflow"] is False
|
||||
finally:
|
||||
RecipeScanner._instance = None
|
||||
settings_manager_module.reset_settings_manager()
|
||||
|
||||
|
||||
def test_load_recipe_file_sync_metadata_exception_yields_false(
|
||||
tmp_path: Path, monkeypatch
|
||||
):
|
||||
RecipeScanner._instance = None
|
||||
settings_manager_module.reset_settings_manager()
|
||||
monkeypatch.setattr(config, "loras_roots", [str(tmp_path)])
|
||||
scanner = RecipeScanner(lora_scanner=StubLoraScanner()) # pyright: ignore[reportArgumentType]
|
||||
try:
|
||||
recipes_dir = Path(config.loras_roots[0]) / "recipes"
|
||||
recipes_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
image_path = recipes_dir / "sync-broken-meta.webp"
|
||||
image_path.write_bytes(b"fake-webp")
|
||||
recipe_path = _recipe_json(recipes_dir, "sync-broken-meta", image_path)
|
||||
|
||||
_mock_metadata(monkeypatch, raises=True)
|
||||
|
||||
loaded = scanner._load_recipe_file_sync(str(recipe_path))
|
||||
|
||||
assert loaded["has_workflow"] is False
|
||||
finally:
|
||||
RecipeScanner._instance = None
|
||||
settings_manager_module.reset_settings_manager()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_paginated_data_normalizes_legacy_checkpoint(recipe_scanner):
|
||||
scanner, _ = recipe_scanner
|
||||
@@ -417,7 +625,6 @@ async def test_get_paginated_data_normalizes_legacy_checkpoint(recipe_scanner):
|
||||
assert checkpoint["name"] == "legacy.safetensors"
|
||||
assert checkpoint["file_name"] == "legacy"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_recipe_by_id_handles_non_dict_checkpoint(recipe_scanner):
|
||||
scanner, _ = recipe_scanner
|
||||
@@ -440,6 +647,53 @@ async def test_get_recipe_by_id_handles_non_dict_checkpoint(recipe_scanner):
|
||||
assert recipe["checkpoint"]["file_name"] == "by-id"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_recipe_by_id_detects_workflow_for_legacy_recipes(
|
||||
recipe_scanner, monkeypatch
|
||||
):
|
||||
scanner, _ = recipe_scanner
|
||||
await scanner.add_recipe(
|
||||
{
|
||||
"id": "legacy-no-flag",
|
||||
"file_path": "/models/recipes/legacy.webp",
|
||||
"title": "Legacy",
|
||||
"modified": 0.0,
|
||||
"created_date": 0.0,
|
||||
"loras": [],
|
||||
}
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
RecipeScanner, "_detect_has_workflow", lambda self, path: True
|
||||
)
|
||||
|
||||
recipe = await scanner.get_recipe_by_id("legacy-no-flag")
|
||||
|
||||
assert recipe is not None
|
||||
assert recipe["has_workflow"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_recipe_by_id_keeps_existing_has_workflow_flag(recipe_scanner):
|
||||
scanner, _ = recipe_scanner
|
||||
await scanner.add_recipe(
|
||||
{
|
||||
"id": "flagged",
|
||||
"file_path": "/models/recipes/flagged.webp",
|
||||
"title": "Flagged",
|
||||
"modified": 0.0,
|
||||
"created_date": 0.0,
|
||||
"loras": [],
|
||||
"has_workflow": False,
|
||||
}
|
||||
)
|
||||
|
||||
recipe = await scanner.get_recipe_by_id("flagged")
|
||||
|
||||
assert recipe is not None
|
||||
assert recipe["has_workflow"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_recipe_by_id_merges_recipe_json_details(recipe_scanner):
|
||||
scanner, _ = recipe_scanner
|
||||
|
||||
@@ -26,6 +26,7 @@ class DummyExifUtils:
|
||||
def __init__(self):
|
||||
self.appended = None
|
||||
self.optimized_calls = 0
|
||||
self.workflow_value = None
|
||||
|
||||
def optimize_image(self, image_data, target_width, format, quality, preserve_metadata):
|
||||
self.optimized_calls += 1
|
||||
@@ -37,6 +38,14 @@ class DummyExifUtils:
|
||||
def extract_image_metadata(self, path):
|
||||
return {}
|
||||
|
||||
def _load_structured_metadata(self, image_path):
|
||||
return {
|
||||
"parameters": None,
|
||||
"prompt": None,
|
||||
"workflow": self.workflow_value,
|
||||
"comment": None,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_save_recipe_video_bypasses_optimization(tmp_path):
|
||||
@@ -213,6 +222,84 @@ async def test_save_recipe_reports_duplicates(tmp_path):
|
||||
assert service._exif_utils.appended[0] == expected_image_path
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_save_recipe_records_has_workflow(tmp_path):
|
||||
exif_utils = DummyExifUtils()
|
||||
exif_utils.workflow_value = '{"nodes": []}'
|
||||
|
||||
class DummyCache:
|
||||
def __init__(self):
|
||||
self.raw_data = []
|
||||
|
||||
async def resort(self):
|
||||
pass
|
||||
|
||||
class DummyScanner:
|
||||
def __init__(self, root):
|
||||
self.recipes_dir = str(root)
|
||||
self._cache = DummyCache()
|
||||
|
||||
async def find_recipes_by_fingerprint(self, fingerprint):
|
||||
return []
|
||||
|
||||
async def add_recipe(self, recipe_data):
|
||||
self._cache.raw_data.append(recipe_data)
|
||||
|
||||
scanner = DummyScanner(tmp_path)
|
||||
service = RecipePersistenceService(
|
||||
exif_utils=exif_utils,
|
||||
card_preview_width=512,
|
||||
logger=logging.getLogger("test"),
|
||||
)
|
||||
|
||||
result = await service.save_recipe(
|
||||
recipe_scanner=scanner,
|
||||
image_bytes=b"image-bytes",
|
||||
image_base64=None,
|
||||
name="Workflow Recipe",
|
||||
tags=[],
|
||||
metadata={"base_model": "sd", "loras": []},
|
||||
)
|
||||
|
||||
stored = json.loads(Path(result.payload["json_path"]).read_text())
|
||||
assert stored["has_workflow"] is True
|
||||
assert scanner._cache.raw_data[0]["has_workflow"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_save_recipe_records_no_workflow(tmp_path):
|
||||
exif_utils = DummyExifUtils()
|
||||
|
||||
class DummyScanner:
|
||||
def __init__(self, root):
|
||||
self.recipes_dir = str(root)
|
||||
|
||||
async def find_recipes_by_fingerprint(self, fingerprint):
|
||||
return []
|
||||
|
||||
async def add_recipe(self, recipe_data):
|
||||
return None
|
||||
|
||||
scanner = DummyScanner(tmp_path)
|
||||
service = RecipePersistenceService(
|
||||
exif_utils=exif_utils,
|
||||
card_preview_width=512,
|
||||
logger=logging.getLogger("test"),
|
||||
)
|
||||
|
||||
result = await service.save_recipe(
|
||||
recipe_scanner=scanner,
|
||||
image_bytes=b"image-bytes",
|
||||
image_base64=None,
|
||||
name="Plain Recipe",
|
||||
tags=[],
|
||||
metadata={"base_model": "sd", "loras": []},
|
||||
)
|
||||
|
||||
stored = json.loads(Path(result.payload["json_path"]).read_text())
|
||||
assert stored["has_workflow"] is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_save_recipe_persists_checkpoint_metadata(tmp_path):
|
||||
exif_utils = DummyExifUtils()
|
||||
|
||||
Reference in New Issue
Block a user