# SPDX-FileCopyrightText: The Docling Contributors # SPDX-License-Identifier: MIT from __future__ import annotations import copy import importlib.util import json import sys from pathlib import Path from typing import Any SCRIPTS_DIR = Path(__file__).resolve().parents[1] / ".github/scripts" sys.path.insert(0, str(SCRIPTS_DIR)) SPEC = importlib.util.spec_from_file_location("groundtruth_diff", SCRIPTS_DIR / "groundtruth_diff.py") assert SPEC is not None and SPEC.loader is not None gt = importlib.util.module_from_spec(SPEC) sys.modules[SPEC.name] = gt SPEC.loader.exec_module(gt) DOC_PATH = "tests/data/doc/sample.json" def make_doc() -> dict[str, Any]: prov = [{"page_no": 1, "bbox": {"l": 1, "t": 2, "r": 3, "b": 4}}] return { "body": {"children": [{"$ref": "#/texts/0"}, {"$ref": "#/tables/0"}]}, "texts": [ {"label": "section_header", "text": "Intro", "prov": prov, "children": []}, {"label": "text", "text": "Hello world", "prov": prov, "children": []}, ], "tables": [ { "label": "table", "prov": prov, "children": [{"$ref": "#/texts/1"}], "data": { "num_rows": 1, "num_cols": 2, "table_cells": [{"text": "a"}, {"text": "b"}], }, } ], "pages": {"1": {"size": {"width": 10, "height": 10}}}, } def summarize(old: dict[str, Any], new: dict[str, Any]) -> Any: return gt.summarize_file( DOC_PATH, json.dumps(old).encode(), json.dumps(new, indent=2).encode(), status="modified", ) def test_test_data_counts_except_source_code() -> None: assert gt.is_groundtruth_path("tests/data/doc/2408.09869v3_enriched.gt.md") assert gt.is_groundtruth_path("tests/data/doc/virtual_text_list/input.json") assert gt.is_groundtruth_path("tests/data/chunker/0_out_chunks.json") assert gt.is_groundtruth_path("tests/data/viz/2408.09869v3_enriched_viz_p1.png") # Source code stays in the model's diff: pytest collects tests/ recursively. assert not gt.is_groundtruth_path("tests/data/chunker_repo/repos/docling/export.py") assert not gt.is_groundtruth_path("tests/data/repo_chunking/sample.ts") assert not gt.is_groundtruth_path("tests/test_utils.py") assert not gt.is_groundtruth_path("docs/data/a.json") def test_images_are_compared_by_bytes() -> None: path = "tests/data/viz/a.png" png = b"\x89PNG\r\n\x1a\n\x00\xff" assert gt.summarize_file(path, png, png, status="modified").kind == gt.ChangeKind.FORMATTING change = gt.summarize_file(path, png, png + b"\x00", status="modified") assert change.kind == gt.ChangeKind.BINARY assert change.details == ["10 → 11 bytes"] def test_reformatted_json_and_moved_boxes_are_not_content_changes() -> None: old = make_doc() assert summarize(old, copy.deepcopy(old)).kind == gt.ChangeKind.FORMATTING moved = copy.deepcopy(old) moved["texts"][0]["prov"][0]["bbox"]["l"] = 5 moved["pages"]["1"]["size"]["width"] = 11 assert summarize(old, moved).kind == gt.ChangeKind.COORDINATES def test_text_table_and_reading_order_changes_are_classified() -> None: old = make_doc() text = copy.deepcopy(old) text["texts"][1]["text"] = "Hello there" change = summarize(old, text) assert change.kind == gt.ChangeKind.TEXT assert change.details == ["1 text item(s) changed"] cells = copy.deepcopy(old) cells["tables"][0]["data"]["table_cells"][1]["text"] = "c" assert summarize(old, cells).details == ["table 0: 1 cell(s) changed"] order = copy.deepcopy(old) order["body"]["children"].reverse() change = summarize(old, order) assert change.kind == gt.ChangeKind.STRUCTURE assert "reading order changed" in change.details relabel = copy.deepcopy(old) relabel["texts"][0]["label"] = "title" change = summarize(old, relabel) assert change.kind == gt.ChangeKind.STRUCTURE assert "section_header: 1 → 0" in change.details def test_markdown_whitespace_and_table_rows() -> None: path = "tests/data/doc/a.gt.md" old = b"# T\n\n| a | b |\n|---|---|\n| 1 | 2 |\n" formatted = gt.summarize_file(path, old, old + b"\n\n", status="modified") assert formatted.kind == gt.ChangeKind.FORMATTING joined = gt.summarize_file(path, b"one\n\ntwo\n", b"one two\n", status="modified") assert joined.kind == gt.ChangeKind.TEXT assert joined.details == ["line or paragraph breaks changed"] change = gt.summarize_file(path, old, old.replace(b"| 1 |", b"| 9 |"), status="modified") assert change.kind == gt.ChangeKind.TABLES assert change.details == ["+1 / -1 lines", "Markdown table rows changed"] def test_a_failed_read_of_a_modified_file_is_not_reported_as_added() -> None: # read_blob gives None for any git failure, for example a blob that the # partial clone cannot fetch. doc = json.dumps(make_doc()).encode() for old, new, side in ((None, doc, "base"), (doc, None, "head")): change = gt.summarize_file(DOC_PATH, old, new, status="modified") assert change.kind == gt.ChangeKind.UNPARSABLE assert change.details == [f"cannot read the {side} version"] added = gt.summarize_file(DOC_PATH, None, doc, status="added") assert added.kind == gt.ChangeKind.ADDED renamed = gt.summarize_file(DOC_PATH, None, doc, status="renamed") assert renamed.kind == gt.ChangeKind.UNPARSABLE def test_render_lists_relevant_kinds_first() -> None: changes = [ gt.FileChange("f1.json", gt.ChangeKind.FORMATTING), gt.FileChange("f2.json", gt.ChangeKind.STRUCTURE, ["reading order changed"]), ] lines = gt.render_markdown(changes).splitlines() assert lines[2] == "| structure | 1 |" assert lines[5] == "- **structure** `f2.json`: reading order changed" def test_render_markdown_escapes_data_from_the_pr() -> None: doc = make_doc() changed = copy.deepcopy(doc) changed["texts"][0]["label"] = "x\n\n**safe** @org/team ![i](http://evil/p.png)" change = gt.summarize_file(DOC_PATH, json.dumps(doc).encode(), json.dumps(changed).encode(), status="modified") path = "tests/data/a` @org/team `.md" rendered = gt.render_markdown([change, gt.FileChange(path, gt.ChangeKind.ADDED)]).splitlines() assert len(rendered) == 7 detail, added = rendered[5], rendered[6] assert "**safe**" not in detail assert "![i](" not in detail assert "@org" not in detail assert added == f"- **added** ``{path}``"