from dataclasses import replace from decimal import Decimal from dx_productivity.bridge.protocol import load_request from dx_productivity.domain.models import NumberingMode, Severity from dx_productivity.domain.planner import build_annotation_plan def test_plan_skips_collision_with_component_outside_scope( snapshot_factory, settings ): snapshot = snapshot_factory(target_ref="R?", outside_ref="R1") plan = build_annotation_plan(snapshot, settings(start_number=1)) assert plan.rows[0].new_refdes == "R2" assert not [ issue for issue in plan.issues if issue.severity is Severity.BLOCKING ] def test_plan_blocks_unresolved_prefix( component_factory, snapshot_factory, settings ): unresolved = component_factory(refdes="", symbol_refdes_default=None) snapshot = snapshot_factory(components=(unresolved,)) plan = build_annotation_plan(snapshot, settings()) assert any(issue.code == "missing-prefix" for issue in plan.issues) assert plan.can_apply is False def test_plan_accepts_explicit_prefix_override( component_factory, snapshot_factory, settings ): unresolved = component_factory(refdes="", symbol_refdes_default=None) snapshot = snapshot_factory(components=(unresolved,)) plan = build_annotation_plan( snapshot, settings(prefix_overrides=((unresolved.logical_group_key, "TP"),)), ) assert plan.rows[0].new_refdes == "TP1" assert plan.can_apply is True def test_empty_selected_scope_never_falls_back(snapshot_factory, settings): snapshot = snapshot_factory() snapshot = replace( snapshot, components=tuple( replace(component, selected=False) for component in snapshot.components ), ) plan = build_annotation_plan(snapshot, settings()) assert plan.rows == () assert any(issue.code == "empty-scope" for issue in plan.issues) assert plan.can_apply is False def test_multiunit_component_consumes_one_number( fixture_path, settings ): snapshot = load_request(fixture_path / "request-multiunit.json") plan = build_annotation_plan(snapshot, settings()) assert len(plan.rows) == 1 assert plan.rows[0].bridge_ids == ("u1a", "u1b") assert plan.rows[0].new_refdes == "U1" def test_page_mode_uses_explicit_sheet_mapping( component_factory, snapshot_factory, settings ): component = component_factory( sheet_name="Power", sheet_number=None, refdes="C?", x=Decimal("1") ) snapshot = snapshot_factory(components=(component,)) plan = build_annotation_plan( snapshot, settings( numbering_mode=NumberingMode.PAGE, sheet_number_overrides=(("Power", 4),), ), ) assert plan.rows[0].new_refdes == "C401" assert plan.can_apply is True def test_out_of_scope_multiunit_member_blocks_rename( component_factory, snapshot_factory, settings ): """Regression: renaming the in-scope member while a claimed member of the same logical component stays outside the scope silently duplicated U1.""" from dx_productivity.domain.models import Severity in_scope = component_factory( bridge_id="u1a", stable_uid="uid-u1", full_path="ROOT\\U1\\A", logical_group_key="uid-u1", refdes="U?", sheet_number=1, current_sheet=True, ) out_scope = 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, current_sheet=False, selected=False, ) snapshot = snapshot_factory(components=(in_scope, out_scope)) plan = build_annotation_plan(snapshot, settings()) assert not plan.can_apply assert any(issue.code == "out-of-scope-member" for issue in plan.issues) def test_out_of_scope_member_value_is_occupied( component_factory, snapshot_factory, settings ): out_scope = component_factory( bridge_id="outside", stable_uid="uid-u1", full_path="ROOT\\U1", logical_group_key="uid-u1", refdes="R1", selected=False, x=Decimal("20"), ) snapshot = snapshot_factory( components=( component_factory(refdes="R?"), out_scope, ) ) plan = build_annotation_plan(snapshot, settings(start_number=1)) assert plan.rows[0].new_refdes == "R2" def test_out_of_scope_placeholder_member_allows_rename( component_factory, snapshot_factory, settings ): sibling = component_factory( bridge_id="u1b", stable_uid="uid-u1", full_path="ROOT\\U1\\B", logical_group_key="uid-u1", refdes="U?", sheet_number=2, current_sheet=False, selected=False, ) snapshot = snapshot_factory( components=( component_factory( bridge_id="u1a", stable_uid="uid-u1", full_path="ROOT\\U1\\A", logical_group_key="uid-u1", refdes="U?", ), sibling, ) ) plan = build_annotation_plan(snapshot, settings()) assert plan.can_apply assert plan.rows[0].new_refdes == "U1" def test_negative_row_tolerance_becomes_blocking_issue( snapshot_factory, settings ): from decimal import Decimal as D from dx_productivity.domain.models import Severity plan = build_annotation_plan( snapshot_factory(), settings(row_tolerance=D("-1")) ) assert not plan.can_apply assert any( issue.code == "invalid-settings" and issue.severity is Severity.BLOCKING for issue in plan.issues ) def test_page_mode_overflow_warns(component_factory, snapshot_factory, settings): from dx_productivity.domain.models import Severity components = tuple( component_factory( bridge_id=f"r{i}", stable_uid=f"uid-r{i}", full_path=f"ROOT\\R{i}", logical_group_key=f"uid-r{i}", x=Decimal(10 + i), ) for i in range(1, 4) ) snapshot = snapshot_factory(components=components) plan = build_annotation_plan( snapshot, settings(numbering_mode=NumberingMode.PAGE, page_step=2, start_number=1), ) assert any( issue.code == "page-overflow" and issue.severity is Severity.WARNING for issue in plan.issues ) def test_page_mode_never_produces_zero_refdes( component_factory, snapshot_factory, settings ): component = component_factory( bridge_id="r0", stable_uid="uid-r0", full_path="ROOT\\R0", logical_group_key="uid-r0", sheet_number=0, ) snapshot = snapshot_factory(components=(component,)) plan = build_annotation_plan( snapshot, settings(numbering_mode=NumberingMode.PAGE, page_start=0), ) assert not plan.can_apply assert any(issue.code == "invalid-numbering" for issue in plan.issues) def test_sheet_override_on_numbered_sheet_is_reported( component_factory, snapshot_factory, settings ): from dx_productivity.domain.models import Severity component = component_factory(sheet_name="Power", refdes="C?") snapshot = snapshot_factory(components=(component,)) plan = build_annotation_plan( snapshot, settings(sheet_number_overrides=(("Power", 9),)), ) assert any( issue.code == "sheet-override-ignored" and issue.severity is Severity.INFORMATIONAL for issue in plan.issues )