from dataclasses import replace from decimal import Decimal from pathlib import Path import shutil from typing import Any, Callable import pytest from dx_productivity.domain.models import ( AnnotationSettings, ComponentRecord, DesignSnapshot, LogicalComponent, PlanRow, ) from dx_productivity.domain.planner import build_annotation_plan @pytest.fixture def fixture_path() -> Path: return Path(__file__).parent / "fixtures" @pytest.fixture def component_factory() -> Callable[..., ComponentRecord]: def factory(**overrides: Any) -> ComponentRecord: defaults: dict[str, Any] = { "bridge_id": "c0001", "stable_uid": "uid-c0001", "full_path": "ROOT\\C0001", "logical_group_key": "uid-c0001", "sheet_name": "1", "sheet_number": 1, "x": Decimal("10"), "y": Decimal("10"), "y_axis_up": True, "refdes": "R?", "symbol_refdes_default": "R?", "selected": True, "current_sheet": True, "part_number": "PART", "value": "10K", "locked": False, } defaults.update(overrides) return ComponentRecord(**defaults) return factory @pytest.fixture def group_factory( component_factory: Callable[..., ComponentRecord], ) -> Callable[..., LogicalComponent]: def factory(key: str, **overrides: Any) -> LogicalComponent: prefix = overrides.pop("prefix", "R") sheet = overrides.pop("sheet", 1) x = Decimal(str(overrides.pop("x", "10"))) y = Decimal(str(overrides.pop("y", "10"))) member = component_factory( bridge_id=key, stable_uid=key, logical_group_key=key, sheet_number=sheet, x=x, y=y, **overrides, ) normalized_y = y if member.y_axis_up else -y return LogicalComponent( key=key, members=(member,), prefix=prefix, sheet_number=sheet, x=x, normalized_y=normalized_y, ) return factory @pytest.fixture def settings() -> Callable[..., AnnotationSettings]: def factory(**overrides: Any) -> AnnotationSettings: return replace(AnnotationSettings(), **overrides) return factory @pytest.fixture def snapshot_factory( component_factory: Callable[..., ComponentRecord], ) -> Callable[..., DesignSnapshot]: def factory( target_ref: str = "R?", outside_ref: str | None = None, **overrides: Any ) -> DesignSnapshot: components = [component_factory(refdes=target_ref)] if outside_ref is not None: components.append( component_factory( bridge_id="outside", stable_uid="uid-outside", full_path="ROOT\\OUTSIDE", logical_group_key="uid-outside", refdes=outside_ref, selected=False, x=Decimal("20"), ) ) snapshot = DesignSnapshot( format_version=2, run_id="run-test", snapshot_id="snapshot-test", design_id="design-test", design_path="C:\\Projects\\test.prj", components=tuple(components), ) return replace(snapshot, **overrides) return factory @pytest.fixture def plan_row_factory() -> Callable[..., PlanRow]: def factory( bridge_id: str, old_refdes: str, new_refdes: str, **overrides: Any ) -> PlanRow: prefix = "".join(char for char in new_refdes if char.isalpha()) or None row = PlanRow( logical_key=bridge_id, bridge_ids=(bridge_id,), full_path=f"ROOT\\{bridge_id}", prefix=prefix, old_refdes=old_refdes, new_refdes=new_refdes, status="pending", changed=old_refdes.casefold() != new_refdes.casefold(), ) return replace(row, **overrides) return factory @pytest.fixture def powershell() -> str: executable = shutil.which("pwsh") or shutil.which("powershell") if executable is None: pytest.skip("PowerShell is not available") return executable @pytest.fixture def snapshot(snapshot_factory: Callable[..., DesignSnapshot]) -> DesignSnapshot: return snapshot_factory() @pytest.fixture def plan(snapshot: DesignSnapshot) -> Any: return build_annotation_plan(snapshot, AnnotationSettings())