feat: add complete yiyezhqiu productivity suite

This commit is contained in:
阿白叔 committed 2026-10-10 17:10:32 +08:00
commit e422ca8b2a
155 files changed
+18959

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
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"
+90
View File
@@ -0,0 +1,90 @@
import pytest
from dx_productivity.domain.models import NumberingMode
from dx_productivity.domain.numbering import allocate_numbers
def test_continuous_allocation_skips_occupied(group_factory, settings):
groups = (
group_factory("a", prefix="R"),
group_factory("b", prefix="R"),
)
result = allocate_numbers(groups, {"R1", "r3"}, settings(start_number=1))
assert result == {"a": "R2", "b": "R4"}
def test_continuous_sequences_are_independent_per_prefix(group_factory, settings):
groups = (
group_factory("r1", prefix="R"),
group_factory("c1", prefix="C"),
group_factory("r2", prefix="R"),
group_factory("c2", prefix="C"),
)
result = allocate_numbers(groups, set(), settings(start_number=1))
assert result == {"r1": "R1", "c1": "C1", "r2": "R2", "c2": "C2"}
def test_page_mode_starts_sheet_three_at_301(group_factory, settings):
groups = (group_factory("a", prefix="C", sheet=3),)
result = allocate_numbers(
groups,
set(),
settings(numbering_mode=NumberingMode.PAGE),
)
assert result == {"a": "C301"}
def test_page_mode_keeps_sheet_and_prefix_sequences_independent(
group_factory, settings
):
groups = (
group_factory("r2a", prefix="R", sheet=2),
group_factory("c2", prefix="C", sheet=2),
group_factory("r2b", prefix="R", sheet=2),
group_factory("r3", prefix="R", sheet=3),
)
result = allocate_numbers(
groups,
{"R201"},
settings(numbering_mode=NumberingMode.PAGE),
)
assert result == {
"r2a": "R202",
"c2": "C201",
"r2b": "R203",
"r3": "R301",
}
def test_page_mode_rejects_missing_numeric_sheet(group_factory, settings):
with pytest.raises(ValueError, match="numeric sheet"):
allocate_numbers(
(group_factory("a", sheet=None),),
set(),
settings(numbering_mode=NumberingMode.PAGE),
)
@pytest.mark.parametrize(
"overrides",
[
{"start_number": 0},
{"numbering_mode": NumberingMode.PAGE, "page_step": 0},
{"numbering_mode": NumberingMode.PAGE, "page_start": -1},
],
)
def test_invalid_numeric_ranges_are_rejected(overrides, group_factory, settings):
with pytest.raises(ValueError, match="number"):
allocate_numbers(
(group_factory("a"),),
set(),
settings(**overrides),
)
+78
View File
@@ -0,0 +1,78 @@
import random
from decimal import Decimal
import pytest
from dx_productivity.domain.models import SortOrder
from dx_productivity.domain.ordering import order_components
def test_default_order_is_sheet_top_to_bottom_then_left_to_right(
group_factory, settings
):
groups = (
group_factory("b", sheet=1, x="30", y="100"),
group_factory("a", sheet=1, x="10", y="100"),
group_factory("c", sheet=1, x="5", y="80"),
group_factory("d", sheet=2, x="1", y="500"),
)
ordered = order_components(groups, settings(row_tolerance=Decimal("5")))
assert [group.key for group in ordered] == ["a", "b", "c", "d"]
def test_row_clustering_uses_fixed_anchor_not_chain_drift(group_factory, settings):
groups = (
group_factory("a", x="30", y="100"),
group_factory("b", x="10", y="96"),
group_factory("c", x="20", y="92"),
)
ordered = order_components(groups, settings(row_tolerance=Decimal("5")))
assert [group.key for group in ordered] == ["b", "a", "c"]
@pytest.mark.parametrize(
("sort_order", "expected"),
[
(SortOrder.TOP_DOWN_LEFT_RIGHT, ["tl", "tr", "bl", "br"]),
(SortOrder.TOP_DOWN_RIGHT_LEFT, ["tr", "tl", "br", "bl"]),
(SortOrder.LEFT_RIGHT_TOP_DOWN, ["tl", "bl", "tr", "br"]),
(SortOrder.RIGHT_LEFT_TOP_DOWN, ["tr", "br", "tl", "bl"]),
],
)
def test_all_scan_directions(sort_order, expected, group_factory, settings):
groups = (
group_factory("br", x="100", y="0"),
group_factory("tl", x="0", y="100"),
group_factory("bl", x="0", y="0"),
group_factory("tr", x="100", y="100"),
)
ordered = order_components(
groups,
settings(sort_order=sort_order, row_tolerance=Decimal("5")),
)
assert [group.key for group in ordered] == expected
def test_order_is_deterministic_for_equal_coordinates(group_factory, settings):
original = [group_factory(key, x="10", y="10") for key in "dbca"]
expected = ["a", "b", "c", "d"]
randomizer = random.Random(795)
for _ in range(50):
randomizer.shuffle(original)
ordered = order_components(tuple(original), settings())
assert [group.key for group in ordered] == expected
def test_negative_tolerance_is_rejected(group_factory, settings):
with pytest.raises(ValueError, match="row_tolerance"):
order_components(
(group_factory("a"),),
settings(row_tolerance=Decimal("-1")),
)
+272
View File
@@ -0,0 +1,272 @@
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
)
+171
View File
@@ -0,0 +1,171 @@
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)
+90
View File
@@ -0,0 +1,90 @@
from dx_productivity.domain.schedule import build_apply_schedule
def _triples(steps):
return [
(step.bridge_id, step.expected_refdes, step.new_refdes) for step in steps
]
def test_schedule_breaks_refdes_swap_with_prefix_placeholder(plan_row_factory):
rows = (
plan_row_factory("a", "R1", "R2"),
plan_row_factory("b", "R2", "R1"),
)
steps = build_apply_schedule(rows)
assert _triples(steps) == [
("a", "R1", "R?"),
("b", "R2", "R1"),
("a", "R?", "R2"),
]
assert [step.sequence for step in steps] == [1, 2, 3]
assert [step.final_refdes for step in steps] == ["R2", "R1", "R2"]
def test_schedule_orders_acyclic_dependency_from_tail(plan_row_factory):
rows = (
plan_row_factory("a", "R1", "R2"),
plan_row_factory("b", "R2", "R3"),
)
steps = build_apply_schedule(rows)
assert _triples(steps) == [("b", "R2", "R3"), ("a", "R1", "R2")]
def test_schedule_breaks_three_component_cycle_deterministically(plan_row_factory):
rows = (
plan_row_factory("c", "R3", "R1"),
plan_row_factory("b", "R2", "R3"),
plan_row_factory("a", "R1", "R2"),
)
steps = build_apply_schedule(rows)
assert _triples(steps) == [
("a", "R1", "R?"),
("c", "R3", "R1"),
("b", "R2", "R3"),
("a", "R?", "R2"),
]
def test_schedule_ignores_preserved_rows(plan_row_factory):
row = plan_row_factory("a", "R1", "R1", changed=False, status="preserved")
assert build_apply_schedule((row,)) == ()
def test_schedule_allows_restore_to_blank_reference(plan_row_factory):
row = plan_row_factory("a", "R1", "")
steps = build_apply_schedule((row,))
assert _triples(steps) == [("a", "R1", "")]
def test_schedule_rejects_duplicate_targets_across_rows(plan_row_factory):
rows = (
plan_row_factory("a", "R1", "R2"),
plan_row_factory("b", "R?", "R2"),
)
import pytest
with pytest.raises(ValueError, match="duplicate target"):
build_apply_schedule(rows)
def test_schedule_rejects_duplicate_suffixed_targets(plan_row_factory):
rows = (
plan_row_factory("a", "R1", "R2A"),
plan_row_factory("b", "R?", "R2A"),
)
import pytest
with pytest.raises(ValueError, match="duplicate target"):
build_apply_schedule(rows)
+81
View File
@@ -0,0 +1,81 @@
from dx_productivity.domain.models import Severity
from dx_productivity.domain.validation import validate_plan
def test_validation_blocks_duplicate_source_formal_refdes(
component_factory, snapshot_factory, plan_row_factory
):
first = component_factory(
bridge_id="a",
stable_uid="a",
logical_group_key="a",
refdes="R1",
)
second = component_factory(
bridge_id="b",
stable_uid="b",
logical_group_key="b",
refdes="r1",
)
snapshot = snapshot_factory(components=(first, second))
issues = validate_plan(snapshot, ())
assert any(
issue.code == "duplicate-source-refdes"
and issue.severity is Severity.BLOCKING
for issue in issues
)
def test_validation_blocks_duplicate_proposed_output(
snapshot_factory, plan_row_factory
):
rows = (
plan_row_factory("a", "R?", "R1", logical_key="a"),
plan_row_factory("b", "R?", "r1", logical_key="b"),
)
issues = validate_plan(snapshot_factory(), rows)
assert any(issue.code == "duplicate-proposed-refdes" for issue in issues)
def test_validation_blocks_output_occupied_outside_target(
component_factory, snapshot_factory, plan_row_factory
):
target = component_factory(refdes="R?")
outside = component_factory(
bridge_id="outside",
stable_uid="outside",
logical_group_key="outside",
refdes="R1",
selected=False,
)
snapshot = snapshot_factory(components=(target, outside))
rows = (plan_row_factory("c0001", "R?", "R1"),)
issues = validate_plan(snapshot, rows)
assert any(issue.code == "occupied-output" for issue in issues)
def test_validation_allows_same_refdes_on_members_of_one_logical_component(
component_factory, snapshot_factory
):
first = component_factory(
bridge_id="u1a",
stable_uid="u1",
logical_group_key="u1",
refdes="U1",
)
second = component_factory(
bridge_id="u1b",
stable_uid="u1",
logical_group_key="u1",
refdes="U1",
)
issues = validate_plan(snapshot_factory(components=(first, second)), ())
assert not [issue for issue in issues if issue.code == "duplicate-source-refdes"]