@@ -12,11 +12,20 @@ def _prepare_fake_repo(tmp_path, monkeypatch):
1212 return repo_root
1313
1414
15+ def _create_full_static (repo_root ):
16+ """Create static/index.html + static/assets/*.js/.css (complete build)."""
17+ static_dir = repo_root / "static"
18+ assets_dir = static_dir / "assets"
19+ assets_dir .mkdir (parents = True )
20+ (static_dir / "index.html" ).write_text ("<!doctype html>" , encoding = "utf-8" )
21+ (assets_dir / "index-abc123.js" ).write_text ("/* js */" , encoding = "utf-8" )
22+ (assets_dir / "index-abc123.css" ).write_text ("/* css */" , encoding = "utf-8" )
23+ return static_dir
24+
25+
1526def test_prepare_webui_frontend_assets_reuses_prebuilt_static_without_source (tmp_path , monkeypatch , caplog ):
1627 repo_root = _prepare_fake_repo (tmp_path , monkeypatch )
17- static_index = repo_root / "static" / "index.html"
18- static_index .parent .mkdir (parents = True )
19- static_index .write_text ("<!doctype html>" , encoding = "utf-8" )
28+ _create_full_static (repo_root )
2029
2130 monkeypatch .delenv ("WEBUI_AUTO_BUILD" , raising = False )
2231 monkeypatch .delenv ("WEBUI_FORCE_BUILD" , raising = False )
@@ -28,6 +37,7 @@ def test_prepare_webui_frontend_assets_reuses_prebuilt_static_without_source(tmp
2837 assert "检测到可直接复用的前端静态产物" in caplog .text
2938 assert "未找到前端项目,无法自动构建" not in caplog .text
3039 assert "未检测到 npm,无法自动构建前端" not in caplog .text
40+ assert "assets/ 目录不存在或无 CSS/JS 文件" not in caplog .text
3141
3242
3343def test_prepare_webui_frontend_assets_fails_without_static_or_source (tmp_path , monkeypatch , caplog ):
@@ -40,3 +50,64 @@ def test_prepare_webui_frontend_assets_fails_without_static_or_source(tmp_path,
4050 assert webui_frontend .prepare_webui_frontend_assets () is False
4151
4252 assert "未找到前端项目,无法自动构建" in caplog .text
53+
54+
55+ def test_prepare_webui_frontend_assets_warns_when_assets_missing (tmp_path , monkeypatch , caplog ):
56+ """index.html 存在但 static/assets/ 缺失时应发出 WebUI 显示异常警告(Issue #944)。"""
57+ repo_root = _prepare_fake_repo (tmp_path , monkeypatch )
58+ static_index = repo_root / "static" / "index.html"
59+ static_index .parent .mkdir (parents = True )
60+ static_index .write_text ("<!doctype html>" , encoding = "utf-8" )
61+ # No assets directory created — simulates incomplete/broken build
62+
63+ monkeypatch .delenv ("WEBUI_AUTO_BUILD" , raising = False )
64+ monkeypatch .delenv ("WEBUI_FORCE_BUILD" , raising = False )
65+ monkeypatch .setattr (webui_frontend .shutil , "which" , lambda _ : None )
66+
67+ with caplog .at_level (logging .WARNING ):
68+ result = webui_frontend .prepare_webui_frontend_assets ()
69+
70+ assert result is True # function still returns True (index.html present)
71+ assert "assets/ 目录不存在或无 CSS/JS 文件" in caplog .text
72+ assert "WebUI 将因缺少样式与脚本而显示异常" in caplog .text
73+
74+
75+ def test_prepare_webui_frontend_assets_auto_build_disabled_warns_when_assets_missing (tmp_path , monkeypatch , caplog ):
76+ """WEBUI_AUTO_BUILD=false 且 assets 缺失时也应发出警告。"""
77+ repo_root = _prepare_fake_repo (tmp_path , monkeypatch )
78+ static_index = repo_root / "static" / "index.html"
79+ static_index .parent .mkdir (parents = True )
80+ static_index .write_text ("<!doctype html>" , encoding = "utf-8" )
81+ # No assets directory — simulates state where only index.html exists
82+
83+ monkeypatch .setenv ("WEBUI_AUTO_BUILD" , "false" )
84+ monkeypatch .delenv ("WEBUI_FORCE_BUILD" , raising = False )
85+
86+ with caplog .at_level (logging .WARNING ):
87+ result = webui_frontend .prepare_webui_frontend_assets ()
88+
89+ assert result is True # index.html present, still returns True
90+ assert "assets/ 目录不存在或无 CSS/JS 文件" in caplog .text
91+
92+
93+ def test_has_static_assets_returns_false_for_missing_dir (tmp_path ):
94+ assert webui_frontend ._has_static_assets (tmp_path / "nonexistent" ) is False
95+
96+
97+ def test_has_static_assets_returns_false_for_empty_assets (tmp_path ):
98+ (tmp_path / "assets" ).mkdir ()
99+ assert webui_frontend ._has_static_assets (tmp_path ) is False
100+
101+
102+ def test_has_static_assets_returns_true_when_js_present (tmp_path ):
103+ assets = tmp_path / "assets"
104+ assets .mkdir ()
105+ (assets / "main.js" ).write_text ("" , encoding = "utf-8" )
106+ assert webui_frontend ._has_static_assets (tmp_path ) is True
107+
108+
109+ def test_has_static_assets_returns_true_when_css_present (tmp_path ):
110+ assets = tmp_path / "assets"
111+ assets .mkdir ()
112+ (assets / "style.css" ).write_text ("" , encoding = "utf-8" )
113+ assert webui_frontend ._has_static_assets (tmp_path ) is True
0 commit comments