import importlib from pathlib import Path from dx_productivity.domain.models import RestoreItem, RestoreRequest, Severity def _request(restore_refdes: str) -> RestoreRequest: return RestoreRequest( design_id="design-test", source_backup=Path("C:/Backups/run.json"), changes=( RestoreItem( bridge_id="old-bridge-id", stable_uid="uid-c0001", full_path="ROOT\\C0001", expected_value="R1", restore_value=restore_refdes, ), ), ) def test_restore_plan_matches_current_component_by_stable_identity( snapshot_factory, ) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory(target_ref="R1") plan = restore.build_restore_plan(snapshot, _request("R?")) assert plan.can_apply assert plan.rows[0].bridge_ids == ("c0001",) assert plan.rows[0].old_refdes == "R1" assert plan.rows[0].new_refdes == "R?" assert plan.schedule[0].new_refdes == "R?" def test_restore_plan_blocks_component_changed_after_backup( snapshot_factory, ) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory(target_ref="R9") plan = restore.build_restore_plan(snapshot, _request("R?")) assert not plan.can_apply assert any( issue.code == "restore-stale" and issue.severity is Severity.BLOCKING for issue in plan.issues ) def test_restore_plan_blocks_collision_outside_restore_set( snapshot_factory, ) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory(target_ref="R1", outside_ref="R2") plan = restore.build_restore_plan(snapshot, _request("R2")) assert not plan.can_apply assert any(issue.code == "restore-collision" for issue in plan.issues) def test_restore_plan_can_restore_original_blank_reference( snapshot_factory, ) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory(target_ref="R1") plan = restore.build_restore_plan(snapshot, _request("")) assert plan.can_apply assert plan.schedule[0].new_refdes == "" def test_restore_matches_multiunit_members_by_bridge_id( snapshot_factory, component_factory ) -> None: """Regression: multi-unit backups share one stable_uid; matching by uid alone made every item ambiguous and blocked the whole restore.""" restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory( components=( component_factory( bridge_id="u1a", stable_uid="uid-u1", full_path="ROOT\\U1\\A", logical_group_key="uid-u1", refdes="U1", symbol_refdes_default="U1", sheet_number=1, ), component_factory( bridge_id="u1b", stable_uid="uid-u1", full_path="ROOT\\U1\\B", logical_group_key="uid-u1", refdes="U1", symbol_refdes_default="U1", sheet_number=2, ), ) ) request = RestoreRequest( design_id="design-test", source_backup=Path("C:/Backups/run.json"), changes=( RestoreItem( bridge_id="u1a", stable_uid="uid-u1", full_path="ROOT\\U1\\A", expected_value="U1", restore_value="U?", ), RestoreItem( bridge_id="u1b", stable_uid="uid-u1", full_path="ROOT\\U1\\B", expected_value="U1", restore_value="U?", ), ), ) plan = restore.build_restore_plan(snapshot, request) assert plan.can_apply assert len(plan.rows) == 1 assert plan.rows[0].bridge_ids == ("u1a", "u1b") assert plan.schedule[0].new_refdes == "U?" def test_restore_blocks_locked_component( snapshot_factory, component_factory ) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory( components=(component_factory(refdes="R1", locked=True),) ) plan = restore.build_restore_plan(snapshot, _request("R?")) assert not plan.can_apply assert any(issue.code == "restore-locked" for issue in plan.issues) def test_restore_treats_already_restored_component_as_satisfied( snapshot_factory, ) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory(target_ref="R?") plan = restore.build_restore_plan(snapshot, _request("R?")) assert not [ issue for issue in plan.issues if issue.severity is Severity.BLOCKING ] assert plan.rows[0].changed is False assert plan.schedule == () def test_restore_detects_suffixed_target_collision(snapshot_factory) -> None: restore = importlib.import_module("dx_productivity.domain.restore") snapshot = snapshot_factory(target_ref="R1", outside_ref="R1A") plan = restore.build_restore_plan(snapshot, _request("R1A")) assert not plan.can_apply assert any(issue.code == "restore-collision" for issue in plan.issues)