File size: 5,192 Bytes
3530326
 
270d701
 
3530326
 
 
 
270d701
3530326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8b1739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3530326
 
 
 
 
 
 
 
270d701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from __future__ import annotations

import pytest

from parser_output import (
    extract_bounding_boxes,
    extract_tables,
    render_text,
    shape_parse_response,
    shape_parser_output,
)


def test_extracts_and_normalizes_bounding_boxes() -> None:
    raw = """
{"label":"title","bbox":[100,200,900,400]}
<bbox label="table">[0.1, 0.3, 0.8, 0.9]</bbox>
{"label":"invalid","bbox":[5,5,1,1]}
"""

    boxes = extract_bounding_boxes(raw)

    assert len(boxes) == 2
    assert boxes[0]["label"] == "title"
    assert boxes[0]["bbox"] == [0.1, 0.2, 0.9, 0.4]
    assert boxes[1]["bbox"] == [0.1, 0.3, 0.8, 0.9]


def test_sanitizes_tables() -> None:
    raw = """
<table onclick="steal()">
  <tr><th scope="col">Name</th><td><script>alert(1)</script>Ada</td></tr>
</table>
"""

    table = extract_tables(raw)

    assert "<table" in table
    assert 'scope="col"' in table
    assert "onclick" not in table
    assert "<script" not in table


def test_sanitizes_rendered_markdown() -> None:
    rendered = render_text(
        '# Heading\n\n<script>alert(1)</script>\n\n[unsafe](javascript:alert(1))'
    )

    assert "<h1>Heading</h1>" in rendered
    assert "<script" not in rendered
    assert "javascript:" not in rendered


def test_rendered_text_keeps_a_visible_marker_for_html_tables() -> None:
    rendered = render_text(
        "html:\n<table><tr><td>Quarterly revenue</td></tr></table>"
    )

    assert "html:" in rendered
    assert "<code>&lt;table&gt;...&lt;/table&gt;</code>" in rendered
    assert "Quarterly revenue" not in rendered
    assert "<table>" not in rendered


def test_table_only_output_has_nonempty_text_html() -> None:
    shaped = shape_parser_output("<table><tr><td>Ada</td></tr></table>")

    assert shaped["tables_html"] == "<table><tbody><tr><td>Ada</td></tr></tbody></table>"
    assert "&lt;table&gt;...&lt;/table&gt;" in shaped["text_html"]


def test_shapes_empty_output() -> None:
    assert shape_parser_output("") == {
        "raw_output": "",
        "output_chars": 0,
        "boxes": [],
        "tables_html": "",
        "text_html": "",
    }


def test_shapes_structured_parse_blocks() -> None:
    body = {
        "id": "parse-123",
        "pages": [
            {
                "index": 0,
                "type": "blocks",
                "blocks": [
                    {"type": "text", "text": {"content": "# Report"}},
                    {
                        "type": "table",
                        "table": {
                            "type": "html",
                            "title": "Revenue",
                            "html": (
                                '<table onclick="bad()"><tr><td>42</td></tr></table>'
                            ),
                            "bounding_box_normalized": {
                                "top_left_x": 0.1,
                                "top_left_y": 0.2,
                                "bottom_right_x": 0.8,
                                "bottom_right_y": 0.7,
                            },
                        },
                    },
                    {
                        "type": "image",
                        "image": {
                            "id": "img-0",
                            "description": "A bar chart",
                            "category": "other",
                            "bounding_box_normalized": {
                                "top_left_x": 0.2,
                                "top_left_y": 0.3,
                                "bottom_right_x": 0.9,
                                "bottom_right_y": 0.8,
                            },
                        },
                    },
                ],
            }
        ],
        "meta": {"billed_units": {"pages": 1}},
    }

    shaped = shape_parse_response(body)

    assert shaped["text_output"].startswith("# Report")
    assert "Image: A bar chart" in shaped["text_output"]
    assert "<h1>Report</h1>" in shaped["text_html"]
    assert "onclick" not in shaped["tables_html"]
    assert [box["label"] for box in shaped["boxes"]] == ["Revenue", "other"]
    assert shaped["boxes"][0]["bbox"] == [0.1, 0.2, 0.8, 0.7]
    assert '"id": "parse-123"' in shaped["raw_output"]
    assert shaped["response"] == body


def test_shapes_markdown_parse_page() -> None:
    shaped = shape_parse_response(
        {
            "id": "parse-456",
            "pages": [
                {
                    "index": 0,
                    "type": "markdown",
                    "markdown": {
                        "content": "## Heading\n\nBody",
                        "images": [],
                    },
                }
            ],
        }
    )

    assert shaped["text_output"] == "## Heading\n\nBody"
    assert "<h2>Heading</h2>" in shaped["text_html"]


@pytest.mark.parametrize(
    "body",
    [
        {},
        {"pages": []},
        {"pages": [{"index": 0, "type": "unknown"}]},
        {"pages": [{"index": 0, "type": "blocks", "blocks": [{}]}]},
    ],
)
def test_rejects_invalid_structured_parse_responses(body: dict) -> None:
    with pytest.raises((TypeError, ValueError)):
        shape_parse_response(body)