Files
ComfyUI-Lora-Manager/tests/utils/test_model_filename_extension.py
Will Miao 2ceb1e2850 fix(scanner): stop truncating dotted model file names (#1112)
A LoRA named `lora-sd1.5-backlight_slider_v10.safetensors` showed up in the
manager as `lora-sd1`, hid itself from searches for the rest of its name, and
collapsed into the same lora syntax tag as every sibling sharing the prefix.

The name was cut twice.  `_process_model_file()` imports a third-party
`.civitai.info` sidecar by handing `from_civitai_info()` the local stem with
the extension already stripped, and the builder then stripped a second
"extension" from it -- `os.path.splitext` reads everything after the last dot
as one, so the version dot in `1.5` ended the name.  The download path never
hit this because API filenames keep their extension and only need one strip.

Pass the real basename from the migration site, and make the builder strip
only a recognized model extension (`strip_model_extension`), so both input
shapes resolve to the same stem.  The `model_name` fallback that reused the
same expression is fixed with it: on a sidecar without `model.name` the
display name was truncated too.

Libraries already corrupted do not heal on their own: the incremental Refresh
skips paths already in the cache (only a full rebuild reloads metadata) and
startup hydrates rows from SQLite as-is, so the wrong name survives restarts.
Reconcile now compares each cached row against the stem of its file path --
one string compare per file and no extra syscall, so a clean library pays
nothing -- and repairs mismatching rows through `load_metadata()` (which
normalizes the sidecar) and the existing in-place `_sync_cache_from_metadata_impl()`
path, which writes a targeted single-row SQL delta instead of a full save.
Repairs are one-shot, and a missing or corrupt sidecar keeps its row so a full
rebuild can recreate it without losing tags or civitai data.

Tests: the builder keeps dotted stems for all four model classes and still
strips real extensions; the migration writes the full local name to the
sidecar; and reconcile repairs memory, sidecar and SQLite row, runs exactly
once, and never reads metadata on a clean library.
2026-09-15 09:07:56 +08:00

107 lines
3.3 KiB
Python

"""Regression tests for issue #1112.
Dotted model file names (``lora-sd1.5-backlight_slider_v10.safetensors``) must
not be truncated when metadata is built from a CivitAI payload. The builder has
to strip at most one *known* model extension: API filenames keep their
extension, while migration paths (``.civitai.info``) pass the local stem.
"""
import pytest
from py.utils.models import (
CheckpointMetadata,
EmbeddingMetadata,
LoraMetadata,
OtherModelMetadata,
strip_model_extension,
)
DOTTED_STEM = "lora-sd1.5-backlight_slider_v10"
MODEL_CLASSES = [
LoraMetadata,
CheckpointMetadata,
EmbeddingMetadata,
OtherModelMetadata,
]
@pytest.mark.parametrize(
("raw", "expected"),
[
# Dotted stems are not extensions.
(DOTTED_STEM, DOTTED_STEM),
(f"{DOTTED_STEM}.safetensors", DOTTED_STEM),
("model.v1.5.safetensors", "model.v1.5"),
("a.b.c", "a.b.c"),
# Every scanner extension is recognized, case-insensitively.
("weights.GGUF", "weights"),
("weights.pt2", "weights"),
("weights.ckpt", "weights"),
# Extension-free plain names are unchanged.
("plain_name", "plain_name"),
("", ""),
],
)
def test_strip_model_extension_strips_only_known_extensions(raw, expected):
assert strip_model_extension(raw) == expected
@pytest.mark.parametrize("model_cls", MODEL_CLASSES)
@pytest.mark.parametrize(
"file_name",
[
f"{DOTTED_STEM}.safetensors", # CivitAI API / download shape
DOTTED_STEM, # .civitai.info migration shape (already extension-free)
],
)
def test_from_civitai_info_keeps_dotted_stem(model_cls, file_name):
version_info = {
"baseModel": "SD 1.5",
"name": "v1.0",
"model": {"name": "Light Control", "description": "", "tags": []},
}
file_info = {"name": file_name, "sizeKB": 1024, "hashes": {"SHA256": "a" * 64}}
metadata = model_cls.from_civitai_info(
version_info, file_info, f"/models/{DOTTED_STEM}.safetensors"
)
assert metadata.file_name == DOTTED_STEM
assert metadata.model_name == "Light Control"
@pytest.mark.parametrize("model_cls", MODEL_CLASSES)
def test_from_civitai_info_model_name_fallback_uses_full_stem(model_cls):
"""A sidecar without ``model.name`` must fall back to the full local stem."""
version_info = {"baseModel": "SD 1.5", "model": {"description": "", "tags": []}}
file_info = {"name": DOTTED_STEM, "sizeKB": 1024, "hashes": {}}
metadata = model_cls.from_civitai_info(
version_info, file_info, f"/models/{DOTTED_STEM}.safetensors"
)
assert metadata.file_name == DOTTED_STEM
assert metadata.model_name == DOTTED_STEM
@pytest.mark.parametrize("model_cls", MODEL_CLASSES)
def test_from_civitai_info_model_name_still_wins_over_stem(model_cls):
"""The CivitAI model name stays authoritative when present."""
version_info = {
"baseModel": "SDXL",
"model": {"name": "Chiaroscuro Light", "description": "", "tags": ["light"]},
}
file_info = {
"name": f"{DOTTED_STEM}.safetensors",
"sizeKB": 1024,
"hashes": {},
}
metadata = model_cls.from_civitai_info(
version_info, file_info, f"/models/{DOTTED_STEM}.safetensors"
)
assert metadata.file_name == DOTTED_STEM
assert metadata.model_name == "Chiaroscuro Light"