-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathtest_obsidian_yaml_formatting.py
More file actions
190 lines (152 loc) · 6.1 KB
/
test_obsidian_yaml_formatting.py
File metadata and controls
190 lines (152 loc) · 6.1 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Integration tests for Obsidian-compatible YAML formatting in write_note tool."""
import pytest
from basic_memory.mcp.tools import write_note
@pytest.mark.asyncio
async def test_write_note_tags_yaml_format(app, project_config, test_project):
"""Test that write_note creates files with proper YAML list format for tags."""
# Create a note with tags using write_note
result = await write_note(
project=test_project.name,
title="YAML Format Test",
directory="test",
content="Testing YAML tag formatting",
tags=["system", "overview", "reference"],
)
# Verify the note was created successfully
assert "Created note" in result
assert "file_path: test/YAML Format Test.md" in result
# Read the file directly to check YAML formatting
file_path = project_config.home / "test" / "YAML Format Test.md"
content = file_path.read_text(encoding="utf-8")
# Should use YAML list format
assert "tags:" in content
assert "- system" in content
assert "- overview" in content
assert "- reference" in content
# Should NOT use JSON array format
assert '["system"' not in content
assert '"overview"' not in content
assert '"reference"]' not in content
@pytest.mark.asyncio
async def test_write_note_stringified_json_tags(app, project_config, test_project):
"""Test that stringified JSON arrays are handled correctly."""
# This simulates the issue where AI assistants pass tags as stringified JSON
result = await write_note(
project=test_project.name,
title="Stringified JSON Test",
directory="test",
content="Testing stringified JSON tag input",
tags='["python", "testing", "json"]', # Stringified JSON array
)
# Verify the note was created successfully
assert "Created note" in result
# Read the file to check formatting
file_path = project_config.home / "test" / "Stringified JSON Test.md"
content = file_path.read_text(encoding="utf-8")
# Should properly parse the JSON and format as YAML list
assert "tags:" in content
assert "- python" in content
assert "- testing" in content
assert "- json" in content
# Should NOT have the original stringified format issues
assert '["python"' not in content
assert '"testing"' not in content
assert '"json"]' not in content
@pytest.mark.asyncio
async def test_write_note_single_tag_yaml_format(app, project_config, test_project):
"""Test that single tags are still formatted as YAML lists."""
await write_note(
project=test_project.name,
title="Single Tag Test",
directory="test",
content="Testing single tag formatting",
tags=["solo-tag"],
)
file_path = project_config.home / "test" / "Single Tag Test.md"
content = file_path.read_text(encoding="utf-8")
# Single tag should still use list format
assert "tags:" in content
assert "- solo-tag" in content
@pytest.mark.asyncio
async def test_write_note_no_tags(app, project_config, test_project):
"""Test that notes without tags work normally."""
await write_note(
project=test_project.name,
title="No Tags Test",
directory="test",
content="Testing note without tags",
tags=None,
)
file_path = project_config.home / "test" / "No Tags Test.md"
content = file_path.read_text(encoding="utf-8")
# Should not have tags field in frontmatter
assert "tags:" not in content
assert "title: No Tags Test" in content
@pytest.mark.asyncio
async def test_write_note_empty_tags_list(app, project_config, test_project):
"""Test that empty tag lists are handled properly."""
await write_note(
project=test_project.name,
title="Empty Tags Test",
directory="test",
content="Testing empty tag list",
tags=[],
)
file_path = project_config.home / "test" / "Empty Tags Test.md"
content = file_path.read_text(encoding="utf-8")
# Should not add tags field to frontmatter for empty lists
assert "tags:" not in content
@pytest.mark.asyncio
async def test_write_note_update_preserves_yaml_format(app, project_config, test_project):
"""Test that updating a note preserves the YAML list format."""
# First, create the note
await write_note(
project=test_project.name,
title="Update Format Test",
directory="test",
content="Initial content",
tags=["initial", "tag"],
)
# Then update it with new tags
result = await write_note(
project=test_project.name,
title="Update Format Test",
directory="test",
content="Updated content",
tags=["updated", "new-tag", "format"],
overwrite=True,
)
# Should be an update, not a new creation
assert "Updated note" in result
# Check the file format
file_path = project_config.home / "test" / "Update Format Test.md"
content = file_path.read_text(encoding="utf-8")
# Should have proper YAML formatting for updated tags
assert "tags:" in content
assert "- updated" in content
assert "- new-tag" in content
assert "- format" in content
# Old tags should be gone
assert "- initial" not in content
assert "- tag" not in content
# Content should be updated
assert "Updated content" in content
assert "Initial content" not in content
@pytest.mark.asyncio
async def test_complex_tags_yaml_format(app, project_config, test_project):
"""Test that complex tags with special characters format correctly."""
await write_note(
project=test_project.name,
title="Complex Tags Test",
directory="test",
content="Testing complex tag formats",
tags=["python-3.9", "api_integration", "v2.0", "nested/category", "under_score"],
)
file_path = project_config.home / "test" / "Complex Tags Test.md"
content = file_path.read_text(encoding="utf-8")
# All complex tags should format correctly
assert "- python-3.9" in content
assert "- api_integration" in content
assert "- v2.0" in content
assert "- nested/category" in content
assert "- under_score" in content