Skip to content

Commit 45bddbe

Browse files
committed
Add mcp integration test for kebab case when writing note names
1 parent aad9c42 commit 45bddbe

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/basic_memory/file_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ async def update_frontmatter(path: FilePath, updates: Dict[str, Any]) -> str:
236236
raise FileError(f"Failed to update frontmatter: {e}")
237237

238238

239-
def sanitize_for_filename(text, replacement="-"):
239+
def sanitize_for_filename(text: str, replacement: str = "-") -> str:
240240
"""
241241
Sanitize string to be safe for use as a note title
242242
Replaces path separators and other problematic characters

test-int/mcp/test_write_note_integration.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import pytest
1111
from fastmcp import Client
12+
from unittest.mock import patch
13+
14+
from basic_memory.config import ConfigManager
1215

1316

1417
@pytest.mark.asyncio
@@ -282,3 +285,64 @@ async def test_write_note_preserve_frontmatter(mcp_server, app):
282285
assert "# Created note" in response_text
283286
assert "file_path: test/Frontmatter Note.md" in response_text
284287
assert "permalink: test/frontmatter-note" in response_text
288+
289+
290+
@pytest.mark.asyncio
291+
async def test_write_note_kebab_filenames_basic(mcp_server):
292+
"""Test note creation with kebab_filenames=True and invalid filename characters."""
293+
294+
config = ConfigManager().config
295+
curr_config_val = config.kebab_filenames
296+
config.kebab_filenames = True
297+
298+
with patch.object(ConfigManager, "config", config):
299+
async with Client(mcp_server) as client:
300+
result = await client.call_tool(
301+
"write_note",
302+
{
303+
"title": "My Note: With/Invalid|Chars?",
304+
"folder": "my-folder",
305+
"content": "Testing kebab-case and invalid characters.",
306+
"tags": "kebab,invalid,filename",
307+
},
308+
)
309+
310+
assert len(result.content) == 1
311+
response_text = result.content[0].text
312+
313+
# File path and permalink should be kebab-case and sanitized
314+
assert "file_path: my-folder/my-note-with-invalid-chars.md" in response_text
315+
assert "permalink: my-folder/my-note-with-invalid-chars" in response_text
316+
317+
# Restore original config value
318+
config.kebab_filenames = curr_config_val
319+
320+
321+
@pytest.mark.asyncio
322+
async def test_write_note_kebab_filenames_repeat_invalid(mcp_server):
323+
"""Test note creation with multiple invalid and repeated characters."""
324+
325+
config = ConfigManager().config
326+
curr_config_val = config.kebab_filenames
327+
config.kebab_filenames = True
328+
329+
with patch.object(ConfigManager, "config", config):
330+
async with Client(mcp_server) as client:
331+
result = await client.call_tool(
332+
"write_note",
333+
{
334+
"title": 'Crazy<>:"|?*Note/Name',
335+
"folder": "my-folder",
336+
"content": "Should be fully kebab-case and safe.",
337+
"tags": "crazy,filename,test",
338+
},
339+
)
340+
341+
assert len(result.content) == 1
342+
response_text = result.content[0].text
343+
344+
assert "file_path: my-folder/crazy-note-name.md" in response_text
345+
assert "permalink: my-folder/crazy-note-name" in response_text
346+
347+
# Restore original config value
348+
config.kebab_filenames = curr_config_val

0 commit comments

Comments
 (0)