- checks.py:确定性检查(find_zero_length / find_duplicate_entities 传 window + 纯 Python 派生空文字、未闭合多段线)。问题带 source=deterministic,handle 来自 工具/数据本身,不依赖 LLM 回忆 - findings.py:解析 LLM 的 JSON 结论 + handle 交叉校验(引用未知 handle 时保留问题、 剥掉 handle、verified=False)+ findings 装配 - config.py:新增 DETERMINISTIC_CHECKS / VALIDATE_HANDLES / CHECK_UNCLOSED_POLYLINES 开关与 ZERO_LENGTH_TOLERANCE / DUP_TOLERANCE / DUP_GROUP_LIMIT 实机验证:find_zero_length / find_duplicate_entities 的 window 参数在基座上生效 (schema 未声明,C++ 侧 parseCommonQuery 实际支持)。
105 lines
3.7 KiB
Python
105 lines
3.7 KiB
Python
"""`dwg_review.config` 单元测试(只验证纯函数与常量类型,不依赖真实环境变量)。"""
|
|
|
|
import os
|
|
|
|
from dwg_review import config
|
|
|
|
|
|
def test_resolve_path_relative_uses_project_root():
|
|
assert config._resolve_path("reports") == config.PROJECT_ROOT / "reports"
|
|
|
|
|
|
def test_resolve_path_absolute_unchanged(tmp_path):
|
|
assert config._resolve_path(str(tmp_path)) == tmp_path
|
|
|
|
|
|
def test_project_root_is_repo_root():
|
|
assert (config.PROJECT_ROOT / "dwg_review" / "config.py").is_file()
|
|
|
|
|
|
def test_read_env_prefers_environ(monkeypatch):
|
|
monkeypatch.setenv("CAD_TEST_ENV_PROBE", "hello")
|
|
assert config._read_env("CAD_TEST_ENV_PROBE") == "hello"
|
|
|
|
|
|
def test_read_env_missing_returns_empty(monkeypatch):
|
|
monkeypatch.delenv("CAD_TEST_DEFINITELY_MISSING_XYZ", raising=False)
|
|
assert config._read_env("CAD_TEST_DEFINITELY_MISSING_XYZ") == ""
|
|
|
|
|
|
def test_read_bool_defaults_when_unset(monkeypatch):
|
|
monkeypatch.delenv("CAD_TEST_BOOL_UNSET", raising=False)
|
|
assert config._read_bool("CAD_TEST_BOOL_UNSET", True) is True
|
|
assert config._read_bool("CAD_TEST_BOOL_UNSET", False) is False
|
|
|
|
|
|
def test_read_bool_empty_string_uses_default(monkeypatch):
|
|
monkeypatch.setenv("CAD_TEST_BOOL_EMPTY", " ")
|
|
assert config._read_bool("CAD_TEST_BOOL_EMPTY", True) is True
|
|
|
|
|
|
def test_read_bool_truthy_and_falsy(monkeypatch):
|
|
for raw in ("1", "true", "TRUE", "Yes", "on"):
|
|
monkeypatch.setenv("CAD_TEST_BOOL", raw)
|
|
assert config._read_bool("CAD_TEST_BOOL", False) is True
|
|
for raw in ("0", "false", "no", "off", "nope"):
|
|
monkeypatch.setenv("CAD_TEST_BOOL", raw)
|
|
assert config._read_bool("CAD_TEST_BOOL", True) is False
|
|
|
|
|
|
def test_has_api_key_reflects_module_value(monkeypatch):
|
|
monkeypatch.setattr(config, "API_KEY", "")
|
|
assert config.has_api_key() is False
|
|
monkeypatch.setattr(config, "API_KEY", "sk-x")
|
|
assert config.has_api_key() is True
|
|
|
|
|
|
def test_has_api_key_ignores_whitespace(monkeypatch):
|
|
monkeypatch.setattr(config, "API_KEY", " ")
|
|
assert config.has_api_key() is False
|
|
|
|
|
|
def test_constants_have_expected_types():
|
|
assert isinstance(config.HOST, str)
|
|
assert isinstance(config.PORT, int)
|
|
assert isinstance(config.CALL_TIMEOUT, float)
|
|
assert isinstance(config.CALL_RETRIES, int)
|
|
assert isinstance(config.MAX_FRAME_BYTES, int)
|
|
assert isinstance(config.IMAGE_MAX_EDGE, int)
|
|
assert isinstance(config.IMAGE_FORMAT, str)
|
|
assert isinstance(config.CHECKLIST_PATH, type(config.PROJECT_ROOT))
|
|
|
|
|
|
def test_credibility_constants_have_expected_types():
|
|
assert isinstance(config.DETERMINISTIC_CHECKS, bool)
|
|
assert isinstance(config.VALIDATE_HANDLES, bool)
|
|
assert isinstance(config.CHECK_UNCLOSED_POLYLINES, bool)
|
|
assert isinstance(config.ZERO_LENGTH_TOLERANCE, float)
|
|
assert isinstance(config.DUP_TOLERANCE, float)
|
|
assert isinstance(config.DUP_GROUP_LIMIT, int)
|
|
|
|
|
|
def test_credibility_defaults_are_on():
|
|
# 默认开启,保证默认拿到可信度收益
|
|
assert config.DETERMINISTIC_CHECKS is True
|
|
assert config.VALIDATE_HANDLES is True
|
|
assert config.CHECK_UNCLOSED_POLYLINES is True
|
|
|
|
|
|
def test_checklist_path_resolves_relative_to_root():
|
|
assert config.CHECKLIST_PATH.name == "review_checklist.json"
|
|
assert config.CHECKLIST_PATH.parent == config.PROJECT_ROOT
|
|
|
|
|
|
def test_port_default_matches_baseline():
|
|
# 基座固定监听 127.0.0.1:8080;若此值被改动,请同步更新基座与文档。
|
|
assert config.HOST == "127.0.0.1"
|
|
assert config.PORT == 8080
|
|
|
|
|
|
def test_os_name_guard_in_read_env(monkeypatch):
|
|
# 非 Windows 平台不应触碰注册表
|
|
monkeypatch.setattr(os, "name", "posix")
|
|
monkeypatch.delenv("CAD_TEST_ENV_PROBE2", raising=False)
|
|
assert config._read_env("CAD_TEST_ENV_PROBE2") == ""
|