-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathtest_write_note_integration.py
More file actions
348 lines (271 loc) · 11.5 KB
/
test_write_note_integration.py
File metadata and controls
348 lines (271 loc) · 11.5 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Integration tests for write_note MCP tool.
Comprehensive tests covering all scenarios including note creation, content formatting,
tag handling, error conditions, and edge cases from bug reports.
"""
from textwrap import dedent
import pytest
from fastmcp import Client
from unittest.mock import patch
from basic_memory.config import ConfigManager
@pytest.mark.asyncio
async def test_write_note_basic_creation(mcp_server, app):
"""Test creating a simple note with basic content."""
async with Client(mcp_server) as client:
result = await client.call_tool(
"write_note",
{
"title": "Simple Note",
"folder": "basic",
"content": "# Simple Note\n\nThis is a simple note for testing.",
"tags": "simple,test",
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: basic/Simple Note.md" in response_text
assert "permalink: basic/simple-note" in response_text
assert "## Tags" in response_text
assert "- simple, test" in response_text
@pytest.mark.asyncio
async def test_write_note_no_tags(mcp_server, app):
"""Test creating a note without tags."""
async with Client(mcp_server) as client:
result = await client.call_tool(
"write_note",
{
"title": "No Tags Note",
"folder": "test",
"content": "Just some plain text without tags.",
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: test/No Tags Note.md" in response_text
assert "permalink: test/no-tags-note" in response_text
# Should not have tags section when no tags provided
@pytest.mark.asyncio
async def test_write_note_update_existing(mcp_server, app):
"""Test updating an existing note."""
async with Client(mcp_server) as client:
# Create initial note
result1 = await client.call_tool(
"write_note",
{
"title": "Update Test",
"folder": "test",
"content": "# Update Test\n\nOriginal content.",
"tags": "original",
},
)
assert "# Created note" in result1.content[0].text
# Update the same note
result2 = await client.call_tool(
"write_note",
{
"title": "Update Test",
"folder": "test",
"content": "# Update Test\n\nUpdated content with changes.",
"tags": "updated,modified",
},
)
assert len(result2.content) == 1
assert result2.content[0].type == "text"
response_text = result2.content[0].text
assert "# Updated note" in response_text
assert "file_path: test/Update Test.md" in response_text
assert "permalink: test/update-test" in response_text
assert "- updated, modified" in response_text
@pytest.mark.asyncio
async def test_write_note_tag_array(mcp_server, app):
"""Test creating a note with tag array (Issue #38 regression test)."""
async with Client(mcp_server) as client:
# This reproduces the exact bug from Issue #38
result = await client.call_tool(
"write_note",
{
"title": "Array Tags Test",
"folder": "test",
"content": "Testing tag array handling",
"tags": ["python", "testing", "integration", "mcp"],
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: test/Array Tags Test.md" in response_text
assert "permalink: test/array-tags-test" in response_text
assert "## Tags" in response_text
assert "python" in response_text
@pytest.mark.asyncio
async def test_write_note_custom_permalink(mcp_server, app):
"""Test custom permalink handling (Issue #93 regression test)."""
async with Client(mcp_server) as client:
content_with_custom_permalink = dedent("""
---
permalink: custom/my-special-permalink
---
# Custom Permalink Note
This note has a custom permalink in frontmatter.
- [note] Testing custom permalink preservation
""").strip()
result = await client.call_tool(
"write_note",
{
"title": "Custom Permalink Note",
"folder": "notes",
"content": content_with_custom_permalink,
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: notes/Custom Permalink Note.md" in response_text
assert "permalink: custom/my-special-permalink" in response_text
@pytest.mark.asyncio
async def test_write_note_unicode_content(mcp_server, app):
"""Test handling unicode content including emojis."""
async with Client(mcp_server) as client:
unicode_content = "# Unicode Test 🚀\n\nThis note has emoji 🎉 and unicode ♠♣♥♦\n\n- [note] Testing unicode handling 测试"
result = await client.call_tool(
"write_note",
{
"title": "Unicode Test 🌟",
"folder": "test",
"content": unicode_content,
"tags": "unicode,emoji,测试",
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: test/Unicode Test 🌟.md" in response_text
# Permalink should be sanitized
assert "permalink: test/unicode-test" in response_text
assert "## Tags" in response_text
@pytest.mark.asyncio
async def test_write_note_complex_content_with_observations_relations(mcp_server, app):
"""Test creating note with complex content including observations and relations."""
async with Client(mcp_server) as client:
complex_content = dedent("""
# Complex Note
This note demonstrates the full knowledge format.
## Observations
- [tech] Uses Python and FastAPI
- [design] Follows MCP protocol specification
- [note] Integration tests are comprehensive
## Relations
- implements [[MCP Protocol]]
- depends_on [[FastAPI Framework]]
- tested_by [[Integration Tests]]
## Additional Content
Some more regular markdown content here.
""").strip()
result = await client.call_tool(
"write_note",
{
"title": "Complex Knowledge Note",
"folder": "knowledge",
"content": complex_content,
"tags": "complex,knowledge,relations",
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: knowledge/Complex Knowledge Note.md" in response_text
assert "permalink: knowledge/complex-knowledge-note" in response_text
# Should show observation and relation counts
assert "## Observations" in response_text
assert "tech: 1" in response_text
assert "design: 1" in response_text
assert "note: 1" in response_text
assert "## Relations" in response_text
# Should show outgoing relations
assert "## Tags" in response_text
assert "complex, knowledge, relations" in response_text
@pytest.mark.asyncio
async def test_write_note_preserve_frontmatter(mcp_server, app):
"""Test that custom frontmatter is preserved when updating notes."""
async with Client(mcp_server) as client:
content_with_frontmatter = dedent("""
---
title: Frontmatter Note
type: note
version: 1.0
author: Test Author
status: draft
---
# Frontmatter Note
This note has custom frontmatter that should be preserved.
""").strip()
result = await client.call_tool(
"write_note",
{
"title": "Frontmatter Note",
"folder": "test",
"content": content_with_frontmatter,
"tags": "frontmatter,preservation",
},
)
assert len(result.content) == 1
assert result.content[0].type == "text"
response_text = result.content[0].text
assert "# Created note" in response_text
assert "file_path: test/Frontmatter Note.md" in response_text
assert "permalink: test/frontmatter-note" in response_text
@pytest.mark.asyncio
async def test_write_note_kebab_filenames_basic(mcp_server):
"""Test note creation with kebab_filenames=True and invalid filename characters."""
config = ConfigManager().config
curr_config_val = config.kebab_filenames
config.kebab_filenames = True
with patch.object(ConfigManager, "config", config):
async with Client(mcp_server) as client:
result = await client.call_tool(
"write_note",
{
"title": "My Note: With/Invalid|Chars?",
"folder": "my-folder",
"content": "Testing kebab-case and invalid characters.",
"tags": "kebab,invalid,filename",
},
)
assert len(result.content) == 1
response_text = result.content[0].text
# File path and permalink should be kebab-case and sanitized
assert "file_path: my-folder/my-note-with-invalid-chars.md" in response_text
assert "permalink: my-folder/my-note-with-invalid-chars" in response_text
# Restore original config value
config.kebab_filenames = curr_config_val
@pytest.mark.asyncio
async def test_write_note_kebab_filenames_repeat_invalid(mcp_server):
"""Test note creation with multiple invalid and repeated characters."""
config = ConfigManager().config
curr_config_val = config.kebab_filenames
config.kebab_filenames = True
with patch.object(ConfigManager, "config", config):
async with Client(mcp_server) as client:
result = await client.call_tool(
"write_note",
{
"title": 'Crazy<>:"|?*Note/Name',
"folder": "my-folder",
"content": "Should be fully kebab-case and safe.",
"tags": "crazy,filename,test",
},
)
assert len(result.content) == 1
response_text = result.content[0].text
assert "file_path: my-folder/crazy-note-name.md" in response_text
assert "permalink: my-folder/crazy-note-name" in response_text
# Restore original config value
config.kebab_filenames = curr_config_val