|
| 1 | +"""Exercise the actual UI schema and save handler with an isolated session. |
| 2 | +
|
| 3 | +Empty welcome strings must overwrite old custom text; omitted fields must not. |
| 4 | +This does not exercise FastAPI authentication, file uploads or a real database. |
| 5 | +""" |
| 6 | + |
| 7 | +import ast |
| 8 | +import asyncio |
| 9 | +import json |
| 10 | +from pathlib import Path |
| 11 | +from types import SimpleNamespace |
| 12 | +from typing import Optional |
| 13 | +from unittest.mock import AsyncMock, Mock |
| 14 | + |
| 15 | +import pytest |
| 16 | +from pydantic import BaseModel, Field |
| 17 | + |
| 18 | +ROOT = Path(__file__).resolve().parents[1] |
| 19 | + |
| 20 | + |
| 21 | +def load_node(path, name, namespace): |
| 22 | + node = next( |
| 23 | + n for n in ast.parse(path.read_text()).body if getattr(n, "name", None) == name |
| 24 | + ) |
| 25 | + if isinstance(node, ast.AsyncFunctionDef): |
| 26 | + node.decorator_list = [] |
| 27 | + exec( |
| 28 | + compile(ast.Module(body=[node], type_ignores=[]), str(path), "exec"), namespace |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "fields,expected", |
| 34 | + [ |
| 35 | + ({"welcome": "", "welcome_desc": ""}, {"welcome": "", "welcome_desc": ""}), |
| 36 | + ( |
| 37 | + {"welcome": "New custom title"}, |
| 38 | + {"welcome": "New custom title", "welcome_desc": "Custom description"}, |
| 39 | + ), |
| 40 | + ( |
| 41 | + {"theme": "#123456"}, |
| 42 | + {"welcome": "Custom title", "welcome_desc": "Custom description"}, |
| 43 | + ), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_save_welcome_defaults(fields, expected): |
| 47 | + model = SimpleNamespace( |
| 48 | + id=42, |
| 49 | + configuration=json.dumps( |
| 50 | + { |
| 51 | + "welcome": "Custom title", |
| 52 | + "welcome_desc": "Custom description", |
| 53 | + "auto_ds": True, |
| 54 | + } |
| 55 | + ), |
| 56 | + ) |
| 57 | + namespace = { |
| 58 | + "BaseModel": BaseModel, |
| 59 | + "Field": Field, |
| 60 | + "Optional": Optional, |
| 61 | + "List": list, |
| 62 | + "json": json, |
| 63 | + "SessionDep": object, |
| 64 | + "CurrentUser": object, |
| 65 | + "Trans": object, |
| 66 | + "UploadFile": object, |
| 67 | + "Form": lambda: None, |
| 68 | + "get_ws_assistant": Mock(return_value=model), |
| 69 | + "clear_ui_cache": AsyncMock(), |
| 70 | + } |
| 71 | + load_node(ROOT / "common/core/schemas.py", "BaseCreatorDTO", namespace) |
| 72 | + load_node( |
| 73 | + ROOT / "apps/system/schemas/system_schema.py", "AssistantUiSchema", namespace |
| 74 | + ) |
| 75 | + load_node(ROOT / "apps/system/api/assistant.py", "ui", namespace) |
| 76 | + session, user, trans = Mock(), object(), object() |
| 77 | + result = asyncio.run( |
| 78 | + namespace["ui"]( |
| 79 | + session, user, trans, json.dumps({"id": 42, **fields}), files=[] |
| 80 | + ) |
| 81 | + ) |
| 82 | + stored = json.loads(result.configuration) |
| 83 | + assert {key: stored[key] for key in expected} == expected |
| 84 | + assert stored["auto_ds"] is True |
| 85 | + if "theme" in fields: |
| 86 | + assert stored["theme"] == fields["theme"] |
| 87 | + namespace["get_ws_assistant"].assert_called_once_with(session, 42, user, trans) |
| 88 | + session.add.assert_called_once_with(model) |
| 89 | + session.commit.assert_called_once() |
| 90 | + namespace["clear_ui_cache"].assert_awaited_once_with(42) |
0 commit comments