Files
douyi_guaji_main/tests/test_auto_update.py
T
2026-06-18 07:57:38 +08:00

181 lines
5.6 KiB
Python

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 == [
[str(tmp_path / "DouyinGuaji-3.3.4-Setup.exe"), "/VERYSILENT", "/CURRENTUSER"]
]
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_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