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.

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

"""Tests for MaxBodyMiddleware, SecurityHeadersMiddleware, and the /api/health auth gate."""

import asyncio
import importlib.util
import json
import os
import re
import sys
import types
from pathlib import Path

import pytest
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse, Response
from fastapi.testclient import TestClient
from starlette.middleware.gzip import GZipMiddleware


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


@pytest.fixture(scope = "module")
def main_module():
    import main as _main  # noqa: F401
    return _main


@pytest.fixture(scope = "module")
def zstd():
    if sys.version_info >= (3, 14):
        from compression import zstd  # novermin
    else:
        from backports import zstd
    return zstd


def _make_protected_app(
    max_bytes: int,
    main_module,
    request_max_bytes_getter = None,
    upload_passthrough_prefixes: tuple = (),
    upload_passthrough_max_bytes_getter = None,
    upload_passthrough_exact_paths: tuple = (),
    chunked_upload_exact_paths: tuple = (),
):
    app = FastAPI()
    app.add_middleware(
        main_module.MaxBodyMiddleware,
        max_bytes_getter = lambda: max_bytes,
        protected_prefixes = (
            "/v1/chat/completions",
            "/v1/systemone",
            "/api/auth",
            "/api/inference",
            "/api/settings",
            "/api/train",
        ),
        request_max_bytes_getter = request_max_bytes_getter,
        upload_passthrough_prefixes = upload_passthrough_prefixes,
        upload_passthrough_max_bytes_getter = upload_passthrough_max_bytes_getter,
        upload_passthrough_exact_paths = upload_passthrough_exact_paths,
        chunked_upload_exact_paths = chunked_upload_exact_paths,
    )

    @app.post("/v1/chat/completions")
    async def chat(payload: dict):
        return {"ok": True, "n": len(payload.get("text", ""))}

    @app.post("/v1/systemone")
    async def systemone(payload: dict):
        return {"ok": True, "n": len(payload.get("text", ""))}

    @app.post("/api/auth/login")
    async def login(request: Request):
        return {"ok": True, "total": len(await request.body())}

    @app.post("/api/other")
    async def other(payload: dict):
        return {"ok": True, "unprotected": True}

    @app.put("/api/settings/upload-limit")
    async def update_upload_limit(payload: dict):
        return {"ok": True, "limit": payload.get("max_upload_size_mb")}

    @app.post("/api/train/upload")
    async def upload(request: Request):
        total = 0
        chunks = 0
        async for chunk in request.stream():
            if chunk:
                chunks += 1
                total += len(chunk)
        return {"ok": True, "chunks": chunks, "total": total}

    @app.post("/api/inference/audio/transcribe/raw")
    async def transcribe_raw(request: Request):
        return {"ok": True, "total": len(await request.body())}

    @app.get("/api/train/status")
    async def status_get():
        return {"ok": True, "get": True}

    return app


