File size: 2,562 Bytes
6918275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from __future__ import annotations

import sys
from pathlib import Path

import pytest

PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from helpers.dirty_json import DirtyJson


@pytest.mark.parametrize(
    ("payload", "expected"),
    [
        (
            '{"tool_name":"x","tool_args":{}}',
            {"tool_name": "x", "tool_args": {}},
        ),
        ("[1, 2, 3]", [1, 2, 3]),
    ],
)
def test_completed_true_when_root_is_explicitly_closed(payload, expected) -> None:
    parser = DirtyJson()

    assert parser.parse(payload) == expected
    assert parser.completed is True


def test_completed_false_when_root_hits_eof_before_closing() -> None:
    parser = DirtyJson()

    assert parser.parse('{"tool_name":"x","tool_args":{}') == {
        "tool_name": "x",
        "tool_args": {},
    }
    assert parser.completed is False


def test_completed_remains_true_after_trailing_content() -> None:
    parser = DirtyJson()

    assert parser.feed('{"tool_name":"x","tool_args":{}}') == {
        "tool_name": "x",
        "tool_args": {},
    }
    assert parser.completed is True

    assert parser.feed(" trailing noise") == {
        "tool_name": "x",
        "tool_args": {},
    }

    assert parser.completed is True


def test_value_keeps_unescaped_markdown_quotes_until_structural_delimiter() -> None:
    payload = (
        "{\n"
        '    "tool_name": "response",\n'
        '    "tool_args": {\n'
        '        "text": "The rule:\\n\\n> *"'
        'Create a child AGENTS.md when a folder becomes a boundary."'
        '*\\n\\nAdding `css/AGENTS.md` that says *"'
        'this folder contains CSS"'
        '* is duplication."\n'
        "    }\n"
        "}"
    )

    parsed = DirtyJson.parse_string(payload)

    assert parsed["tool_args"] == {
        "text": (
            "The rule:\n\n"
            '> *"Create a child AGENTS.md when a folder becomes a boundary."*\n\n'
            'Adding `css/AGENTS.md` that says *"this folder contains CSS"* is duplication.'
        )
    }


def test_value_keeps_unescaped_quotes_on_single_line() -> None:
    parsed = DirtyJson.parse_string(
        '{"text":"He said "hello" before closing","ok":true}'
    )

    assert parsed == {"text": 'He said "hello" before closing', "ok": True}


def test_value_can_still_end_before_quoted_key_when_comma_is_missing() -> None:
    parsed = DirtyJson.parse_string('{"first":"one" "second":"two"}')

    assert parsed == {"first": "one", "second": "two"}