File size: 3,658 Bytes
0f9caed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Testy FastAPI vrstvy — health, admin auth, runtime settings API."""

from fastapi.testclient import TestClient


def client_for(app_module):
    return TestClient(app_module.app)


def test_health_no_auth(app_module):
    c = client_for(app_module)
    r = c.get("/health")
    assert r.status_code == 200
    data = r.json()
    assert data["status"] == "ok"
    assert data["engine"]["state"] == "stopped"
    assert data["settings_revision"] == 0
    assert "disk_free_gb" in data


def test_admin_requires_auth(app_module):
    c = client_for(app_module)
    assert c.get("/admin/settings").status_code == 401
    assert c.get("/admin/settings",
                 headers={"Authorization": "Bearer spatny"}).status_code == 401


def test_admin_get_settings(app_module, auth):
    c = client_for(app_module)
    r = c.get("/admin/settings", headers=auth)
    assert r.status_code == 200
    data = r.json()
    assert data["settings"]["model"] == "Qwen/Qwen3-Coder-Next-FP8"
    assert "model" in data["engine_fields"]
    assert "max_steps" not in data["engine_fields"]


def test_admin_post_instant_setting(app_module, auth):
    c = client_for(app_module)
    r = c.post("/admin/settings", json={"max_steps": 44, "temperature": 0.5},
               headers=auth)
    assert r.status_code == 200
    data = r.json()
    assert data["applied"] == {"max_steps": 44, "temperature": 0.5}
    assert data["engine_reload_needed"] is False
    assert data["engine_reload_started"] is False
    assert data["revision"] == 1
    # nastavení se opravdu projevilo
    assert app_module.SETTINGS.get().max_steps == 44


def test_admin_post_engine_setting_without_reload(app_module, auth):
    c = client_for(app_module)
    r = c.post("/admin/settings?reload=false",
               json={"model": "org/deferred"}, headers=auth)
    data = r.json()
    assert data["engine_reload_needed"] is True
    assert data["engine_reload_started"] is False
    assert app_module.ENGINE.status_dict()["state"] == "stopped"


def test_admin_post_validation_errors(app_module, auth):
    c = client_for(app_module)
    r = c.post("/admin/settings", json={"agent_mode": "nesmysl"}, headers=auth)
    data = r.json()
    assert data["applied"] == {}
    assert "agent_mode" in data["errors"]


def test_admin_presets(app_module, auth):
    c = client_for(app_module)
    r = c.get("/admin/presets", headers=auth)
    assert r.status_code == 200
    presets = r.json()["presets"]
    assert "qwen3-coder-30b" in presets
    assert presets["qwen3-235b-fp8"]["model"] == "Qwen/Qwen3-235B-A22B-Instruct-2507-FP8"


def test_v1_models(app_module, auth):
    c = client_for(app_module)
    assert c.get("/v1/models").status_code == 401
    r = c.get("/v1/models", headers=auth)
    ids = {m["id"] for m in r.json()["data"]}
    assert ids == {"code-agent", "code-agent-llm"}


def test_v1_passthrough_engine_down(app_module, auth):
    c = client_for(app_module)
    r = c.post("/v1/chat/completions", headers=auth,
               json={"model": "code-agent-llm",
                     "messages": [{"role": "user", "content": "ahoj"}]})
    assert r.status_code == 503


def test_agent_chat_needs_runner(app_module):
    out = list(app_module.agent_chat("test", []))
    assert any("runner_url" in chunk for chunk in out)


def test_history_normalization(app_module):
    # Gradio 6 content-bloky i prosté stringy
    text = app_module._history_text([{"type": "text", "text": "a"},
                                     {"type": "text", "text": "b"}])
    assert text == "a\nb"
    assert app_module._history_text("plain") == "plain"
    assert app_module._history_text(None) == ""