-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: prefer bundled dashboard over stale data dist #8172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import os | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| # 将项目根目录添加到 sys.path | ||
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | ||
|
|
@@ -8,6 +9,7 @@ | |
|
|
||
| import pytest | ||
|
|
||
| from astrbot.core.utils.io import should_use_bundled_dashboard_dist | ||
| from main import check_dashboard_files, check_env | ||
|
|
||
|
|
||
|
|
@@ -175,8 +177,9 @@ async def test_check_dashboard_files_exists_but_version_mismatch(monkeypatch): | |
| """Tests that a warning is logged when dashboard version mismatches.""" | ||
| monkeypatch.setattr(os.path, "exists", lambda x: True) | ||
|
|
||
| with mock.patch("main.get_dashboard_version") as mock_get_version: | ||
| mock_get_version.return_value = "v0.0.1" # A different version | ||
| with mock.patch( | ||
| "main.get_dashboard_version", mock.AsyncMock(return_value="v0.0.1") | ||
| ): | ||
|
|
||
| with mock.patch("main.logger.warning") as mock_logger_warning: | ||
| await check_dashboard_files() | ||
|
|
@@ -185,6 +188,64 @@ async def test_check_dashboard_files_exists_but_version_mismatch(monkeypatch): | |
| assert "WebUI version mismatch" in call_args[0] | ||
|
|
||
|
|
||
| def test_should_use_bundled_dashboard_dist_when_data_dist_is_stale(tmp_path): | ||
| user_dist = tmp_path / "user-dist" | ||
| bundled_dist = tmp_path / "bundled-dist" | ||
| (user_dist / "assets").mkdir(parents=True) | ||
| (bundled_dist / "assets").mkdir(parents=True) | ||
| (user_dist / "assets" / "version").write_text("v4.24.2", encoding="utf-8") | ||
| (bundled_dist / "assets" / "version").write_text("v4.24.4", encoding="utf-8") | ||
|
|
||
| with mock.patch( | ||
| "astrbot.core.utils.io.get_bundled_dashboard_dist_path", | ||
| return_value=bundled_dist, | ||
| ): | ||
| assert should_use_bundled_dashboard_dist(user_dist, "v4.24.4") is True | ||
|
|
||
|
|
||
| def test_should_keep_data_dist_when_version_file_is_malformed(tmp_path): | ||
| user_dist = tmp_path / "user-dist" | ||
| bundled_dist = tmp_path / "bundled-dist" | ||
| (user_dist / "assets").mkdir(parents=True) | ||
| (bundled_dist / "assets").mkdir(parents=True) | ||
| (user_dist / "assets" / "version").write_text("not-a-version", encoding="utf-8") | ||
|
|
||
| with mock.patch( | ||
| "astrbot.core.utils.io.get_bundled_dashboard_dist_path", | ||
| return_value=bundled_dist, | ||
| ): | ||
| assert should_use_bundled_dashboard_dist(user_dist, "4.24.4") is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_dashboard_files_uses_bundled_dist_when_data_dist_is_stale( | ||
| tmp_path, | ||
| ): | ||
| """Tests that a stale data/dist does not override bundled dashboard assets.""" | ||
| data_dir = tmp_path / "data" | ||
| data_dist = data_dir / "dist" | ||
| bundled_dist = tmp_path / "bundled-dist" | ||
| data_dist.mkdir(parents=True) | ||
| bundled_dist.mkdir() | ||
|
Comment on lines
+220
to
+229
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (testing): Use an AsyncMock (or async stub) when patching Since |
||
|
|
||
| with mock.patch("main.get_astrbot_data_path", return_value=str(data_dir)): | ||
| with mock.patch( | ||
| "main.get_dashboard_version", mock.AsyncMock(return_value="v0.0.1") | ||
| ): | ||
| with mock.patch( | ||
| "main.should_use_bundled_dashboard_dist", return_value=True | ||
| ): | ||
| with mock.patch( | ||
| "main.get_bundled_dashboard_dist_path", | ||
| return_value=Path(bundled_dist), | ||
| ): | ||
| with mock.patch("main.download_dashboard") as mock_download: | ||
| result = await check_dashboard_files() | ||
|
|
||
| assert result == str(bundled_dist) | ||
| mock_download.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_dashboard_files_with_webui_dir_arg(monkeypatch): | ||
| """Tests that providing a valid webui_dir skips all checks.""" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic in get_dashboard_version can be simplified and made more efficient. The current implementation makes redundant calls to _read_dashboard_dist_version and get_bundled_dashboard_dist_path. Since should_use_bundled_dashboard_dist already performs the necessary checks, the return paths can be streamlined to avoid re-reading the same files.