File size: 2,287 Bytes
62787e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import contextlib
import io
import unittest
from unittest import mock

from sec_parser import sec_parser as sp


class NormalizeTextMarkupTests(unittest.TestCase):
    def test_valid_utf8_bypasses_detwingle(self):
        payload = "Revenue — 2025".encode("utf-8")

        with mock.patch.object(
            sp.UnicodeDammit,
            "detwingle",
            side_effect=AssertionError("detwingle should not be called for valid utf-8"),
        ):
            normalized = sp.normalize_text_markup(payload)

        self.assertEqual(normalized, "Revenue — 2025")

    def test_invalid_utf8_still_uses_detwingle_path(self):
        payload = b'\x93quoted\x94'

        with mock.patch.object(sp.UnicodeDammit, "detwingle", wraps=sp.UnicodeDammit.detwingle) as detwingle:
            normalized = sp.normalize_text_markup(payload)

        self.assertTrue(detwingle.called)
        self.assertIn("quoted", normalized)


class DebugPrintGatingTests(unittest.TestCase):
    def test_parse_html_filing_stage_debug_is_off_by_default(self):
        html = "<html><body><p>Hello world</p></body></html>"
        stdout = io.StringIO()

        with mock.patch.dict("os.environ", {}, clear=False):
            with mock.patch.object(sp, "is_document_layout_positioned", return_value=False):
                with mock.patch.object(sp, "parse_positioned_html_islands_via_ocr", return_value=(None, False)):
                    with contextlib.redirect_stdout(stdout):
                        sp.parse_html_filing(html, form_type="")

        self.assertNotIn("→ stage 0 (raw):", stdout.getvalue())

    def test_parse_html_filing_stage_debug_can_be_enabled(self):
        html = "<html><body><p>Hello world</p></body></html>"
        stdout = io.StringIO()

        with mock.patch.dict("os.environ", {"SEC_PARSER_DEBUG": "1"}, clear=False):
            with mock.patch.object(sp, "is_document_layout_positioned", return_value=False):
                with mock.patch.object(sp, "parse_positioned_html_islands_via_ocr", return_value=(None, False)):
                    with contextlib.redirect_stdout(stdout):
                        sp.parse_html_filing(html, form_type="")

        self.assertIn("→ stage 0 (raw):", stdout.getvalue())


if __name__ == "__main__":
    unittest.main()