feat(backend): deliver Phase 3 document and voice integration
Add authenticated upload, review, retry and knowledge-selection HTTP flows with bounded JSON processing and frozen OCR policy. Preserve Gemini/OpenAI provider selection and fenced lifecycle semantics; retain FAQ preambles and uncertain vision candidates, and expose opt-in uncalibrated voice transport diagnostics. Include frontend integration examples, aligned contracts and deterministic regressions. Validation: 1767 provider-free tests passed; all 404 PostgreSQL scenarios have passing evidence across the documented runs.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
"""Frozen OCR admission: automatic validation, legacy policy and non-OCR gates."""
|
||||
import hashlib
|
||||
from uuid import uuid4
|
||||
import pytest
|
||||
from app.modules.documents.extraction import PageBlockExtractor, ReviewRequired, ExtractionError
|
||||
from app.modules.documents.worker import validate_source
|
||||
from app.modules.documents.extraction_artifacts import ocr_review_required
|
||||
from app.modules.documents.review import prepare_chunks, FactualReviewRequired
|
||||
from tests.support.extraction import FakeVision, pdf_bytes
|
||||
from tests.support.formats import image_bytes
|
||||
|
||||
|
||||
def profile(settings, required):
|
||||
value = settings.model_copy(update={'ocr_review_required': required}).processing_profile()
|
||||
value['extraction']['vision'] = dict(provider='gemini', endpoint='https://test.invalid', model='fixture-flash', timeout_seconds=5)
|
||||
return value
|
||||
|
||||
|
||||
@pytest.mark.parametrize('kind', ['png', 'jpeg', 'scan'])
|
||||
@pytest.mark.parametrize('required', [True, False, None])
|
||||
def test_ocr_gate_and_worker_source_validation(settings, kind, required):
|
||||
frozen = profile(settings, required is not False)
|
||||
if required is None:
|
||||
frozen['extraction'].pop('ocr_review_required')
|
||||
content = image_bytes('JPEG' if kind == 'jpeg' else 'PNG')
|
||||
mime = 'image/jpeg' if kind == 'jpeg' else 'image/png'
|
||||
if kind == 'scan':
|
||||
content = pdf_bytes([[('image', content, 10, 20)]])
|
||||
mime = 'application/pdf'
|
||||
doc = dict(id=uuid4(), title='OCR', file_sha256=hashlib.sha256(content).hexdigest(), processing_profile=frozen, mime_type=mime)
|
||||
kwargs = dict(document_id=doc['id'], title=doc['title'], original_sha256=doc['file_sha256'], content=content, mime_type=mime, processing_profile=frozen)
|
||||
if required is not False:
|
||||
with pytest.raises(ReviewRequired):
|
||||
PageBlockExtractor(FakeVision()).extract_for_review(**kwargs)
|
||||
else:
|
||||
envelope = PageBlockExtractor(FakeVision()).extract_for_review(**kwargs)
|
||||
assert envelope.quality_decision == 'passed' and not envelope.review_annotations
|
||||
validate_source(envelope, doc, content)
|
||||
legacy = dict(frozen, extraction={k: v for k, v in frozen['extraction'].items() if k != 'ocr_review_required'})
|
||||
with pytest.raises(ExtractionError):
|
||||
validate_source(envelope, dict(doc, processing_profile=legacy), content)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('warning', ['Uncertain order', 'unreadable region', 'ناخوانا'])
|
||||
def test_disabling_supervisor_preserves_quality_failures(settings, warning):
|
||||
frozen = profile(settings, False)
|
||||
content = image_bytes('PNG')
|
||||
with pytest.raises(ReviewRequired):
|
||||
PageBlockExtractor(FakeVision(warnings=(warning,))).extract_for_review(document_id=uuid4(), title='OCR',
|
||||
original_sha256=hashlib.sha256(content).hexdigest(), content=content, mime_type='image/png', processing_profile=frozen)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('text,model', [('', 'fixture-flash'), ('\ufffd', 'fixture-flash'), ('[TODO]', 'fixture-flash'), ('Evidence', 'wrong-model')])
|
||||
def test_automatic_validation_is_not_waived(settings, text, model):
|
||||
content = image_bytes('PNG')
|
||||
with pytest.raises(ValueError):
|
||||
PageBlockExtractor(FakeVision(text=text, actual_model=model)).extract_for_review(document_id=uuid4(), title='OCR',
|
||||
original_sha256=hashlib.sha256(content).hexdigest(), content=content, mime_type='image/png', processing_profile=profile(settings, False))
|
||||
|
||||
|
||||
def test_direct_pdf_still_needs_review(settings):
|
||||
content = pdf_bytes([[('text', 'Article 1: fee is 5%', 10, 20)]])
|
||||
with pytest.raises(ReviewRequired):
|
||||
PageBlockExtractor().extract_for_review(document_id=uuid4(), title='PDF', original_sha256=hashlib.sha256(content).hexdigest(),
|
||||
content=content, mime_type='application/pdf', processing_profile=profile(settings, False))
|
||||
|
||||
|
||||
def test_factual_approval_independent(settings):
|
||||
frozen = profile(settings, False)
|
||||
content = b'Question: fee?\nAnswer: 5%\n'
|
||||
envelope = PageBlockExtractor().extract_for_review(document_id=uuid4(), title='FAQ', original_sha256=hashlib.sha256(content).hexdigest(),
|
||||
content=content, mime_type='text/markdown', processing_profile=frozen)
|
||||
with pytest.raises(FactualReviewRequired):
|
||||
prepare_chunks(envelope, frozen, {}, 'a' * 64)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('bad', [None, 'false', 0, 1, []])
|
||||
def test_invalid_snapshot_fails_closed(settings, bad):
|
||||
frozen = profile(settings, False); frozen['extraction']['ocr_review_required'] = bad
|
||||
with pytest.raises(ValueError):
|
||||
ocr_review_required(frozen)
|
||||
|
||||
|
||||
def test_setting_defaults_and_enqueue_snapshot(settings, monkeypatch):
|
||||
assert settings.ocr_review_required is True
|
||||
monkeypatch.setenv('INGEST_OCR_REVIEW_REQUIRED', 'false')
|
||||
values = settings.model_dump(); values.pop('ocr_review_required')
|
||||
configured = type(settings)(_env_file=None, **values)
|
||||
frozen = configured.processing_profile()
|
||||
monkeypatch.setenv('INGEST_OCR_REVIEW_REQUIRED', 'true')
|
||||
assert ocr_review_required(frozen) is False
|
||||
assert type(settings)(_env_file=None, **values).ocr_review_required is True
|
||||
Reference in New Issue
Block a user