class TestMaxBodyMiddleware:
    def test_small_protected_body_passes(self, main_module):
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)
        r = c.post("/v1/chat/completions", json = {"text": "x" * 100})
        assert r.status_code == 200
        assert r.json()["n"] == 100

    def test_large_declared_content_length_rejected(self, main_module):
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)
        r = c.post("/v1/chat/completions", json = {"text": "x" * 5000})
        assert r.status_code == 413
        assert "too large" in r.json()["detail"].lower()

    def test_zstd_body_is_decoded_on_systemone(self, main_module, zstd):
        c = TestClient(_make_protected_app(4096, main_module))
        headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"}
        payload = json.dumps({"text": "x" * 3000}).encode()

        decoded = c.post("/v1/systemone", content = zstd.compress(payload), headers = headers)
        frames = c.post(
            "/v1/systemone",
            content = zstd.compress(payload[:1500]) + zstd.compress(payload[1500:]),
            headers = headers,
        )
        oversized = c.post(
            "/v1/systemone",
            content = zstd.compress(json.dumps({"text": "x" * 100_000}).encode()),
            headers = headers,
        )
        garbage = c.post("/v1/systemone", content = b"not zstd", headers = headers)
        truncated = c.post("/v1/systemone", content = zstd.compress(payload)[:-4], headers = headers)

        assert decoded.status_code == 200
        assert decoded.json()["n"] == 3000
        assert frames.status_code == 200
        assert frames.json()["n"] == 3000
        assert oversized.status_code == 413
        assert garbage.status_code == 400
        assert "zstd" in garbage.json()["detail"]
        assert truncated.status_code == 400

    def test_zstd_bomb_is_not_decoded_off_systemone(self, main_module, zstd):
        c = TestClient(_make_protected_app(1 << 30, main_module))
        bomb = zstd.compress(b"\0" * (64 * 1024 * 1024))
        r = c.post(
            "/api/auth/login",
            content = bomb,
            headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"},
        )
        assert r.status_code == 200
        assert r.json()["total"] == len(bomb)

    def test_zstd_bomb_on_systemone_hits_decoded_cap(self, main_module, zstd):
        c = TestClient(_make_protected_app(1 << 30, main_module))
        bomb = zstd.compress(json.dumps({"text": "x" * (9 * 1024 * 1024)}).encode())
        r = c.post(
            "/v1/systemone",
            content = bomb,
            headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"},
        )
        assert len(bomb) < 64 * 1024
        assert r.status_code == 413
        assert "zstd" in r.json()["detail"]

    def test_zstd_frame_flood_reads_each_byte_once(self, main_module, zstd):
        fed = []

        class CountingDecompressor:
            def __init__(self):
                self._d = zstd.ZstdDecompressor()

            def decompress(self, data, *args, **kwargs):
                fed.append(len(data))
                return self._d.decompress(data, *args, **kwargs)

            def __getattr__(self, name):
                return getattr(self._d, name)

        counting = types.SimpleNamespace(
            ZstdDecompressor = CountingDecompressor,
            ZstdError = zstd.ZstdError,
            get_frame_size = zstd.get_frame_size,
        )
        flood = zstd.compress(b"") * 1023 + zstd.compress(b"{}")
        assert main_module._zstd_decode(counting, flood, 1 << 20) == b"{}"
        assert sum(fed) == len(flood)
        with pytest.raises(zstd.ZstdError):
            main_module._zstd_decode(zstd, zstd.compress(b"") + flood, 1 << 20)

    def test_unprotected_prefix_passes_large_body(self, main_module):
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)
        r = c.post("/api/other", json = {"text": "x" * 5000})
        assert r.status_code == 200
        assert r.json()["unprotected"] is True

    def test_route_specific_cap_overrides_default(self, main_module):
        app = _make_protected_app(
            4096,
            main_module,
            request_max_bytes_getter = lambda path: 128 if path.endswith("/transcribe/raw") else 4096,
        )
        c = TestClient(app)

        rejected = c.post(
            "/api/inference/audio/transcribe/raw",
            content = b"x" * 129,
        )
        accepted = c.post(
            "/api/inference/audio/transcribe/raw",
            content = b"x" * 128,
        )

        assert rejected.status_code == 413
        assert accepted.status_code == 200
        assert accepted.json()["total"] == 128

    def test_stt_routes_use_audio_specific_caps(self, main_module):
        from utils.upload_limits import (
            STT_AUDIO_JSON_MAX_BYTES,
            STT_AUDIO_RAW_MAX_BYTES,
            upload_request_limit_bytes,
        )

        assert (
            main_module._get_request_body_max_bytes("/api/inference/audio/transcribe/raw")
            == STT_AUDIO_RAW_MAX_BYTES
        )
        assert (
            main_module._get_request_body_max_bytes("/api/inference/audio/transcribe")
            == STT_AUDIO_JSON_MAX_BYTES
        )
        # The OpenAI transcription/translation routes are multipart, so they get headroom over the raw cap.
        for path in (
            "/v1/audio/transcriptions",
            "/api/inference/audio/transcriptions",
            "/v1/audio/translations",
            "/api/inference/audio/translations",
        ):
            assert main_module._get_request_body_max_bytes(path) == upload_request_limit_bytes(
                STT_AUDIO_RAW_MAX_BYTES
            ), path
            assert path in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS, path
            assert main_module._get_upload_passthrough_request_max_bytes(path) == (
                upload_request_limit_bytes(STT_AUDIO_RAW_MAX_BYTES)
            ), path
            assert main_module._get_upload_passthrough_request_max_bytes(path + "/") == (
                upload_request_limit_bytes(STT_AUDIO_RAW_MAX_BYTES)
            ), path
        from utils.upload_limits import audio_input_max_bytes

        for path in ("/v1/audio/inputs", "/api/inference/audio/inputs"):
            assert path in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS, path
            assert main_module._get_upload_passthrough_request_max_bytes(path) == (
                audio_input_max_bytes()
            ), path
        from utils.upload_limits import (
            VIDEO_INPUT_REFERENCE_JSON_MAX_BYTES,
            VIDEO_INPUT_REFERENCE_MAX_BYTES,
        )

        for path in ("/v1/videos", "/api/inference/videos"):
            expected = max(
                upload_request_limit_bytes(VIDEO_INPUT_REFERENCE_MAX_BYTES),
                VIDEO_INPUT_REFERENCE_JSON_MAX_BYTES,
            )
            assert main_module._get_request_body_max_bytes(path) == expected, path
            assert path in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS, path
            assert main_module._get_upload_passthrough_request_max_bytes(path) == expected, path
            assert VIDEO_INPUT_REFERENCE_JSON_MAX_BYTES > (
                4 * ((VIDEO_INPUT_REFERENCE_MAX_BYTES + 2) // 3)
            )
        assert "/v1/videos/video_abc" not in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS
        assert main_module._get_request_body_max_bytes("/v1/videos/video_abc") == (
            main_module.default_request_body_limit_bytes()
        )

    def test_settings_put_body_over_cap_rejected(self, main_module):
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)
        r = c.put(
            "/api/settings/upload-limit",
            json = {"max_upload_size_mb": 500, "padding": "x" * 5000},
        )
        assert r.status_code == 413
        assert "too large" in r.json()["detail"].lower()

    def test_chunked_upload_over_cap_rejected(self, main_module):
        # Chunked transfer-encoding must not bypass a Content-Length-only check.
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)

        def gen():
            yield b'{"text":"'
            yield b"x" * 800
            yield b'"}'
            yield b"\n" + b"y" * 500

        r = c.post(
            "/v1/chat/completions",
            content = gen(),
            headers = {"content-type": "application/json"},
        )
        assert r.status_code == 413
        assert "too large" in r.json()["detail"].lower()

    def test_chunked_upload_under_cap_passes(self, main_module):
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)

        def gen():
            yield b'{"text":"'
            yield b"x" * 50
            yield b'"}'

        r = c.post(
            "/v1/chat/completions",
            content = gen(),
            headers = {"content-type": "application/json"},
        )
        assert r.status_code == 200
        assert r.json()["n"] == 50

    def test_get_not_subject_to_cap(self, main_module):
        app = _make_protected_app(1024, main_module)
        c = TestClient(app)
        r = c.get("/api/train/status")
        assert r.status_code == 200

    def test_upload_passthrough_uses_dedicated_declared_cap(self, main_module):
        app = _make_protected_app(
            128,
            main_module,
            upload_passthrough_prefixes = ("/api/train/upload",),
            upload_passthrough_max_bytes_getter = lambda: 1024,
        )
        c = TestClient(app)
        r = c.post(
            "/api/train/upload",
            content = b"x" * 512,
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 200
        assert r.json()["total"] == 512

    def test_diffusion_dataset_upload_in_body_passthrough(self, main_module):
        # The upload route needs the exact-path passthrough with the upload cap, else near-limit
        # batches 413; its JSON sub-routes keep the small cap.
        from utils.upload_limits import (
            default_request_body_limit_bytes,
            upload_request_limit_bytes,
        )

        path = "/api/train/diffusion/dataset"
        assert path in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS
        assert not any(path.startswith(p) for p in main_module._BODY_UPLOAD_PASSTHROUGH_PREFIXES)
        cap = main_module._get_upload_passthrough_request_max_bytes(path)
        assert cap == upload_request_limit_bytes()
        assert cap > default_request_body_limit_bytes()

    def test_library_uploads_are_capped_before_parsing(self, main_module):
        from utils.upload_limits import (
            LIBRARY_UPLOAD_MAX_BYTES,
            default_request_body_limit_bytes,
            upload_request_limit_bytes,
        )

        assert "/api/library" in main_module._BODY_PROTECTED_PREFIXES
        for path in ("/api/library/uploads", "/api/library/uploads/"):
            assert main_module._get_upload_passthrough_request_max_bytes(path) == (
                upload_request_limit_bytes(LIBRARY_UPLOAD_MAX_BYTES)
            ), path
        path = "/api/library/uploads/abc/text"
        assert path not in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS
        assert main_module._get_upload_passthrough_request_max_bytes(path) == (
            default_request_body_limit_bytes()
        )

    def test_browser_posts_are_capped_before_auth(self, main_module):
        from routes.browser import router

        posts = [route.path for route in router.routes if "POST" in route.methods]
        assert posts
        for path in posts:
            assert any(
                f"/api/browser{path}".startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES
            ), path

    def test_auth_posts_are_capped_before_auth(self, main_module):
        # login / refresh / desktop-login are reachable without a session.
        from routes.auth import router

        posts = [route.path for route in router.routes if "POST" in route.methods]
        assert posts
        for path in posts:
            assert any(
                f"/api/auth{path}".startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES
            ), path

    def test_diffusion_dataset_json_subroutes_keep_default_cap(self, main_module):
        # A prefix match would let large JSON sub-route bodies bypass the default cap.
        from utils.upload_limits import default_request_body_limit_bytes
        for path in (
            "/api/train/diffusion/dataset/my-set/caption/img.png",
            "/api/train/diffusion/dataset/import-example",
        ):
            assert path not in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS, path
            assert not any(
                path.startswith(p) for p in main_module._BODY_UPLOAD_PASSTHROUGH_PREFIXES
            ), path
            assert main_module._get_upload_passthrough_request_max_bytes(path) == (
                default_request_body_limit_bytes()
            ), path

    def test_diffusion_dataset_trailing_slash_gets_upload_cap(self, main_module):
        # The trailing-slash variant reaches the middleware before redirect_slashes: match it too.
        from utils.upload_limits import (
            default_request_body_limit_bytes,
            upload_request_limit_bytes,
        )

        slashed = "/api/train/diffusion/dataset/"
        assert main_module._get_upload_passthrough_request_max_bytes(slashed) == (
            upload_request_limit_bytes()
        )
        app = _make_protected_app(
            128,
            main_module,
            upload_passthrough_max_bytes_getter = lambda _p: 1024,
            upload_passthrough_exact_paths = ("/api/train/diffusion/dataset",),
        )

        @app.post("/api/train/diffusion/dataset")
        async def upload(request: Request):
            body = await request.body()
            return {"total": len(body)}

        c = TestClient(app)
        for path in ("/api/train/diffusion/dataset", "/api/train/diffusion/dataset/"):
            r = c.post(
                path,
                content = b"x" * 512,
                headers = {"content-type": "application/octet-stream"},
            )
            assert r.status_code == 200, path
            assert r.json()["total"] == 512, path
        r = c.post(
            "/api/train/diffusion/dataset/import-example/",
            content = b"x" * 512,
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 413
        assert (
            main_module._get_upload_passthrough_request_max_bytes(
                "/api/train/diffusion/dataset/import-example/"
            )
            == default_request_body_limit_bytes()
        )

    def test_v1_surface_is_body_protected(self, main_module):
        # Every /v1 POST must be body-capped via the blanket prefix.
        for path in (
            "/v1/images/generations",
            "/v1/audio/generate",
            "/v1/audio/speech",
            "/v1/audio/transcriptions",
            "/v1/audio/translations",
            "/v1/embeddings",
            "/v1/responses",
            "/v1/messages",
            "/v1/chat/completions",
        ):
            assert any(path.startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES), path

    def test_llama_passthrough_posts_are_capped(self, main_module):
        from fastapi import Request
        from fastapi.testclient import TestClient

        for path in ("/tokenize", "/tokenize/", "/detokenize", "/detokenize/"):
            assert any(path.startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES), path

        app = FastAPI()
        app.add_middleware(
            main_module.MaxBodyMiddleware,
            max_bytes_getter = lambda: 64,
            protected_prefixes = main_module._BODY_PROTECTED_PREFIXES,
        )

        @app.post("/tokenize")
        async def _tokenize(request: Request):
            return {"n": len(await request.body())}

        with TestClient(app) as c:
            assert c.post("/tokenize", content = b"x" * 64).status_code == 200
            assert c.post("/tokenize", content = b"x" * 65).status_code == 413

    def test_upload_passthrough_rejects_declared_body_over_dedicated_cap(self, main_module):
        app = _make_protected_app(
            128,
            main_module,
            upload_passthrough_prefixes = ("/api/train/upload",),
            upload_passthrough_max_bytes_getter = lambda: 256,
        )
        c = TestClient(app)
        r = c.post(
            "/api/train/upload",
            content = b"x" * 512,
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 413
        assert "256" in r.json()["detail"]

    def test_upload_passthrough_requires_content_length(self, main_module):
        app = _make_protected_app(
            128,
            main_module,
            upload_passthrough_prefixes = ("/api/train/upload",),
            upload_passthrough_max_bytes_getter = lambda: 1024,
        )
        c = TestClient(app)

        def gen():
            yield b"x" * 64
            yield b"y" * 64

        r = c.post(
            "/api/train/upload",
            content = gen(),
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 411
        assert "Content-Length" in r.json()["detail"]

    def test_exact_path_passthrough_without_content_length_is_capped_not_refused(self, main_module):
        app = _make_protected_app(
            128,
            main_module,
            upload_passthrough_exact_paths = ("/api/train/upload",),
            chunked_upload_exact_paths = ("/api/train/upload",),
            upload_passthrough_max_bytes_getter = lambda path: 1024,
        )
        c = TestClient(app)

        def small():
            yield b"x" * 256
            yield b"y" * 256

        r = c.post(
            "/api/train/upload",
            content = small(),
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 200

        def large():
            yield b"x" * 1024
            yield b"y" * 1024

        r = c.post(
            "/api/train/upload",
            content = large(),
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 413

    def test_a_passthrough_outside_the_chunked_set_still_demands_a_length(self, main_module):
        """Only opted-in paths may skip Content-Length; big uploads keep 411 so no pre-auth body is held."""
        app = _make_protected_app(
            128,
            main_module,
            upload_passthrough_exact_paths = ("/api/train/upload",),
            chunked_upload_exact_paths = (),
            upload_passthrough_max_bytes_getter = lambda path: 1024,
        )
        c = TestClient(app)

        def body():
            yield b"x" * 256

        r = c.post(
            "/api/train/upload",
            content = body(),
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 411

    def test_exact_path_passthrough_does_not_cover_subroutes(self, main_module):
        app = FastAPI()
        app.add_middleware(
            main_module.MaxBodyMiddleware,
            max_bytes_getter = lambda: 128,
            protected_prefixes = ("/api/train",),
            upload_passthrough_exact_paths = ("/api/train/ds",),
            upload_passthrough_max_bytes_getter = lambda path: 10_000,
        )

        @app.post("/api/train/ds")
        async def _upload(request: Request):
            total = 0
            async for chunk in request.stream():
                total += len(chunk)
            return {"ok": True, "total": total}

        @app.post("/api/train/ds/import-example")
        async def _import(payload: dict):
            return {"ok": True}

        c = TestClient(app)
        r = c.post(
            "/api/train/ds",
            content = b"x" * 512,
            headers = {"content-type": "application/octet-stream"},
        )
        assert r.status_code == 200 and r.json()["total"] == 512
        r = c.post("/api/train/ds/import-example", json = {"text": "x" * 5000})
        assert r.status_code == 413


def _make_csp_app(main_module, attach_nonce: str | None = None):
    app = FastAPI()
    app.add_middleware(main_module.SecurityHeadersMiddleware)

    @app.get("/plain")
    async def plain():
        return {"ok": True}

    @app.get("/with-nonce")
    async def with_nonce():
        headers = {}
        if attach_nonce:
            headers[main_module._CSP_SCRIPT_NONCE_HEADER] = attach_nonce
        return Response(
            content = b"<html></html>",
            media_type = "text/html",
            headers = headers,
        )

    return app


def _make_api_cache_app(main_module, file_path: Path | None = None):
    app = FastAPI()
    app.add_middleware(main_module.SecurityHeadersMiddleware)

    @app.get("/api/inference/monitor")
    async def monitor():
        return {"entries": []}

    @app.get("/api/video/asset")
    async def asset():
        return Response(
            content = b"mp4",
            media_type = "video/mp4",
            headers = {"Cache-Control": "private, max-age=31536000, immutable"},
        )

    @app.get("/api/file")
    async def file():
        return FileResponse(file_path)

    return app


class TestSecurityHeadersMiddleware:
    def test_csp_has_no_unsafe_inline_for_script_src(self, main_module):
        app = _make_csp_app(main_module)
        c = TestClient(app)
        r = c.get("/plain")
        assert r.status_code == 200
        csp = r.headers["content-security-policy"]
        # Parse per-directive so style-src unsafe-inline does not false-match.
        directives = {
            chunk.strip().split(" ", 1)[0]: chunk.strip()
            for chunk in csp.split(";")
            if chunk.strip()
        }
        assert "script-src" in directives
        assert "'unsafe-inline'" not in directives["script-src"]
        assert "'unsafe-inline'" in directives["style-src"]

    def test_default_security_headers_present(self, main_module):
        app = _make_csp_app(main_module)
        c = TestClient(app)
        r = c.get("/plain")
        assert r.headers["x-frame-options"] == "DENY"
        assert r.headers["x-content-type-options"] == "nosniff"
        assert r.headers["referrer-policy"] == "no-referrer"
        permissions_policy = r.headers["permissions-policy"]
        assert "camera=()" in permissions_policy
        assert "microphone=(self)" in permissions_policy
        assert "geolocation=()" in permissions_policy
        assert r.headers["server"] == "unsloth-studio"

    def test_api_read_without_cache_policy_is_no_store(self, main_module):
        # Polled JSON like the API monitor: Chromium/WebView2 would write every poll to its disk cache.
        app = _make_api_cache_app(main_module)
        r = TestClient(app).get("/api/inference/monitor")
        assert r.status_code == 200
        assert r.headers["cache-control"] == "no-store"

    def test_api_route_cache_policy_is_kept(self, main_module):
        app = _make_api_cache_app(main_module)
        r = TestClient(app).get("/api/video/asset")
        assert r.headers["cache-control"] == "private, max-age=31536000, immutable"

    def test_api_file_with_validators_stays_revalidatable(self, main_module, tmp_path):
        path = tmp_path / "out.png"
        path.write_bytes(b"png")
        app = _make_api_cache_app(main_module, file_path = path)
        r = TestClient(app).get("/api/file")
        assert r.status_code == 200
        assert "etag" in r.headers
        assert "cache-control" not in r.headers

    def test_non_api_response_gets_no_cache_policy(self, main_module):
        app = _make_csp_app(main_module)
        r = TestClient(app).get("/plain")
        assert "cache-control" not in r.headers

    def test_mirror_endpoints_in_connect_src(self, main_module, monkeypatch):
        monkeypatch.setenv("HF_ENDPOINT", "https://hf-mirror.com")
        monkeypatch.setenv("HF_DATASETS_SERVER", "https://ds.example.com")
        app = _make_csp_app(main_module)
        r = TestClient(app).get("/plain")
        csp = r.headers["content-security-policy"]
        directives = {
            chunk.strip().split(" ", 1)[0]: chunk.strip()
            for chunk in csp.split(";")
            if chunk.strip()
        }
        assert "https://hf-mirror.com" in directives["connect-src"]
        assert "https://ds.example.com" in directives["connect-src"]

    def test_connect_src_unchanged_without_mirror(self, main_module, monkeypatch):
        monkeypatch.delenv("HF_ENDPOINT", raising = False)
        monkeypatch.delenv("HF_DATASETS_SERVER", raising = False)
        app = _make_csp_app(main_module)
        r = TestClient(app).get("/plain")
        csp = r.headers["content-security-policy"]
        assert "hf-mirror.com" not in csp
        assert (
            "connect-src 'self' https://huggingface.co "
            "https://datasets-server.huggingface.co;" in csp
        )

    def test_internal_nonce_header_is_spliced_into_csp_and_stripped(self, main_module):
        nonce = "test-nonce-abc"
        app = _make_csp_app(main_module, attach_nonce = nonce)
        c = TestClient(app)
        r = c.get("/with-nonce")
        csp = r.headers["content-security-policy"]
        assert f"'nonce-{nonce}'" in csp
        assert main_module._CSP_SCRIPT_NONCE_HEADER not in {k.lower() for k in r.headers.keys()}

    def test_build_csp_helper_shape(self, main_module):
        plain = main_module._build_csp()
        assert "script-src 'self';" in plain
        assert "'unsafe-inline'" not in plain.split("script-src", 1)[1].split(";", 1)[0]
        nonced = main_module._build_csp("XYZ")
        assert "script-src 'self' 'nonce-XYZ';" in nonced

    def test_docs_csp_never_widens_script_src(self, main_module):
        # Docs may relax style/font/worker only: a third-party script could read stored tokens.
        docs = main_module._build_csp(docs = True)
        directives = {
            chunk.strip().split(" ", 1)[0]: chunk.strip()
            for chunk in docs.split(";")
            if chunk.strip()
        }
        assert directives["script-src"] == "script-src 'self'"
        assert "'unsafe-inline'" not in directives["script-src"]
        assert "cdn.jsdelivr.net" not in docs
        nonced = main_module._build_csp("XYZ", docs = True)
        assert "script-src 'self' 'nonce-XYZ';" in nonced

        assert "blob:" in directives["worker-src"]
        assert main_module._DOCS_FONT_CSS in directives["style-src"]
        assert main_module._DOCS_FONT_FILES in directives["font-src"]

        plain = main_module._build_csp()
        assert main_module._DOCS_FONT_CSS not in plain
        assert main_module._DOCS_FONT_FILES not in plain
        assert "worker-src 'self';" in plain
        assert "font-src 'self' data:;" in plain

    def test_docs_paths_get_the_relaxed_csp(self, main_module):
        assert "/docs" in main_module._DOCS_PATHS
        assert "/redoc" in main_module._DOCS_PATHS
        assert "/docs/oauth2-redirect" in main_module._DOCS_PATHS

    def test_middleware_relaxes_only_the_docs_paths(self, main_module):
        app = _make_csp_app(main_module)

        @app.get("/docs")
        async def docs():
            return {"ok": True}

        @app.get("/docs/")
        async def docs_slash():
            return {"ok": True}

        c = TestClient(app)
        relaxed = c.get("/docs").headers["content-security-policy"]
        assert main_module._DOCS_FONT_CSS in relaxed

        for path in ("/docs/", "/plain"):
            strict = c.get(path).headers["content-security-policy"]
            assert main_module._DOCS_FONT_CSS not in strict, path

    def test_docs_pages_load_no_third_party_script(self, main_module):
        # Built-in docs point at a CDN; they must stay off so the vendored same-path routes win.
        assert main_module.app.docs_url is None
        assert main_module.app.redoc_url is None
        assert main_module.app.swagger_ui_oauth2_redirect_url is None

        paths = {getattr(route, "path", None) for route in main_module.app.routes}
        assert {"/docs", "/docs/oauth2-redirect", "/redoc", "/openapi.json"} <= paths

        c = TestClient(main_module.app)
        for path in ("/docs", "/redoc", "/docs/oauth2-redirect"):
            body = c.get(path).text
            assert "cdn.jsdelivr.net" not in body, path
            assert "fastapi.tiangolo.com" not in body, path

    def test_docs_inline_script_runs_off_the_response_nonce(self, main_module):
        # Swagger's init is inline, so the nonce must be spliced into the header to match the tag.
        c = TestClient(main_module.app)
        for path in ("/docs", "/docs/oauth2-redirect"):
            r = c.get(path)
            csp = r.headers["content-security-policy"]
            nonce = re.search(r"'nonce-([^']+)'", csp)
            assert nonce, f"{path} served no nonce"
            assert f'<script nonce="{nonce.group(1)}">' in r.text, path
            assert main_module._CSP_SCRIPT_NONCE_HEADER not in {k.lower() for k in r.headers}

        assert (
            "nonce-"
            not in TestClient(main_module.app).get("/redoc").headers["content-security-policy"]
        )

    def test_docs_urls_follow_the_root_path(self, main_module):
        c = TestClient(main_module.app, root_path = "/studio")
        docs = c.get("/docs").text
        assert "'/studio/openapi.json'" in docs
        assert "'/studio/docs/oauth2-redirect'" in docs
        for name in ("swagger-ui-bundle.js", "swagger-ui.css", "favicon-32x32.png"):
            assert f"/studio/docs-assets/{name}" in docs, name

        redoc = c.get("/redoc").text
        assert 'spec-url="/studio/openapi.json"' in redoc
        assert "/studio/docs-assets/redoc.standalone.js" in redoc

        plain = TestClient(main_module.app).get("/docs").text
        assert "/studio/" not in plain
        assert "'/openapi.json'" in plain

    def test_swagger_nonce_survives_a_reflowed_upstream_template(self, main_module):
        # fastapi is unpinned: a reflowed page must still get the nonce, not a 500.
        reflowed = (
            "<html><body><script src='/docs-assets/swagger-ui-bundle.js'></script>\n"
            "<script>\n  const ui = SwaggerUIBundle({url: '/openapi.json'})\n</script>\n"
            "</body></html>"
        )
        r = main_module._nonced_docs_response(reflowed, tag = main_module._SWAGGER_INIT_TAG)
        nonce = r.headers[main_module._CSP_SCRIPT_NONCE_HEADER]
        body = r.body.decode()
        assert f'<script nonce="{nonce}">' in body
        assert "<script src='/docs-assets/swagger-ui-bundle.js'></script>" in body

        with pytest.raises(RuntimeError):
            main_module._nonced_docs_response(
                "<html><body>no inline script</body></html>",
                tag = main_module._SWAGGER_INIT_TAG,
            )

    def test_docs_assets_are_served_from_this_origin(self, main_module):
        c = TestClient(main_module.app)
        for name in ("swagger-ui-bundle.js", "swagger-ui.css", "redoc.standalone.js"):
            r = c.get(f"{main_module._DOCS_ASSETS_URL}/{name}")
            assert r.status_code == 200, name
            assert len(r.content) > 10_000, name

    def test_img_and_media_allow_https_sources(self, main_module):
        # img-src/media-src allow any https source for model-card media; mirrors tauri.conf.json.
        csp = main_module._build_csp()
        directives = {
            chunk.strip().split()[0]: chunk.strip().split()
            for chunk in csp.split(";")
            if chunk.strip()
        }
        for name in ("img-src", "media-src"):
            assert name in directives, f"missing {name} directive"
            # Tokenise and compare with == so CodeQL does not read `in` as URL sanitisation.
            assert any(src == "https:" for src in directives[name])

    def test_headers_applied_to_streaming_response(self, main_module):
        from fastapi.responses import StreamingResponse

        app = FastAPI()
        app.add_middleware(main_module.SecurityHeadersMiddleware)

        @app.get("/stream")
        async def stream():
            async def gen():
                yield b"a"
                yield b"b"

            return StreamingResponse(gen(), media_type = "text/plain")

        r = TestClient(app).get("/stream")
        assert r.status_code == 200
        assert r.text == "ab"
        assert r.headers["x-content-type-options"] == "nosniff"
        assert r.headers["server"] == "unsloth-studio"
        assert "content-security-policy" in r.headers

    @pytest.mark.parametrize(
        "path", ["/api/inference/artifact-preview-frame", "/api/inference/mcp-app-frame"]
    )
    def test_frame_shells_omit_x_frame_options(self, main_module, path):
        app = FastAPI()
        app.add_middleware(main_module.SecurityHeadersMiddleware)

        @app.get(path)
        async def frame():
            return Response(content = b"<html></html>", media_type = "text/html")

        r = TestClient(app).get(path)
        assert r.status_code == 200
        assert "x-frame-options" not in {k.lower() for k in r.headers.keys()}
        assert r.headers["referrer-policy"] == "no-referrer"

    def test_response_start_with_tuple_headers_is_hardened(self, main_module):
        # Middleware must coerce tuple-valued raw headers to a list and still inject security headers
        import asyncio

        async def _inner_app(scope, receive, send):
            await send(
                {
                    "type": "http.response.start",
                    "status": 200,
                    "headers": ((b"content-type", b"text/plain"),),
                }
            )
            await send({"type": "http.response.body", "body": b"ok"})

        captured = {}

        async def _send(message):
            if message["type"] == "http.response.start":
                captured["headers"] = dict(message["headers"])

        async def _receive():
            return {"type": "http.request"}

        mw = main_module.SecurityHeadersMiddleware(_inner_app)
        asyncio.run(mw({"type": "http", "path": "/plain"}, _receive, _send))

        hdrs = captured["headers"]
        assert hdrs[b"server"] == b"unsloth-studio"
        assert b"content-security-policy" in hdrs
        assert hdrs[b"x-frame-options"] == b"DENY"

    def test_is_pure_asgi_not_basehttp_middleware(self, main_module):
        # Must stay pure ASGI: BaseHTTPMiddleware broke SSE disconnect detection.
        from starlette.middleware.base import BaseHTTPMiddleware

        cls = main_module.SecurityHeadersMiddleware
        assert not issubclass(cls, BaseHTTPMiddleware)
        assert not hasattr(cls, "dispatch")

    def test_forwards_receive_channel_unchanged(self, main_module):
        # Receive must be forwarded untouched so client disconnects reach the handler.
        seen = {}

        async def inner_app(scope, receive, send):
            seen["receive"] = receive
            await send({"type": "http.response.start", "status": 200, "headers": []})
            await send({"type": "http.response.body", "body": b"ok", "more_body": False})

        mw = main_module.SecurityHeadersMiddleware(inner_app)
        sentinel_receive = object()
        sent = []

        async def send(message):
            sent.append(message)

        async def run():
            await mw(
                {"type": "http", "path": "/plain", "headers": []},
                sentinel_receive,
                send,
            )

        asyncio.run(run())
        assert seen["receive"] is sentinel_receive
        start = next(m for m in sent if m["type"] == "http.response.start")
        names = {n.lower() for n, _ in start["headers"]}
        assert b"content-security-policy" in names
        assert b"server" in names

    def test_streaming_response_survives_client_disconnect(self, main_module):
        from fastapi import FastAPI, Request
        from fastapi.responses import StreamingResponse

        state = {"cleaned_up": False}
        app = FastAPI()
        app.add_middleware(main_module.SecurityHeadersMiddleware)

        @app.get("/v1/chat/completions")
        async def stream(request: Request):
            async def gen():
                try:
                    for i in range(1000):
                        if await request.is_disconnected():
                            break
                        yield f"data: {i}\n\n".encode()
                        await asyncio.sleep(0.01)
                finally:
                    state["cleaned_up"] = True

            return StreamingResponse(gen(), media_type = "text/event-stream")

        scope = {
            "type": "http",
            "asgi": {"version": "3.0", "spec_version": "2.3"},
            "http_version": "1.1",
            "method": "GET",
            "path": "/v1/chat/completions",
            "raw_path": b"/v1/chat/completions",
            "query_string": b"",
            "root_path": "",
            "scheme": "http",
            "headers": [(b"host", b"testserver")],
            "client": ("127.0.0.1", 50000),
            "server": ("127.0.0.1", 80),
        }

        async def run():
            body_started = asyncio.Event()
            calls = {"n": 0}

            async def receive():
                calls["n"] += 1
                if calls["n"] == 1:
                    return {"type": "http.request", "body": b"", "more_body": False}
                await body_started.wait()
                return {"type": "http.disconnect"}

            sent = []

            async def send(message):
                sent.append(message)
                if message["type"] == "http.response.body" and message.get("body"):
                    body_started.set()

            await asyncio.wait_for(app(scope, receive, send), timeout = 5.0)
            return sent

        sent = asyncio.run(run())
        assert state["cleaned_up"] is True
        start = next(m for m in sent if m["type"] == "http.response.start")
        names = {n.lower() for n, _ in start["headers"]}
        assert b"content-security-policy" in names
        assert b"server" in names


class TestResearchPortMiddleware:
    def test_is_pure_asgi_and_forwards_receive_unchanged(self, main_module):
        from starlette.middleware.base import BaseHTTPMiddleware

        cls = main_module.ResearchPortMiddleware
        assert not issubclass(cls, BaseHTTPMiddleware)
        assert not hasattr(cls, "dispatch")

        seen = {}

        class Supervisor:
            def note_server_address(self, server):
                seen["server"] = server

        async def inner_app(scope, receive, send):
            seen["receive"] = receive
            await send({"type": "http.response.start", "status": 200, "headers": []})
            await send({"type": "http.response.body", "body": b"ok", "more_body": False})

        request_app = type("App", (), {})()
        request_app.state = type("State", (), {"research_supervisor": Supervisor()})()
        sentinel_receive = object()

        async def send(_message):
            return None

        asyncio.run(
            cls(inner_app)(
                {
                    "type": "http",
                    "path": "/api/research/runs/run-1/events",
                    "app": request_app,
                    "server": ("127.0.0.1", 4321),
                },
                sentinel_receive,
                send,
            )
        )

        assert seen["receive"] is sentinel_receive
        assert seen["server"] == ("127.0.0.1", 4321)


class TestFrontendAssets:
    def test_setup_frontend_records_whether_a_catch_all_exists(self, tmp_path, main_module):
        """The lifespan reads this to decide whether the engine paths still need their own
        GET denial: without a catch-all they match on method alone and answer 405."""
        app = FastAPI()
        assert not main_module.setup_frontend(app, tmp_path / "missing")
        assert not getattr(app.state, "frontend_mounted", False)

        (tmp_path / "index.html").write_text("<!doctype html><title>x</title>")
        mounted = FastAPI()
        assert main_module.setup_frontend(mounted, tmp_path)
        assert mounted.state.frontend_mounted is True

    def test_assets_mount_is_matched_before_the_api_routes(self, tmp_path, main_module):
        from fastapi.routing import APIRoute
        from starlette.routing import Mount

        (tmp_path / "index.html").write_text("<!doctype html><title>x</title>")
        assets = tmp_path / "assets"
        assets.mkdir()
        (assets / "app-abc123.js").write_text("export const a = 1;" * 200, encoding = "utf-8")
        app = FastAPI()

        @app.get("/api/ping")
        def ping():
            return {"ok": True}

        assert main_module.setup_frontend(app, tmp_path)
        routes = app.router.routes
        mount_at = next(
            i for i, r in enumerate(routes) if isinstance(r, Mount) and r.path == "/assets"
        )
        api_at = [i for i, r in enumerate(routes) if isinstance(r, APIRoute)]
        assert mount_at < min(api_at)
        assert routes[-1].path == "/{full_path:path}"

        client = TestClient(app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000))
        response = client.get("/assets/app-abc123.js", headers = {"Accept-Encoding": "gzip"})
        assert response.status_code == 200
        assert response.text == "export const a = 1;" * 200
        assert response.headers["cache-control"] == "public, max-age=31536000, immutable"
        assert response.headers["content-encoding"] == "gzip"
        assert client.get("/api/ping").json() == {"ok": True}
        assert client.get("/assets/missing.js").status_code == 404
        assert "<title>x</title>" in client.get("/some/spa/route").text

    def test_a_service_worker_update_gets_a_worker_that_unregisters_itself(
        self, tmp_path, main_module
    ):
        """Another app's worker left on this origin updates against /sw.js; the app shell would fail
        that update forever, so the browser gets a worker that retires the registration."""
        (tmp_path / "index.html").write_text("<!doctype html><title>x</title>")
        (tmp_path / "real-sw.js").write_text("// shipped", encoding = "utf-8")
        app = FastAPI()
        assert main_module.setup_frontend(app, tmp_path)
        client = TestClient(app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000))
        worker = {"Service-Worker": "script"}

        response = client.get("/sw.js", headers = worker)
        assert response.status_code == 200
        assert response.headers["content-type"].startswith("text/javascript")
        assert response.headers["cache-control"] == "no-store"
        assert response.headers["service-worker-allowed"] == "/"
        assert "self.registration.unregister()" in response.text and "fetch" not in response.text
        assert "unregister()" in client.get("/nested/service-worker.js", headers = worker).text
        # Page loads and files that exist are untouched.
        assert "<title>x</title>" in client.get("/sw.js").text
        assert client.get("/real-sw.js", headers = worker).text == "// shipped"
        assert client.get("/api/nope", headers = worker).status_code == 404

    def test_a_second_setup_frontend_keeps_the_first_assets_mount_winning(
        self, tmp_path, main_module
    ):
        """Embedded hosts rerun run_server on one app; the first /assets mount still wins."""
        first, second = tmp_path / "first", tmp_path / "second"
        for root, body in ((first, "first"), (second, "second")):
            (root / "assets").mkdir(parents = True)
            (root / "index.html").write_text("<!doctype html>")
            (root / "assets" / "app.js").write_text(body, encoding = "utf-8")
        app = FastAPI()
        assert main_module.setup_frontend(app, first)
        assert main_module.setup_frontend(app, second)
        client = TestClient(app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000))
        assert client.get("/assets/app.js").text == "first"

    def test_desktop_frontend_is_available_only_through_live_tunnel(self, tmp_path, main_module):
        (tmp_path / "index.html").write_text("<!doctype html><title>remote</title>")
        assets = tmp_path / "assets"
        assets.mkdir()
        (assets / "app.js").write_text("export {};", encoding = "utf-8")
        app = FastAPI()
        app.state.cloudflare_url = None
        assert main_module.setup_frontend(app, tmp_path, tunnel_only = True)
        loopback_client = TestClient(
            app, base_url = "http://127.0.0.1:8888", client = ("127.0.0.1", 40000)
        )
        client = TestClient(app)
        remote_client = TestClient(app, base_url = "https://remote.trycloudflare.com")
        headers = {"CF-Connecting-IP": "198.51.100.7"}
        assert client.get("/").status_code == 404
        assert client.get("/assets/app.js").status_code == 404
        assert loopback_client.get("/").status_code == 200
        assert loopback_client.get("/assets/app.js").status_code == 200
        proxied_loopback = TestClient(
            app,
            base_url = "http://127.0.0.1:8888",
            client = ("127.0.0.1", 40001),
            headers = {"X-Forwarded-For": "203.0.113.7"},
        )
        assert proxied_loopback.get("/").status_code == 404
        assert proxied_loopback.get("/assets/app.js").status_code == 404
        assert loopback_client.get("/", headers = {"Host": "evil.example:8888"}).status_code == 404
        assert remote_client.get("/", headers = headers).status_code == 404

        app.state.cloudflare_url = "https://remote.trycloudflare.com"
        assert remote_client.get("/", headers = headers).status_code == 200
        assert remote_client.get("/settings/api", headers = headers).status_code == 200
        assert remote_client.get("/assets/app.js", headers = headers).status_code == 200
        assert loopback_client.get("/", headers = headers).status_code == 404
        assert loopback_client.get("/").status_code == 200
        assert client.get("/", headers = headers).status_code == 404
        assert client.get("/settings/api", headers = headers).status_code == 404
        assert client.get("/").status_code == 404

    def test_hashed_assets_are_compressed_and_cached(self, tmp_path, main_module):
        content = b"export const value = 'responsive';\n" * 200
        (tmp_path / "page-abc123.js").write_bytes(content)
        app = FastAPI()
        assets_app = GZipMiddleware(
            main_module.ImmutableStaticFiles(directory = tmp_path),
            minimum_size = 1024,
            compresslevel = 6,
        )
        app.mount("/assets", assets_app, name = "assets")

        response = TestClient(app).get(
            "/assets/page-abc123.js",
            headers = {"Accept-Encoding": "gzip"},
        )

        assert response.status_code == 200
        assert response.content == content
        assert response.headers["content-encoding"] == "gzip"
        assert response.headers["cache-control"] == (main_module._IMMUTABLE_ASSET_CACHE_CONTROL)
        assert "accept-encoding" in response.headers["vary"].lower()

    def test_asset_revalidation_keeps_immutable_cache_header(self, tmp_path, main_module):
        (tmp_path / "page-abc123.js").write_text("export {};", encoding = "utf-8")
        app = FastAPI()
        app.mount(
            "/assets",
            main_module.ImmutableStaticFiles(directory = tmp_path),
            name = "assets",
        )
        client = TestClient(app)
        first = client.get("/assets/page-abc123.js")

        response = client.get(
            "/assets/page-abc123.js",
            headers = {"If-None-Match": first.headers["etag"]},
        )

        assert response.status_code == 304
        assert response.headers["cache-control"] == (main_module._IMMUTABLE_ASSET_CACHE_CONTROL)

    def test_range_request_is_not_compressed(self, tmp_path, main_module):
        content = b"export const value = 'responsive';\n" * 200
        (tmp_path / "page-abc123.js").write_bytes(content)
        app = FastAPI()
        assets_app = main_module._AssetGZipMiddleware(
            main_module.ImmutableStaticFiles(directory = tmp_path),
            minimum_size = 1024,
            compresslevel = 6,
        )
        app.mount("/assets", assets_app, name = "assets")

        response = TestClient(app).get(
            "/assets/page-abc123.js",
            headers = {"Accept-Encoding": "gzip", "Range": "bytes=0-99"},
        )

        assert response.status_code == 206
        assert response.headers.get("content-encoding") != "gzip"
        assert response.headers["content-range"] == f"bytes 0-99/{len(content)}"
        assert response.content == content[:100]
        assert response.headers["cache-control"] == (main_module._IMMUTABLE_ASSET_CACHE_CONTROL)


@pytest.fixture
def health_app(tmp_path, monkeypatch):
    """Mount /api/health on a fresh app against an isolated auth db."""
    from auth import storage

    monkeypatch.setattr(storage, "DB_PATH", tmp_path / "auth.db")
    monkeypatch.setattr(storage, "_BOOTSTRAP_PW_PATH", tmp_path / ".bootstrap_password")
    monkeypatch.setattr(storage, "_bootstrap_password", None)

    import main as _main

    # Pin the hardware snapshot (MLX self-repair can hold it); health_check reads all three fields.
    monkeypatch.setattr(_main, "_hardware_snapshot", lambda: (False, None, None))
    app = FastAPI()
    app.add_api_route("/api/health", _main.health_check, methods = ["GET"])

    import secrets as _secrets

    storage.create_initial_user(
        username = storage.DEFAULT_ADMIN_USERNAME,
        password = "human-password-123",
        jwt_secret = _secrets.token_urlsafe(64),
        must_change_password = False,
    )
    return app


class TestHealthAuthGate:
    # Bootstrap fields are unauth for the Tauri watchdog and SPA; version/device_type need a bearer.
    LAUNCHER_BITS = (
        "service",
        "studio_root_id",
        "chat_only",
        "desktop_protocol_version",
        "desktop_manageability_version",
        "supports_desktop_auth",
        "supports_desktop_backend_ownership",
        "native_path_leases_supported",
    )
    FINGERPRINT_FIELDS = ("version", "studio_version", "device_type")

    def test_no_auth_exposes_launcher_bits(self, health_app):
        c = TestClient(health_app)
        r = c.get("/api/health")
        assert r.status_code == 200
        body = r.json()
        assert body["status"] == "healthy"
        assert "timestamp" in body
        for field in self.LAUNCHER_BITS:
            assert field in body, f"missing launcher bit: {field}"
        assert body["service"] == "Unsloth UI Backend"
        for forbidden in self.FINGERPRINT_FIELDS:
            assert forbidden not in body

    def test_invalid_bearer_returns_launcher_bits_only(self, health_app):
        # The async dep must be awaited, else any Bearer header passes.
        c = TestClient(health_app)
        r = c.get(
            "/api/health",
            headers = {"Authorization": "Bearer not-a-real-token"},
        )
        assert r.status_code == 200
        body = r.json()
        assert body["status"] == "healthy"
        for field in self.LAUNCHER_BITS:
            assert field in body
        for forbidden in self.FINGERPRINT_FIELDS:
            assert forbidden not in body

    def test_valid_bearer_returns_full_payload(self, health_app):
        from auth import storage
        from auth.authentication import create_access_token

        token = create_access_token(storage.DEFAULT_ADMIN_USERNAME)
        c = TestClient(health_app)
        r = c.get(
            "/api/health",
            headers = {"Authorization": f"Bearer {token}"},
        )
        assert r.status_code == 200
        body = r.json()
        assert body["status"] == "healthy"
        for field in self.LAUNCHER_BITS + self.FINGERPRINT_FIELDS:
            assert field in body, f"missing: {field}"


# Captured pre-feature with both vars unset: the default CSP must stay byte-identical.
_MAIN_CSP_DEFAULT = (
    "default-src 'self'; img-src 'self' data: blob: https:; "
    "media-src 'self' data: blob: https:; "
    "connect-src 'self' https://huggingface.co https://datasets-server.huggingface.co; "
    "style-src 'self' 'unsafe-inline'; script-src 'self' 'nonce-NONCE'; "
    "worker-src 'self'; font-src 'self' data:; frame-src 'self'; "
    "frame-ancestors 'none'; form-action 'self'; base-uri 'self'"
)
_MAIN_CSP_DOCS = (
    "default-src 'self'; img-src 'self' data: blob: https:; "
    "media-src 'self' data: blob: https:; "
    "connect-src 'self' https://huggingface.co https://datasets-server.huggingface.co; "
    "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; "
    "script-src 'self' 'nonce-NONCE'; worker-src 'self' blob:; "
    "font-src 'self' data: https://fonts.gstatic.com; frame-src 'self'; "
    "frame-ancestors 'none'; form-action 'self'; base-uri 'self'"
)
_MAIN_CSP_COLAB = (
    "default-src 'self'; img-src 'self' data: blob: https:; "
    "media-src 'self' data: blob: https:; "
    "connect-src 'self' blob: data: https://huggingface.co "
    "https://datasets-server.huggingface.co https://*.prod.colab.dev "
    "wss://*.prod.colab.dev https://*.googleusercontent.com "
    "wss://*.googleusercontent.com; style-src 'self' 'unsafe-inline'; "
    "script-src 'self' 'nonce-NONCE' https://*.prod.colab.dev "
    "https://*.googleusercontent.com; worker-src 'self'; font-src 'self' data:; "
    "frame-src 'self'; frame-ancestors *; form-action 'self'; base-uri 'self'"
)


def _connect_src(policy: str) -> list[str]:
    """Exact source tokens of connect-src. Substring checks pass on a prefix."""
    for chunk in policy.split(";"):
        tokens = chunk.strip().split()
        if tokens and tokens[0] == "connect-src":
            return tokens[1:]
    raise AssertionError(f"no connect-src in {policy!r}")


class TestCspHfEndpoints:
    @pytest.fixture(autouse = True)
    def _clean_env(self, monkeypatch):
        monkeypatch.delenv("HF_ENDPOINT", raising = False)
        monkeypatch.delenv("HF_DATASETS_SERVER", raising = False)
        import utils.hf_endpoint as _mod

        monkeypatch.setattr(_mod, "_ds_mirror_warned", False)
        monkeypatch.setattr(_mod, "_rejected_warned", set())
        yield

    @pytest.mark.parametrize(
        "kwargs, expected",
        [
            ({}, _MAIN_CSP_DEFAULT),
            ({"docs": True}, _MAIN_CSP_DOCS),
        ],
    )
    def test_unmirrored_policy_is_byte_identical_to_pre_pr(self, main_module, kwargs, expected):
        assert main_module._build_csp("NONCE", **kwargs) == expected

    def test_unmirrored_colab_policy_is_byte_identical_to_pre_pr(self, main_module, monkeypatch):
        monkeypatch.setattr(main_module, "_IS_COLAB", True)
        assert main_module._build_csp("NONCE") == _MAIN_CSP_COLAB

    @pytest.mark.parametrize("blank", ["", "   ", "\t"])
    def test_blank_env_vars_leave_the_policy_identical(self, main_module, monkeypatch, blank):
        monkeypatch.setenv("HF_ENDPOINT", blank)
        monkeypatch.setenv("HF_DATASETS_SERVER", blank)
        assert main_module._build_csp("NONCE") == _MAIN_CSP_DEFAULT

    def test_endpoints_set_to_the_official_hosts_do_not_duplicate_sources(
        self, main_module, monkeypatch
    ):
        monkeypatch.setenv("HF_ENDPOINT", "https://huggingface.co")
        monkeypatch.setenv("HF_DATASETS_SERVER", "https://datasets-server.huggingface.co")
        assert main_module._build_csp("NONCE") == _MAIN_CSP_DEFAULT

    def test_a_mirror_adds_exactly_its_two_origins(self, main_module, monkeypatch):
        monkeypatch.setenv("HF_ENDPOINT", "https://hf-mirror.com")
        monkeypatch.setenv("HF_DATASETS_SERVER", "https://ds.example.com")
        policy = main_module._build_csp("NONCE")
        assert _connect_src(policy) == [
            "'self'",
            "https://huggingface.co",
            "https://datasets-server.huggingface.co",
            "https://hf-mirror.com",
            "https://ds.example.com",
        ]
        baseline = dict(
            chunk.strip().split(" ", 1)
            for chunk in _MAIN_CSP_DEFAULT.split(";")
            if chunk.strip() and " " in chunk.strip()
        )
        actual = dict(
            chunk.strip().split(" ", 1)
            for chunk in policy.split(";")
            if chunk.strip() and " " in chunk.strip()
        )
        for directive, value in baseline.items():
            if directive == "connect-src":
                continue
            assert actual[directive] == value, directive

    def test_a_mirrored_colab_policy_keeps_every_colab_source(self, main_module, monkeypatch):
        monkeypatch.setattr(main_module, "_IS_COLAB", True)
        monkeypatch.setenv("HF_ENDPOINT", "https://hf-mirror.com")
        sources = _connect_src(main_module._build_csp("NONCE"))
        for required in _connect_src(_MAIN_CSP_COLAB):
            assert required in sources, required
        assert "https://hf-mirror.com" in sources

    def test_a_mirror_is_listed_once_even_if_both_vars_name_it(self, main_module, monkeypatch):
        monkeypatch.setenv("HF_ENDPOINT", "https://hf-mirror.com")
        monkeypatch.setenv("HF_DATASETS_SERVER", "https://hf-mirror.com")
        sources = _connect_src(main_module._build_csp("NONCE"))
        assert sources.count("https://hf-mirror.com") == 1

    @pytest.mark.parametrize(
        "hostile",
        [
            "https://hf-mirror.com; script-src *",
            "https://hf-mirror.com *",
            "https://hf-mirror.com\nscript-src *",
            "https://hf-mirror.com,https://evil.com",
            "javascript:alert(1)",
            "https://user:[email protected]",
        ],
    )
    def test_an_endpoint_cannot_forge_a_directive(self, main_module, monkeypatch, hostile):
        """A bad env var must not widen the policy -- it falls back to the default."""
        monkeypatch.setenv("HF_ENDPOINT", hostile)
        policy = main_module._build_csp("NONCE")
        assert policy == _MAIN_CSP_DEFAULT
        assert policy.count(";") == _MAIN_CSP_DEFAULT.count(";")

    def test_the_header_on_a_real_response_carries_the_mirror(self, main_module, monkeypatch):
        monkeypatch.setenv("HF_ENDPOINT", "https://hf-mirror.com")
        monkeypatch.setenv("HF_DATASETS_SERVER", "https://ds.example.com")
        app = _make_csp_app(main_module)
        r = TestClient(app).get("/plain")
        sources = _connect_src(r.headers["content-security-policy"])
        assert "https://hf-mirror.com" in sources
        assert "https://ds.example.com" in sources

    @pytest.mark.parametrize(
        "endpoint, expected_source",
        [
            ("https://hub.internal/hf", "https://hub.internal"),
            ("https://hub.internal:8443/hf/v2", "https://hub.internal:8443"),
            ("https://hf-mirror.com", "https://hf-mirror.com"),
            ("http://localhost:8080", "http://localhost:8080"),
        ],
    )
    def test_a_path_prefixed_mirror_is_listed_as_an_origin(
        self, main_module, monkeypatch, endpoint, expected_source
    ):
        """Host-source path without trailing / matches only that URL; list mirrors by origin."""
        monkeypatch.setenv("HF_ENDPOINT", endpoint)
        sources = _connect_src(main_module._build_csp("NONCE"))
        assert expected_source in sources
        assert not any(
            s.count("/") > 2 for s in sources if s.startswith(("http://", "https://"))
        ), sources

    @pytest.mark.parametrize(
        "endpoint, loopback_sees, remote_sees",
        [
            ("http://127.0.0.1:9700", "http://127.0.0.1:9700", "https://huggingface.co"),
            ("http://localhost:8080", "http://localhost:8080", "https://huggingface.co"),
            ("https://hf-mirror.com", "https://hf-mirror.com", "https://hf-mirror.com"),
        ],
    )
    def test_a_loopback_endpoint_is_only_reported_to_a_loopback_client(
        self, main_module, monkeypatch, endpoint, loopback_sees, remote_sees
    ):
        """Loopback means browser localhost: elsewhere it is dead or leaks the Hub token."""
        monkeypatch.setenv("HF_ENDPOINT", endpoint)
        local = TestClient(main_module.app, client = ("127.0.0.1", 40000))
        assert local.get("/api/health").json()["hf_endpoint"] == loopback_sees
        remote = TestClient(main_module.app, client = ("192.168.1.50", 40000))
        assert remote.get("/api/health").json()["hf_endpoint"] == remote_sees

    def test_a_tunneled_client_is_not_mistaken_for_a_local_one(self, main_module, monkeypatch):
        """Through the tunnel the socket peer is loopback: cloudflared, not the visitor."""
        monkeypatch.setenv("HF_ENDPOINT", "http://127.0.0.1:9700")
        c = TestClient(main_module.app, client = ("127.0.0.1", 40000))
        tunneled = c.get("/api/health", headers = {"CF-Connecting-IP": "8.8.8.8"})
        assert tunneled.json()["hf_endpoint"] == "https://huggingface.co"
        assert c.get("/api/health").json()["hf_endpoint"] == "http://127.0.0.1:9700"
        # A forged header from a non-loopback peer is ignored (client_ip's rule).
        remote = TestClient(main_module.app, client = ("192.168.1.50", 40000))
        forged = remote.get("/api/health", headers = {"CF-Connecting-IP": "127.0.0.1"})
        assert forged.json()["hf_endpoint"] == "https://huggingface.co"

    @pytest.mark.parametrize(
        "endpoint, remote_sees",
        [
            ("http://127.0.0.1:9700", "https://huggingface.co"),
            ("https://10.0.0.5:8443", "https://huggingface.co"),
            ("https://192.168.1.9", "https://huggingface.co"),
            ("https://[fd00::1]", "https://huggingface.co"),
            ("https://hf-mirror.com", "https://hf-mirror.com"),
        ],
    )
    def test_a_private_network_endpoint_is_not_reported_to_a_remote_browser(
        self, main_module, monkeypatch, endpoint, remote_sees
    ):
        """10.0.0.5 means the VISITOR's 10.0.0.5, one step out from localhost. A
        LAN client is on the backend's network and still gets the real value."""
        monkeypatch.setenv("HF_ENDPOINT", endpoint)
        tunneled = TestClient(main_module.app, client = ("127.0.0.1", 40000))
        seen = tunneled.get("/api/health", headers = {"CF-Connecting-IP": "8.8.8.8"}).json()[
            "hf_endpoint"
        ]
        assert seen == remote_sees
        lan = TestClient(main_module.app, client = ("192.168.1.50", 40000))
        lan_expected = "https://huggingface.co" if "127.0.0.1" in endpoint else endpoint
        assert lan.get("/api/health").json()["hf_endpoint"] == lan_expected


class TestRemoteAccessCORS:
    """Tunnel publishing admits the tunnel origin only; reflecting every origin exposed the local API."""

    def test_configured_cors_exposes_typesafe_request_id(self, main_module):
        cors = next(
            middleware
            for middleware in main_module.app.user_middleware
            if middleware.cls is main_module.RemoteAccessCORSMiddleware
        )
        app = FastAPI()
        app.add_middleware(cors.cls, **{**cors.kwargs, "remote_access_state": app.state})

        @app.get("/decision")
        async def decision():
            return Response(headers = {"x-typesafe-request-id": "decision-request"})

        response = TestClient(app).get("/decision", headers = {"Origin": "tauri://localhost"})
        assert response.status_code == 200
        assert response.headers["access-control-allow-origin"] in ("*", "tauri://localhost")
        assert response.headers["x-typesafe-request-id"] == "decision-request"
        exposed = response.headers["access-control-expose-headers"].lower().split(",")
        assert "x-typesafe-request-id" in {header.strip() for header in exposed}

    TUNNEL = "https://demo-abc.trycloudflare.com"

    @staticmethod
    def _client(main_module, allow_origins):
        from utils.host_policy import cors_origins_for_mode

        app = FastAPI()
        app.state.cloudflare_url = None

        @app.get("/api/auth/status")
        async def _status():
            return {"ok": True}

        app.add_middleware(
            main_module.RemoteAccessCORSMiddleware,
            remote_access_state = app.state,
            allow_origins = allow_origins or cors_origins_for_mode(api_only = True, secure = False),
            allow_credentials = True,
            allow_methods = ["*"],
            allow_headers = ["*"],
            max_age = 60,
        )
        return app, TestClient(app)

    def _allowed(self, client, origin):
        response = client.get("/api/auth/status", headers = {"Origin": origin})
        return response.headers.get("access-control-allow-origin")

    def _preflight(self, client, origin):
        return client.options(
            "/api/auth/status",
            headers = {
                "Origin": origin,
                "Access-Control-Request-Method": "GET",
                "Access-Control-Request-Headers": "authorization",
            },
        ).status_code

    def test_a_published_tunnel_does_not_admit_an_arbitrary_origin(self, main_module):
        app, client = self._client(main_module, None)
        app.state.cloudflare_url = self.TUNNEL

        for origin in ("https://evil.example", "null", "http://127.0.0.1:9999"):
            assert self._allowed(client, origin) is None, origin
            assert self._preflight(client, origin) == 400, origin

    def test_the_tunnel_origin_itself_is_admitted(self, main_module):
        app, client = self._client(main_module, None)
        app.state.cloudflare_url = self.TUNNEL + "/"

        for origin in (self.TUNNEL, self.TUNNEL + ":443"):
            assert self._allowed(client, origin) == origin, origin
            assert self._preflight(client, origin) == 200, origin

        app.state.cloudflare_url = None
        assert self._allowed(client, self.TUNNEL) is None

    def test_the_desktop_app_keeps_its_origin_either_way(self, main_module):
        app, client = self._client(main_module, None)

        for published in (None, self.TUNNEL):
            app.state.cloudflare_url = published
            assert self._allowed(client, "tauri://localhost") == "tauri://localhost"
            assert self._preflight(client, "tauri://localhost") == 200

    def test_the_browser_served_default_is_unchanged(self, main_module):
        """Outside plain api-only the startup list is already ["*"]; nothing here narrows it."""
        app, client = self._client(main_module, ["*"])

        for published in (None, self.TUNNEL):
            app.state.cloudflare_url = published
            assert self._allowed(client, "https://evil.example") == "https://evil.example"


def test_health_reports_the_default_for_a_settings_saved_endpoint(main_module, monkeypatch):
    from starlette.requests import Request
    import utils.hub_settings as hub_settings

    local = Request({"type": "http", "headers": [], "client": ("127.0.0.1", 1)})
    monkeypatch.setenv("HF_ENDPOINT", "https://hub.internal")
    monkeypatch.setenv("HF_DATASETS_SERVER", "https://hub.internal")
    monkeypatch.setattr(hub_settings, "_saved_only_endpoints", frozenset(), raising = False)
    assert main_module._reportable_hf_endpoints(local) == {
        "hf_endpoint": "https://hub.internal",
        "hf_datasets_server": "https://hub.internal",
    }
    monkeypatch.setattr(
        hub_settings, "_saved_only_endpoints", frozenset({"https://hub.internal"}), raising = False
    )
    assert main_module._reportable_hf_endpoints(local) == {
        "hf_endpoint": "https://huggingface.co",
        "hf_datasets_server": "https://datasets-server.huggingface.co",
    }


def test_every_api_write_route_is_body_capped(main_module):
    # FastAPI reads bodies before auth, so an uncapped /api write lets anyone send any size.
    writes = [
        route.path
        for route in main_module.app.routes
        if getattr(route, "methods", None)
        and route.methods & {"POST", "PUT", "PATCH", "DELETE"}
        and route.path.startswith("/api/")
    ]
    assert writes
    for path in writes:
        assert any(path.startswith(p) for p in main_module._BODY_PROTECTED_PREFIXES), path


def _walk_routes(routes):
    from fastapi import routing
    included = getattr(routing, "_IncludedRouter", None)
    for route in routes:
        if included is not None and isinstance(route, included):
            yield from _walk_routes(route.effective_candidates())
        else:
            yield route


def test_every_write_route_is_body_capped_or_an_explicit_upload(main_module, monkeypatch):
    # Walks every route the app serves, root ones included (/models/load), so a new route outside
    # the old prefix list can't come back uncapped. Upload passthroughs carry their own cap.
    from fastapi.testclient import TestClient

    monkeypatch.setattr(main_module, "default_request_body_limit_bytes", lambda: 1024)
    monkeypatch.setattr(main_module, "STT_AUDIO_RAW_MAX_BYTES", 1024)
    monkeypatch.setattr(main_module, "STT_AUDIO_JSON_MAX_BYTES", 1024)
    targets = set()
    for route in _walk_routes(main_module.app.routes):
        methods = getattr(route, "methods", None)
        # Mounts (/mcp, static assets) have no methods; they take POSTs too.
        writes = (methods or {"POST"}) & {"POST", "PUT", "PATCH", "DELETE"}
        if not writes:
            continue
        path = re.sub(r"\{[^}]+\}", "x", route.path)
        for form in {path.rstrip("/") or "/", path.rstrip("/") + "/"}:
            for method in writes:
                targets.add((method, form))
    assert ("POST", "/models/load") in targets and ("POST", "/models/unload/") in targets

    def is_upload(method, path):
        return method != "DELETE" and (
            path.rstrip("/") in main_module._BODY_UPLOAD_PASSTHROUGH_EXACT_PATHS
            or path.startswith(main_module._BODY_UPLOAD_PASSTHROUGH_PREFIXES)
            or main_module._UPLOAD_PASSTHROUGH_RE.match(path) is not None
        )

    def body():
        for _ in range(4):
            yield b"x" * 1000

    uncapped = []
    with_uploads = 0
    client = TestClient(main_module.app, base_url = "http://127.0.0.1", follow_redirects = False)
    for method, path in sorted(targets):
        if is_upload(method, path):
            with_uploads += 1
            continue
        r = client.request(
            method, path, content = body(), headers = {"content-type": "application/json"}
        )
        if r.status_code != 413:
            uncapped.append((method, path, r.status_code))
    assert with_uploads
    assert uncapped == []


def test_rag_document_uploads_pass_through_on_their_own_cap(main_module, monkeypatch):
    from core.rag import config as rag_config
    from utils.upload_limits import upload_request_limit_bytes

    # A positive cap, whatever RAG_MAX_UPLOAD_BYTES the suite runs with (0 is covered separately).
    monkeypatch.setattr(rag_config, "MAX_UPLOAD_BYTES", 200 * 1024 * 1024)

    pattern = main_module._RAG_DOCUMENT_UPLOAD_RE
    for path in (
        "/api/rag/knowledge-bases/kb1/documents",
        "/api/rag/threads/t1/documents",
        "/api/rag/projects/p1/documents/",
        "/api/rag/extract-text",
    ):
        assert pattern.match(path), path
        assert main_module._get_upload_passthrough_request_max_bytes(
            path
        ) == upload_request_limit_bytes(rag_config.MAX_UPLOAD_BYTES)
    # JSON routes keep the default buffered cap.
    for path in (
        "/api/rag/search",
        "/api/rag/knowledge-bases",
        "/api/rag/knowledge-bases/kb1/linked-folders",
    ):
        assert not pattern.match(path), path


def test_sandbox_uploads_pass_through_on_their_own_cap(main_module):
    from utils.upload_limits import SANDBOX_UPLOAD_MAX_BYTES, upload_request_limit_bytes

    pattern = main_module._UPLOAD_PASSTHROUGH_RE
    for path in (
        "/api/inference/sandbox/thread-1/upload",
        "/api/inference/sandbox/_/upload/",
        "/v1/sandbox/thread-1/upload",
    ):
        assert pattern.match(path), path
        assert main_module._get_upload_passthrough_request_max_bytes(
            path
        ) == upload_request_limit_bytes(SANDBOX_UPLOAD_MAX_BYTES)
    for path in ("/api/inference/sandbox/thread-1/reveal", "/api/inference/sandbox/a/b/upload"):
        assert not pattern.match(path), path
    assert pattern.match("/api/rag/threads/t1/documents")


def test_pattern_passthrough_streams_and_refuses_oversized_declared_bodies(main_module):
    app = FastAPI()
    app.add_middleware(
        main_module.MaxBodyMiddleware,
        max_bytes_getter = lambda: 128,
        protected_prefixes = ("/api/",),
        upload_passthrough_max_bytes_getter = lambda path: 4096,
        upload_passthrough_pattern = re.compile(r"^/api/rag/threads/[^/]+/documents$"),
    )

    @app.post("/api/rag/threads/{tid}/documents")
    async def upload(request: Request):
        return {"total": len(await request.body())}

    @app.post("/api/skills")
    async def skills(payload: dict):
        return {"ok": True}

    c = TestClient(app)
    # Over the buffered default but under the upload cap: passes through.
    assert c.post("/api/rag/threads/t/documents", content = b"x" * 1024).json() == {"total": 1024}
    assert c.post("/api/rag/threads/t/documents", content = b"x" * 8192).status_code == 413

    def gen():
        for _ in range(4):
            yield b"y" * 64

    # A newly covered prefix: a chunked body past the cap is refused before the route runs.
    assert (
        c.post(
            "/api/skills", content = gen(), headers = {"content-type": "application/json"}
        ).status_code
        == 413
    )


def test_rag_zero_upload_cap_means_unlimited(main_module, monkeypatch):
    from core.rag import config as rag_config
    monkeypatch.setattr(rag_config, "MAX_UPLOAD_BYTES", 0)
    assert (
        main_module._get_upload_passthrough_request_max_bytes("/api/rag/threads/t1/documents")
        == sys.maxsize
    )


def test_delete_bodies_and_root_path_are_capped(main_module):
    app = FastAPI(root_path = "/studio")
    app.add_middleware(
        main_module.MaxBodyMiddleware,
        max_bytes_getter = lambda: 128,
        protected_prefixes = ("/api/",),
    )

    @app.api_route("/api/models/delete-cached", methods = ["DELETE", "POST"])
    async def delete_cached(request: Request):
        return {"total": len(await request.body())}

    c = TestClient(app, root_path = "/studio")

    def gen():
        for _ in range(4):
            yield b"y" * 64

    # DELETE with a body past the cap, and a plain DELETE without one.
    assert c.request("DELETE", "/studio/api/models/delete-cached", content = gen()).status_code == 413
    assert c.request("DELETE", "/studio/api/models/delete-cached").json() == {"total": 0}
    # The root_path prefix in scope["path"] does not hide the protected prefix.
    assert c.post("/studio/api/models/delete-cached", content = b"x" * 512).status_code == 413
    assert c.post("/studio/api/models/delete-cached", content = b"x" * 64).json() == {"total": 64}


def test_a_withheld_body_is_not_counted_as_running_inference(main_module, health_app):
    """A request counts only once its body is here, so a withheld body is not counted as running
    inference."""
    from auth import storage
    from auth.authentication import create_access_token
    from core.inference import llama_keepwarm, media_keepwarm

    async def counted_while_withheld(path: str, headers: dict) -> int:
        waiting, hang_up = asyncio.Event(), asyncio.Event()

        async def receive():
            waiting.set()
            await hang_up.wait()
            return {"type": "http.disconnect"}

        async def send(message):
            pass

        scope = {
            "type": "http",
            "asgi": {"version": "3.0"},
            "http_version": "1.1",
            "method": "POST",
            "scheme": "http",
            "path": path,
            "raw_path": path.encode(),
            "query_string": b"",
            "root_path": "",
            "headers": [(k.lower().encode(), v.encode()) for k, v in headers.items()],
            "client": ("203.0.113.7", 5555),
            "server": ("127.0.0.1", 8888),
        }
        request = asyncio.ensure_future(main_module.app(scope, receive, send))
        asked = asyncio.ensure_future(waiting.wait())
        # Either the body is being waited for, or the route refused the request before asking for one.
        await asyncio.wait({request, asked}, return_when = asyncio.FIRST_COMPLETED)
        counted = llama_keepwarm.other_inference_request_count(current_request_counted = False)
        counted += sum(
            media_keepwarm.other_request_count(owner)
            for owner in set(media_keepwarm._TRACKED_PATHS.values())
        )
        hang_up.set()
        asked.cancel()
        await request
        return counted

    tracked = {
        *(
            prefix.rstrip("/") + suffix
            for prefix in llama_keepwarm._INFERENCE_PREFIXES
            for suffix in llama_keepwarm._INFERENCE_SUFFIXES
        ),
        *llama_keepwarm._INFERENCE_EXACT_PATHS,
        *media_keepwarm._TRACKED_PATHS,
        "/p/run/v1/chat/completions",
        "/p/run/checkpoint-1/v1/chat/completions",
    }
    session = create_access_token(subject = storage.DEFAULT_ADMIN_USERNAME)
    for path in sorted(tracked):
        for framing in ({"Content-Length": "2"}, {"Transfer-Encoding": "chunked"}):
            for credential in ({}, {"Authorization": f"Bearer {session}"}):
                if (
                    credential
                    and "Content-Length" in framing
                    and path in main_module._VIDEO_MULTIPART_UPLOAD_PATHS
                ):
                    # The videos upload passes through on its Content-Length; its route reads the multipart body
                    # itself, after the auth dependencies, so only a credentialed client can hold it counted.
                    continue
                headers = {"Content-Type": "application/json", **framing, **credential}
                assert asyncio.run(counted_while_withheld(path, headers)) == 0, (path, headers)


def test_zstd_decodes_run_one_at_a_time(main_module, monkeypatch):
    # Decoding happens before the route's auth check, so concurrent unauthenticated bombs must not
    # each inflate to the full cap at once.
    import types
    import httpx

    fake = types.ModuleType("backports.zstd")

    class ZstdError(Exception):
        pass

    fake.ZstdError = ZstdError
    pkg = types.ModuleType("backports")
    pkg.zstd = fake
    monkeypatch.setitem(sys.modules, "backports", pkg)
    monkeypatch.setitem(sys.modules, "backports.zstd", fake)
    monkeypatch.setitem(sys.modules, "compression", None)
    state = {"now": 0, "peak": 0}

    import time

    def slow_decode(zstd, data, limit):
        state["now"] += 1
        state["peak"] = max(state["peak"], state["now"])
        time.sleep(0.2)
        state["now"] -= 1
        return json.dumps({"text": "x"}).encode()

    monkeypatch.setattr(main_module, "_zstd_decode", slow_decode)
    app = _make_protected_app(4096, main_module)

    async def run():
        transport = httpx.ASGITransport(app = app)
        async with httpx.AsyncClient(transport = transport, base_url = "http://t") as client:
            headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"}
            return await asyncio.gather(
                *(
                    client.post("/v1/systemone", content = b"z" * 64, headers = headers)
                    for _ in range(4)
                )
            )

    responses = asyncio.run(run())
    assert [r.status_code for r in responses] == [200] * 4
    assert state["peak"] == 1