188 lines
6.2 KiB
Python
188 lines
6.2 KiB
Python
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()
|