File size: 11,903 Bytes
f76b6e4 a07daae 41b3e98 a07daae 41b3e98 a07daae 621cf5d a07daae 621cf5d a07daae f76b6e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | from nexus_visual_weaver.lora_adapter import (
_short_error,
_status,
adapter_to_dict,
is_compatible,
load_and_apply,
unload_all,
)
from nexus_visual_weaver.schema import AdapterRecipe
class FakeLoraPipe:
def __init__(self) -> None:
self.loaded: list[tuple[str, dict]] = []
self.adapters: tuple[list[str], list[float]] | None = None
self.unloaded = False
def load_lora_weights(self, repo_id: str, **kwargs) -> None:
"""
Record a call to load LoRA weights for later assertion in tests.
"""
self.loaded.append((repo_id, kwargs))
def set_adapters(self, adapter_names: list[str], adapter_weights: list[float]) -> None:
"""
Store adapter names and their corresponding weights for later inspection.
"""
self.adapters = (adapter_names, adapter_weights)
def unload_lora_weights(self) -> None:
self.unloaded = True
class FailingLoraPipe(FakeLoraPipe):
def load_lora_weights(self, repo_id: str, **kwargs) -> None:
raise RuntimeError("adapter load failed")
class FailingSetAdapterPipe(FakeLoraPipe):
def set_adapters(self, adapter_names: list[str], adapter_weights: list[float]) -> None:
raise RuntimeError("adapter apply failed")
class UnsupportedPipe:
pass
def test_load_and_apply_reports_loaded_for_compatible_pipe() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
weight=0.6,
)
pipe = FakeLoraPipe()
result = load_and_apply(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B")
assert result["status"] == "loaded"
assert result["repo_id"] == "example/style-lora"
assert pipe.loaded == [("example/style-lora", {"adapter_name": "nexus_style"})]
assert pipe.adapters == (["nexus_style"], [0.6])
def test_load_and_apply_reports_skipped_for_incompatible_adapter() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-4B",
task="style",
)
result = load_and_apply(FakeLoraPipe(), recipe, "black-forest-labs/FLUX.2-klein-9B")
assert result["status"] == "skipped_incompatible"
assert "not declared compatible" in result["message"]
def test_load_and_apply_reports_unsupported_pipeline() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
)
result = load_and_apply(UnsupportedPipe(), recipe, "black-forest-labs/FLUX.2-klein-9B")
assert result["status"] == "unsupported_pipeline"
def test_load_and_apply_reports_failed_without_raising() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
)
result = load_and_apply(FailingLoraPipe(), recipe, "black-forest-labs/FLUX.2-klein-9B")
assert result["status"] == "failed"
assert "RuntimeError" in result["message"]
def test_load_and_apply_unloads_when_apply_fails() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
)
pipe = FailingSetAdapterPipe()
result = load_and_apply(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B")
assert result["status"] == "failed"
assert pipe.loaded == [("example/style-lora", {"adapter_name": "nexus_style"})]
assert pipe.unloaded is True
def test_unload_all_uses_pipeline_unload_hook() -> None:
pipe = FakeLoraPipe()
unload_all(pipe)
assert pipe.unloaded is True
def test_unload_all_handles_pipes_without_unload_method() -> None:
# UnsupportedPipe has no unload_lora_weights method, should not raise
pipe = UnsupportedPipe()
unload_all(pipe) # Should complete without error
def test_unload_all_silences_exceptions() -> None:
class RaisingPipe:
def unload_lora_weights(self) -> None:
raise RuntimeError("unload failed")
unload_all(RaisingPipe()) # Should not raise
# --- _status tests ---
def test_status_returns_dict_with_status_field() -> None:
result = _status("disabled")
assert result["status"] == "disabled"
assert result["repo_id"] is None
assert result["adapter_for"] is None
assert result["weight"] is None
def test_status_with_recipe_extracts_recipe_fields() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
weight=0.6,
)
result = _status("loaded", recipe)
assert result["repo_id"] == "example/style-lora"
assert result["adapter_for"] == "black-forest-labs/FLUX.2-klein-9B"
assert result["weight"] == 0.6
def test_status_with_extra_kwargs_included() -> None:
result = _status("loaded", message="Adapter applied.", adapter_name="nexus_style")
assert result["message"] == "Adapter applied."
assert result["adapter_name"] == "nexus_style"
# --- _short_error tests ---
def test_lora_short_error_includes_class_name() -> None:
exc = ValueError("bad input")
result = _short_error(exc)
assert result.startswith("ValueError:")
assert "bad input" in result
def test_lora_short_error_truncates_at_240() -> None:
exc = RuntimeError("x" * 300)
result = _short_error(exc)
# Should be truncated
assert len(result) <= len("RuntimeError: ") + 240
assert result.endswith("...")
def test_lora_short_error_collapses_newlines() -> None:
exc = OSError("line one\nline two")
result = _short_error(exc)
assert "\n" not in result
assert "line one" in result
# --- adapter_to_dict tests ---
def test_adapter_to_dict_returns_complete_dict() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
weight=0.75,
license="apache-2.0",
)
result = adapter_to_dict(recipe)
assert result["repo_id"] == "example/style-lora"
assert result["adapter_for"] == "black-forest-labs/FLUX.2-klein-9B"
assert result["task"] == "style"
assert result["weight"] == 0.75
assert result["license"] == "apache-2.0"
assert isinstance(result, dict)
def test_adapter_to_dict_includes_all_fields() -> None:
recipe = AdapterRecipe(
repo_id="example/adult-lora",
adapter_for="model/base",
task="style",
adult_only=True,
requires_image=True,
runtime_enabled=False,
)
result = adapter_to_dict(recipe)
assert result["adult_only"] is True
assert result["requires_image"] is True
assert result["runtime_enabled"] is False
# --- is_compatible tests ---
def test_is_compatible_returns_true_for_matching_adapter() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B") is True
def test_is_compatible_returns_false_for_runtime_disabled() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
runtime_enabled=False,
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B") is False
def test_is_compatible_returns_false_for_adult_only_without_adult_mode() -> None:
recipe = AdapterRecipe(
repo_id="example/adult-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
adult_only=True,
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B", adult_mode=False) is False
def test_is_compatible_returns_true_for_adult_only_with_adult_mode() -> None:
recipe = AdapterRecipe(
repo_id="example/adult-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
adult_only=True,
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B", adult_mode=True) is True
def test_is_compatible_returns_false_for_requires_image() -> None:
recipe = AdapterRecipe(
repo_id="example/inpaint-lora",
adapter_for="black-forest-labs/FLUX.2-klein-4B",
task="inpaint",
requires_image=True,
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "black-forest-labs/FLUX.2-klein-4B") is False
def test_is_compatible_returns_false_for_pipeline_without_lora_support() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
)
assert is_compatible(UnsupportedPipe(), recipe, "black-forest-labs/FLUX.2-klein-9B") is False
def test_is_compatible_returns_false_for_incompatible_target() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "completely/different-model") is False
def test_is_compatible_returns_true_for_compatible_repo_ids() -> None:
recipe = AdapterRecipe(
repo_id="example/style-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
compatible_repo_ids=["black-forest-labs/FLUX.2-klein-4B"],
)
pipe = FakeLoraPipe()
assert is_compatible(pipe, recipe, "black-forest-labs/FLUX.2-klein-4B") is True
# --- load_and_apply with adult_only recipe tests ---
def test_load_and_apply_skips_adult_only_in_non_adult_mode() -> None:
recipe = AdapterRecipe(
repo_id="example/adult-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
adult_only=True,
)
pipe = FakeLoraPipe()
result = load_and_apply(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B", adult_mode=False)
assert result["status"] == "skipped_incompatible"
assert "Adult Mode is off" in result["message"]
def test_load_and_apply_loads_adult_only_in_adult_mode() -> None:
recipe = AdapterRecipe(
repo_id="example/adult-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
adult_only=True,
)
pipe = FakeLoraPipe()
result = load_and_apply(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B", adult_mode=True)
assert result["status"] == "loaded"
def test_load_and_apply_skips_requires_image_adapter() -> None:
recipe = AdapterRecipe(
repo_id="example/inpaint-lora",
adapter_for="black-forest-labs/FLUX.2-klein-4B",
task="inpaint",
requires_image=True,
)
pipe = FakeLoraPipe()
result = load_and_apply(pipe, recipe, "black-forest-labs/FLUX.2-klein-4B")
assert result["status"] == "skipped_incompatible"
assert "image-conditioning" in result["message"]
def test_load_and_apply_uses_weight_name_when_present() -> None:
recipe = AdapterRecipe(
repo_id="example/weight-lora",
adapter_for="black-forest-labs/FLUX.2-klein-9B",
task="style",
weight_name="style_weights.safetensors",
)
pipe = FakeLoraPipe()
load_and_apply(pipe, recipe, "black-forest-labs/FLUX.2-klein-9B")
assert len(pipe.loaded) == 1
_, kwargs = pipe.loaded[0]
assert kwargs.get("weight_name") == "style_weights.safetensors"
def test_load_and_apply_no_recipe_returns_disabled() -> None:
pipe = FakeLoraPipe()
result = load_and_apply(pipe, None, "any/model")
assert result["status"] == "disabled"
assert result["repo_id"] is None
|