"""Tests for view_note tool that exercise the full stack with SQLite.""" from textwrap import dedent from unittest.mock import MagicMock, patch import pytest import pytest_asyncio from basic_memory.mcp.tools import write_note, view_note from basic_memory.schemas.search import SearchResponse, SearchItemType @pytest_asyncio.fixture async def mock_call_get(): """Mock for call_get to simulate different responses.""" with patch("basic_memory.mcp.tools.read_note.call_get") as mock: # Default to 404 - not found mock_response = MagicMock() mock_response.status_code = 404 mock.return_value = mock_response yield mock @pytest_asyncio.fixture async def mock_search(): """Mock for search tool.""" with patch("basic_memory.mcp.tools.read_note.search_notes.fn") as mock: # Default to empty results mock.return_value = SearchResponse(results=[], current_page=1, page_size=1) yield mock @pytest.mark.asyncio async def test_view_note_basic_functionality(app): """Test viewing a note creates an artifact.""" # First create a note await write_note.fn( title="Test View Note", folder="test", content="# Test View Note\n\nThis is test content for viewing.", ) # View the note result = await view_note.fn("Test View Note") # Should contain artifact XML assert '" in result # Should contain the note content within the artifact assert "# Test View Note" in result assert "This is test content for viewing." in result # Should have confirmation message assert "✅ Note displayed as artifact" in result @pytest.mark.asyncio async def test_view_note_with_frontmatter_title(app): """Test viewing a note extracts title from frontmatter.""" # Create note with frontmatter content = dedent(""" --- title: "Frontmatter Title" tags: [test] --- # Frontmatter Title Content with frontmatter title. """).strip() await write_note.fn(title="Frontmatter Title", folder="test", content=content) # View the note result = await view_note.fn("Frontmatter Title") # Should extract title from frontmatter assert 'title="Frontmatter Title"' in result assert "✅ Note displayed as artifact: **Frontmatter Title**" in result @pytest.mark.asyncio async def test_view_note_with_heading_title(app): """Test viewing a note extracts title from first heading when no frontmatter.""" # Create note with heading but no frontmatter title content = "# Heading Title\n\nContent with heading title." await write_note.fn(title="Heading Title", folder="test", content=content) # View the note result = await view_note.fn("Heading Title") # Should extract title from heading assert 'title="Heading Title"' in result assert "✅ Note displayed as artifact: **Heading Title**" in result @pytest.mark.asyncio async def test_view_note_unicode_content(app): """Test viewing a note with Unicode content.""" content = "# Unicode Test 🚀\n\nThis note has emoji 🎉 and unicode ♠♣♥♦" await write_note.fn(title="Unicode Test 🚀", folder="test", content=content) # View the note result = await view_note.fn("Unicode Test 🚀") # Should handle Unicode properly assert "🚀" in result assert "🎉" in result assert "♠♣♥♦" in result assert '