fix: close adversarial review findings on document parsing

Malformed download URLs are rejected before the second request, error bodies are read only up to a bound, the telemetry summary keeps structural dispatch for duck-typed results, and the BMP magic now requires a valid DIB header. Docs and design are aligned with the implementation, and new test wording is neutral.
This commit is contained in:
2026-10-22 02:09:06 -04:00
parent bb621cad4d
commit cb1e08f1f3
10 changed files with 180 additions and 25 deletions
+16
View File
@@ -5,6 +5,7 @@ retry_exhausted/circuit_open/stalled 三组断言即设计 §6 ③ 的 G1 契约
"""
import asyncio
from types import SimpleNamespace
import httpx
import pytest
@@ -826,6 +827,21 @@ class TestDocumentTelemetry:
assert OcrClient._summarize(_LAYOUT_OK) == "<elements n=1 pages=1>"
assert OcrClient._summarize(_TEXT_OK) == "LINE-1"
def test_summary_keeps_structural_dispatch_for_duck_typed_results(self):
"""旧实现按结构分派(`hasattr(result, "text")`)。下游测试替身可能返回非库类型的鸭子对象,
改成 isinstance 会让它们在开启遥测后以原生 AttributeError 漏出——无登记的回归。"""
assert OcrClient._summarize(SimpleNamespace(text="hello", raw={})) == "hello"
assert (
OcrClient._summarize(SimpleNamespace(elements=[1, 2], page_sizes=[(1.0, 1.0)]))
== "<elements n=2 pages=1>"
)
assert (
OcrClient._summarize(
SimpleNamespace(elements=[1], page_sizes=[(1.0, 1.0)], markdown="abc")
)
== "<document elements=1 pages=1 md_chars=3>"
)
async def test_failed_attempt_row_carries_the_class_prefix(self):
recorder = _MemoryRecorder()
client, _, _ = _client([_src()], [ResultInvalidError("bad package")], telemetry=recorder)