feat: add OCR result and transport types

This commit is contained in:
2026-07-21 22:24:51 -04:00
parent 88127a6e8b
commit 2fdd14f64b
2 changed files with 120 additions and 0 deletions
+58
View File
@@ -154,3 +154,61 @@ class TestAuxTypes:
raw={"id": "x"},
)
assert s.raw["id"] == "x"
class TestOcrTypes:
"""M3 OCR 五类型(设计 §3.1): 可构造、frozen、全必填无默认。"""
def _element(self):
from polygateway.types import OcrLayoutElement
return OcrLayoutElement(type="table", bbox=(41.0, 48.0, 218.0, 282.0), page_index=0)
def test_layout_element_fields(self):
el = self._element()
assert el.type == "table" and el.bbox == (41.0, 48.0, 218.0, 282.0) and el.page_index == 0
with pytest.raises(dataclasses.FrozenInstanceError):
el.type = "text"
def test_text_result_provenance(self):
from polygateway.types import OcrTextResult
r = OcrTextResult(
text="TOSHIBA",
source_name="monkey_1",
usage=Usage(0, 0),
latency_ms=3200,
call_id="cid",
raw={"success": True},
)
assert r.text == "TOSHIBA" and r.usage.prompt_tokens == 0
with pytest.raises(dataclasses.FrozenInstanceError):
r.text = "x"
def test_layout_result_provenance(self):
from polygateway.types import OcrLayoutResult
r = OcrLayoutResult(
elements=[self._element()],
page_sizes=[(759.0, 540.0)],
source_name="monkey_1",
usage=Usage(0, 0),
latency_ms=8000,
call_id="cid",
raw={"has_table": True},
)
assert r.elements[0].type == "table" and r.page_sizes[0] == (759.0, 540.0)
def test_transport_results(self):
from polygateway.types import OcrLayoutTransportResult, OcrTextTransportResult
t = OcrTextTransportResult(text="", raw={})
assert t.text == "" # 空串 = 合法"无文字"
lt = OcrLayoutTransportResult(elements=[], page_sizes=[(1.0, 1.0)], raw={})
assert lt.elements == [] # 空 elements = 合法"无元素"
def test_all_fields_required(self):
from polygateway.types import OcrTextResult
with pytest.raises(TypeError):
OcrTextResult(text="x") # 溯源件不可省略