背景:逐图框审查固定「整体 + 2x2 象限」截图,大图幅(A0/A1)读不清细节。 改为按**实体分布**自适应决定截几张、截哪里。 - 新增 dwg_review/tiling.py(纯函数): SHEET_SIZES_MM / frame_world_size / frame_scale(sheet_scale>0 用之,否则由 bbox/纸张反算,否则 default)/ effective_px(与 downsample 同规则)/ world_per_px / is_text_type(标准名 + 自定义类名含 TEXT/LABEL/DIM/NOTE/TAG/ ANNO/TITLE)/ cluster_text_flags / tiles_for_box / subdivide / plan_regions (文字簇优先、空白区不出图、max_tiles 截断、窗口夹到图框范围) - 新增 verify_vlm_resolution_offline.py:PIL 合成类图纸图(精确像素字高 + 可控线间距/线宽/虚线)→ 问 VLM 强约束 JSON → 定 R_text / R_line。 实测:R_text=12px(黑底;白底 10px);R_line=6px。 另:实测该模型**无法可靠计数**(6 条线在 2~10px 间距下答 4/10/4/5/4/4), 故线条判据改用「1 条 vs 2 条」二选一。 - config 新增 TILING_ENABLED / TEXT_HEIGHT_MM / TEXT_READABLE_PX=12 / LINE_READABLE_PX=6 / TILE_MAX_COUNT / TILE_OVERLAP / HEATMAP_CELL_DIV / HEATMAP_CLUSTER_DILATE / HEATMAP_MAX_AREA_FRAC;EVIDENCE_DETAIL_TILES 与 EVIDENCE_MAX_IMAGES 保留作回退路径参数 - llm_client 新增 image_size_b64(测量绘图区实际像素,决定单张覆盖范围) - phases/evidence.capture_images 支持瓦片规划:先截整体图并测像素 → plan_regions 成功用规划窗口,否则退回 sub_windows(行为零变化); 末尾始终复位整体视口。collect_frame 传实体/文字 bbox;任一信封 total>returned(截断)→ 退回固定策略,避免误删有内容的瓦片 - 测试:tests/test_tiling.py(28 项)、tests/test_evidence.py 新增规划路径与 降级用例、test_config / test_llm_client 补新常量与 image_size_b64; 全量 283 项全绿
294 lines
11 KiB
Python
294 lines
11 KiB
Python
"""`dwg_review.phases.evidence` 单元测试。"""
|
|
|
|
import base64
|
|
import io
|
|
|
|
import pytest
|
|
|
|
from dwg_review.phases.evidence import (
|
|
_is_truncated,
|
|
_plan_tiles,
|
|
_text_boxes,
|
|
bbox_to_window,
|
|
collect_frame,
|
|
)
|
|
from tests.fakes import FakeMcpClient, frame, make_ctx, ok_image, ok_text
|
|
|
|
|
|
def test_bbox_to_window_mapping():
|
|
assert bbox_to_window([1, 2, 3, 4]) == {
|
|
"min_x": 1.0,
|
|
"min_y": 2.0,
|
|
"max_x": 3.0,
|
|
"max_y": 4.0,
|
|
}
|
|
|
|
|
|
def test_bbox_to_window_non_numeric_known_gap():
|
|
"""已知瑕疵:4 元素但含非数值的 bbox 会抛 ValueError,上游 collect_frame 只判长度。"""
|
|
with pytest.raises(ValueError):
|
|
bbox_to_window([0, "a", 10, 10])
|
|
|
|
|
|
def test_collect_frame_returns_none_for_missing_bbox():
|
|
ctx = make_ctx(client=FakeMcpClient())
|
|
assert collect_frame(ctx, 1, {}) is None
|
|
assert collect_frame(ctx, 1, {"bbox": None}) is None
|
|
|
|
|
|
def test_collect_frame_returns_none_for_bad_bbox_length():
|
|
ctx = make_ctx(client=FakeMcpClient())
|
|
assert collect_frame(ctx, 1, {"bbox": [0, 0, 10]}) is None
|
|
|
|
|
|
def build_evidence_client(image=ok_image("QUJD")):
|
|
return FakeMcpClient(
|
|
responses={
|
|
"query_entities": ok_text({"total": 5, "items": [{"handle": "A"}]}),
|
|
"get_text_content": ok_text({"total": 1, "items": [{"text": "你好"}]}),
|
|
"get_dimensions": ok_text({"total": 2, "items": []}),
|
|
"get_block_references": ok_text({"total": 0, "items": []}),
|
|
"get_polylines_info": ok_text({"total": 3, "items": []}),
|
|
"get_viewport_screenshot": image,
|
|
}
|
|
)
|
|
|
|
|
|
def test_collect_frame_assembles_evidence():
|
|
ctx = make_ctx(client=build_evidence_client())
|
|
ev = collect_frame(ctx, 2, frame([0, 0, 100, 50]))
|
|
assert ev["index"] == 2
|
|
assert ev["window"] == {"min_x": 0.0, "min_y": 0.0, "max_x": 100.0, "max_y": 50.0}
|
|
assert ev["entities"]["total"] == 5
|
|
assert ev["texts"]["total"] == 1
|
|
assert ev["dimensions"]["total"] == 2
|
|
assert ev["blocks"]["total"] == 0
|
|
assert ev["polylines"]["total"] == 3
|
|
assert ev["source_type"] == "cad"
|
|
assert ev["images"] == [{"b64": "QUJD", "mime": "image/png", "label": "overall"}]
|
|
|
|
|
|
def test_collect_frame_calls_expected_tools():
|
|
ctx = make_ctx(client=build_evidence_client(), frame_limit=99)
|
|
collect_frame(ctx, 1, frame([1, 2, 3, 4]))
|
|
client = ctx.client
|
|
|
|
win = {"min_x": 1.0, "min_y": 2.0, "max_x": 3.0, "max_y": 4.0}
|
|
assert client.arguments_of("zoom_to_window") == dict(win, margin=0.02)
|
|
for name in (
|
|
"query_entities",
|
|
"get_text_content",
|
|
"get_dimensions",
|
|
"get_block_references",
|
|
"get_polylines_info",
|
|
):
|
|
assert client.arguments_of(name) == {"window": win, "limit": 99}
|
|
assert client.arguments_of("get_viewport_screenshot") == {}
|
|
|
|
|
|
def test_collect_frame_without_image():
|
|
ctx = make_ctx(client=build_evidence_client(image=ok_text({})))
|
|
ev = collect_frame(ctx, 1, frame([0, 0, 10, 10]))
|
|
assert ev["images"] == []
|
|
|
|
|
|
def test_collect_frame_tolerates_empty_structured_data():
|
|
ctx = make_ctx(client=FakeMcpClient()) # 全部默认 ok_text({})
|
|
ev = collect_frame(ctx, 1, frame([0, 0, 10, 10]))
|
|
assert ev["entities"] == {}
|
|
assert ev["images"] == []
|
|
|
|
|
|
def test_sub_windows_2x2():
|
|
from dwg_review.phases.evidence import sub_windows
|
|
|
|
win = {"min_x": 0.0, "min_y": 0.0, "max_x": 10.0, "max_y": 10.0}
|
|
subs = sub_windows(win, 4)
|
|
assert len(subs) == 4
|
|
# 行优先:左上 → 右上 → 左下 → 右下
|
|
assert subs[0] == {"min_x": 0.0, "min_y": 5.0, "max_x": 5.0, "max_y": 10.0}
|
|
assert subs[1] == {"min_x": 5.0, "min_y": 5.0, "max_x": 10.0, "max_y": 10.0}
|
|
assert subs[2] == {"min_x": 0.0, "min_y": 0.0, "max_x": 5.0, "max_y": 5.0}
|
|
assert subs[3] == {"min_x": 5.0, "min_y": 0.0, "max_x": 10.0, "max_y": 5.0}
|
|
|
|
|
|
def test_sub_windows_disabled():
|
|
from dwg_review.phases.evidence import sub_windows
|
|
|
|
win = {"min_x": 0.0, "min_y": 0.0, "max_x": 10.0, "max_y": 10.0}
|
|
assert sub_windows(win, 0) == []
|
|
assert sub_windows(win, 1) == []
|
|
|
|
|
|
def test_collect_frame_captures_multiple_images(monkeypatch):
|
|
from dwg_review import config
|
|
from dwg_review.phases.evidence import collect_frame
|
|
|
|
monkeypatch.setattr(config, "EVIDENCE_DETAIL_TILES", 4)
|
|
ctx = make_ctx(client=build_evidence_client())
|
|
ev = collect_frame(ctx, 1, frame([0, 0, 100, 50]))
|
|
assert [img["label"] for img in ev["images"]] == [
|
|
"overall",
|
|
"tile-1",
|
|
"tile-2",
|
|
"tile-3",
|
|
"tile-4",
|
|
]
|
|
|
|
|
|
def test_collect_dispatches_cad_source():
|
|
from dwg_review.phases.evidence import collect
|
|
|
|
ctx = make_ctx(client=build_evidence_client())
|
|
ev = collect(ctx, 1, frame([0, 0, 10, 10]))
|
|
assert ev is not None
|
|
assert ev["source_type"] == "cad"
|
|
|
|
|
|
def test_collect_returns_none_for_unsupported_source():
|
|
from dwg_review.phases.evidence import collect
|
|
|
|
ctx = make_ctx(client=FakeMcpClient())
|
|
assert collect(ctx, 1, {"source_type": "pdf", "bbox": [0, 0, 1, 1]}) is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 自适应瓦片规划
|
|
# ---------------------------------------------------------------------------
|
|
@pytest.fixture
|
|
def tiling_env(monkeypatch):
|
|
"""把瓦片相关配置钉死,使用例不受外部环境变量影响。"""
|
|
from dwg_review import config
|
|
|
|
for name, value in {
|
|
"TILING_ENABLED": True,
|
|
"TEXT_HEIGHT_MM": 2.0,
|
|
"TEXT_READABLE_PX": 12,
|
|
"LINE_READABLE_PX": 6,
|
|
"TILE_MAX_COUNT": 36,
|
|
"TILE_OVERLAP": 0.0,
|
|
"HEATMAP_CELL_DIV": 32,
|
|
"HEATMAP_CLUSTER_DILATE": 0,
|
|
"HEATMAP_MAX_AREA_FRAC": 0.5,
|
|
"IMAGE_MAX_EDGE": 1280,
|
|
"EVIDENCE_MAX_IMAGES": 5,
|
|
"EVIDENCE_DETAIL_TILES": 4,
|
|
}.items():
|
|
monkeypatch.setattr(config, name, value)
|
|
return config
|
|
|
|
|
|
def _png_b64(w=2000, h=1414):
|
|
from PIL import Image
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (w, h), "black").save(buf, format="PNG")
|
|
return base64.b64encode(buf.getvalue()).decode("ascii")
|
|
|
|
|
|
def _tiling_client(b64, entity_type="LINE"):
|
|
return FakeMcpClient(
|
|
responses={
|
|
"query_entities": ok_text(
|
|
{"total": 1, "items": [{"handle": "A", "type": entity_type,
|
|
"bbox": [0, 0, 60000, 20000]}]}
|
|
),
|
|
"get_text_content": ok_text({"total": 0, "items": []}),
|
|
"get_dimensions": ok_text({"total": 0, "items": []}),
|
|
"get_block_references": ok_text({"total": 0, "items": []}),
|
|
"get_polylines_info": ok_text({"total": 0, "items": []}),
|
|
"get_viewport_screenshot": ok_image(b64),
|
|
}
|
|
)
|
|
|
|
|
|
def test_is_truncated():
|
|
assert _is_truncated({"total": 5, "items": [1]}) is True
|
|
assert _is_truncated({"total": 1, "items": [1]}) is False
|
|
assert _is_truncated({"items": [1, 2]}) is False
|
|
assert _is_truncated(None, {}, {"total": 3, "items": []}) is True
|
|
assert _is_truncated() is False
|
|
|
|
|
|
def test_text_boxes_includes_std_and_custom():
|
|
entities = {"items": [
|
|
{"type": "LINE", "bbox": [0, 0, 1, 1]},
|
|
{"type": "AECDLABEL", "bbox": [2, 2, 3, 3]},
|
|
{"type": "TEXT", "bbox": [4, 4, 5, 5]},
|
|
]}
|
|
texts = {"items": [{"bbox": [6, 6, 7, 7]}]}
|
|
dimensions = {"items": [{"bbox": [8, 8, 9, 9]}]}
|
|
boxes = _text_boxes(entities, texts, dimensions)
|
|
for expected in ((2, 2, 3, 3), (4, 4, 5, 5), (6, 6, 7, 7), (8, 8, 9, 9)):
|
|
assert tuple(float(v) for v in expected) in boxes
|
|
assert (0.0, 0.0, 1.0, 1.0) not in boxes # 普通线不算文字
|
|
|
|
|
|
def test_plan_tiles_degrades(tiling_env):
|
|
win = {"min_x": 0.0, "min_y": 0.0, "max_x": 100.0, "max_y": 50.0}
|
|
f = frame([0, 0, 100, 50], sheet_size="A0", sheet_scale=100.0)
|
|
boxes = [(0.0, 0.0, 10.0, 10.0)]
|
|
assert _plan_tiles(win, f, boxes, [], False, (100, 50)) != [] # 正常路径非空
|
|
assert _plan_tiles(win, None, boxes, [], False, (100, 50)) == [] # 缺图框
|
|
assert _plan_tiles(win, f, [], [], False, (100, 50)) == [] # 无实体
|
|
assert _plan_tiles(win, f, boxes, [], False, None) == [] # 无像素
|
|
assert _plan_tiles(win, f, boxes, [], True, (100, 50)) == [] # 数据截断
|
|
# 比例未知(sheet_scale=0 且 sheet_size 不在纸张表)→ 无法规划
|
|
unknown = frame([0, 0, 100, 50], sheet_size="ZZ", sheet_scale=0.0)
|
|
assert _plan_tiles(win, unknown, boxes, [], False, (100, 50)) == []
|
|
|
|
|
|
def test_plan_tiles_disabled_by_config(tiling_env, monkeypatch):
|
|
tiling_env.TILING_ENABLED = False
|
|
win = {"min_x": 0.0, "min_y": 0.0, "max_x": 100.0, "max_y": 50.0}
|
|
f = frame([0, 0, 100, 50], sheet_size="A0", sheet_scale=100.0)
|
|
assert _plan_tiles(win, f, [(0.0, 0.0, 10.0, 10.0)], [], False, (100, 50)) == []
|
|
|
|
|
|
def test_capture_images_tiles_line_cluster(tiling_env):
|
|
"""线条簇:单张可覆盖 → 2 张瓦片(行优先)。"""
|
|
pytest.importorskip("PIL")
|
|
ctx = make_ctx(client=_tiling_client(_png_b64()))
|
|
ev = collect_frame(
|
|
ctx, 1, frame([0, 0, 120000, 40000], sheet_size="A0", sheet_scale=100.0)
|
|
)
|
|
assert [i["label"] for i in ev["images"]] == ["overall", "tile-1", "tile-2"]
|
|
|
|
|
|
def test_capture_images_tiles_text_cluster_is_finer(tiling_env):
|
|
"""文字簇用更细尺度 → 瓦片更多,并受 EVIDENCE_MAX_IMAGES 截断。"""
|
|
pytest.importorskip("PIL")
|
|
ctx = make_ctx(client=_tiling_client(_png_b64(), entity_type="AECDLABEL"))
|
|
ev = collect_frame(
|
|
ctx, 1, frame([0, 0, 120000, 40000], sheet_size="A0", sheet_scale=100.0)
|
|
)
|
|
labels = [i["label"] for i in ev["images"]]
|
|
assert labels == ["overall", "tile-1", "tile-2", "tile-3", "tile-4"] # 上限 5 张
|
|
|
|
|
|
def test_capture_images_falls_back_without_scale(tiling_env):
|
|
"""缺 sheet_size/sheet_scale → 退回固定 2x2 象限。"""
|
|
pytest.importorskip("PIL")
|
|
ctx = make_ctx(client=_tiling_client(_png_b64()))
|
|
ev = collect_frame(ctx, 1, frame([0, 0, 120000, 40000], sheet_size="ZZ"))
|
|
assert [i["label"] for i in ev["images"]] == [
|
|
"overall", "tile-1", "tile-2", "tile-3", "tile-4",
|
|
]
|
|
|
|
|
|
def test_capture_images_falls_back_when_truncated(tiling_env):
|
|
"""实体被截断 → 禁用内容过滤,退回固定策略。"""
|
|
pytest.importorskip("PIL")
|
|
client = _tiling_client(_png_b64())
|
|
client.responses["query_entities"] = ok_text(
|
|
{"total": 500, "items": [{"handle": "A", "type": "LINE",
|
|
"bbox": [0, 0, 60000, 20000]}]}
|
|
)
|
|
ctx = make_ctx(client=client)
|
|
ev = collect_frame(
|
|
ctx, 1, frame([0, 0, 120000, 40000], sheet_size="A0", sheet_scale=100.0)
|
|
)
|
|
assert [i["label"] for i in ev["images"]] == [
|
|
"overall", "tile-1", "tile-2", "tile-3", "tile-4",
|
|
]
|