feat: add startup forced update check

This commit is contained in:
chengma
2026-07-07 17:58:28 +08:00
parent 610a2304cc
commit 95b180e9cf
9 changed files with 527 additions and 12 deletions
+92 -1
View File
@@ -12,7 +12,7 @@ sys.path.insert(0, os.path.dirname(__file__))
from _helpers import TempDirMixin
from app import gui
from app import accounts, ai, appconfig, db, prompts
from app import accounts, ai, appconfig, db, prompts, update_check
if gui.QT_IMPORT_ERROR is not None:
raise unittest.SkipTest("PySide6 未安装")
@@ -203,6 +203,97 @@ class GuiTests(TempDirMixin, unittest.TestCase):
self.assert_removed(temp_dir)
def test_startup_update_gate_forced_blocks_and_opens_download(self):
boxes = []
class FakeButton:
def __init__(self, label):
self.label = label
self.enabled = True
def setEnabled(self, enabled):
self.enabled = enabled
class FakeMessageBox:
Warning = object()
AcceptRole = object()
RejectRole = object()
def __init__(self, parent=None):
self.parent = parent
self.icon = None
self.title = ""
self.text = ""
self.informative_text = ""
self.buttons = {}
self.default_button = None
boxes.append(self)
def setIcon(self, icon):
self.icon = icon
def setWindowTitle(self, title):
self.title = title
def setText(self, text):
self.text = text
def setInformativeText(self, text):
self.informative_text = text
def addButton(self, label, role):
button = FakeButton(label)
self.buttons[label] = button
return button
def setDefaultButton(self, button):
self.default_button = button
def exec(self):
return 0
def clickedButton(self):
return self.buttons["下载新版"]
result = update_check.UpdateCheckResult(
current_version="1.0.0",
checked=True,
forced=True,
latest_version="1.2.0",
min_supported_version="1.1.0",
download_url="https://example.test/cmshopee.zip",
message="必须升级",
)
opened = []
with mock.patch("app.gui.QMessageBox", FakeMessageBox):
allowed = gui._run_startup_update_gate(
checker=lambda: result,
opener=opened.append,
)
self.assertFalse(allowed)
self.assertEqual(["https://example.test/cmshopee.zip"], opened)
self.assertEqual("必须升级", boxes[0].title)
self.assertIn("当前版本:1.0.0", boxes[0].informative_text)
self.assertIn("线上版本:1.2.0", boxes[0].informative_text)
self.assertIn("保留 data/ 目录", boxes[0].informative_text)
self.assertEqual(boxes[0].buttons["下载新版"], boxes[0].default_button)
def test_startup_update_gate_check_failure_allows_entry_and_logs(self):
result = update_check.UpdateCheckResult(
current_version="1.0.0",
checked=True,
forced=False,
error="启动版本检查失败,已允许继续使用:网络超时",
)
with mock.patch("app.gui.diagnostics.write_diagnostic_log") as write_log:
allowed = gui._run_startup_update_gate(checker=lambda: result)
self.assertTrue(allowed)
write_log.assert_called_once()
def test_status_callbacks_classify_success_warning_and_failure(self):
with self.make_temp_dir() as temp_dir:
statuses = []