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
import asyncio
import hashlib
import json
import threading
from types import SimpleNamespace
import pytest
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from auth import policy
from auth.authentication import authenticated_via_api_key, get_current_subject
from routes import (
chat_generation_runs,
chat_history,
profile_stats,
prompts,
research_runs,
settings,
)
from state import active_generations
from storage import (
api_usage_db,
chat_generation_runs_db,
profile_stats_db,
research_runs_db,
studio_db,
)
from utils import keyless_api_access
from utils.account_context import (
OWNER,
AccountContext,
arun_as,
bind_account,
current_account_id,
reset_account,
run_as,
)
from utils.paths import studio_db_path
ALICE = AccountContext("a" * 32, "alice")
BOB = AccountContext("b" * 32, "bob")
ACCOUNTS = {account.username: account for account in (OWNER, ALICE, BOB)}
@pytest.fixture(autouse = True)
def account_databases(monkeypatch):
monkeypatch.setattr(policy, "installation_is_multi_user", lambda: True)
for account in ACCOUNTS.values():
monkeypatch.setattr(studio_db, "_schema_ready", set())
monkeypatch.setattr(chat_generation_runs_db, "_schema_ready", set())
run_as(account, chat_generation_runs_db.get_run, "initialize-schema")
active_generations.reset_for_tests()
keyless_api_access._reset_scope_cache()
profile_stats_db.invalidate_profile_stats_cache()
yield
active_generations.reset_for_tests()
keyless_api_access._reset_scope_cache()
profile_stats_db.invalidate_profile_stats_cache()
@pytest.fixture
def client(monkeypatch):
app = FastAPI()
async def subject(request: Request):
account = ACCOUNTS[request.headers.get("x-test-account", "alice")]
marker = bind_account(account)
try:
yield account.username
finally:
reset_account(marker)
app.dependency_overrides[get_current_subject] = subject
app.dependency_overrides[authenticated_via_api_key] = lambda: False
app.include_router(chat_history.router, prefix = "/chat")
app.include_router(chat_generation_runs.router, prefix = "/runs")
app.include_router(settings.router, prefix = "/settings")
app.include_router(prompts.router, prefix = "/prompts")
app.include_router(profile_stats.router, prefix = "/profile")
app.include_router(research_runs.router, prefix = "/research")
async def remove_sandboxes(*args):
return 0, []
monkeypatch.setattr(chat_history, "_remove_sandboxes", remove_sandboxes)
monkeypatch.setattr(chat_history, "_remove_thread_rag_data", lambda *args, **kwargs: None)
with TestClient(app) as test_client:
yield test_client
def seed_chat(
account,
thread_id = "private",
text = "private text",
):
def seed():
studio_db.upsert_chat_thread(
{
"id": thread_id,
"title": text,
"modelType": "base",
"modelId": text,
"createdAt": 1,
}
)
studio_db.upsert_chat_message(
{
"id": "message",
"threadId": thread_id,
"role": "user",
"createdAt": 2,
"content": [{"type": "text", "text": text}],
"attachments": [
{
"id": "attachment",
"name": "private.txt",
"type": "file",
"contentType": "text/plain",
"content": [{"type": "text", "text": text}],
}
],
}
)
run_as(account, seed)
def seed_run(
account,
run_id = "run",
thread_id = "private",
):
return run_as(
account,
chat_generation_runs_db.create_run,
run_id = run_id,
owner_subject = account.username,
thread_id = thread_id,
user_message_id = "message",
assistant_message_id = "reply",
request_payload = {"model": "local", "messages": [{"role": "user", "content": "Hello"}]},
)[0]
@pytest.mark.parametrize(
"method,path,body",
[
("GET", "/chat/threads/private", None),
("PATCH", "/chat/threads/private", {"title": "stolen"}),
("GET", "/chat/threads/private/messages", None),
("GET", "/chat/threads/private/messages/message", None),
(
"PUT",
"/chat/threads/private/messages/message",
{
"id": "message",
"threadId": "private",
"role": "user",
"createdAt": 2,
"attachments": [{"id": "attachment", "name": "renamed.txt"}],
},
),
(
"POST",
"/chat/threads/private/fork",
{
"messageId": "message",
"newThreadId": "stolen",
"createdAt": 3,
},
),
("GET", "/chat/attachments/message/attachment/file", None),
("DELETE", "/chat/attachments/message/attachment", None),
("GET", "/runs/run", None),
("POST", "/runs/run/cancel", None),
("POST", "/runs/run/events", None),
],
)
def test_foreign_ids_do_not_authorize_reads_or_writes(client, method, path, body):
seed_chat(BOB)
seed_run(BOB)
response = client.request(method, path, json = body)
assert response.status_code == 404, response.text
assert run_as(BOB, studio_db.get_chat_thread, "private")["title"] == "private text"
assert run_as(BOB, chat_generation_runs_db.get_run, "run")["status"] == "queued"
def test_list_export_and_delete_stay_in_the_account(client):
seed_chat(BOB)
seed_run(BOB)
assert client.get("/chat/threads").json()["threads"] == []
assert client.get("/runs/active?threadId=private").json() == {"runs": []}
assert client.get("/chat/attachments").json()["attachments"] == []
assert client.get("/chat/export").json()["threadCount"] == 0
bob_event = threading.Event()
with run_as(
BOB, active_generations.ActiveGeneration, bob_event, thread_id = "private", run_id = "run"
):
assert (
client.request("DELETE", "/chat/threads", json = {"ids": ["private"]}).status_code == 200
)
assert not bob_event.is_set()
assert run_as(BOB, studio_db.get_chat_thread, "private") is not None
assert run_as(BOB, studio_db.get_chat_attachment, "message", "attachment") is not None
@pytest.mark.parametrize("supervisor_name", ["chat_generation_supervisor", "research_supervisor"])
def test_cancellation_never_uses_an_unscoped_supervisor(supervisor_name):
calls = []
request = SimpleNamespace(
app = SimpleNamespace(
state = SimpleNamespace(
**{
supervisor_name: SimpleNamespace(cancel = calls.append),
}
)
)
)
alice_event, bob_event = threading.Event(), threading.Event()
with (
run_as(ALICE, active_generations.ActiveGeneration, alice_event, run_id = "same"),
run_as(BOB, active_generations.ActiveGeneration, bob_event, run_id = "same"),
):
run_as(
ALICE,
chat_generation_runs.cancel_account_run,
request,
"same",
supervisor_name = supervisor_name,
)
assert alice_event.is_set() and not bob_event.is_set()
assert calls == ([] if supervisor_name == "chat_generation_supervisor" else ["same"])
@pytest.mark.parametrize(
"cleanup",
[
chat_history._cancel_deleted_research_runs,
chat_history._cancel_research_runs,
chat_history._cancel_chat_generation_runs,
],
)
@pytest.mark.parametrize("supervisor_present", [True, False])
def test_thread_cleanup_scopes_run_ids(cleanup, supervisor_present):
calls = []
supervisor = SimpleNamespace(cancel = calls.append) if supervisor_present else None
request = SimpleNamespace(
app = SimpleNamespace(
state = SimpleNamespace(
research_supervisor = supervisor,
chat_generation_supervisor = supervisor,
)
)
)
alice_event, bob_event = threading.Event(), threading.Event()
with (
run_as(ALICE, active_generations.ActiveGeneration, alice_event, run_id = "same"),
run_as(BOB, active_generations.ActiveGeneration, bob_event, run_id = "same"),
):
run_as(ALICE, cleanup, request, ["same"])
assert alice_event.is_set() and not bob_event.is_set()
research = supervisor_present and cleanup is not chat_history._cancel_chat_generation_runs
assert calls == (["same"] if research else [])
def test_cancel_route_scopes_same_id_in_two_accounts(client):
for account in (ALICE, BOB):
seed_chat(account)
seed_run(account)
alice_event, bob_event = threading.Event(), threading.Event()
with (
run_as(ALICE, active_generations.ActiveGeneration, alice_event, run_id = "run"),
run_as(BOB, active_generations.ActiveGeneration, bob_event, run_id = "run"),
):
assert client.post("/runs/run/cancel").status_code == 200
assert alice_event.is_set() and not bob_event.is_set()
assert run_as(BOB, chat_generation_runs_db.get_run, "run")["status"] == "queued"
def test_create_refuses_a_foreign_supervisor_slot_before_writing(client, monkeypatch):
seed_chat(ALICE)
monkeypatch.setattr(
chat_generation_runs, "_sanitize_request", lambda payload: payload.requestPayload
)
body = {
"runId": "same",
"threadId": "private",
"userMessageId": "message",
"assistantMessageId": "reply",
"requestPayload": {"model": "local"},
}
with run_as(BOB, active_generations.ActiveGeneration, threading.Event(), run_id = "same"):
assert client.post("/runs", json = body).status_code == 404
assert run_as(ALICE, chat_generation_runs_db.get_run, "same") is None
def test_event_wait_executor_retains_account_context(client, monkeypatch):
for account in (OWNER, ALICE):
seed_chat(account, text = account.username)
seed_run(account)
run_as(account, chat_generation_runs_db.request_cancel, "run")
seen = []
original = chat_generation_runs_db.wait_for_events
def wait(*args):
seen.append(current_account_id())
return original(*args)
monkeypatch.setattr(chat_generation_runs_db, "wait_for_events", wait)
response = client.post("/runs/run/events")
assert response.status_code == 200
assert seen == [ALICE.account_id]
events = [
json.loads(line[6:]) for line in response.text.splitlines() if line.startswith("data: ")
]
expected = run_as(ALICE, chat_generation_runs_db.list_events, "run")
assert [(event["seq"], event["createdAt"]) for event in events] == [
(event["seq"], event["createdAt"]) for event in expected
]
def test_clear_history_reaps_only_its_own_images_and_leaves_foreign_runs(client, monkeypatch):
from core.inference import search_images
calls = []
monkeypatch.setattr(
search_images,
"snapshot_and_fence_registrations",
lambda: calls.append(("snapshot", current_account_id())) or set(),
)
monkeypatch.setattr(
search_images,
"clear_cache",
lambda *args: calls.append(("clear", current_account_id())),
)
for account in (ALICE, BOB):
seed_chat(account)
seed_run(account)
bob_event = threading.Event()
with run_as(
BOB, active_generations.ActiveGeneration, bob_event, thread_id = "private", run_id = "run"
):
for _ in range(2):
response = client.request(
"DELETE", "/chat", json = {"ids": ["private"], "operationId": "same-clear"}
)
assert response.status_code == 200, response.text
assert not bob_event.is_set()
assert calls == [("snapshot", ALICE.account_id), ("clear", ALICE.account_id)]
assert run_as(BOB, studio_db.get_chat_thread, "private") is not None
OWNER_PATHS = [
(method, path)
for path, methods in {
"/hugging-face-cache": ("GET", "PUT"),
"/llama-cpp-path": ("GET", "PUT"),
"/upload-limit": ("PUT",),
"/tool-result-limit": ("PUT",),
"/ml-warmup": ("PUT",),
"/helper-precache": ("PUT",),
"/developer-mode": ("PUT",),
"/download-transport": ("PUT",),
"/xet-notice/reserve": ("POST",),
"/model-memory": ("GET", "PUT"),
"/vram-budget": ("GET", "PUT"),
"/diffusion-accelerator-fallback": ("GET", "DELETE"),
"/coding-agents": ("GET",),
"/openai-auto-switch": ("GET", "PUT"),
"/embedding-model": ("GET", "PUT", "DELETE"),
"/embedding-model/resolve": ("GET",),
"/embedding-model/unload": ("POST",),
"/embedding-model/device": ("PUT",),
"/preview-links/rotate": ("POST",),
"/remote-access": ("GET",),
"/remote-access/start": ("POST",),
"/remote-access/stop": ("POST",),
"/remote-access/auto-start": ("PUT",),
"/lan-access": ("GET",),
"/lan-access/start": ("POST",),
"/lan-access/stop": ("POST",),
"/lan-access/auto-start": ("PUT",),
"/lan-access/port": ("PUT",),
"/lan-access/addresses": ("PUT",),
"/preview-sharing": ("GET", "PUT"),
"/keyless-api-access": ("GET", "PUT"),
"/debug/logs/sources": ("GET",),
"/debug/logs": ("GET",),
}.items()
for method in methods
]
@pytest.mark.parametrize("method,path", OWNER_PATHS)
def test_every_owner_setting_rejects_managed_accounts(client, method, path):
response = client.request(method, "/settings" + path, json = {})
assert response.status_code == 403, response.text
@pytest.mark.parametrize(
"path,body",
[
("/personalization", {"profile": {"displayName": "Alice"}}),
("/chat-preferences", {"show_model_disclaimer": True}),
("/current-date-prompt", {"enabled": False}),
("/chat-memory", {"enabled": False}),
],
)
def test_personal_settings_do_not_change_owner_rows(client, path, body):
owner_record = {
"version": 1,
"profile": {"displayName": "Original owner"},
"future": {"keep": True},
}
studio_db.upsert_app_settings({"personalization": owner_record})
with studio_db.get_connection() as conn:
before = tuple(
conn.execute("SELECT * FROM app_settings WHERE key = 'personalization'").fetchone()
)
assert client.put("/settings" + path, json = body).status_code == 200
with studio_db.get_connection() as conn:
after = tuple(
conn.execute("SELECT * FROM app_settings WHERE key = 'personalization'").fetchone()
)
assert after == before
assert studio_db.get_app_setting("personalization") == owner_record
def test_shared_policy_reads_owner_storage_and_restores_context(client, monkeypatch):
seen = []
monkeypatch.setattr(
settings, "get_upload_limit_mb", lambda: seen.append(current_account_id()) or 100
)
assert client.get("/settings/upload-limit").status_code == 200
assert seen == [OWNER.account_id]
assert (
client.put("/settings/personalization", json = {"profile": {"nickname": "alice"}}).status_code
== 200
)
assert (
run_as(ALICE, studio_db.get_app_setting, "personalization")["profile"]["nickname"]
== "alice"
)
assert studio_db.get_app_setting("personalization", None) is None
def test_owner_setting_keeps_200_and_single_account_policy_is_inert(client, monkeypatch):
monkeypatch.setattr(
settings,
"_llama_cpp_path_response",
lambda: settings.LlamaCppPathResponse(source = "default", editable = True, available = False),
)
response = client.get("/settings/llama-cpp-path", headers = {"x-test-account": "unsloth"})
assert response.status_code == 200, response.text
monkeypatch.setattr(policy, "installation_is_multi_user", lambda: False)
owner = client.get("/settings/llama-cpp-path", headers = {"x-test-account": "unsloth"})
assert owner.status_code == 200
assert client.get("/settings/llama-cpp-path").status_code == 403
@pytest.fixture
def fresh_override_cache(monkeypatch):
from utils import openai_auto_switch_settings
monkeypatch.setattr(openai_auto_switch_settings, "_cache", {})
def test_managed_account_saves_its_own_model_overrides(client, fresh_override_cache):
from utils.openai_auto_switch_settings import MODEL_OVERRIDES_SETTING_KEY
saved = client.put(
"/settings/openai-auto-switch/overrides",
json = {"model_id": "org/model:Q4_K_M", "max_seq_length": 8192},
)
assert saved.status_code == 200, saved.text
assert saved.json()["overrides"]["org/model:Q4_K_M"]["max_seq_length"] == 8192
alice = client.get("/settings/openai-auto-switch/overrides")
assert alice.status_code == 200 and "org/model:Q4_K_M" in alice.json()["overrides"]
bob = client.get("/settings/openai-auto-switch/overrides", headers = {"x-test-account": "bob"})
assert bob.status_code == 200 and bob.json()["overrides"] == {}
assert run_as(OWNER, studio_db.get_app_setting, MODEL_OVERRIDES_SETTING_KEY, None) is None
def test_managed_load_uses_its_own_override_then_the_owners(fresh_override_cache):
from utils.openai_auto_switch_settings import resolve_override_for_load, set_model_override
run_as(OWNER, set_model_override, "org/model", max_seq_length = 4096)
assert run_as(ALICE, resolve_override_for_load, "org/model")[1]["max_seq_length"] == 4096
run_as(ALICE, set_model_override, "org/model", max_seq_length = 16384)
assert run_as(ALICE, resolve_override_for_load, "org/model")[1]["max_seq_length"] == 16384
assert run_as(BOB, resolve_override_for_load, "org/model")[1]["max_seq_length"] == 4096
assert run_as(OWNER, resolve_override_for_load, "org/model")[1]["max_seq_length"] == 4096
assert run_as(BOB, resolve_override_for_load, "org/other") == (None, {})
def test_managed_default_save_shadows_the_owners_override(client, fresh_override_cache):
from utils.openai_auto_switch_settings import resolve_override_for_load, set_model_override
run_as(
OWNER,
set_model_override,
"org/model:Q4_K_M",
llama_extra_args = ["--no-warmup"],
max_seq_length = 4096,
gpu_layers = 10,
)
url = "/settings/openai-auto-switch/overrides"
defaults = {"model_id": "org/model:Q4_K_M", "remove": False, "llama_extra_args": []}
saved = client.put(url, json = defaults)
assert saved.status_code == 200, saved.text
assert saved.json()["overrides"] == {"org/model:Q4_K_M": {"llama_extra_args": []}}
for load in (("org/model:Q4_K_M",), ("org/model", None, "Q4_K_M")):
key, override = run_as(ALICE, resolve_override_for_load, *load)
assert key == "org/model:Q4_K_M" and override == {"llama_extra_args": []}
shown = client.get(url, params = {"model_id": "org/model", "gguf_variant": "Q4_K_M"}).json()
assert shown["resolved"] == {"llama_extra_args": []}
owner = run_as(OWNER, resolve_override_for_load, "org/model:Q4_K_M")[1]
assert owner["llama_extra_args"] == ["--no-warmup"] and owner["max_seq_length"] == 4096
assert owner["gpu_layers"] == 10
assert client.put(url, json = {"model_id": "org/model:Q4_K_M", "remove": False}).json()[
"overrides"
] == {"org/model:Q4_K_M": {"llama_extra_args": []}}
forgot = client.put(url, json = {"model_id": "org/model:Q4_K_M", "remove": True})
assert forgot.status_code == 200 and forgot.json()["overrides"] == {}
assert run_as(ALICE, resolve_override_for_load, "org/model:Q4_K_M")[1] == owner
def test_pinned_models_are_per_account(client):
empty = client.get("/settings/pinned-models")
assert empty.status_code == 200 and empty.json() == {
"pinned": None,
"connected": None,
"embedding": None,
}
saved = client.put(
"/settings/pinned-models", json = {"pinned": ["org/a::Q4_K_M", "org/b", "org/b"]}
)
assert saved.status_code == 200, saved.text
assert saved.json() == {
"pinned": ["org/a::Q4_K_M", "org/b"],
"connected": None,
"embedding": None,
}
both = client.put("/settings/pinned-models", json = {"connected": ["external::c1::gpt"]}).json()
assert both == {
"pinned": ["org/a::Q4_K_M", "org/b"],
"connected": ["external::c1::gpt"],
"embedding": None,
}
embed = client.put(
"/settings/pinned-models", json = {"embedding": ["unsloth/bge-m3", "unsloth/bge-m3"]}
).json()
assert embed["embedding"] == ["unsloth/bge-m3"] and embed["connected"] == ["external::c1::gpt"]
cleared = client.put("/settings/pinned-models", json = {"pinned": []}).json()
assert cleared["pinned"] == [] and cleared["connected"] == ["external::c1::gpt"]
bob = client.get("/settings/pinned-models", headers = {"x-test-account": "bob"}).json()
assert bob == {"pinned": None, "connected": None, "embedding": None}
assert (
run_as(OWNER, studio_db.get_app_setting, settings.PINNED_MODELS_SETTING_KEY, None) is None
)
@pytest.mark.parametrize(
"body",
[
{"pinned": ["x"], "extra": 1},
{"pinned": [""]},
{"pinned": ["x"] * (settings.MAX_PINNED_MODELS + 1)},
{"connected": [1]},
{"embedding": [""]},
],
)
def test_pinned_models_rejects_bad_payloads(client, body):
assert client.put("/settings/pinned-models", json = body).status_code == 422
def test_managed_last_model_key_survives_username_rename(client):
payload = {"id": "alice-model", "kind": "gguf"}
assert client.put("/settings/last-local-model", json = payload).status_code == 200
renamed = AccountContext(ALICE.account_id, "new-name")
result = run_as(renamed, settings.get_last_local_model, current_subject = renamed.username)
assert result.id == "alice-model"
digest = hashlib.sha256(b"unsloth").hexdigest()[:32]
assert settings._last_local_model_key("unsloth") == f"last_local_model_load:{digest}"
def test_owner_chats_prompts_and_settings_survive_alice(client):
seed_chat(OWNER, text = "owner chat")
seed_chat(ALICE, text = "alice chat")
studio_db.upsert_chat_settings_merge({"autoTitle": False})
assert client.put("/chat/settings", json = {"autoTitle": True}).status_code == 200
prompt = {"id": "same", "name": "Owner", "text": "owner prompt", "createdAt": 1, "updatedAt": 1}
studio_db.upsert_prompt_entry(prompt)
assert (
client.put("/prompts/entries/same", json = {**prompt, "text": "alice prompt"}).status_code
== 200
)
assert client.delete("/prompts/entries/same").status_code == 204
assert client.get("/chat/export").json()["threads"][0]["title"] == "alice chat"
assert studio_db.list_prompt_entries()[0]["text"] == "owner prompt"
assert studio_db.list_chat_settings()["autoTitle"] is False
assert studio_db.get_chat_thread("private")["title"] == "owner chat"
assert studio_db_path().name == "studio.db" and "accounts" not in str(studio_db_path())
def test_keyless_cache_is_always_populated_from_the_owner():
studio_db.upsert_app_settings(
{"keyless_api_access_scope": "off", "keyless_api_access_tools": False}
)
run_as(
ALICE,
studio_db.upsert_app_settings,
{"keyless_api_access_scope": "full", "keyless_api_access_tools": True},
)
assert run_as(ALICE, keyless_api_access.get_keyless_api_access_settings) == ("off", False)
assert keyless_api_access.get_keyless_api_access_settings() == ("off", False)
with pytest.raises(ValueError, match = "owner"):
run_as(ALICE, keyless_api_access.set_keyless_api_access, "full", tools = True)
assert keyless_api_access.get_keyless_api_access_settings() == ("off", False)
def test_profile_cache_cannot_follow_a_reused_username(monkeypatch):
seed_chat(ALICE, text = "Alice model")
seed_chat(BOB, text = "Bob model")
for account in (ALICE, BOB):
run_as(
account,
studio_db.upsert_chat_message,
{
"id": "reply",
"threadId": "private",
"role": "assistant",
"createdAt": 3,
"content": [{"type": "text", "text": "reply"}],
},
)
renamed_bob = AccountContext(BOB.account_id, "alice")
async def stats(account):
return await arun_as(
account,
profile_stats.get_profile_stats(
days = 30,
tz_offset_minutes = 0,
tz = "",
current_subject = "alice",
),
)
alice = asyncio.run(stats(ALICE))
bob = asyncio.run(stats(renamed_bob))
assert "Alice model" in str(alice)
assert "Alice model" not in str(bob)
assert "Bob model" in str(bob)
def test_alternating_accounts_keep_their_cached_profile_stats(monkeypatch):
"""An actor change must not invalidate a cache the storage layer already keys by account."""
folds = []
fold_messages = profile_stats_db._fold_messages
monkeypatch.setattr(
profile_stats_db,
"_fold_messages",
lambda *args: folds.append(current_account_id()) or fold_messages(*args),
)
async def stats(account):
return await arun_as(
account,
profile_stats.get_profile_stats(
days = 30,
tz_offset_minutes = 0,
tz = "",
current_subject = "reader",
),
)
payloads = [asyncio.run(stats(account)) for account in (ALICE, BOB, ALICE, BOB)]
assert payloads[0] is payloads[2]
assert payloads[1] is payloads[3]
assert folds == [ALICE.account_id, BOB.account_id]
def test_api_usage_statistics_read_only_the_account_database(client):
receipt = api_usage_db.ApiUsageReceipt(
id = "receipt",
subject = "bob",
endpoint = "/v1/chat/completions",
model = "bob-model",
status = "completed",
prompt_tokens = 10,
completion_tokens = 20,
total_tokens = 30,
created_at = 1,
via_api_key = True,
)
run_as(BOB, api_usage_db.record_api_usage, receipt)
response = client.get("/profile/stats")
assert response.status_code == 200, response.text
assert "bob-model" not in response.text
def test_usage_export_reads_only_the_account_database(client):
receipt = api_usage_db.ApiUsageReceipt(
id = "receipt",
subject = "bob",
endpoint = "/v1/chat/completions",
model = "bob-model",
status = "completed",
prompt_tokens = 10,
completion_tokens = 20,
total_tokens = 30,
created_at = 1,
via_api_key = True,
)
run_as(BOB, api_usage_db.record_api_usage, receipt)
as_bob = client.get("/profile/stats/export", headers = {"x-test-account": "bob"})
assert as_bob.status_code == 200, as_bob.text
assert "bob-model" in as_bob.text
response = client.get("/profile/stats/export")
assert response.status_code == 200, response.text
assert "bob-model" not in response.text
def test_single_account_cancel_still_calls_the_original_supervisor(monkeypatch):
monkeypatch.setattr(policy, "installation_is_multi_user", lambda: False)
calls = []
request = SimpleNamespace(
app = SimpleNamespace(
state = SimpleNamespace(
chat_generation_supervisor = SimpleNamespace(cancel = calls.append),
)
)
)
chat_generation_runs.cancel_account_run(
request, "old-run", supervisor_name = "chat_generation_supervisor"
)
assert calls == ["old-run"]
def test_single_account_cancel_without_supervisor_keeps_existing_behavior(client, monkeypatch):
monkeypatch.setattr(policy, "installation_is_multi_user", lambda: False)
seed_chat(OWNER)
seed_run(OWNER)
calls = []
monkeypatch.setattr(
chat_generation_runs, "cancel_account_run", lambda *args, **kwargs: calls.append(args)
)
response = client.post("/runs/run/cancel", headers = {"x-test-account": "unsloth"})
assert response.status_code == 200
assert calls == []
def test_deep_research_foreign_run_is_not_visible_or_cancellable(client):
seed_chat(BOB)
run_as(
BOB,
research_runs_db.create_run,
run_id = "research",
owner_subject = "bob",
thread_id = "private",
user_message_id = "message",
assistant_message_id = None,
config = {},
)
assert client.get("/research/active?threadId=private").json() == {"runs": [], "hasRun": False}
assert client.get("/research/research").status_code == 404
assert client.post("/research/research/cancel").status_code == 404
assert run_as(BOB, research_runs_db.get_run, "research")["status"] == "planning"
def test_research_cleanup_reaches_the_account_keyed_supervisor():
from core import research_runs as research_runs_core
supervisor = research_runs_core.ResearchSupervisor.__new__(
research_runs_core.ResearchSupervisor
)
supervisor._cancel_events = {}
request = SimpleNamespace(
app = SimpleNamespace(state = SimpleNamespace(research_supervisor = supervisor))
)
alice_event = run_as(ALICE, supervisor._cancel_event, "same")
bob_event = run_as(BOB, supervisor._cancel_event, "same")
run_as(ALICE, chat_history._cancel_research_runs, request, ["same"])
assert alice_event.is_set() and not bob_event.is_set()
def test_chat_memory_is_per_account(client):
from utils.paths import studio_root
saved = client.put("/settings/chat-memory", json = {"text": "Alice uses uv.", "enabled": False})
assert saved.status_code == 200, saved.text
assert (studio_root() / "accounts" / ALICE.account_id / "AGENTS.md").read_text(
encoding = "utf-8"
) == "Alice uses uv.\n"
assert not (studio_root() / "AGENTS.md").exists()
owner = client.get("/settings/chat-memory", headers = {"x-test-account": OWNER.username}).json()
assert owner["text"] == "" and owner["enabled"] is True
bob = client.get("/settings/chat-memory", headers = {"x-test-account": "bob"}).json()
assert bob["text"] == "" and bob["enabled"] is True
alice = client.get("/settings/chat-memory").json()
assert alice["text"] == "Alice uses uv.\n" and alice["enabled"] is False
def test_ml_warmup_is_saved_for_owner_and_shared_with_managed_accounts(client, monkeypatch):
from utils.ml_warmup_settings import ML_WARMUP_FEATURES_SETTING_KEY
monkeypatch.delenv("UNSLOTH_STUDIO_DISABLE_TORCH_WARM", raising = False)
assert client.get("/settings/ml-warmup").json() == {
"features": [],
"effective_features": [],
"disabled_by_env": False,
}
response = client.put(
"/settings/ml-warmup",
headers = {"x-test-account": OWNER.username},
json = {"features": ["text"]},
)
assert response.status_code == 200, response.text
assert response.json() == {
"features": ["text"],
"effective_features": ["hardware", "text"],
"disabled_by_env": False,
}
assert run_as(OWNER, studio_db.get_app_setting, ML_WARMUP_FEATURES_SETTING_KEY) == ["text"]
assert run_as(ALICE, studio_db.get_app_setting, ML_WARMUP_FEATURES_SETTING_KEY, None) is None
assert client.get("/settings/ml-warmup").json()["features"] == ["text"]
monkeypatch.setenv("UNSLOTH_STUDIO_DISABLE_TORCH_WARM", "1")
assert client.get("/settings/ml-warmup").json() == {
"features": ["text"],
"effective_features": ["hardware", "text"],
"disabled_by_env": True,
}
@pytest.mark.parametrize("invalid", ["text", True, 1, None, ["unknown"], ["text", 1]])
def test_ml_warmup_api_requires_a_list_of_supported_features(client, invalid):
response = client.put(
"/settings/ml-warmup",
headers = {"x-test-account": OWNER.username},
json = {"features": invalid},
)
assert response.status_code == 422, response.text
@pytest.mark.parametrize("mask", range(32))
def test_every_ml_warmup_selection_round_trips_without_persisting_dependencies(
client, monkeypatch, mask
):
from utils.ml_warmup_settings import ML_WARMUP_FEATURES_SETTING_KEY
monkeypatch.delenv("UNSLOTH_STUDIO_DISABLE_TORCH_WARM", raising = False)
names = ("hardware", "text", "datasets", "media", "quantization")
selected = [feature for bit, feature in enumerate(names) if mask & (1 << bit)]
effective = set(selected)
if effective & {"text", "media", "quantization"}:
effective.add("hardware")
expected = {
"features": selected,
"effective_features": [name for name in names if name in effective],
"disabled_by_env": False,
}
response = client.put(
"/settings/ml-warmup",
headers = {"x-test-account": OWNER.username},
json = {"features": list(reversed(selected)) + selected},
)
assert response.status_code == 200, response.text
assert response.json() == expected
assert client.get("/settings/ml-warmup").json() == expected
assert run_as(OWNER, studio_db.get_app_setting, ML_WARMUP_FEATURES_SETTING_KEY) == selected
assert run_as(ALICE, studio_db.get_app_setting, ML_WARMUP_FEATURES_SETTING_KEY, None) is None