feat: add complete yiyezhqiu productivity suite
This commit is contained in:
commit
e422ca8b2a
155 files changed
+18959
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import importlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from dx_productivity.bridge.protocol import load_apply_request
|
||||
from dx_productivity.bridge.run_store import RunStore
|
||||
from dx_productivity.domain.models import ApplyStatus, RunPaths
|
||||
from dx_productivity.domain.planner import build_annotation_plan
|
||||
from dx_productivity.domain.models import AnnotationSettings
|
||||
|
||||
|
||||
def _run_paths(tmp_path: Path, run_id: str) -> RunPaths:
|
||||
root = tmp_path / "runs" / run_id
|
||||
root.mkdir(parents=True)
|
||||
return RunPaths(
|
||||
run_id,
|
||||
root,
|
||||
root / "request.json",
|
||||
root / "result.tsv",
|
||||
root / "locate.tsv",
|
||||
root / "apply-status.json",
|
||||
)
|
||||
|
||||
|
||||
def test_restore_creates_second_backup_before_publishing_result(
|
||||
qtbot,
|
||||
tmp_path,
|
||||
snapshot_factory,
|
||||
) -> None:
|
||||
page_module = importlib.import_module("dx_productivity.ui.history_page")
|
||||
controller_module = importlib.import_module(
|
||||
"dx_productivity.ui.history_controller"
|
||||
)
|
||||
original = snapshot_factory(target_ref="R?")
|
||||
applied = snapshot_factory(target_ref="R1")
|
||||
store = RunStore(tmp_path / "local")
|
||||
annotation_plan = build_annotation_plan(original, AnnotationSettings())
|
||||
first_backup = store.write_backup(original, annotation_plan)
|
||||
store.record_status(
|
||||
ApplyStatus(1, original.run_id, "applied", 1, first_backup)
|
||||
)
|
||||
paths = _run_paths(tmp_path, applied.run_id)
|
||||
paths.apply_status.write_text('{"stale":true}', encoding="utf-8")
|
||||
page = page_module.HistoryPage()
|
||||
qtbot.addWidget(page)
|
||||
controller = controller_module.HistoryController(
|
||||
page,
|
||||
run_store=store,
|
||||
run_paths=paths,
|
||||
confirm_restore=lambda entry, count: True,
|
||||
)
|
||||
controller.load_snapshot(applied)
|
||||
|
||||
result = controller.restore_latest()
|
||||
|
||||
assert result == paths.result
|
||||
assert result.read_text(encoding="utf-8") == "1\trefdes\tc0001\t\tR1\tR?\tR?\n"
|
||||
metadata = load_apply_request(paths.apply_request)
|
||||
assert metadata.backup_path.exists()
|
||||
assert metadata.backup_path != first_backup
|
||||
assert not paths.apply_status.exists()
|
||||
controller.stop()
|
||||
|
||||
|
||||
def test_restore_cancel_does_not_publish_transaction(
|
||||
qtbot,
|
||||
tmp_path,
|
||||
snapshot_factory,
|
||||
) -> None:
|
||||
page_module = importlib.import_module("dx_productivity.ui.history_page")
|
||||
controller_module = importlib.import_module(
|
||||
"dx_productivity.ui.history_controller"
|
||||
)
|
||||
original = snapshot_factory(target_ref="R?")
|
||||
applied = snapshot_factory(target_ref="R1")
|
||||
store = RunStore(tmp_path / "local")
|
||||
plan = build_annotation_plan(original, AnnotationSettings())
|
||||
backup = store.write_backup(original, plan)
|
||||
store.record_status(ApplyStatus(1, original.run_id, "applied", 1, backup))
|
||||
paths = _run_paths(tmp_path, applied.run_id)
|
||||
page = page_module.HistoryPage()
|
||||
qtbot.addWidget(page)
|
||||
controller = controller_module.HistoryController(
|
||||
page,
|
||||
run_store=store,
|
||||
run_paths=paths,
|
||||
confirm_restore=lambda entry, count: False,
|
||||
)
|
||||
controller.load_snapshot(applied)
|
||||
|
||||
assert controller.restore_latest() is None
|
||||
assert not paths.result.exists()
|
||||
assert not paths.apply_request.exists()
|
||||
@@ -0,0 +1,43 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
|
||||
from dx_productivity.domain.models import HistoryEntry
|
||||
|
||||
|
||||
def _entry(run_id: str, timestamp: datetime) -> HistoryEntry:
|
||||
return HistoryEntry(
|
||||
timestamp=timestamp,
|
||||
run_id=run_id,
|
||||
design_id="design-test",
|
||||
backup_path=Path(f"C:/Backups/{run_id}.json"),
|
||||
state="applied",
|
||||
changed_count=3,
|
||||
)
|
||||
|
||||
|
||||
def test_history_page_shows_latest_entry_first(qtbot) -> None:
|
||||
module = importlib.import_module("dx_productivity.ui.history_page")
|
||||
page = module.HistoryPage()
|
||||
qtbot.addWidget(page)
|
||||
now = datetime.now(timezone.utc)
|
||||
entries = (
|
||||
_entry("newest", now),
|
||||
_entry("older", now - timedelta(days=1)),
|
||||
)
|
||||
|
||||
page.set_entries(entries)
|
||||
|
||||
assert page.row_run_id(0) == "newest"
|
||||
assert page.selected_entry() == entries[0]
|
||||
assert page.restore_button.isEnabled()
|
||||
|
||||
|
||||
def test_history_page_disables_restore_when_empty(qtbot) -> None:
|
||||
module = importlib.import_module("dx_productivity.ui.history_page")
|
||||
page = module.HistoryPage()
|
||||
qtbot.addWidget(page)
|
||||
|
||||
page.set_entries(())
|
||||
|
||||
assert not page.restore_button.isEnabled()
|
||||
@@ -0,0 +1,117 @@
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QToolButton
|
||||
|
||||
from dx_productivity.ui.main_window import MainWindow
|
||||
from dx_productivity.ui.app import create_application, create_main_window
|
||||
from dx_productivity.ui.refdes_page import RefDesPage
|
||||
from dx_productivity.ui.history_page import HistoryPage
|
||||
from dx_productivity.ui.theme import Theme, contrast_ratio, theme_tokens
|
||||
|
||||
|
||||
def test_main_window_has_approved_minimum_and_navigation(qtbot):
|
||||
window = MainWindow()
|
||||
qtbot.addWidget(window)
|
||||
|
||||
assert window.minimumWidth() == 1100
|
||||
assert window.minimumHeight() == 700
|
||||
assert window.navigation_items()[:3] == ["自动位号", "属性批改", "属性显示"]
|
||||
assert window.apply_button.text() == "应用位号"
|
||||
|
||||
|
||||
def test_icon_buttons_have_accessible_names(qtbot):
|
||||
window = MainWindow()
|
||||
qtbot.addWidget(window)
|
||||
|
||||
for button in window.findChildren(QToolButton):
|
||||
assert button.accessibleName()
|
||||
assert button.toolTip()
|
||||
|
||||
|
||||
def test_light_and_dark_text_contrast_meets_normal_text_requirement():
|
||||
for theme in (Theme.LIGHT, Theme.DARK):
|
||||
tokens = theme_tokens(theme)
|
||||
assert contrast_ratio(tokens.text_primary, tokens.canvas) >= 4.5
|
||||
assert contrast_ratio(tokens.text_secondary, tokens.canvas) >= 4.5
|
||||
|
||||
|
||||
def test_theme_toggle_changes_window_theme(qtbot):
|
||||
window = MainWindow(theme=Theme.LIGHT)
|
||||
qtbot.addWidget(window)
|
||||
|
||||
qtbot.mouseClick(window.theme_button, Qt.MouseButton.LeftButton)
|
||||
|
||||
assert window.theme is Theme.DARK
|
||||
|
||||
|
||||
def test_minimum_layout_has_no_children_outside_window(qtbot):
|
||||
window = MainWindow()
|
||||
qtbot.addWidget(window)
|
||||
window.resize(1100, 700)
|
||||
window.show()
|
||||
qtbot.waitExposed(window)
|
||||
|
||||
bounds = window.rect()
|
||||
for child in window.findChildren(QToolButton):
|
||||
top_left = child.mapTo(window, child.rect().topLeft())
|
||||
bottom_right = child.mapTo(window, child.rect().bottomRight())
|
||||
assert bounds.contains(top_left)
|
||||
assert bounds.contains(bottom_right)
|
||||
|
||||
|
||||
def test_request_opens_live_refdes_page(qtbot, fixture_path):
|
||||
window = create_main_window(fixture_path / "request-basic.json")
|
||||
qtbot.addWidget(window)
|
||||
|
||||
assert isinstance(window.pages.widget(0), RefDesPage)
|
||||
assert window.refdes_controller.plan is not None
|
||||
assert window.apply_button.isEnabled() is True
|
||||
|
||||
|
||||
def test_request_connects_operation_history_to_same_snapshot(
|
||||
qtbot, fixture_path, monkeypatch, tmp_path
|
||||
):
|
||||
monkeypatch.setenv("LOCALAPPDATA", str(tmp_path))
|
||||
|
||||
window = create_main_window(fixture_path / "request-basic.json")
|
||||
qtbot.addWidget(window)
|
||||
|
||||
assert isinstance(window.pages.widget(6), HistoryPage)
|
||||
assert window.history_controller.snapshot is window.refdes_controller.snapshot
|
||||
assert window.history_controller.run_paths is window.refdes_controller.run_paths
|
||||
|
||||
|
||||
def test_application_loads_windows_chinese_ui_font(qapp):
|
||||
from PySide6.QtGui import QFontDatabase
|
||||
|
||||
create_application([])
|
||||
|
||||
assert any("YaHei" in family for family in QFontDatabase.families())
|
||||
|
||||
|
||||
def test_main_window_uses_yiyezhqiu_brand_and_feature_pages(qtbot):
|
||||
from dx_productivity.ui.bom_page import BomPage
|
||||
from dx_productivity.ui.inspection_page import InspectionPage
|
||||
from dx_productivity.ui.property_change_page import PropertyChangePage
|
||||
from dx_productivity.ui.property_visibility_page import (
|
||||
PropertyVisibilityPage,
|
||||
)
|
||||
|
||||
window = create_main_window()
|
||||
qtbot.addWidget(window)
|
||||
|
||||
assert window.windowTitle() == "一叶知秋_重构版"
|
||||
assert window.brand_label.text() == "一叶知秋_重构版"
|
||||
assert isinstance(window.pages.widget(1), PropertyChangePage)
|
||||
assert isinstance(window.pages.widget(2), PropertyVisibilityPage)
|
||||
assert isinstance(window.pages.widget(3), InspectionPage)
|
||||
assert isinstance(window.pages.widget(5), BomPage)
|
||||
|
||||
|
||||
def test_main_window_wires_settings_page(qtbot):
|
||||
from dx_productivity.ui.settings_page import SettingsPage
|
||||
|
||||
window = create_main_window()
|
||||
qtbot.addWidget(window)
|
||||
|
||||
assert isinstance(window.pages.widget(7), SettingsPage)
|
||||
assert window.settings_page.version_label.text().startswith("当前版本 0.1.0")
|
||||
@@ -0,0 +1,187 @@
|
||||
import hashlib
|
||||
|
||||
from dx_productivity.bridge.protocol import load_apply_request
|
||||
from dx_productivity.bridge.run_store import RunStore
|
||||
from dx_productivity.domain.models import RunPaths
|
||||
from dx_productivity.ui import refdes_controller as controller_module
|
||||
from dx_productivity.ui.refdes_controller import RefDesController
|
||||
from dx_productivity.ui.refdes_page import RefDesPage
|
||||
|
||||
|
||||
def test_blocking_issue_disables_apply(
|
||||
qtbot, component_factory, snapshot_factory
|
||||
):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
controller = RefDesController(page)
|
||||
unresolved = component_factory(refdes="", symbol_refdes_default=None)
|
||||
|
||||
controller.load_snapshot(snapshot_factory(components=(unresolved,)))
|
||||
|
||||
assert controller.can_apply is False
|
||||
assert page.issue_text() == "1 个阻断项"
|
||||
|
||||
|
||||
def test_valid_plan_enables_apply(qtbot, snapshot_factory):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
controller = RefDesController(page)
|
||||
|
||||
controller.load_snapshot(snapshot_factory())
|
||||
|
||||
assert controller.can_apply is True
|
||||
assert controller.plan is not None
|
||||
assert controller.plan.rows[0].new_refdes == "R1"
|
||||
|
||||
|
||||
def test_scope_change_recomputes_without_mutating_snapshot(
|
||||
qtbot, snapshot_factory
|
||||
):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
controller = RefDesController(page)
|
||||
snapshot = snapshot_factory(target_ref="R?", outside_ref="R1")
|
||||
controller.load_snapshot(snapshot)
|
||||
|
||||
page.full_design_button.click()
|
||||
|
||||
assert controller.snapshot is snapshot
|
||||
assert controller.plan is not None
|
||||
assert len(controller.plan.rows) == 2
|
||||
|
||||
|
||||
def test_apply_writes_metadata_before_atomic_schedule(
|
||||
qtbot, tmp_path, snapshot_factory, monkeypatch
|
||||
):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
snapshot = snapshot_factory()
|
||||
run_root = tmp_path / "runs" / snapshot.run_id
|
||||
run_root.mkdir(parents=True)
|
||||
paths = RunPaths(
|
||||
snapshot.run_id,
|
||||
run_root,
|
||||
run_root / "request.json",
|
||||
run_root / "result.tsv",
|
||||
run_root / "locate.tsv",
|
||||
run_root / "apply-status.json",
|
||||
)
|
||||
controller = RefDesController(page, run_store=RunStore(tmp_path), run_paths=paths)
|
||||
controller.load_snapshot(snapshot)
|
||||
paths.apply_status.write_text('{"stale": true}', encoding="utf-8")
|
||||
write_schedule = controller_module.write_schedule
|
||||
|
||||
def guarded_write_schedule(path, schedule):
|
||||
assert paths.apply_request.exists()
|
||||
write_schedule(path, schedule)
|
||||
|
||||
monkeypatch.setattr(controller_module, "write_schedule", guarded_write_schedule)
|
||||
|
||||
result = controller.apply()
|
||||
|
||||
assert result == paths.result
|
||||
assert not paths.apply_status.exists()
|
||||
assert result.read_text(encoding="utf-8") == "1\trefdes\tc0001\t\tR?\tR1\tR1\n"
|
||||
assert controller.pending_backup is not None
|
||||
assert controller.pending_backup.exists()
|
||||
apply_request = load_apply_request(paths.apply_request)
|
||||
assert apply_request.run_id == snapshot.run_id
|
||||
assert apply_request.snapshot_id == snapshot.snapshot_id
|
||||
assert apply_request.backup_path == controller.pending_backup
|
||||
assert apply_request.schedule_sha256 == hashlib.sha256(result.read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def test_locate_request_is_published_to_mailbox(qtbot, tmp_path, snapshot_factory):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
snapshot = snapshot_factory()
|
||||
run_root = tmp_path / "run"
|
||||
run_root.mkdir()
|
||||
paths = RunPaths(
|
||||
snapshot.run_id,
|
||||
run_root,
|
||||
run_root / "request.json",
|
||||
run_root / "result.tsv",
|
||||
run_root / "locate.tsv",
|
||||
run_root / "apply-status.json",
|
||||
)
|
||||
controller = RefDesController(page, run_paths=paths)
|
||||
controller.load_snapshot(snapshot)
|
||||
|
||||
page.locate_requested.emit("c0001")
|
||||
|
||||
assert paths.locate.read_text(encoding="utf-8") == "c0001\n"
|
||||
|
||||
|
||||
def _make_controller(qtbot, tmp_path, snapshot, store=True):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
run_root = tmp_path / "runs" / snapshot.run_id
|
||||
run_root.mkdir(parents=True)
|
||||
paths = RunPaths(
|
||||
snapshot.run_id,
|
||||
run_root,
|
||||
run_root / "request.json",
|
||||
run_root / "result.tsv",
|
||||
run_root / "locate.tsv",
|
||||
run_root / "apply-status.json",
|
||||
)
|
||||
run_store = RunStore(tmp_path) if store else None
|
||||
controller = RefDesController(page, run_store=run_store, run_paths=paths)
|
||||
controller.load_snapshot(snapshot)
|
||||
return controller
|
||||
|
||||
|
||||
def test_rejected_status_without_backup_does_not_crash_polling(
|
||||
qtbot, tmp_path, snapshot_factory
|
||||
):
|
||||
"""Regression: a rejected transaction writes backup_path=null; the old
|
||||
record_status raised inside the polling slot and silently killed it."""
|
||||
snapshot = snapshot_factory()
|
||||
controller = _make_controller(qtbot, tmp_path, snapshot)
|
||||
controller.run_paths.apply_status.write_text(
|
||||
'{"format_version": 2, "run_id": "%s", "state": "rejected", '
|
||||
'"changed_count": 0, "backup_path": null, "failing_sequence": null, '
|
||||
'"error": "Apply schedule expected value is stale", '
|
||||
'"rollback_state": null}' % snapshot.run_id,
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
with qtbot.waitSignal(controller.apply_completed, timeout=5000):
|
||||
controller._poll_apply_status()
|
||||
|
||||
|
||||
def test_corrupt_status_file_reports_failure(qtbot, tmp_path, snapshot_factory):
|
||||
snapshot = snapshot_factory()
|
||||
controller = _make_controller(qtbot, tmp_path, snapshot)
|
||||
controller.run_paths.apply_status.write_text("{not json", encoding="utf-8")
|
||||
|
||||
with qtbot.waitSignal(controller.apply_failed, timeout=5000):
|
||||
controller._poll_apply_status()
|
||||
|
||||
|
||||
def test_apply_reentry_is_rejected_and_refresh_is_ignored(
|
||||
qtbot, tmp_path, snapshot_factory
|
||||
):
|
||||
import pytest
|
||||
|
||||
snapshot = snapshot_factory()
|
||||
controller = _make_controller(qtbot, tmp_path, snapshot)
|
||||
controller._apply_in_flight = True
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
controller.apply()
|
||||
|
||||
|
||||
def test_missing_heartbeat_fails_transaction(
|
||||
qtbot, tmp_path, snapshot_factory
|
||||
):
|
||||
import time
|
||||
|
||||
snapshot = snapshot_factory()
|
||||
controller = _make_controller(qtbot, tmp_path, snapshot)
|
||||
controller._apply_in_flight = True
|
||||
controller._apply_started_at = time.monotonic() - 3600
|
||||
|
||||
with qtbot.waitSignal(controller.apply_failed, timeout=5000):
|
||||
controller._poll_apply_status()
|
||||
@@ -0,0 +1,56 @@
|
||||
from decimal import Decimal
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
|
||||
from dx_productivity.domain.models import (
|
||||
NumberingMode,
|
||||
ProcessingMode,
|
||||
Scope,
|
||||
SortOrder,
|
||||
)
|
||||
from dx_productivity.ui.refdes_page import RefDesPage
|
||||
|
||||
|
||||
def test_refdes_defaults_match_approved_design(qtbot):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
|
||||
assert page.scope() is Scope.SELECTED
|
||||
assert page.processing_mode() is ProcessingMode.FILL_ONLY
|
||||
assert page.numbering_mode() is NumberingMode.CONTINUOUS
|
||||
assert page.sort_order() is SortOrder.TOP_DOWN_LEFT_RIGHT
|
||||
assert page.start_number() == 1
|
||||
assert page.row_tolerance() == Decimal("5")
|
||||
|
||||
|
||||
def test_page_controls_emit_settings_changed(qtbot):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
with qtbot.waitSignal(page.settings_changed):
|
||||
page.full_design_button.click()
|
||||
|
||||
assert page.scope() is Scope.FULL_DESIGN
|
||||
|
||||
|
||||
def test_search_filters_preview_without_removing_plan_rows(
|
||||
qtbot, snapshot_factory, settings
|
||||
):
|
||||
from dx_productivity.domain.planner import build_annotation_plan
|
||||
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
plan = build_annotation_plan(snapshot_factory(), settings())
|
||||
page.set_plan(plan)
|
||||
|
||||
page.search_input.setText("no-match")
|
||||
|
||||
assert page.preview_model.rowCount() == 1
|
||||
assert page.preview_table.isRowHidden(0) is True
|
||||
|
||||
|
||||
def test_preview_table_has_stable_column_count(qtbot):
|
||||
page = RefDesPage()
|
||||
qtbot.addWidget(page)
|
||||
|
||||
assert page.preview_model.columnCount() == 8
|
||||
assert page.preview_table.horizontalHeader().minimumSectionSize() >= 40
|
||||
@@ -0,0 +1,97 @@
|
||||
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
|
||||
@@ -0,0 +1,106 @@
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
|
||||
def make_manifest(version: str = "1.1.0"):
|
||||
from dx_productivity.update.models import UpdateManifest
|
||||
|
||||
return UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": version,
|
||||
"channel": "development",
|
||||
"min_supported_version": "1.0.0",
|
||||
"package_url": "file:///release.zip",
|
||||
"sha256": "a" * 64,
|
||||
"release_notes": "迁移旧版属性工具",
|
||||
"published_at": "2026-09-11",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_settings_page_requires_second_confirmation_before_install(qtbot, monkeypatch):
|
||||
from dx_productivity.ui.settings_page import SettingsPage
|
||||
from dx_productivity.ui.settings_controller import UpdateStageResult
|
||||
|
||||
page = SettingsPage(current_version="1.0.0")
|
||||
qtbot.addWidget(page)
|
||||
page.show_update(make_manifest())
|
||||
monkeypatch.setattr(
|
||||
QMessageBox,
|
||||
"question",
|
||||
lambda *args, **kwargs: QMessageBox.StandardButton.Yes,
|
||||
)
|
||||
|
||||
assert page.install_button.isEnabled()
|
||||
assert page.install_requested_emissions == 0
|
||||
page.install_button.click()
|
||||
assert page.install_requested_emissions == 0
|
||||
assert page.download_requested_emissions == 1
|
||||
from pathlib import Path
|
||||
|
||||
page.show_stage_result(UpdateStageResult("ready", make_manifest(), Path("release.zip"), "ready"))
|
||||
page.install_button.click()
|
||||
assert page.install_requested_emissions == 1
|
||||
|
||||
|
||||
def test_settings_page_does_not_offer_install_until_staging_finishes(qtbot, monkeypatch):
|
||||
from dx_productivity.ui.settings_page import SettingsPage
|
||||
from dx_productivity.ui.settings_controller import UpdateStageResult
|
||||
from pathlib import Path
|
||||
|
||||
page = SettingsPage(current_version="1.0.0")
|
||||
qtbot.addWidget(page)
|
||||
manifest = make_manifest()
|
||||
page.show_update(manifest)
|
||||
monkeypatch.setattr(
|
||||
QMessageBox,
|
||||
"question",
|
||||
lambda *args, **kwargs: QMessageBox.StandardButton.Yes,
|
||||
)
|
||||
|
||||
page.install_button.click()
|
||||
assert not page.install_button.isEnabled()
|
||||
assert page.status_label.text() == "正在下载并校验"
|
||||
page.show_stage_result(UpdateStageResult("failed", manifest, None, "网络不可用"))
|
||||
assert not page.install_button.isEnabled()
|
||||
assert page.status_label.text() == "网络不可用"
|
||||
page.show_stage_result(UpdateStageResult("ready", manifest, Path("release.zip"), "ready"))
|
||||
assert page.install_button.isEnabled()
|
||||
assert page.install_button.text() == "确认安装"
|
||||
|
||||
|
||||
def test_settings_page_disables_install_for_non_available_result(qtbot):
|
||||
from dx_productivity.ui.settings_page import SettingsPage
|
||||
from dx_productivity.update.service import UpdateCheckResult
|
||||
from dx_productivity.update.models import SemVer
|
||||
|
||||
page = SettingsPage(current_version="1.0.0")
|
||||
qtbot.addWidget(page)
|
||||
page.show_result(UpdateCheckResult("up-to-date", SemVer.parse("1.0.0"), None, "latest"))
|
||||
|
||||
assert not page.install_button.isEnabled()
|
||||
assert page.status_label.text() == "已是最新"
|
||||
|
||||
|
||||
def test_settings_page_disables_unsigned_stable_install(qtbot):
|
||||
from dx_productivity.ui.settings_page import SettingsPage
|
||||
from dx_productivity.update.models import UpdateManifest
|
||||
|
||||
page = SettingsPage(current_version="1.0.0")
|
||||
qtbot.addWidget(page)
|
||||
manifest = UpdateManifest.from_mapping(
|
||||
{
|
||||
"product": "yiyezhqiu-refactored",
|
||||
"version": "1.1.0",
|
||||
"channel": "stable",
|
||||
"min_supported_version": "1.0.0",
|
||||
"package_url": "file:///release.zip",
|
||||
"sha256": "a" * 64,
|
||||
"release_notes": "test",
|
||||
"published_at": "2026-09-11",
|
||||
}
|
||||
)
|
||||
page.show_update(manifest)
|
||||
|
||||
assert not page.install_button.isEnabled()
|
||||
assert "签名" in page.status_label.text()
|
||||
Reference in new issue
Block a user