from dx_productivity.features.assembly_refdes.planner import ( ComponentFootprint, DeletePlan, FabricationText, MovePlan, plan_assembly_refdes, render_plan, ) def _text(index, **overrides): defaults = dict( index=index, text_type=1, side=1, is_assembly=True, text_string="C1", orientation=0.0, component_name="C1", ) defaults.update(overrides) return FabricationText(**defaults) def test_refdes_moves_to_component_origin(): texts = [_text(0)] footprints = {"C1": ComponentFootprint("C1", 10.0, 20.0, 0.5, 0.3)} plans, processed = plan_assembly_refdes(texts, footprints) assert processed == 1 plan = plans[0] assert isinstance(plan, MovePlan) assert (plan.x, plan.y) == (10.0, 20.0) assert plan.rotation == 0 assert plan.height == 0.3 # within [0.1, 0.8] keeps the outline height def test_small_and_large_heights_clamp_to_max(): texts = [_text(0), _text(1, text_string="R10000", component_name="R1")] footprints = { "C1": ComponentFootprint("C1", 0.0, 0.0, 0.5, 0.05), "R1": ComponentFootprint("R1", 0.0, 0.0, 0.5, 2.0), } plans, _ = plan_assembly_refdes(texts, footprints) assert all(isinstance(p, MovePlan) and p.height == 0.8 for p in plans) def test_non_refdes_fab_text_is_deleted(): texts = [_text(0, text_type=0)] footprints = {"C1": ComponentFootprint("C1", 0, 0, 1, 1)} plans, processed = plan_assembly_refdes(texts, footprints) assert processed == 0 assert isinstance(plans[0], DeletePlan) def test_board_level_text_without_component_is_untouched(): texts = [_text(0, component_name="")] plans, _ = plan_assembly_refdes(texts, {}) assert plans == () def test_bottom_side_vertical_text_gets_270(): texts = [_text(0, orientation=270, side=0)] footprints = {"C1": ComponentFootprint("C1", 1, 2, 0.4, 0.5)} plans, _ = plan_assembly_refdes(texts, footprints) assert plans[0].rotation == 270 assert plans[0].height == 0.4 # x half span def test_diagonal_uses_the_legacy_factor(): texts = [_text(0, orientation=45)] footprints = {"C1": ComponentFootprint("C1", 0, 0, 1.0, 1.0)} plans, _ = plan_assembly_refdes(texts, footprints) # legacy: full span * 0.54 / 2 = half span * 0.54 assert plans[0].height == 0.54 def test_steep_angles_flip_orientation(): texts = [ _text(0, orientation=135, component_name="C1"), _text(1, orientation=15, component_name="C2"), ] footprints = { "C1": ComponentFootprint("C1", 0, 0, 0.5, 0.5), "C2": ComponentFootprint("C2", 0, 0, 0.5, 0.5), } plans, _ = plan_assembly_refdes(texts, footprints) assert plans[0].rotation == 315 # 135 branch assert plans[0].height == 0.27 # half span * 0.54 assert plans[1].rotation == 15 # else branch keeps ori assert plans[1].height == 0.4 # generic branches force 0.4 def test_long_refdes_gets_narrow_font_and_fixed_height(): texts = [_text(0, text_string="R10000")] footprints = {"C1": ComponentFootprint("C1", 0, 0, 0.5, 0.5)} plans, _ = plan_assembly_refdes(texts, footprints) assert plans[0].font == "Arial Narrow" def test_render_plan_lines(): texts = [_text(0), _text(1, text_type=0)] footprints = {"C1": ComponentFootprint("C1", 10.0, 20.0, 0.5, 0.3)} plans, _ = plan_assembly_refdes(texts, footprints) rendered = render_plan(plans) assert rendered.startswith("MOVE|0|10.0000|20.0000|0.00|0.3000|Arial\n") assert "DELETE|1\n" in rendered