Spaces:
Sleeping
Sleeping
File size: 918 Bytes
2026abe | 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 | """Unit tests for the clean_json_text and safe_parse_json utilities."""
import pytest
from app.utils.json_parser import clean_json_text, safe_parse_json
def test_clean_json_markdown_blocks():
"""Test extracting JSON from markdown code block fences."""
raw = "```json\n{\"key\": \"value\"}\n```"
cleaned = clean_json_text(raw)
assert cleaned == '{"key": "value"}'
def test_clean_json_trailing_commas():
"""Test removing trailing commas from objects and arrays."""
raw = '{"items": [1, 2, 3,], "name": "test",}'
cleaned = clean_json_text(raw)
parsed = safe_parse_json(cleaned)
assert parsed["items"] == [1, 2, 3]
assert parsed["name"] == "test"
def test_safe_parse_fallback():
"""Test safe fallback on invalid JSON text."""
invalid = "This is not json"
result = safe_parse_json(invalid, default={"error": "fallback"})
assert result == {"error": "fallback"}
|