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
"""Review-time simulation of every path a video attachment can travel; not part of the PR."""
from __future__ import annotations
import base64
import math
import pytest
pytest.importorskip("torch")
from core.inference.llama_cpp import LlamaCppBackend # noqa: E402
from models.inference import ( # noqa: E402
ChatCompletionRequest,
InferenceStatusResponse,
_InferenceRuntimeFields,
)
from routes.inference import ( # noqa: E402
_MAX_VIDEO_B64_CHARS,
_inject_video_part,
_video_b64_rejection,
)
LIMIT = 256 * 1024 * 1024
def _props_backend(props):
b = LlamaCppBackend.__new__(LlamaCppBackend)
b._has_video_input = False
b._query_server_props = lambda: props
return b
@pytest.mark.parametrize("n", [0, 1, 2, 3, 4, 5, 6, 1023, 1024, 3000, 3001, 3002])
def test_the_ceiling_formula_matches_what_base64_actually_produces(n):
assert len(base64.b64encode(b"x" * n)) == 4 * math.ceil(n / 3)
def test_a_clip_of_exactly_the_composer_limit_is_admitted():
# 268435456 = 3*89478485 + 1, so the last byte costs a full padded quad.
assert _MAX_VIDEO_B64_CHARS == 357913944
assert 4 * math.ceil(LIMIT / 3) == 357913944
exact = "A" * 357913944
assert _video_b64_rejection(exact)[1] is None
def test_the_old_floor_expression_would_have_refused_it():
floored = (LIMIT * 4) // 3
assert floored == 357913941
assert floored < 4 * math.ceil(LIMIT / 3)
def test_one_character_over_the_ceiling_is_refused_413():
assert _video_b64_rejection("A" * (_MAX_VIDEO_B64_CHARS + 1))[1] == (
413,
"Video file is too large (max 256 MB).",
)
def test_the_data_uri_header_is_not_counted_against_the_cap():
payload = "A" * _MAX_VIDEO_B64_CHARS
stripped, rejection = _video_b64_rejection(f"data:video/mp4;base64,{payload}")
assert rejection is None
assert stripped == payload
@pytest.mark.parametrize(
"mime",
["video/mp4", "video/quicktime", "video/webm", "video/x-matroska", "video/x-msvideo"],
)
def test_every_container_the_composer_accepts_survives_the_data_uri_strip(mime):
stripped, rejection = _video_b64_rejection(f"data:{mime};base64,QUJD")
assert rejection is None and stripped == "QUJD"
@pytest.mark.parametrize("bad", ["", "data:", "data:video/mp4;base64", "data:,"])
def test_an_unreadable_payload_is_a_400_not_a_crash(bad):
stripped, rejection = _video_b64_rejection(bad)
assert rejection == (400, "Could not read the provided video file.")
def test_a_bare_payload_with_no_header_is_passed_through_untouched():
assert _video_b64_rejection("QUJD") == ("QUJD", None)
def test_a_build_too_old_to_declare_modalities_reports_no_video():
"""Older llama.cpp builds omit /props modalities, so a missing key must read as no video."""
b = _props_backend({"default_generation_settings": {"n_ctx": 4096}})
assert b._query_server_n_ctx() == 4096
assert b._has_video_input is False
@pytest.mark.parametrize(
"props",
[
{"modalities": None},
{"modalities": []},
{"modalities": "vision"},
{"modalities": {"vision": True}},
{"modalities": {"video": None}},
{"modalities": {"video": 0}},
{"modalities": {"video": "false"}},
{},
],
)
def test_a_malformed_modalities_block_never_crashes_the_context_readback(props):
b = _props_backend({**props, "default_generation_settings": {"n_ctx": 2048}})
assert b._query_server_n_ctx() == 2048
assert isinstance(b._has_video_input, bool)
def test_only_a_real_json_true_turns_the_capability_on():
for value, expected in ((True, True), (False, False), (None, False), (0, False)):
b = _props_backend({"modalities": {"video": value}})
b._query_server_n_ctx()
assert b._has_video_input is expected, value
def test_a_swap_to_a_model_without_video_does_not_inherit_the_old_answer():
"""A stale True offers video on a model that cannot take it; the completion then fails."""
b = _props_backend(
{"modalities": {"video": True}, "default_generation_settings": {"n_ctx": 8192}}
)
b._query_server_n_ctx()
assert b._has_video_input is True
b._query_server_props = lambda: {
"modalities": {"video": False},
"default_generation_settings": {"n_ctx": 8192},
}
b._query_server_n_ctx()
assert b._has_video_input is False
def test_an_unreachable_props_leaves_the_capability_off_rather_than_guessing():
b = _props_backend(None)
b._has_video_input = True
assert b._query_server_n_ctx() is None
assert "self._has_video_input = False" in _llama_cpp_source()
def _llama_cpp_source() -> str:
from pathlib import Path
return (
Path(__file__).resolve().parent.parent / "core" / "inference" / "llama_cpp.py"
).read_text(encoding = "utf-8")
def test_the_load_path_and_the_unload_path_both_clear_the_capability():
src = _llama_cpp_source()
assert src.count("self._has_video_input = False") == 2
class _Resp:
def __init__(self, status_code, payload):
self.status_code = status_code
self._payload = payload
def json(self):
if isinstance(self._payload, Exception):
raise self._payload
return self._payload
def _stub_props_http(
monkeypatch,
resp,
record = None,
):
"""Replace httpx.get inside the backend module and capture the call."""
import core.inference.llama_cpp as llama_mod
def _get(url, **kwargs):
if record is not None:
record.update(url = url, **kwargs)
if isinstance(resp, Exception):
raise resp
return resp
monkeypatch.setattr(llama_mod.httpx, "get", _get)
def _live_backend(api_key = None):
b = LlamaCppBackend.__new__(LlamaCppBackend)
b._has_video_input = False
b._api_key = api_key
b._port = 9999
b._host = "127.0.0.1"
return b
@pytest.mark.parametrize("junk", [[1, 2], "props", 7, None, 3.5])
def test_a_props_body_that_is_not_an_object_is_rejected_not_crashed(monkeypatch, junk):
"""A proxy or future build may answer with a list; the readback must give None, not raise."""
b = _live_backend()
_stub_props_http(monkeypatch, _Resp(200, junk))
assert b._query_server_props() is None
assert b._query_server_n_ctx() is None
assert b._has_video_input is False
@pytest.mark.parametrize("status", [401, 403, 404, 500, 503])
def test_a_non_200_props_never_claims_video(monkeypatch, status):
b = _live_backend()
_stub_props_http(monkeypatch, _Resp(status, {"modalities": {"video": True}}))
assert b._query_server_props() is None
assert b._has_video_input is False
def test_an_undecodable_props_body_is_swallowed(monkeypatch):
b = _live_backend()
_stub_props_http(monkeypatch, _Resp(200, ValueError("not json")))
assert b._query_server_props() is None
def test_a_dead_server_is_swallowed(monkeypatch):
b = _live_backend()
_stub_props_http(monkeypatch, OSError("connection refused"))
assert b._query_server_props() is None
def test_the_props_request_carries_the_child_api_key_when_direct_stream_set_one(monkeypatch):
"""llama-server api-key middleware 401s keyless /props; under UNSLOTH_DIRECT_STREAM=1 the child
key must be sent, else the video capability silently reads False."""
record: dict = {}
b = _live_backend(api_key = "secret-token")
_stub_props_http(monkeypatch, _Resp(200, {"modalities": {"video": True}}), record = record)
b._query_server_props()
assert record.get("headers") == {"Authorization": "Bearer secret-token"}
def test_the_props_request_sends_no_auth_header_when_there_is_no_child_key(monkeypatch):
record: dict = {}
b = _live_backend(api_key = None)
_stub_props_http(monkeypatch, _Resp(200, {}), record = record)
b._query_server_props()
assert record.get("headers") is None
def test_the_runtime_field_is_declared_and_defaults_off_for_every_non_gguf_model():
"""Non-GGUF models never set has_video_input, so it must default to False and refuse video."""
assert "has_video_input" in _InferenceRuntimeFields.model_fields
assert InferenceStatusResponse().has_video_input is False
def test_the_generic_runtime_mapper_actually_picks_the_capability_up():
"""`_llama_runtime_fields` maps a response field to `_<name>` on the backend.
If that mapping missed, `has_video_input` would be hardcoded False on the
wire and the whole feature would be unreachable from the UI."""
from routes.inference import _llama_runtime_fields
class _Stub:
pass
backend = _Stub()
for name in _InferenceRuntimeFields.model_fields:
setattr(backend, f"_{name}", None)
backend._has_video_input = True
backend._has_audio_input = False
for extra in (
"requested_spec_mode",
"requested_parallel_slots",
"effective_parallel_slots",
"requested_extra_args",
"is_diffusion",
):
setattr(backend, extra, None)
fields = _llama_runtime_fields(backend)
assert fields["has_video_input"] is True
assert fields["has_audio_input"] is False
def test_an_old_client_that_sends_no_video_field_is_unaffected():
req = ChatCompletionRequest(messages = [{"role": "user", "content": "hi"}])
assert req.video_base64 is None
def test_an_old_backend_would_ignore_the_new_field_rather_than_422():
"""ChatCompletionRequest uses extra: allow, so an older backend silently drops a newer app's clip."""
assert ChatCompletionRequest.model_config["extra"] == "allow"
def test_the_field_round_trips_through_json_unchanged():
payload = "data:video/mp4;base64,QUJD"
req = ChatCompletionRequest.model_validate(
{"messages": [{"role": "user", "content": "hi"}], "video_base64": payload}
)
assert req.video_base64 == payload
assert req.model_dump()["video_base64"] == payload
def test_an_empty_message_list_is_a_no_op():
messages: list[dict] = []
_inject_video_part(messages, "AAAA")
assert messages == []
def test_a_user_turn_with_a_none_content_is_promoted_without_losing_the_clip():
messages = [{"role": "user", "content": None}]
_inject_video_part(messages, "AAAA")
assert messages[0]["content"] == [
{"type": "text", "text": ""},
{"type": "input_video", "input_video": {"data": "AAAA"}},
]
def test_the_clip_lands_beside_an_image_rather_than_replacing_it():
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "compare these"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AA"}},
],
}
]
_inject_video_part(messages, "VVVV")
types = [p["type"] for p in messages[0]["content"]]
assert types == ["text", "image_url", "input_video"]
def test_a_tool_turn_after_the_last_user_turn_does_not_steal_the_clip():
messages = [
{"role": "user", "content": "watch this"},
{"role": "assistant", "content": "", "tool_calls": [{"id": "1"}]},
{"role": "tool", "tool_call_id": "1", "content": "42"},
]
_inject_video_part(messages, "VVVV")
assert messages[0]["content"][-1]["type"] == "input_video"
assert messages[2]["content"] == "42"
def test_a_long_multi_turn_thread_only_carries_one_clip():
messages = []
for i in range(20):
messages.append({"role": "user", "content": f"q{i}"})
messages.append({"role": "assistant", "content": f"a{i}"})
messages.append({"role": "user", "content": "last"})
_inject_video_part(messages, "VVVV")
injected = [
m
for m in messages
if isinstance(m["content"], list) and any(p["type"] == "input_video" for p in m["content"])
]
assert len(injected) == 1
assert injected[0]["content"][0]["text"] == "last"
# --------------------------------------------------------------------------
# E. the refusal paths, read off the handler
# --------------------------------------------------------------------------
def _routes_source() -> str:
from pathlib import Path
return (Path(__file__).resolve().parent.parent / "routes" / "inference.py").read_text(
encoding = "utf-8"
)
@pytest.mark.parametrize(
"needle",
[
"raise HTTPException(status_code = 400, detail = _VIDEO_INPUT_REFUSAL)",
"_video_clips = _local_video_clips(payload, model_info)",
'if not getattr(llama_backend, "_has_video_input", False):',
'"Video input is not supported together with guided decoding or client-supplied tools yet."',
'"Cannot count tokens for messages containing video."',
],
)
def test_every_path_that_cannot_serve_a_clip_refuses_out_loud(needle):
assert needle in _routes_source()