from __future__ import annotations 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" call_marker = "run_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") assert "run_startup_update_check_early(" in text assert target_import in text assert text.index("run_startup_update_check_early(") < 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_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