Skip to content

Commit 30cc8a3

Browse files
Add WebUI env preflight tests
1 parent d05851b commit 30cc8a3

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

‎tests/test_webui_env_preflight.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import pytest
2+
import yaml
3+
4+
pytest.importorskip("gradio")
5+
6+
from skillopt_webui import app as webui_app
7+
8+
9+
def _write_config(tmp_path, model):
10+
config_path = tmp_path / "config.yaml"
11+
config_path.write_text(
12+
yaml.safe_dump({
13+
"model": model,
14+
"env": {"name": "searchqa"},
15+
}),
16+
encoding="utf-8",
17+
)
18+
return str(config_path)
19+
20+
21+
def test_build_training_env_loads_project_dotenv(tmp_path, monkeypatch):
22+
monkeypatch.setattr(webui_app, "PROJECT_ROOT", tmp_path)
23+
(tmp_path / ".env").write_text(
24+
"\n".join([
25+
"export QWEN_CHAT_BASE_URL=http://qwen.example/v1",
26+
"QWEN_CHAT_MODEL=test-model",
27+
"QWEN_CHAT_API_KEY='secret-value'",
28+
]),
29+
encoding="utf-8",
30+
)
31+
32+
env = webui_app.build_training_env()
33+
34+
assert env["QWEN_CHAT_BASE_URL"] == "http://qwen.example/v1"
35+
assert env["QWEN_CHAT_MODEL"] == "test-model"
36+
assert env["QWEN_CHAT_API_KEY"] == "secret-value"
37+
38+
39+
def test_preflight_reports_missing_openai_chat_endpoint(tmp_path, monkeypatch):
40+
monkeypatch.delenv("AZURE_OPENAI_ENDPOINT", raising=False)
41+
monkeypatch.delenv("OPTIMIZER_AZURE_OPENAI_ENDPOINT", raising=False)
42+
monkeypatch.delenv("TARGET_AZURE_OPENAI_ENDPOINT", raising=False)
43+
config_path = _write_config(
44+
tmp_path,
45+
{
46+
"backend": "qwen",
47+
"optimizer_backend": "openai_chat",
48+
"target_backend": "openai_chat",
49+
},
50+
)
51+
52+
error = webui_app.validate_training_config(config_path, {})
53+
54+
assert "missing Azure/OpenAI-compatible endpoint for optimizer, target" in error
55+
assert "model.backend is qwen" in error
56+
57+
58+
def test_preflight_reports_unreachable_qwen_endpoint(tmp_path, monkeypatch):
59+
monkeypatch.setattr(webui_app, "_can_connect_to_url", lambda _url: False)
60+
config_path = _write_config(
61+
tmp_path,
62+
{
63+
"backend": "qwen",
64+
"optimizer_backend": "qwen_chat",
65+
"target_backend": "qwen_chat",
66+
"qwen_chat_base_url": "http://127.0.0.1:9/v1",
67+
},
68+
)
69+
70+
error = webui_app.validate_training_config(config_path, {})
71+
72+
assert "cannot connect to qwen_chat endpoint" in error
73+
assert "127.0.0.1:9" in error
74+
75+
76+
def test_preflight_accepts_reachable_qwen_endpoint(tmp_path, monkeypatch):
77+
seen_urls = []
78+
monkeypatch.setattr(webui_app, "_can_connect_to_url", lambda url: seen_urls.append(url) or True)
79+
config_path = _write_config(
80+
tmp_path,
81+
{
82+
"optimizer_backend": "qwen_chat",
83+
"target_backend": "qwen_chat",
84+
"qwen_chat_base_url": "http://qwen.example/v1",
85+
},
86+
)
87+
88+
assert webui_app.validate_training_config(config_path, {}) is None
89+
assert seen_urls == ["http://qwen.example/v1", "http://qwen.example/v1"]

0 commit comments

Comments
 (0)