130 lines
5.3 KiB
Python
130 lines
5.3 KiB
Python
import json
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
|
|
def test_settings_controller_persists_only_non_secret_values(tmp_path: Path):
|
|
from dx_productivity.ui.settings_controller import SettingsController
|
|
|
|
controller = SettingsController(settings_path=tmp_path / "settings.json")
|
|
controller.save_settings("https://example.invalid/manifest.json", "stable", True)
|
|
|
|
data = json.loads((tmp_path / "settings.json").read_text(encoding="utf-8"))
|
|
|
|
assert data == {
|
|
"source": "https://example.invalid/manifest.json",
|
|
"channel": "stable",
|
|
"auto_check": True,
|
|
}
|
|
|
|
|
|
def test_settings_controller_auto_check_is_due_after_24_hours(tmp_path: Path):
|
|
from dx_productivity.ui.settings_controller import SettingsController
|
|
|
|
controller = SettingsController(settings_path=tmp_path / "settings.json")
|
|
settings = {"source": "file:///manifest.json", "channel": "development", "auto_check": True}
|
|
now = datetime(2026, 9, 11, tzinfo=timezone.utc)
|
|
|
|
assert controller.auto_check_due(settings, now=now)
|
|
controller.save_settings("file:///manifest.json", "development", True)
|
|
controller.record_check(now=now)
|
|
settings = controller.load_settings()
|
|
assert not controller.auto_check_due(settings, now=now + timedelta(hours=23))
|
|
assert controller.auto_check_due(settings, now=now + timedelta(hours=25))
|
|
|
|
|
|
def test_settings_controller_check_emits_result_for_local_manifest(qtbot, tmp_path: Path):
|
|
import json
|
|
from dx_productivity.ui.settings_controller import SettingsController
|
|
|
|
manifest = tmp_path / "manifest.json"
|
|
manifest.write_text(
|
|
json.dumps(
|
|
{
|
|
"product": "yiyezhqiu-refactored",
|
|
"version": "1.1.0",
|
|
"channel": "development",
|
|
"min_supported_version": "1.0.0",
|
|
"package_url": "file:///release.zip",
|
|
"sha256": "a" * 64,
|
|
"release_notes": "test",
|
|
"published_at": "2026-09-11",
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
controller = SettingsController(current_version="1.0.0")
|
|
|
|
with qtbot.waitSignal(controller.check_finished, timeout=3000) as signal:
|
|
controller.check_updates(manifest.as_uri(), "development")
|
|
|
|
assert signal.args[0].status == "available"
|
|
assert controller._thread is None
|
|
|
|
|
|
def test_settings_controller_stages_update_before_install_request(qtbot, tmp_path: Path):
|
|
from dx_productivity.ui.settings_controller import SettingsController
|
|
from dx_productivity.update.models import UpdateManifest
|
|
|
|
class FakeStager:
|
|
def download_and_verify(self, manifest, destination):
|
|
destination.mkdir(parents=True, exist_ok=True)
|
|
staged = destination / "1.1.0.zip"
|
|
staged.write_bytes(b"verified")
|
|
return staged
|
|
|
|
manifest = UpdateManifest.from_mapping(
|
|
{
|
|
"product": "yiyezhqiu-refactored",
|
|
"version": "1.1.0",
|
|
"channel": "development",
|
|
"min_supported_version": "1.0.0",
|
|
"package_url": "file:///release.zip",
|
|
"sha256": "a" * 64,
|
|
"release_notes": "test",
|
|
"published_at": "2026-09-11",
|
|
}
|
|
)
|
|
controller = SettingsController(stager=FakeStager(), staging_path=tmp_path / "updates")
|
|
|
|
with qtbot.waitSignal(controller.stage_finished, timeout=3000) as signal:
|
|
controller.stage_update(manifest)
|
|
|
|
result = signal.args[0]
|
|
assert result.state == "ready"
|
|
assert result.staged_zip == tmp_path / "updates" / "1.1.0.zip"
|
|
assert controller._stage_thread is None
|
|
assert controller.staged_path(manifest) == result.staged_zip
|
|
|
|
|
|
def test_settings_save_preserves_daily_check_timestamp_for_same_source(tmp_path):
|
|
from dx_productivity.ui.settings_controller import SettingsController
|
|
controller = SettingsController(settings_path=tmp_path / "settings.json")
|
|
now = datetime(2026, 10, 10, tzinfo=timezone.utc)
|
|
controller.save_settings("file:///manifest.json", "development", True)
|
|
controller.record_check(now=now)
|
|
|
|
controller.save_settings(" file:///manifest.json ", "DEVELOPMENT", True)
|
|
|
|
assert not controller.auto_check_due(controller.load_settings(), now=now + timedelta(hours=1))
|
|
controller.save_settings("file:///new-manifest.json", "development", True)
|
|
assert controller.auto_check_due(controller.load_settings(), now=now + timedelta(hours=1))
|
|
|
|
|
|
def test_real_local_update_check_failure_then_retry_releases_worker(qtbot, tmp_path):
|
|
from dx_productivity.ui.settings_controller import SettingsController
|
|
from tests.update.test_service import write_manifest
|
|
controller = SettingsController(current_version="1.0.0", settings_path=tmp_path / "settings.json")
|
|
manifest = tmp_path / "manifest.json"
|
|
manifest.write_text("{broken", encoding="utf-8")
|
|
|
|
with qtbot.waitSignal(controller.check_finished, timeout=3000) as failure:
|
|
controller.check_updates(manifest.as_uri(), "stable")
|
|
assert failure.args[0].status == "error"
|
|
assert controller._thread is None
|
|
write_manifest(manifest, "file:///package.zip")
|
|
with qtbot.waitSignal(controller.check_finished, timeout=3000) as success:
|
|
controller.check_updates(manifest.as_uri(), "stable")
|
|
assert success.args[0].status == "available"
|
|
assert controller._thread is None
|