Files
douyi_guaji_main/tests/test_startup_update_bootstrap.py
T
Your Name 329b7756f4
Windows Package CI / package-fresh-install (push) Waiting to run
fix(gui): 增强 WiFi ADB 发现和更新进度
2026-06-29 00:15:13 +08:00

200 lines
7.4 KiB
Python

from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
def _assert_bootstrap_before(path: str, *late_markers: str) -> None:
text = Path(path).read_text(encoding="utf-8")
import_marker = (
"from douyin.updater.bootstrap import run_startup_update_check_early"
if "run_startup_update_check_early(" in text
else "from gui.components.update_progress import run_gui_startup_update_check_early"
)
call_marker = "run_startup_update_check_early(" if "run_startup_update_check_early(" in text else "run_gui_startup_update_check_early("
assert import_marker in text
assert call_marker in text
call_index = text.index(call_marker)
for marker in late_markers:
assert marker in text
assert call_index < text.index(marker)
def _assert_packaging_uses_wrapper(wrapper_path: str, target_script: str) -> None:
script = Path("packaging/pyinstaller/build_windows.ps1").read_text(encoding="utf-8")
assert f'Join-Path $BuildSourceRoot "{wrapper_path}"' in script
assert f'Join-Path $BuildSourceRoot "{target_script}"' not in script
def _assert_wrapper_imports_target_after_bootstrap(path: str, target_import: str) -> None:
text = Path(path).read_text(encoding="utf-8")
call_marker = "run_startup_update_check_early(" if "run_startup_update_check_early(" in text else "run_gui_startup_update_check_early("
assert call_marker in text
assert target_import in text
assert text.index(call_marker) < text.index(target_import)
def _assert_source_entry_bootstraps_under_main_guard(path: str, late_marker: str) -> None:
text = Path(path).read_text(encoding="utf-8")
guard_marker = 'if __name__ == "__main__":'
call_marker = "run_startup_update_check_early("
assert guard_marker in text
assert call_marker in text
assert late_marker in text
assert text.index(guard_marker) < text.index(call_marker) < text.index(late_marker)
def test_run_cli_runs_update_bootstrap_before_heavy_imports():
_assert_bootstrap_before(
"bin/run_cli_entry.py",
"from bin.run_cli import main",
)
_assert_wrapper_imports_target_after_bootstrap("bin/run_cli_entry.py", "from bin.run_cli import main")
_assert_source_entry_bootstraps_under_main_guard(
"bin/run_cli.py",
"from douyin.core.douyin_fudai import fudai_analyse",
)
_assert_packaging_uses_wrapper("bin\\run_cli_entry.py", "bin\\run_cli.py")
def test_gui_app_runs_update_bootstrap_before_gui_imports():
_assert_bootstrap_before(
"gui/gui_app_entry.py",
"from gui.gui_app import main",
)
_assert_wrapper_imports_target_after_bootstrap("gui/gui_app_entry.py", "from gui.gui_app import main")
_assert_source_entry_bootstraps_under_main_guard("gui/gui_app.py", "from gui.gui_core import")
def test_dashboard_gui_runs_update_bootstrap_before_gui_imports():
_assert_bootstrap_before(
"gui/gui_dashboard_app_entry.py",
"from gui.dashboard_app.app import main",
)
_assert_wrapper_imports_target_after_bootstrap(
"gui/gui_dashboard_app_entry.py",
"from gui.dashboard_app.app import main",
)
_assert_packaging_uses_wrapper("gui\\gui_dashboard_app_entry.py", "gui\\dashboard_app\\app.py")
def test_dashboard_gui_main_forces_license_required(monkeypatch):
from gui.dashboard_app import app as dashboard_app
calls = {
"app_started": False,
"monitor_started": False,
"required_at_check": "",
"initial_page": "",
"license_locked": None,
}
class FakeDashboardApp:
def __init__(self, *, initial_page="数据看板", license_locked=False) -> None:
calls["app_started"] = True
calls["initial_page"] = initial_page
calls["license_locked"] = license_locked
def _on_runner_line(self, line: str) -> None:
pass
def _start_license_monitor_once(self) -> None:
calls["monitor_started"] = True
def run(self) -> None:
calls["run_called"] = True
class MissingStatus:
ok = False
def fake_require_online_license_or_exit() -> None:
calls["required_at_check"] = os.environ.get("DOUYIN_LICENSE_REQUIRED", "")
monkeypatch.delenv("DOUYIN_LICENSE_REQUIRED", raising=False)
monkeypatch.setattr(dashboard_app, "check_license", lambda: MissingStatus())
monkeypatch.setattr(dashboard_app, "DashboardApp", FakeDashboardApp)
monkeypatch.setattr(dashboard_app, "require_online_license_or_exit", fake_require_online_license_or_exit)
dashboard_app.main()
assert os.environ.get("DOUYIN_LICENSE_REQUIRED") == "1"
assert calls["required_at_check"] == ""
assert calls["app_started"] is True
assert calls["initial_page"] == "授权"
assert calls["license_locked"] is True
assert calls["monitor_started"] is False
assert calls["run_called"] is True
def test_dashboard_gui_main_checks_online_license_when_local_license_exists(monkeypatch):
from gui.dashboard_app import app as dashboard_app
calls = {"required_at_check": "", "monitor_started": False, "initial_page": ""}
class FakeDashboardApp:
def __init__(self, *, initial_page="数据看板", license_locked=False) -> None:
calls["initial_page"] = initial_page
calls["license_locked"] = license_locked
def _start_license_monitor_once(self) -> None:
calls["monitor_started"] = True
def run(self) -> None:
calls["run_called"] = True
class OkStatus:
ok = True
def fake_require_online_license_or_exit() -> None:
calls["required_at_check"] = os.environ.get("DOUYIN_LICENSE_REQUIRED", "")
monkeypatch.delenv("DOUYIN_LICENSE_REQUIRED", raising=False)
monkeypatch.setattr(dashboard_app, "check_license", lambda: OkStatus())
monkeypatch.setattr(dashboard_app, "DashboardApp", FakeDashboardApp)
monkeypatch.setattr(dashboard_app, "require_online_license_or_exit", fake_require_online_license_or_exit)
dashboard_app.main()
assert calls["required_at_check"] == "1"
assert calls["initial_page"] == "数据看板"
assert calls["license_locked"] is False
assert calls["monitor_started"] is True
assert calls["run_called"] is True
def test_web_gui_runs_update_bootstrap_before_gui_imports():
_assert_bootstrap_before(
"gui/web_gui_entry.py",
"from gui.web_gui import main",
)
_assert_wrapper_imports_target_after_bootstrap("gui/web_gui_entry.py", "from gui.web_gui import main")
_assert_source_entry_bootstraps_under_main_guard("gui/web_gui.py", "from douyin.core import path_config")
_assert_packaging_uses_wrapper("gui\\web_gui_entry.py", "gui\\web_gui.py")
def test_run_cli_import_does_not_trigger_startup_update(monkeypatch):
monkeypatch.setenv("DOUYIN_AUTO_UPDATE_ENABLED", "1")
monkeypatch.setenv("DOUYIN_UPDATE_MANIFEST_URL", "http://example.test/update.json")
proc = subprocess.run(
[sys.executable, "-c", "import bin.run_cli; print('import-ok')"],
capture_output=True,
text=True,
timeout=20,
)
assert proc.returncode == 0
assert "import-ok" in proc.stdout
def test_paddlex_model_source_check_disabled_before_runtime_imports():
for path in (
"bin/run_cli.py",
"operations/ocr_operations.py",
"douyin/core/underlying_operations.py",
):
text = Path(path).read_text(encoding="utf-8")
assert "PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK" in text