Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.4 KB
Raw Blame
import pytest

from core.llm_generator.output_parser.errors import OutputParserError
from libs.json_in_md_parser import (
    parse_and_check_json_markdown,
    parse_json_markdown,
)


def test_parse_json_markdown_triple_backticks_json() -> None:
    src = """
    ```json
    {"a": 1, "b": "x"}
    ```
    """
    assert parse_json_markdown(src) == {"a": 1, "b": "x"}


def test_parse_json_markdown_triple_backticks_generic() -> None:
    src = """
    ```
    {"k": [1, 2, 3]}
    ```
    """
    assert parse_json_markdown(src) == {"k": [1, 2, 3]}


def test_parse_json_markdown_single_backticks() -> None:
    src = '`{"x": true}`'
    assert parse_json_markdown(src) == {"x": True}


def test_parse_json_markdown_braces_only() -> None:
    src = '  {\n  \t"ok": "yes"\n}  '
    assert parse_json_markdown(src) == {"ok": "yes"}


def test_parse_json_markdown_not_found() -> None:
    with pytest.raises(ValueError, match="could not find json block in the output\\."):
        parse_json_markdown("no json here")


def test_parse_and_check_json_markdown_missing_key() -> None:
    src = """
    ```
    {"present": 1}
    ```
    """
    with pytest.raises(OutputParserError) as exc:
        parse_and_check_json_markdown(src, ["present", "missing"])
    assert "expected key `missing`" in str(exc.value)


def test_parse_and_check_json_markdown_invalid_json() -> None:
    src = """
    ```json
    {invalid json}
    ```
    """
    with pytest.raises(OutputParserError) as exc:
        parse_and_check_json_markdown(src, [])
    assert "got invalid json object" in str(exc.value)


def test_parse_and_check_json_markdown_success() -> None:
    src = """
    ```json
    {"present": 1, "other": 2}
    ```
    """
    obj = parse_and_check_json_markdown(src, ["present"])
    assert obj == {"present": 1, "other": 2}


def test_parse_and_check_json_markdown_multiple_blocks_fails() -> None:
    src = """
    ```json
    {"a": 1}
    ```
    Some text
    ```json
    {"b": 2}
    ```
    """
    # The current implementation is greedy and will match from the first
    # opening fence to the last closing fence, causing JSON decode failure.
    with pytest.raises(OutputParserError):
        parse_and_check_json_markdown(src, [])


def test_parse_and_check_json_markdown_handles_think_fenced_and_raw_variants() -> None:
    expected = {"keywords": ["2"], "category_id": "2", "category_name": "2"}
    cases = [
        """
        ```json
        [{"keywords": ["2"], "category_id": "2", "category_name": "2"}]
        ```, error: Expecting value: line 1 column 1 (char 0)
        """,
        """
        ```json
        {"keywords": ["2"], "category_id": "2", "category_name": "2"}
        ```, error: Extra data: line 2 column 5 (char 66)
        """,
        '{"keywords": ["2"], "category_id": "2", "category_name": "2"}',
        '[{"keywords": ["2"], "category_id": "2", "category_name": "2"}]',
    ]
    for src in cases:
        obj = parse_and_check_json_markdown(src, ["keywords", "category_id", "category_name"])
        assert obj == expected


def test_parse_json_markdown_backtick_inside_string_value() -> None:
    """Backticks inside JSON string values must not be mistaken for code fences."""
    src = '{"code": "use `print` function", "n": 1}'
    assert parse_json_markdown(src) == {"code": "use `print` function", "n": 1}


def test_parse_json_markdown_backtick_in_surrounding_prose() -> None:
    """Backticks in prose before the JSON must not break extraction."""
    src = 'Here is `the` result: {"a": 1}'
    assert parse_json_markdown(src) == {"a": 1}


def test_parse_json_markdown_fenced_scalar_still_supported() -> None:
    """Fenced content without brackets still parses via the fence fallback."""
    assert parse_json_markdown('```json\n"hello"\n```') == "hello"


def test_parse_and_check_json_markdown_fenced_scalar_raises_output_parser_error() -> None:
    """A valid JSON scalar (number/null/bool) must raise OutputParserError, not TypeError."""
    for src in ("```json\n42\n```", "```json\nnull\n```", "```json\ntrue\n```"):
        with pytest.raises(OutputParserError):
            parse_and_check_json_markdown(src, ["answer"])


def test_parse_and_check_json_markdown_rejects_non_dict_object() -> None:
    """A JSON string that merely contains an expected key as a substring must not pass."""
    with pytest.raises(OutputParserError):
        parse_and_check_json_markdown('```json\n"the generated prompt text"\n```', ["prompt"])