"""Generated extraction fixtures and test-only inspection attestations; no providers."""
import hashlib
from uuid import uuid4
from app.modules.documents.extraction import PageBlockExtractor, ReviewApproval, REVIEW_CHECKS, ReviewRequired
from app.modules.documents.types import ExtractedBlock
from tests.support.formats import image_bytes, docx_bytes
def extract(content, profile, mime="text/markdown", *, vision=None, document_id=None, reviews=()):
return PageBlockExtractor(vision, reviews=reviews).extract(
document_id=document_id or uuid4(), title="Fixture",
original_sha256=hashlib.sha256(content).hexdigest(), content=content,
mime_type=mime, processing_profile=profile)
def inspected_extract(content, profile, mime, *, vision=None, document_id=None):
"""Automated fixture approvals only; not real/manual provider quality evidence."""
document_id = document_id or uuid4()
approvals = []
for _ in range(profile["extraction"]["max_pdf_pages"] + 2):
try:
return extract(content, profile, mime, vision=vision, document_id=document_id, reviews=approvals)
except ReviewRequired as request:
approvals.append(ReviewApproval(request.evidence_sha256, "fixture-only",
"Expected generated source checked", REVIEW_CHECKS))
raise AssertionError("Unbounded review loop")
def pdf_bytes(pages):
import pymupdf
with pymupdf.open() as doc:
for contents in pages:
page = doc.new_page(width=400, height=500)
for kind, value, x, y in contents:
if kind == "text":
page.insert_text((x, y), value)
elif kind == "image":
page.insert_image(pymupdf.Rect(x, y, x + 120, y + 100), stream=value)
return doc.tobytes()
def faq_docx():
def paragraph(value):
return "" + value + ""
rows = ["سؤال: ساعت کاری؟", "پاسخ: از 8 تا 13، پنجشنبه تا 12؛ تعطیلات استثنا.",
"سؤال: تاریخ؟", "پاسخ: 1403/01/02، نرخ 12.5% نیست."]
table = "" + "".join("" + paragraph(v) + "" for v in rows) + ""
xml = (""
+ paragraph("مقدمه ") + table + paragraph("پایان") + "")
return docx_bytes(document=xml.encode())
class FakeVision:
def __init__(self, *, warnings=(), actual_model="fixture-flash", text="تبصره: 12.5% در 1403/01/02؛ شامل استثنا نمیشود."):
self.calls = []
self.warnings, self.actual_model, self.text = warnings, actual_model, text
def transcribe(self, image, *, page, section, processing_profile):
self.calls.append((image, page, section))
group = section + "/group/1"
parent = ExtractedBlock(ordinal=0, block_id=group, text="عنوان اصلی\n",
source_locator=group, section=group, page=page, block_type="visual_group",
heading="عنوان اصلی", group_id=group, method="vision", actual_model=self.actual_model)
child = ExtractedBlock(ordinal=1, block_id=group + "/child/1", text=self.text + "\n",
source_locator=group + "/child/1", section=group, page=page,
parent_block_id=group, group_id=group, method="vision",
actual_model=self.actual_model, warnings=self.warnings)
return parent, child