93 lines
3.3 KiB
Python
93 lines
3.3 KiB
Python
"""`dwg_review.phases.planning` 单元测试。"""
|
|||
|
|
|
||
|
|
from dwg_review.phases.planning import _sort_frames, plan
|
||
|
|
from tests.fakes import FakeMcpClient, frame, make_ctx, ok_text
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# 阅读顺序排序
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
def test_sort_frames_top_to_bottom_then_left_to_right():
|
||
|
|
# bbox = [min_x, min_y, max_x, max_y]
|
||
|
|
top_left = frame([0, 100, 10, 200])
|
||
|
|
top_right = frame([50, 100, 60, 200])
|
||
|
|
bottom_left = frame([0, 0, 10, 50])
|
||
|
|
bottom_right = frame([50, 0, 60, 50])
|
||
|
|
|
||
|
|
ordered = _sort_frames([bottom_right, top_left, bottom_left, top_right])
|
||
|
|
assert ordered == [top_left, top_right, bottom_left, bottom_right]
|
||
|
|
|
||
|
|
|
||
|
|
def test_sort_frames_same_row_by_x():
|
||
|
|
a = frame([90, 0, 100, 10])
|
||
|
|
b = frame([10, 0, 20, 10])
|
||
|
|
assert _sort_frames([a, b]) == [b, a]
|
||
|
|
|
||
|
|
|
||
|
|
def test_sort_frames_handles_missing_bbox():
|
||
|
|
with_bbox = frame([0, 0, 10, 10])
|
||
|
|
without = {"kind": "x"} # 缺 bbox -> 视作全 0
|
||
|
|
ordered = _sort_frames([without, with_bbox])
|
||
|
|
assert with_bbox in ordered and without in ordered
|
||
|
|
|
||
|
|
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
# plan()
|
||
|
|
# ---------------------------------------------------------------------------
|
||
|
|
def build_plan_client(frames):
|
||
|
|
return FakeMcpClient(
|
||
|
|
responses={
|
||
|
|
"get_drawing_info": ok_text({"units": "mm"}),
|
||
|
|
"get_layers": ok_text({"layers": [{"name": "0"}]}),
|
||
|
|
"get_layouts": ok_text({"layouts": [{"name": "Model"}]}),
|
||
|
|
"detect_drawing_frames": ok_text({"items": frames}),
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_returns_all_sections():
|
||
|
|
frames = [frame([0, 0, 10, 10]), frame([0, 20, 10, 30])]
|
||
|
|
ctx = make_ctx(client=build_plan_client(frames))
|
||
|
|
result = plan(ctx)
|
||
|
|
assert result["drawing_info"] == {"units": "mm"}
|
||
|
|
assert result["layers"] == {"layers": [{"name": "0"}]}
|
||
|
|
assert result["layouts"] == {"layouts": [{"name": "Model"}]}
|
||
|
|
assert len(result["frames"]) == 2
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_sorts_frames():
|
||
|
|
lower = frame([0, 0, 10, 10])
|
||
|
|
upper = frame([0, 50, 10, 60])
|
||
|
|
ctx = make_ctx(client=build_plan_client([lower, upper]))
|
||
|
|
assert plan(ctx)["frames"] == [upper, lower]
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_respects_max_frames():
|
||
|
|
frames = [frame([0, i * 10, 10, i * 10 + 5]) for i in range(5)]
|
||
|
|
ctx = make_ctx(client=build_plan_client(frames), max_frames=2)
|
||
|
|
assert len(plan(ctx)["frames"]) == 2
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_calls_with_expected_arguments():
|
||
|
|
ctx = make_ctx(client=build_plan_client([]))
|
||
|
|
plan(ctx)
|
||
|
|
client = ctx.client
|
||
|
|
assert client.arguments_of("get_layers") == {"count_entities": True}
|
||
|
|
assert client.arguments_of("detect_drawing_frames") == {"min_confidence": 0.3}
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_tolerates_non_dict_frames_data():
|
||
|
|
client = FakeMcpClient(
|
||
|
|
responses={
|
||
|
|
"get_drawing_info": ok_text({}),
|
||
|
|
"get_layers": ok_text({}),
|
||
|
|
"get_layouts": ok_text({}),
|
||
|
|
"detect_drawing_frames": ok_text("oops"), # 非 dict
|
||
|
|
}
|
||
|
|
)
|
||
|
|
assert plan(make_ctx(client=client))["frames"] == []
|
||
|
|
|
||
|
|
|
||
|
|
def test_plan_tolerates_missing_items_key():
|
||
|
|
client = FakeMcpClient(responses={"detect_drawing_frames": ok_text({"other": 1})})
|
||
|
|
assert plan(make_ctx(client=client))["frames"] == []
|