Skip to content

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.

5.9 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""routes/llama.py tests: source_build is exposed, and blocking detection runs in a worker thread."""

from __future__ import annotations

import asyncio
import importlib.util
import sys
import threading
import types
from pathlib import Path

import pytest

_BACKEND = Path(__file__).resolve().parents[1]
if str(_BACKEND) not in sys.path:
    sys.path.insert(0, str(_BACKEND))

pytest.importorskip("fastapi")


def _load_route():
    # Stub auth only when its deps are absent; stubs are popped so they never leak.
    stubbed = []
    try:
        import auth.authentication  # noqa: F401
    except Exception:
        auth_pkg = types.ModuleType("auth")
        auth_pkg.__path__ = []
        auth_mod = types.ModuleType("auth.authentication")
        auth_mod.get_current_subject = lambda: "test"
        for name, stub in (("auth", auth_pkg), ("auth.authentication", auth_mod)):
            if name not in sys.modules:
                sys.modules[name] = stub
                stubbed.append(name)
    try:
        spec = importlib.util.spec_from_file_location(
            "llama_route_under_test", str(_BACKEND / "routes" / "llama.py")
        )
        mod = importlib.util.module_from_spec(spec)
        sys.modules["llama_route_under_test"] = mod  # so pydantic resolves forward refs
        spec.loader.exec_module(mod)
        return mod
    finally:
        for name in stubbed:
            sys.modules.pop(name, None)


rl = _load_route()


def test_status_response_exposes_source_build():
    payload = {
        "supported": True,
        "update_available": True,
        "stale": False,
        "installed_tag": None,
        "latest_tag": "b9585",
        "published_repo": "unslothai/llama.cpp",
        "installed_at_utc": None,
        "age_days": None,
        "source_build": True,
        "job": {"state": "idle", "reload_required": False},
    }
    model = rl.LlamaUpdateStatusResponse(**payload)
    assert model.model_dump()["source_build"] is True
    assert model.model_dump()["job"]["reload_required"] is False
    rl.LlamaUpdateStatusResponse(**{**payload, "unexpected": 1})


def test_status_response_exposes_update_size_bytes():
    payload = {
        "supported": True,
        "update_available": True,
        "stale": False,
        "installed_tag": "b9493",
        "latest_tag": "b9518",
        "published_repo": "unslothai/llama.cpp",
        "installed_at_utc": None,
        "age_days": None,
        "source_build": False,
        "update_size_bytes": 123_456_789,
        "job": {"state": "idle"},
    }
    model = rl.LlamaUpdateStatusResponse(**payload)
    assert model.model_dump()["update_size_bytes"] == 123_456_789
    without = {k: v for k, v in payload.items() if k != "update_size_bytes"}
    assert rl.LlamaUpdateStatusResponse(**without).model_dump()["update_size_bytes"] is None


def test_status_response_exposes_update_component():
    model = rl.LlamaUpdateStatusResponse(
        supported = True,
        update_available = True,
        llama_update_available = False,
        update_component = "whisper",
        whisper = {
            "update_available": True,
            "installed_tag": "v1",
            "latest_tag": "v2",
        },
    )
    assert model.model_dump()["update_component"] == "whisper"


def test_changelog_response_exposes_structured_links():
    model = rl.LlamaUpdateChangelogResponse(
        matched = True,
        installed_tag = "b10698",
        latest_tag = "b10715-mix-new",
        changes = [
            {
                "summary": "MTP for Qwen",
                "links": [
                    {
                        "label": "unslothai/llama.cpp#144",
                        "url": "https://github.com/unslothai/llama.cpp/pull/144",
                    }
                ],
            }
        ],
        total_changes = 1,
        release_url = "https://github.com/unslothai/llama.cpp/releases/tag/new",
    )

    assert model.model_dump()["changes"][0]["links"][0]["label"].endswith("#144")


def test_changelog_handler_runs_off_event_loop(monkeypatch):
    seen = {}

    def fake_changelog(
        force_refresh = False,
        installed_tag = None,
        latest_tag = None,
    ):
        seen["thread"] = threading.current_thread()
        seen["refresh"] = force_refresh
        return {"matched": True, "installed_tag": "b1", "latest_tag": "b2"}

    monkeypatch.setattr(rl, "get_update_changelog", fake_changelog)
    out = asyncio.run(rl.llama_update_changelog(force_refresh = True, current_subject = "test"))

    assert out.matched is True
    assert seen == {"thread": seen["thread"], "refresh": True}
    assert seen["thread"] is not threading.main_thread()


def test_backend_status_response_exposes_selection_applied():
    model = rl.LlamaBackendStatusResponse(selection_applied = False)

    assert model.model_dump()["selection_applied"] is False


def test_status_handler_runs_off_event_loop(monkeypatch):
    seen = {}

    def fake_status(force_refresh = False):
        seen["thread"] = threading.current_thread()
        return {
            "supported": True,
            "update_available": True,
            "source_build": True,
            "latest_tag": "b9585",
            "job": {"state": "idle"},
        }

    monkeypatch.setattr(rl, "get_update_status", fake_status)
    out = asyncio.run(rl.llama_update_status(force_refresh = False, current_subject = "t"))
    assert out.source_build is True
    assert seen["thread"] is not threading.main_thread()


def test_update_handler_runs_off_event_loop(monkeypatch):
    seen = {}

    def fake_start():
        seen["thread"] = threading.current_thread()
        return {"started": True, "reason": None, "job": {"state": "running"}}

    monkeypatch.setattr(rl, "start_update", fake_start)
    out = asyncio.run(rl.llama_update(current_subject = "t"))
    assert out.started is True
    assert seen["thread"] is not threading.main_thread()