diff --git a/src/documents/tests/test_zone_ocr.py b/src/documents/tests/test_zone_ocr.py index cb88133ab8..7350d5df37 100644 --- a/src/documents/tests/test_zone_ocr.py +++ b/src/documents/tests/test_zone_ocr.py @@ -2797,6 +2797,41 @@ class TestPrepareFieldUpdate: with pytest.raises(ValueError, match="unknown"): _prepare_field_update(OcrTemplateZone(target="unknown"), "value") + @pytest.mark.parametrize( + ("length", "warns"), + [ + pytest.param(128, False, id="at-the-limit"), + pytest.param(129, True, id="over-the-limit"), + ], + ) + def test_title_is_cut_to_128_characters_with_a_warning( + self, + caplog: pytest.LogCaptureFixture, + length: int, + *, + warns: bool, + ) -> None: + """ + GIVEN: + - A zone targeting the title and extracted text at or over 128 characters + WHEN: + - A field update is prepared for it + THEN: + - At most the first 128 characters are kept + - A warning is logged only when the text was cut + """ + zone = OcrTemplateZoneFactory( + target=OcrTemplateZone.TargetType.TITLE, + name="Heading", + ) + + with caplog.at_level(logging.WARNING, logger="paperless.zone_ocr"): + update = _prepare_field_update(zone, "x" * length) + + assert update is not None + assert update.value == "x" * 128 + assert any("Heading" in message for message in caplog.messages) is warns + @pytest.mark.parametrize( ("ocr_language", "expected"), [ diff --git a/src/documents/zone_ocr.py b/src/documents/zone_ocr.py index b12e2a1bf8..fd99cfe0f9 100644 --- a/src/documents/zone_ocr.py +++ b/src/documents/zone_ocr.py @@ -1078,6 +1078,12 @@ def _prepare_field_update( ) model = CustomFieldInstance case OcrTemplateZone.TargetType.TITLE: + if len(value) > 128: + logger.warning( + "Zone OCR: title from zone '%s' is %d characters, keeping the first 128", + zone.name, + len(value), + ) field_name, typed_value = "title", value[:128] model = Document case OcrTemplateZone.TargetType.ASN: