from dataclasses import replace import pytest from dx_productivity.bridge.protocol import load_request from dx_productivity.domain.identity import ( group_logical_components, is_fill_only_eligible, is_reannotation_eligible, resolve_prefix, select_scope, ) from dx_productivity.domain.models import ProcessingMode, Scope @pytest.mark.parametrize( ("refdes", "symbol_default", "prefix", "resolved"), [ ("R?", None, "R", True), ("tp??", None, "TP", True), ("", "C?", "C", True), ("", None, None, False), ("R12", "R?", "R", True), ], ) def test_resolve_prefix( refdes, symbol_default, prefix, resolved, component_factory ): result = resolve_prefix( component_factory( refdes=refdes, symbol_refdes_default=symbol_default ) ) assert (result.prefix, result.resolved) == (prefix, resolved) def test_selected_scope_does_not_fall_back_when_empty(component_factory): records = (component_factory(selected=False),) assert select_scope(records, Scope.SELECTED) == () def test_current_and_full_scope_are_explicit(component_factory): current = component_factory(bridge_id="current", current_sheet=True) other = component_factory( bridge_id="other", stable_uid="other", current_sheet=False ) records = (current, other) assert select_scope(records, Scope.CURRENT_SHEET) == (current,) assert select_scope(records, Scope.FULL_DESIGN) == records def test_multiunit_symbols_form_one_logical_component(fixture_path): records = load_request(fixture_path / "request-multiunit.json").components groups = group_logical_components(records) assert len(groups) == 1 assert {record.bridge_id for record in groups[0].members} == {"u1a", "u1b"} assert groups[0].key == "uid-u1" assert groups[0].identity_ambiguous is False def test_missing_fallback_key_is_blocked_without_merging(component_factory): first = component_factory( bridge_id="a", stable_uid=None, logical_group_key="", full_path="ROOT\\A" ) second = component_factory( bridge_id="b", stable_uid=None, logical_group_key="", full_path="ROOT\\B" ) groups = group_logical_components((first, second)) assert [group.key for group in groups] == ["ambiguous:a", "ambiguous:b"] assert all(group.identity_ambiguous for group in groups) @pytest.mark.parametrize( ("refdes", "locked", "expected"), [("", False, True), ("R?", False, True), ("R12", False, False), ("R?", True, False)], ) def test_fill_only_eligibility(refdes, locked, expected, component_factory): record = component_factory(refdes=refdes, locked=locked) assert is_fill_only_eligible(record) is expected def test_reannotation_requires_unlocked_resolved_prefix(component_factory): eligible = component_factory(refdes="R12") locked = replace(eligible, locked=True) unresolved = replace(eligible, refdes="", symbol_refdes_default=None) assert is_reannotation_eligible(eligible) is True assert is_reannotation_eligible(locked) is False assert is_reannotation_eligible(unresolved) is False def test_processing_mode_enum_values_remain_bridge_stable(): assert ProcessingMode.FILL_ONLY.value == "fill-only" assert ProcessingMode.REANNOTATE.value == "reannotate"