from dx_productivity.features.property_change import ( PropertyChangeSettings, build_property_change_plan, ) from dx_productivity.domain.models import AttributeRecord, OpType, Severity def _component_with_attributes(component_factory, attributes): return component_factory(attributes=tuple(attributes)) def test_plan_rewrites_selected_components_with_attribute( component_factory, snapshot_factory ): component = _component_with_attributes( component_factory, (AttributeRecord(name="Value", value="10K", visible=True),), ) snapshot = snapshot_factory(components=(component,)) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=((component.bridge_id, "47K"),), ), ) assert plan.can_apply assert plan.rows[0].changed is True assert plan.rows[0].new_refdes == "47K" assert plan.schedule[0].op is OpType.PROPERTY_VALUE assert plan.schedule[0].property_name == "Value" assert plan.schedule[0].expected_refdes == "10K" assert plan.schedule[0].new_refdes == "47K" def test_missing_attribute_is_reported_not_silent( component_factory, snapshot_factory ): """Legacy EFM silently skipped components without the attribute.""" component = _component_with_attributes( component_factory, (AttributeRecord(name="Part Number", value="P1", visible=True),) ) snapshot = snapshot_factory(components=(component,)) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=((component.bridge_id, "47K"),), ), ) assert plan.rows[0].new_refdes is None assert any(issue.code == "skipped-missing" for issue in plan.issues) def test_locked_components_are_skipped(component_factory, snapshot_factory): component = _component_with_attributes( component_factory, (AttributeRecord(name="Value", value="10K", visible=True),), locked=True, ) if False else component_factory( attributes=(AttributeRecord(name="Value", value="10K", visible=True),), locked=True, ) snapshot = snapshot_factory(components=(component,)) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=((component.bridge_id, "47K"),), ), ) assert plan.rows[0].changed is False assert any(issue.code == "skipped-locked" for issue in plan.issues) def test_unselected_components_are_ignored(component_factory, snapshot_factory): component = component_factory( selected=False, attributes=(AttributeRecord(name="Value", value="10K", visible=True),), ) snapshot = snapshot_factory(components=(component,)) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=((component.bridge_id, "47K"),), ), ) assert plan.rows == () assert plan.schedule == () def test_duplicate_overrides_are_blocking(component_factory, snapshot_factory): component = component_factory( attributes=(AttributeRecord(name="Value", value="10K", visible=True),) ) snapshot = snapshot_factory(components=(component,)) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=( (component.bridge_id, "47K"), (component.bridge_id, "22K"), ), ), ) assert any( issue.code == "duplicate-override" and issue.severity is Severity.BLOCKING for issue in plan.issues ) def test_clearing_value_is_a_change(component_factory, snapshot_factory): component = component_factory( attributes=(AttributeRecord(name="Value", value="10K", visible=True),) ) snapshot = snapshot_factory(components=(component,)) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=((component.bridge_id, ""),), ), ) assert plan.can_apply assert plan.rows[0].changed is True assert plan.schedule[0].new_refdes == "" def test_multiple_components_get_independent_schedule_rows( component_factory, snapshot_factory ): components = tuple( component_factory( bridge_id=f"c{i}", stable_uid=f"uid-c{i}", full_path=f"ROOT\C{i}", logical_group_key=f"uid-c{i}", x=__import__("decimal").Decimal(10 + i), attributes=(AttributeRecord(name="Value", value="10K", visible=True),), ) for i in range(1, 3) ) snapshot = snapshot_factory(components=components) plan = build_property_change_plan( snapshot, PropertyChangeSettings( property_name="Value", overrides=(("c1", "47K"), ("c2", "47K")), ), ) assert plan.can_apply assert len(plan.schedule) == 2 assert plan.schedule[0].bridge_id != plan.schedule[1].bridge_id