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]} [0.1, 0.3, 0.8, 0.9] {"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 = """
NameAda
""" table = extract_tables(raw) assert " None: rendered = render_text( '# Heading\n\n\n\n[unsafe](javascript:alert(1))' ) assert "

Heading

" in rendered assert " None: rendered = render_text( "html:\n
Quarterly revenue
" ) assert "html:" in rendered assert "<table>...</table>" in rendered assert "Quarterly revenue" not in rendered assert "" not in rendered def test_table_only_output_has_nonempty_text_html() -> None: shaped = shape_parser_output("
Ada
") assert shaped["tables_html"] == "
Ada
" 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": ( '
42
' ), "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 "

Report

" 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 "

Heading

" 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)