Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| 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><table>...</table></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 "<table>...</table>" 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"] | |
| def test_rejects_invalid_structured_parse_responses(body: dict) -> None: | |
| with pytest.raises((TypeError, ValueError)): | |
| shape_parse_response(body) | |