11import os
22import sys
3+ from pathlib import Path
34
45# 将项目根目录添加到 sys.path
56sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), ".." )))
89
910import pytest
1011
12+ from astrbot .core .utils .io import should_use_bundled_dashboard_dist
1113from main import check_dashboard_files , check_env
1214
1315
@@ -175,8 +177,9 @@ async def test_check_dashboard_files_exists_but_version_mismatch(monkeypatch):
175177 """Tests that a warning is logged when dashboard version mismatches."""
176178 monkeypatch .setattr (os .path , "exists" , lambda x : True )
177179
178- with mock .patch ("main.get_dashboard_version" ) as mock_get_version :
179- mock_get_version .return_value = "v0.0.1" # A different version
180+ with mock .patch (
181+ "main.get_dashboard_version" , mock .AsyncMock (return_value = "v0.0.1" )
182+ ):
180183
181184 with mock .patch ("main.logger.warning" ) as mock_logger_warning :
182185 await check_dashboard_files ()
@@ -185,6 +188,64 @@ async def test_check_dashboard_files_exists_but_version_mismatch(monkeypatch):
185188 assert "WebUI version mismatch" in call_args [0 ]
186189
187190
191+ def test_should_use_bundled_dashboard_dist_when_data_dist_is_stale (tmp_path ):
192+ user_dist = tmp_path / "user-dist"
193+ bundled_dist = tmp_path / "bundled-dist"
194+ (user_dist / "assets" ).mkdir (parents = True )
195+ (bundled_dist / "assets" ).mkdir (parents = True )
196+ (user_dist / "assets" / "version" ).write_text ("v4.24.2" , encoding = "utf-8" )
197+ (bundled_dist / "assets" / "version" ).write_text ("v4.24.4" , encoding = "utf-8" )
198+
199+ with mock .patch (
200+ "astrbot.core.utils.io.get_bundled_dashboard_dist_path" ,
201+ return_value = bundled_dist ,
202+ ):
203+ assert should_use_bundled_dashboard_dist (user_dist , "v4.24.4" ) is True
204+
205+
206+ def test_should_keep_data_dist_when_version_file_is_malformed (tmp_path ):
207+ user_dist = tmp_path / "user-dist"
208+ bundled_dist = tmp_path / "bundled-dist"
209+ (user_dist / "assets" ).mkdir (parents = True )
210+ (bundled_dist / "assets" ).mkdir (parents = True )
211+ (user_dist / "assets" / "version" ).write_text ("not-a-version" , encoding = "utf-8" )
212+
213+ with mock .patch (
214+ "astrbot.core.utils.io.get_bundled_dashboard_dist_path" ,
215+ return_value = bundled_dist ,
216+ ):
217+ assert should_use_bundled_dashboard_dist (user_dist , "4.24.4" ) is False
218+
219+
220+ @pytest .mark .asyncio
221+ async def test_check_dashboard_files_uses_bundled_dist_when_data_dist_is_stale (
222+ tmp_path ,
223+ ):
224+ """Tests that a stale data/dist does not override bundled dashboard assets."""
225+ data_dir = tmp_path / "data"
226+ data_dist = data_dir / "dist"
227+ bundled_dist = tmp_path / "bundled-dist"
228+ data_dist .mkdir (parents = True )
229+ bundled_dist .mkdir ()
230+
231+ with mock .patch ("main.get_astrbot_data_path" , return_value = str (data_dir )):
232+ with mock .patch (
233+ "main.get_dashboard_version" , mock .AsyncMock (return_value = "v0.0.1" )
234+ ):
235+ with mock .patch (
236+ "main.should_use_bundled_dashboard_dist" , return_value = True
237+ ):
238+ with mock .patch (
239+ "main.get_bundled_dashboard_dist_path" ,
240+ return_value = Path (bundled_dist ),
241+ ):
242+ with mock .patch ("main.download_dashboard" ) as mock_download :
243+ result = await check_dashboard_files ()
244+
245+ assert result == str (bundled_dist )
246+ mock_download .assert_not_called ()
247+
248+
188249@pytest .mark .asyncio
189250async def test_check_dashboard_files_with_webui_dir_arg (monkeypatch ):
190251 """Tests that providing a valid webui_dir skips all checks."""
0 commit comments