File size: 4,534 Bytes
ea8c728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from shinka.edit import apply_diff_patch, apply_full_patch
from shinka.utils.languages import (
    get_code_fence_languages,
    get_evolve_comment_prefix,
    get_language_extension,
    normalize_language,
)


def test_language_helpers_support_markdown():
    """Markdown is registered as a language with HTML-style comment markers."""
    assert get_language_extension("markdown") == "md"
    assert get_evolve_comment_prefix("markdown") == "<!--"

    fences = get_code_fence_languages("markdown")
    assert fences[0] == "markdown"
    assert "md" in fences


def test_language_helpers_support_md_alias():
    """The 'md' alias resolves to the canonical 'markdown' language."""
    assert normalize_language("md") == "markdown"
    assert get_language_extension("md") == "md"
    assert get_evolve_comment_prefix("md") == "<!--"

    fences = get_code_fence_languages("md")
    assert fences[0] == "md"
    assert "markdown" in fences


def test_apply_diff_patch_supports_markdown(tmp_path):
    """Diff patching works with HTML-comment EVOLVE-BLOCK markers."""
    original_content = """<!-- EVOLVE-BLOCK-START -->
## Step 1

Run the old command.
<!-- EVOLVE-BLOCK-END -->"""

    patch_content = """<!-- EVOLVE-BLOCK-START -->
<<<<<<< SEARCH
Run the old command.
=======
Run the new command.
>>>>>>> REPLACE
<!-- EVOLVE-BLOCK-END -->"""

    patch_dir = tmp_path / "markdown_diff_patch"
    result = apply_diff_patch(
        patch_str=patch_content,
        original_str=original_content,
        patch_dir=patch_dir,
        language="markdown",
        verbose=False,
    )
    updated_content, num_applied, output_path, error, patch_txt, diff_path = result

    assert error is None
    assert num_applied == 1
    assert "Run the new command." in updated_content
    assert output_path == patch_dir / "main.md"
    assert output_path is not None and output_path.exists()
    assert (patch_dir / "original.md").exists()
    assert diff_path == patch_dir / "edit.diff"
    assert diff_path is not None and diff_path.exists()
    assert patch_txt is not None and "Run the new command." in patch_txt


def test_apply_full_patch_supports_markdown(tmp_path):
    """Full patching works with markdown code fences."""
    original_content = """<!-- EVOLVE-BLOCK-START -->
## Step 1

Run the old command.
<!-- EVOLVE-BLOCK-END -->
"""

    patch_content = """```markdown
<!-- EVOLVE-BLOCK-START -->
## Step 1

Run the new command.
<!-- EVOLVE-BLOCK-END -->
```"""

    patch_dir = tmp_path / "markdown_full_patch"
    result = apply_full_patch(
        patch_str=patch_content,
        original_str=original_content,
        patch_dir=patch_dir,
        language="markdown",
        verbose=False,
    )
    updated_content, num_applied, output_path, error, patch_txt, diff_path = result

    assert error is None
    assert num_applied == 1
    assert "Run the new command." in updated_content
    assert output_path == patch_dir / "main.md"
    assert output_path is not None and output_path.exists()
    assert (patch_dir / "rewrite.txt").exists()
    assert (patch_dir / "original.md").exists()
    assert diff_path == patch_dir / "edit.diff"
    assert diff_path is not None and diff_path.exists()
    assert patch_txt is not None and "Run the new command." in patch_txt


def test_apply_full_patch_supports_md_fence(tmp_path):
    """Full patching accepts the short 'md' fence tag."""
    original_content = """<!-- EVOLVE-BLOCK-START -->
# Title
Some content.
<!-- EVOLVE-BLOCK-END -->
"""

    patch_content = """```md
<!-- EVOLVE-BLOCK-START -->
# Title
Updated content.
<!-- EVOLVE-BLOCK-END -->
```"""

    patch_dir = tmp_path / "markdown_md_fence"
    result = apply_full_patch(
        patch_str=patch_content,
        original_str=original_content,
        patch_dir=patch_dir,
        language="markdown",
        verbose=False,
    )
    updated_content, num_applied, output_path, error, patch_txt, diff_path = result

    assert error is None
    assert num_applied == 1
    assert "Updated content." in updated_content


def test_clean_evolve_markers_strips_html_comments():
    """The marker stripper handles <!-- EVOLVE-BLOCK-START/END --> syntax."""
    from shinka.edit.apply_diff import _clean_evolve_markers

    text = """<!-- EVOLVE-BLOCK-START -->
<<<<<<< SEARCH
old text
=======
new text
>>>>>>> REPLACE
<!-- EVOLVE-BLOCK-END -->"""

    cleaned = _clean_evolve_markers(text)
    assert "EVOLVE-BLOCK-START" not in cleaned
    assert "EVOLVE-BLOCK-END" not in cleaned
    assert "new text" in cleaned