gateway/tests/serviceGeneration/test_md_to_json_consolidation.py
2026-04-29 23:12:46 +02:00

71 lines
2.7 KiB
Python

# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
import pytest
from modules.serviceCenter.services.serviceGeneration.subDocumentUtility import markdownToDocumentJson
def test_basic_paragraph():
result = markdownToDocumentJson("Hello world", "Test")
doc = result["documents"][0]
section = doc["sections"][0]
assert section["content_type"] == "paragraph"
assert section["elements"][0]["content"]["inlineRuns"][0] == {"type": "text", "value": "Hello world"}
def test_inline_bold():
result = markdownToDocumentJson("This is **bold** text", "Test")
runs = result["documents"][0]["sections"][0]["elements"][0]["content"]["inlineRuns"]
assert any(r["type"] == "bold" and r["value"] == "bold" for r in runs)
def test_inline_image():
result = markdownToDocumentJson("Text ![logo](file:abc123) more", "Test")
runs = result["documents"][0]["sections"][0]["elements"][0]["content"]["inlineRuns"]
assert any(r["type"] == "image" and r.get("fileId") == "abc123" for r in runs)
def test_inline_link():
result = markdownToDocumentJson("Click [here](https://example.com)", "Test")
runs = result["documents"][0]["sections"][0]["elements"][0]["content"]["inlineRuns"]
assert any(r["type"] == "link" and r.get("href") == "https://example.com" for r in runs)
def test_table_cells_are_inline_runs():
md = "| A | B |\n| --- | --- |\n| **x** | y |"
result = markdownToDocumentJson(md, "Test")
section = result["documents"][0]["sections"][0]
assert section["content_type"] == "table"
rows = section["elements"][0]["content"]["rows"]
assert isinstance(rows[0][0], list)
def test_bullet_list_inline_runs():
md = "- Item **one**\n- Item two"
result = markdownToDocumentJson(md, "Test")
section = result["documents"][0]["sections"][0]
assert section["content_type"] == "bullet_list"
items = section["elements"][0]["content"]["items"]
assert isinstance(items[0], list)
def test_standalone_image_block():
md = "![Big chart](file:chart123)"
result = markdownToDocumentJson(md, "Test")
section = result["documents"][0]["sections"][0]
assert section["content_type"] == "image"
def test_heading_unchanged():
result = markdownToDocumentJson("# Title", "Test")
section = result["documents"][0]["sections"][0]
assert section["content_type"] == "heading"
assert section["elements"][0]["content"]["text"] == "Title"
assert section["elements"][0]["content"]["level"] == 1
def test_code_block_unchanged():
md = "```python\nprint('hi')\n```"
result = markdownToDocumentJson(md, "Test")
section = result["documents"][0]["sections"][0]
assert section["content_type"] == "code_block"
assert section["elements"][0]["content"]["code"] == "print('hi')"