OCR is the paid, opt-in tier (DOCUMENT_OCR_ENABLED, default off). The per-tier
escalation gate already declines to hop to OCR when it's disabled (the pre-OCR
tier is terminal — no surprise cost), but that left operators blind to how much
OCR demand exists.
evaluate_escalation now returns a structured EscalationDecision:
- "hop" — a higher tier can run; the caller raises EscalateError (queue-hop).
- "suppressed" — the ideal next tier (e.g. ocr) exists but is DISABLED; the caller
indexes the current tier's output as terminal and records the
would-be hop on the new astrolabe_document_escalation_suppressed_total
{from_tier,to_tier,reason} counter instead of hopping.
- None — index as-is (good text, or no such tier at all).
So with OCR off, escalation_suppressed_total{to_tier="ocr"} is the latent OCR
demand an operator weighs before enabling OCR; enabling it converts these into
real document_escalation_total{to_tier="ocr"} hops. next_available_tier gains an
ignore_enabled flag to compute the *ideal* (enabled-gate-ignored) target.
Tests: registry suppressed vs hop vs terminal (incl. structured-hop-not-suppressed
when OCR off but structured available); _parse_pdf_tier records suppressed +
indexes without raising.
Deck #324 (parent #323).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
100 lines
3.8 KiB
Python
100 lines
3.8 KiB
Python
"""Unit tests for the per-tier PDF parse + escalation gate (Deck #323/#324).
|
|
|
|
``processor._parse_pdf_tier`` runs one tier and either indexes the result,
|
|
raises ``EscalateError`` (a real queue-hop), or — when the ideal next tier is
|
|
disabled (OCR off) — records a suppressed escalation and indexes as terminal.
|
|
These exercise the decision without standing up the full ingest pipeline.
|
|
"""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from nextcloud_mcp_server.document_processors.base import ProcessingResult
|
|
from nextcloud_mcp_server.document_processors.escalation import (
|
|
EscalateError,
|
|
EscalationDecision,
|
|
)
|
|
from nextcloud_mcp_server.vector import processor
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
def _registry(result: ProcessingResult, decision):
|
|
reg = MagicMock()
|
|
reg.process_tier = AsyncMock(return_value=result)
|
|
reg.evaluate_escalation = MagicMock(return_value=decision)
|
|
return reg
|
|
|
|
|
|
async def test_good_parse_returns_result(monkeypatch):
|
|
rec = MagicMock()
|
|
sup = MagicMock()
|
|
monkeypatch.setattr(processor, "record_document_escalation", rec)
|
|
monkeypatch.setattr(processor, "record_document_escalation_suppressed", sup)
|
|
result = ProcessingResult(text="clean", metadata={}, processor="fast")
|
|
reg = _registry(result, decision=None)
|
|
out = await processor._parse_pdf_tier(
|
|
reg, b"%PDF", "application/pdf", "f.pdf", "fast", settings=object()
|
|
)
|
|
assert out is result
|
|
rec.assert_not_called()
|
|
sup.assert_not_called()
|
|
|
|
|
|
async def test_low_quality_parse_raises_escalate(monkeypatch):
|
|
rec = MagicMock()
|
|
monkeypatch.setattr(processor, "record_document_escalation", rec)
|
|
result = ProcessingResult(text="", metadata={}, processor="fast")
|
|
reg = _registry(result, decision=EscalationDecision("hop", "ocr", "empty_text"))
|
|
with pytest.raises(EscalateError) as ei:
|
|
await processor._parse_pdf_tier(
|
|
reg, b"%PDF", "application/pdf", "f.pdf", "fast", settings=object()
|
|
)
|
|
assert ei.value.from_tier == "fast"
|
|
assert ei.value.to_tier == "ocr"
|
|
assert ei.value.reason == "empty_text"
|
|
# The escalation is recorded at the decision point.
|
|
rec.assert_called_once_with("fast", "ocr", "empty_text")
|
|
|
|
|
|
async def test_suppressed_decision_indexes_without_hop(monkeypatch):
|
|
"""OCR-off (suppressed): index this tier's result, record the would-be hop,
|
|
do NOT raise EscalateError."""
|
|
rec = MagicMock()
|
|
sup = MagicMock()
|
|
monkeypatch.setattr(processor, "record_document_escalation", rec)
|
|
monkeypatch.setattr(processor, "record_document_escalation_suppressed", sup)
|
|
result = ProcessingResult(text="junk", metadata={}, processor="fast")
|
|
reg = _registry(
|
|
result, decision=EscalationDecision("suppressed", "ocr", "empty_text")
|
|
)
|
|
out = await processor._parse_pdf_tier(
|
|
reg, b"%PDF", "application/pdf", "f.pdf", "fast", settings=object()
|
|
)
|
|
assert out is result # indexed as terminal, no hop
|
|
sup.assert_called_once_with("fast", "ocr", "empty_text")
|
|
rec.assert_not_called()
|
|
|
|
|
|
async def test_hard_failure_returns_result_without_escalating(monkeypatch):
|
|
rec = MagicMock()
|
|
sup = MagicMock()
|
|
monkeypatch.setattr(processor, "record_document_escalation", rec)
|
|
monkeypatch.setattr(processor, "record_document_escalation_suppressed", sup)
|
|
result = ProcessingResult(
|
|
text="",
|
|
metadata={"parse_failed_reason": "oversize"},
|
|
processor="size_guard",
|
|
success=False,
|
|
)
|
|
reg = _registry(result, decision=EscalationDecision("hop", "ocr", "empty_text"))
|
|
out = await processor._parse_pdf_tier(
|
|
reg, b"%PDF", "application/pdf", "big.pdf", "fast", settings=object()
|
|
)
|
|
# success=False short-circuits: the gate is never consulted, no escalation.
|
|
assert out is result
|
|
reg.evaluate_escalation.assert_not_called()
|
|
rec.assert_not_called()
|
|
sup.assert_not_called()
|