|
1 | 1 | """Tests for the edit_note MCP tool.""" |
2 | 2 |
|
| 3 | + |
3 | 4 | import pytest |
4 | 5 |
|
5 | 6 | from basic_memory.mcp.tools.edit_note import edit_note |
| 7 | +from basic_memory.mcp.tools.read_note import read_note |
6 | 8 | from basic_memory.mcp.tools.write_note import write_note |
7 | 9 |
|
8 | 10 |
|
@@ -613,6 +615,70 @@ async def test_edit_note_preserves_permalink_when_frontmatter_missing(client, te |
613 | 615 | # The edit should succeed without validation errors |
614 | 616 |
|
615 | 617 |
|
| 618 | +@pytest.mark.asyncio |
| 619 | +async def test_edit_note_find_replace_rejects_fuzzy_match(client, test_project): |
| 620 | + """find_replace must reject nonexistent identifiers, not fuzzy-match to a similar note.""" |
| 621 | + # Create two notes that could be fuzzy-matched |
| 622 | + await write_note( |
| 623 | + project=test_project.name, |
| 624 | + title="Routing Test A", |
| 625 | + directory="test", |
| 626 | + content="# Routing Test A\nContent A.", |
| 627 | + ) |
| 628 | + await write_note( |
| 629 | + project=test_project.name, |
| 630 | + title="Routing Test B", |
| 631 | + directory="test", |
| 632 | + content="# Routing Test B\nContent B.", |
| 633 | + ) |
| 634 | + |
| 635 | + # Attempt to edit a nonexistent note — should error, not silently edit A or B |
| 636 | + result = await edit_note( |
| 637 | + project=test_project.name, |
| 638 | + identifier="Routing Test NONEXISTENT", |
| 639 | + operation="find_replace", |
| 640 | + content="replaced", |
| 641 | + find_text="Content", |
| 642 | + ) |
| 643 | + |
| 644 | + assert isinstance(result, str) |
| 645 | + assert "# Edit Failed" in result |
| 646 | + |
| 647 | + # Verify neither A nor B was modified |
| 648 | + content_a = await read_note("Routing Test A", project=test_project.name) |
| 649 | + assert "Content A" in content_a |
| 650 | + content_b = await read_note("Routing Test B", project=test_project.name) |
| 651 | + assert "Content B" in content_b |
| 652 | + |
| 653 | + |
| 654 | +@pytest.mark.asyncio |
| 655 | +async def test_edit_note_append_autocreate_not_fuzzy_match(client, test_project): |
| 656 | + """append to a nonexistent note should auto-create it, not fuzzy-match an existing note.""" |
| 657 | + await write_note( |
| 658 | + project=test_project.name, |
| 659 | + title="Existing Note Alpha", |
| 660 | + directory="test", |
| 661 | + content="# Existing Note Alpha\nOriginal content.", |
| 662 | + ) |
| 663 | + |
| 664 | + # Append to a nonexistent note — should create a new note, not edit "Existing Note Alpha" |
| 665 | + result = await edit_note( |
| 666 | + project=test_project.name, |
| 667 | + identifier="Existing Note ZZZZZ", |
| 668 | + operation="append", |
| 669 | + content="# New Note\nBrand new content.", |
| 670 | + ) |
| 671 | + |
| 672 | + assert isinstance(result, str) |
| 673 | + assert "Created note (append)" in result |
| 674 | + assert "fileCreated: true" in result |
| 675 | + |
| 676 | + # Verify original note was NOT modified |
| 677 | + content = await read_note("Existing Note Alpha", project=test_project.name) |
| 678 | + assert "Original content" in content |
| 679 | + assert "Brand new content" not in content |
| 680 | + |
| 681 | + |
616 | 682 | @pytest.mark.asyncio |
617 | 683 | async def test_edit_note_insert_before_section_operation(client, test_project): |
618 | 684 | """Test inserting content before a section heading.""" |
|
0 commit comments