Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Refuse directories the Images or Video engine holds; other guards only cover chat models."""
from __future__ import annotations
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
import routes.models as models_module
from auth.authentication import get_current_subject
from routes.models import router as models_router
from storage import library_db
class _Backend:
"""Minimal stand-in for the Images engine / Video backend delete-guard surface."""
def __init__(
self,
*,
loaded = None,
base = None,
loading = (),
extra = (),
):
self._loaded = loaded
self._base = base
self._loading = tuple(loading)
self._extra = tuple(extra)
def status(self):
if self._loaded is None:
return {"loaded": False}
return {"loaded": True, "repo_id": self._loaded, "base_repo": self._base}
def loaded_repo_ids(self):
return self._extra
def loading_repo_ids(self):
return self._loading
@pytest.fixture(autouse = True)
def forgotten(monkeypatch):
"""Library entries the route dropped: a model's name, folder and star are keyed by its path,
and left behind they would land on the next model saved there."""
ids: list[str] = []
monkeypatch.setattr(library_db, "delete_entry", ids.append)
return ids
@pytest.fixture()
def client(tmp_path, monkeypatch):
outputs = tmp_path / "outputs"
outputs.mkdir()
monkeypatch.setattr(models_module, "outputs_root", lambda: outputs)
monkeypatch.setattr(models_module, "exports_root", lambda: tmp_path / "exports")
entries = {f"model:training:{outputs / 'my-diffusion-model'}": {}, "upload:abc": {}}
monkeypatch.setattr(library_db, "list_entries", lambda: entries)
app = FastAPI()
app.include_router(models_router, prefix = "/api/models")
app.dependency_overrides[get_current_subject] = lambda: "test-user"
return TestClient(app), outputs
def _model_dir(outputs):
d = outputs / "my-diffusion-model"
d.mkdir()
(d / "model_index.json").write_text("{}", encoding = "utf-8")
return d
def _delete(client, path):
return client.request(
"DELETE",
"/api/models/delete-finetuned",
json = {"model_path": str(path), "source": "training"},
)
def test_refuses_to_delete_a_model_the_images_engine_has_loaded(client, forgotten, monkeypatch):
c, outputs = client
target = _model_dir(outputs)
monkeypatch.setattr(
models_module, "_active_diffusion_backend", lambda: _Backend(loaded = str(target))
)
monkeypatch.setattr(models_module, "_active_video_backend", lambda: None)
resp = _delete(c, target)
assert resp.status_code == 400
assert "Unload the model" in resp.json()["detail"]
assert target.exists()
assert forgotten == []
def test_refuses_the_companion_base_and_the_extra_repos_the_engine_reads(client, monkeypatch):
c, outputs = client
target = _model_dir(outputs)
other = outputs / "somewhere-else"
other.mkdir()
monkeypatch.setattr(
models_module,
"_active_diffusion_backend",
lambda: _Backend(loaded = str(other), base = str(target)),
)
monkeypatch.setattr(models_module, "_active_video_backend", lambda: None)
assert _delete(c, target).status_code == 400
monkeypatch.setattr(
models_module,
"_active_diffusion_backend",
lambda: _Backend(loaded = str(other), extra = (str(target),)),
)
assert _delete(c, target).status_code == 400
assert target.exists()
def test_refuses_while_the_video_backend_is_still_fetching_it(client, monkeypatch):
c, outputs = client
target = _model_dir(outputs)
monkeypatch.setattr(models_module, "_active_diffusion_backend", lambda: None)
monkeypatch.setattr(
models_module, "_active_video_backend", lambda: _Backend(loading = (str(target),))
)
resp = _delete(c, target)
assert resp.status_code == 409
assert "loading" in resp.json()["detail"].lower()
assert target.exists()
def test_allows_the_delete_when_no_diffusion_or_video_model_holds_it(client, monkeypatch):
c, outputs = client
target = _model_dir(outputs)
monkeypatch.setattr(
models_module,
"_active_diffusion_backend",
lambda: _Backend(loaded = str(outputs / "another-model")),
)
monkeypatch.setattr(models_module, "_active_video_backend", lambda: _Backend())
assert _delete(c, target).status_code == 200
assert not target.exists()
def test_a_chat_only_install_can_still_delete(client, forgotten, monkeypatch):
"""No diffusion stack installed: the guard must fail OPEN, not 503 every delete."""
c, outputs = client
target = _model_dir(outputs)
monkeypatch.setattr(models_module, "_active_diffusion_backend", lambda: None)
monkeypatch.setattr(models_module, "_active_video_backend", lambda: None)
assert _delete(c, target).status_code == 200
assert not target.exists()
assert forgotten == [f"model:training:{target}"]
def test_an_unreadable_engine_state_fails_closed(client, monkeypatch):
"""The engine exists but cannot report its state: refuse rather than risk the rmtree."""
c, outputs = client
target = _model_dir(outputs)
class _Broken:
def status(self):
raise RuntimeError("engine wedged")
monkeypatch.setattr(models_module, "_active_diffusion_backend", lambda: _Broken())
monkeypatch.setattr(models_module, "_active_video_backend", lambda: None)
resp = _delete(c, target)
assert resp.status_code == 503
assert target.exists()
def test_refuses_the_delete_while_a_diffusion_training_run_is_active(client, monkeypatch):
import sys
import types
c, outputs = client
target = _model_dir(outputs)
monkeypatch.setattr(models_module, "_active_diffusion_backend", lambda: None)
monkeypatch.setattr(models_module, "_active_video_backend", lambda: None)
stub = types.ModuleType("core.training.diffusion_training_service")
stub.get_diffusion_training_service = lambda: types.SimpleNamespace(is_active = lambda: True)
monkeypatch.setitem(sys.modules, "core.training.diffusion_training_service", stub)
resp = _delete(c, target)
assert resp.status_code == 409
assert "diffusion" in resp.json()["detail"].lower()
assert target.exists()
stub.get_diffusion_training_service = lambda: types.SimpleNamespace(is_active = lambda: False)
assert _delete(c, target).status_code == 200
assert not target.exists()
def test_deleting_a_gguf_variant_drops_its_library_entry(client, forgotten, monkeypatch):
"""A GGUF export is listed via its files; deleting one variant drops only its own entry."""
c, outputs = client
run = outputs.parent / "exports" / "my-export"
run.mkdir(parents = True)
q4, q8 = run / "model-Q4_K_M.gguf", run / "model-Q8_0.gguf"
q4.write_bytes(b"x")
q8.write_bytes(b"x")
monkeypatch.setattr(models_module, "_active_diffusion_backend", lambda: None)
monkeypatch.setattr(models_module, "_active_video_backend", lambda: None)
entries = {f"model:exported:{q4}": {}, f"model:exported:{q8}": {}, "upload:abc": {}}
monkeypatch.setattr(library_db, "list_entries", lambda: entries)
body = {"source": "exported", "export_type": "gguf", "gguf_variant": "Q4_K_M"}
response = c.request(
"DELETE", "/api/models/delete-finetuned", json = {"model_path": str(q4), **body}
)
assert response.status_code == 200, response.text
assert not q4.exists() and q8.exists()
assert forgotten == [f"model:exported:{q4}"]