Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

4.0 KB
Raw Blame
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations

from typing import TYPE_CHECKING

from tests._server.conftest import (
    get_user_config_manager,
)
from tests._server.mocks import get_session_manager, token_header, with_session

if TYPE_CHECKING:
    import pytest
    from starlette.testclient import TestClient


SESSION_ID = "session-123"
HEADERS = {
    "Marimo-Session-Id": SESSION_ID,
    **token_header("fake-token"),
}


def test_save_user_config_no_session(client: TestClient) -> None:
    user_config_manager = get_user_config_manager(client)
    response = client.post(
        "/api/kernel/save_user_config",
        headers=HEADERS,
        json={
            "config": user_config_manager.get_config(),
        },
    )
    assert response.status_code == 200, response.text
    assert response.headers["content-type"] == "application/json"
    assert "success" in response.json()


@with_session(SESSION_ID)
def test_save_user_config_with_session(client: TestClient) -> None:
    user_config_manager = get_user_config_manager(client)
    response = client.post(
        "/api/kernel/save_user_config",
        headers=HEADERS,
        json={
            "config": user_config_manager.get_config(),
        },
    )
    assert response.status_code == 200, response.text
    assert response.headers["content-type"] == "application/json"
    assert "success" in response.json()


@with_session(SESSION_ID)
def test_save_user_config_with_partial_config(client: TestClient) -> None:
    """
    Test that save_user_config endpoint works correctly with partial config

    This is a regression test for the KeyError: 'output_max_bytes' bug.
    Before the fix, sending partial config would cause runtime errors when
    the kernel tried to access missing config fields.
    """
    # Create a partial config that's missing required runtime fields
    partial_config = {
        "display": {
            "theme": "light",
        },
    }

    response = client.post(
        "/api/kernel/save_user_config",
        headers=HEADERS,
        json={
            "config": partial_config,
        },
    )

    # Verify the endpoint succeeds
    assert response.status_code == 200, response.text
    assert response.headers["content-type"] == "application/json"
    assert "success" in response.json()


@with_session(SESSION_ID)
def test_save_user_config_denied_capability_does_not_write(
    client: TestClient,
) -> None:
    """A capability-denied request must not touch the config on disk.

    Regression: enforcement used to run after save_config, so a read-only
    connection could persist the config before receiving its 403.
    """
    from unittest.mock import patch

    from marimo._utils.http import HTTPException

    config_manager = get_user_config_manager(client)

    with (
        patch(
            "marimo._server.api.endpoints.config.enforce_consumer_capability",
            side_effect=HTTPException(status_code=403, detail="read-only"),
        ),
        patch.object(config_manager, "save_config") as save_config,
    ):
        response = client.post(
            "/api/kernel/save_user_config",
            headers=HEADERS,
            json={"config": {"display": {"theme": "dark"}}},
        )

    assert response.status_code == 403, response.text
    save_config.assert_not_called()


def test_save_user_config_starts_lsp_when_ty_enabled(
    client: TestClient, monkeypatch: pytest.MonkeyPatch
) -> None:
    called = False

    async def mock_start_lsp_server() -> None:
        nonlocal called
        called = True

    session_manager = get_session_manager(client)
    monkeypatch.setattr(
        session_manager, "start_lsp_server", mock_start_lsp_server
    )

    response = client.post(
        "/api/kernel/save_user_config",
        headers=HEADERS,
        json={
            "config": {
                "completion": {"copilot": False},
                "language_servers": {"ty": {"enabled": True}},
            },
        },
    )

    assert response.status_code == 200, response.text
    assert called is True