合并外部维护版本并同步自动每日功能
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import asyncio
|
||||
import ctypes
|
||||
import logging
|
||||
import tempfile
|
||||
import unittest
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
@@ -319,5 +321,105 @@ class ManualLiveOverrideTests(unittest.TestCase):
|
||||
self.assertTrue(handler._is_live_time())
|
||||
|
||||
|
||||
class StartupTaskSelfHealTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.logger = logging.getLogger("test-startup-self-heal")
|
||||
self.scheduler = SystemScheduler(
|
||||
_FakeConfig({
|
||||
"enable_startup_shortcut": True,
|
||||
"startup_bat": "run.bat",
|
||||
}),
|
||||
self.logger,
|
||||
)
|
||||
|
||||
@patch("app.danmu_queue.os.name", "nt")
|
||||
@patch("app.danmu_queue.project_path")
|
||||
def test_rebuilds_when_task_missing(self, project_path_mock):
|
||||
with tempfile.TemporaryDirectory() as root:
|
||||
root_path = Path(root)
|
||||
(root_path / "run.bat").touch()
|
||||
(root_path / ".venv-tts" / "Scripts").mkdir(parents=True)
|
||||
(root_path / ".venv-tts" / "Scripts" / "pythonw.exe").touch()
|
||||
(root_path / "app").mkdir()
|
||||
(root_path / "app" / "main.py").touch()
|
||||
project_path_mock.side_effect = lambda relative: root_path / relative
|
||||
self.scheduler._startup_task_exists = MagicMock(return_value=False)
|
||||
self.scheduler._create_startup_task = MagicMock(return_value=True)
|
||||
|
||||
self.scheduler.ensure_startup_task_self_heal()
|
||||
|
||||
self.scheduler._create_startup_task.assert_called_once()
|
||||
args = self.scheduler._create_startup_task.call_args.args
|
||||
self.assertEqual(args[0], "BetterGI弹幕排队系统")
|
||||
# 无窗口模式:pythonw 直接运行 main.py,不再弹 cmd 窗口
|
||||
self.assertIn(str(root_path / "app" / "main.py"), args[1])
|
||||
self.assertIn("--role all", args[1])
|
||||
self.assertIn(f"--port {self.scheduler.port}", args[1])
|
||||
|
||||
@patch("app.danmu_queue.os.name", "nt")
|
||||
@patch("app.danmu_queue.project_path")
|
||||
def test_skips_when_task_present(self, project_path_mock):
|
||||
project_path_mock.return_value = MagicMock(
|
||||
exists=MagicMock(return_value=True),
|
||||
__str__=lambda _s: r"C:\proj\run.bat",
|
||||
)
|
||||
self.scheduler._startup_task_exists = MagicMock(return_value=True)
|
||||
self.scheduler._create_startup_task = MagicMock()
|
||||
|
||||
self.scheduler.ensure_startup_task_self_heal()
|
||||
|
||||
self.scheduler._create_startup_task.assert_not_called()
|
||||
|
||||
@patch("app.danmu_queue.os.name", "nt")
|
||||
def test_skips_when_disabled(self):
|
||||
self.scheduler.config.system_cfg["enable_startup_shortcut"] = False
|
||||
self.scheduler._startup_task_exists = MagicMock()
|
||||
self.scheduler._create_startup_task = MagicMock()
|
||||
|
||||
self.scheduler.ensure_startup_task_self_heal()
|
||||
|
||||
self.scheduler._startup_task_exists.assert_not_called()
|
||||
self.scheduler._create_startup_task.assert_not_called()
|
||||
|
||||
|
||||
class ScriptGroupSyncTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.logger = logging.getLogger("test-scriptgroup-sync")
|
||||
self.scheduler = SystemScheduler(
|
||||
_FakeConfig({}),
|
||||
self.logger,
|
||||
)
|
||||
|
||||
def test_copies_and_overwrites_files(self):
|
||||
with tempfile.TemporaryDirectory() as src_dir, tempfile.TemporaryDirectory() as bgi_dir:
|
||||
src = Path(src_dir)
|
||||
(src / "薄荷.json").write_text('{"v": 2}', encoding="utf-8")
|
||||
(src / "晶蝶.json").write_text("{}", encoding="utf-8")
|
||||
dst = Path(bgi_dir) / "User" / "ScriptGroup"
|
||||
dst.mkdir(parents=True)
|
||||
(dst / "薄荷.json").write_text('{"v": 1, "corrupt": true}', encoding="utf-8")
|
||||
(dst / "本地多余.json").write_text("{}", encoding="utf-8")
|
||||
self.scheduler.config.bettergi_work_dir = bgi_dir
|
||||
self.scheduler.config.system_cfg["scriptgroup_backup_dir"] = src_dir
|
||||
|
||||
self.scheduler._sync_script_groups()
|
||||
|
||||
self.assertEqual(
|
||||
(dst / "薄荷.json").read_text(encoding="utf-8"), '{"v": 2}'
|
||||
)
|
||||
self.assertTrue((dst / "晶蝶.json").exists())
|
||||
# 目标目录里多出的文件不删除
|
||||
self.assertTrue((dst / "本地多余.json").exists())
|
||||
|
||||
def test_missing_source_dir_is_skipped(self):
|
||||
with tempfile.TemporaryDirectory() as bgi_dir:
|
||||
self.scheduler.config.bettergi_work_dir = bgi_dir
|
||||
self.scheduler.config.system_cfg["scriptgroup_backup_dir"] = (
|
||||
str(Path(bgi_dir) / "不存在")
|
||||
)
|
||||
# 不抛异常即通过
|
||||
self.scheduler._sync_script_groups()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user