from __future__ import annotations import hashlib import json from pathlib import Path import pytest def test_compare_versions_handles_v_prefix_and_patch_order(): from douyin.updater.force_update import compare_versions assert compare_versions("V3.3.10", "3.3.9") > 0 assert compare_versions("3.3.3", "V3.3.3") == 0 assert compare_versions("3.3.2", "3.3.3") < 0 def test_load_settings_is_disabled_without_manifest_url(tmp_path, monkeypatch): from douyin.core import path_config from douyin.updater.force_update import load_update_settings cfg = tmp_path / "config.json" cfg.write_text(json.dumps({"auto_update": {"enabled": True}}), encoding="utf-8") monkeypatch.setattr(path_config, "get_config_path", lambda: str(cfg)) monkeypatch.delenv("DOUYIN_UPDATE_MANIFEST_URL", raising=False) settings = load_update_settings() assert settings.enabled is False assert settings.manifest_url == "" def test_forced_update_downloads_verifies_and_launches_installer(tmp_path): from douyin.updater.force_update import UpdateSettings, perform_forced_update payload = b"fake installer bytes" expected_sha = hashlib.sha256(payload).hexdigest() settings = UpdateSettings( enabled=True, manifest_url="https://example.test/update.json", download_dir=tmp_path, installer_args=("/VERYSILENT", "/CURRENTUSER"), ) downloaded: list[str] = [] launched: list[list[str]] = [] def fetch_manifest(url: str, timeout: float) -> dict: assert url == settings.manifest_url return { "version": "3.3.4", "url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe", "sha256": expected_sha, "force": True, } def download(url: str, dest: Path, timeout: float) -> None: downloaded.append(url) assert dest.parent.exists() dest.write_bytes(payload) def launch(cmd: list[str]) -> None: launched.append(cmd) result = perform_forced_update( settings, current_version="3.3.3", fetch_manifest=fetch_manifest, download_file=download, launch_installer=launch, ) assert result.status == "update_started" assert result.exit_required is True assert downloaded == ["https://example.test/DouyinGuaji-3.3.4-Setup.exe"] assert launched assert launched[0][:3] == [ str(tmp_path / "DouyinGuaji-3.3.4-Setup.exe"), "/VERYSILENT", "/CURRENTUSER", ] assert any(arg.startswith("/LOG=") for arg in launched[0]) def test_launch_installer_on_windows_waits_for_parent_before_setup(monkeypatch, tmp_path): from douyin.updater import force_update calls: list[tuple[list[str], dict]] = [] monkeypatch.setattr(force_update.sys, "platform", "win32") monkeypatch.setattr(force_update.os, "getpid", lambda: 24680) def fake_popen(cmd, **kwargs): calls.append((list(cmd), kwargs)) class Proc: pass return Proc() monkeypatch.setattr(force_update.subprocess, "Popen", fake_popen) installer = tmp_path / "DouyinGuaji Setup.exe" force_update.launch_installer([str(installer), "/VERYSILENT", "/LOG=C:\\tmp\\install.log"]) assert calls cmd, kwargs = calls[0] assert cmd[:4] == ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass"] assert "-WindowStyle" in cmd script = cmd[-1] assert "Wait-Process -Id 24680" in script assert "Start-Process" in script assert str(installer) in script assert kwargs["cwd"] == str(tmp_path) assert kwargs["close_fds"] is True assert kwargs["stdin"] is force_update.subprocess.DEVNULL def test_forced_update_rejects_hash_mismatch(tmp_path): from douyin.updater.force_update import ( UpdateError, UpdateSettings, perform_forced_update, ) settings = UpdateSettings( enabled=True, manifest_url="https://example.test/update.json", download_dir=tmp_path, ) def fetch_manifest(url: str, timeout: float) -> dict: return { "version": "3.3.4", "url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe", "sha256": "0" * 64, "force": True, } def download(url: str, dest: Path, timeout: float) -> None: dest.write_bytes(b"tampered") with pytest.raises(UpdateError, match="sha256"): perform_forced_update( settings, current_version="3.3.3", fetch_manifest=fetch_manifest, download_file=download, launch_installer=lambda cmd: None, ) def test_forced_update_rejects_insecure_manifest_url_by_default(tmp_path): from douyin.updater.force_update import ( UpdateError, UpdateSettings, perform_forced_update, ) called = False def fetch_manifest(url: str, timeout: float) -> dict: nonlocal called called = True return {} settings = UpdateSettings( enabled=True, manifest_url="http://example.test/update.json", download_dir=tmp_path, ) with pytest.raises(UpdateError, match="HTTPS"): perform_forced_update( settings, current_version="3.3.3", fetch_manifest=fetch_manifest, download_file=lambda url, dest, timeout: None, launch_installer=lambda cmd: None, ) assert called is False def test_forced_update_rejects_cross_origin_installer_url_by_default(tmp_path): from douyin.updater.force_update import ( UpdateError, UpdateSettings, perform_forced_update, ) payload = b"fake installer bytes" expected_sha = hashlib.sha256(payload).hexdigest() downloaded: list[str] = [] settings = UpdateSettings( enabled=True, manifest_url="https://updates.example.test/update.json", download_dir=tmp_path, ) def fetch_manifest(url: str, timeout: float) -> dict: return { "version": "3.3.4", "url": "https://evil.example.test/DouyinGuaji-3.3.4-Setup.exe", "sha256": expected_sha, } def download(url: str, dest: Path, timeout: float) -> None: downloaded.append(url) dest.write_bytes(payload) with pytest.raises(UpdateError, match="同源"): perform_forced_update( settings, current_version="3.3.3", fetch_manifest=fetch_manifest, download_file=download, launch_installer=lambda cmd: None, ) assert downloaded == [] def test_forced_update_ignores_manifest_force_false_for_newer_version(tmp_path): from douyin.updater.force_update import UpdateSettings, perform_forced_update payload = b"forced installer" expected_sha = hashlib.sha256(payload).hexdigest() launched: list[list[str]] = [] def fetch_manifest(url: str, timeout: float) -> dict: return { "version": "3.3.4", "url": "https://example.test/DouyinGuaji-3.3.4-Setup.exe", "sha256": expected_sha, "force": False, } def download(url: str, dest: Path, timeout: float) -> None: dest.write_bytes(payload) result = perform_forced_update( UpdateSettings(enabled=True, manifest_url="https://example.test/update.json", download_dir=tmp_path), current_version="3.3.3", fetch_manifest=fetch_manifest, download_file=download, launch_installer=lambda cmd: launched.append(cmd), ) assert result.status == "update_started" assert launched def test_startup_update_raises_system_exit_after_launch(tmp_path): from douyin.updater.force_update import UpdateResult, run_startup_update_check called: list[str] = [] def perform(*, settings, current_version): called.append(current_version) return UpdateResult("update_started", "started", exit_required=True) with pytest.raises(SystemExit) as exc: run_startup_update_check( app_name="test_app", current_version="3.3.3", perform_update=perform, ) assert exc.value.code == 0 assert called == ["3.3.3"] def test_startup_update_fail_closed_blocks_old_client(tmp_path, monkeypatch): from douyin.updater.force_update import UpdateError, run_startup_update_check monkeypatch.setenv("DOUYIN_AUTO_UPDATE_ENABLED", "1") monkeypatch.setenv("DOUYIN_UPDATE_MANIFEST_URL", "https://example.test/update.json") def perform(*, settings, current_version): raise UpdateError("network down") with pytest.raises(SystemExit) as exc: run_startup_update_check( app_name="test_app", current_version="3.3.3", perform_update=perform, ) assert exc.value.code == 3