|
| 1 | +"""Test that imported conversations are properly indexed with correct permalink and title. |
| 2 | +
|
| 3 | +This test verifies issue #452 - Imported conversations not indexed correctly. |
| 4 | +""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from basic_memory.config import ProjectConfig |
| 10 | +from basic_memory.importers.claude_conversations_importer import ClaudeConversationsImporter |
| 11 | +from basic_memory.markdown import EntityParser |
| 12 | +from basic_memory.markdown.markdown_processor import MarkdownProcessor |
| 13 | +from basic_memory.repository import EntityRepository |
| 14 | +from basic_memory.services import EntityService |
| 15 | +from basic_memory.services.search_service import SearchService |
| 16 | +from basic_memory.schemas.search import SearchQuery |
| 17 | +from basic_memory.sync.sync_service import SyncService |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.asyncio |
| 21 | +async def test_imported_conversations_have_correct_permalink_and_title( |
| 22 | + project_config: ProjectConfig, |
| 23 | + sync_service: SyncService, |
| 24 | + entity_service: EntityService, |
| 25 | + entity_repository: EntityRepository, |
| 26 | + search_service: SearchService, |
| 27 | +): |
| 28 | + """Test that imported conversations have correct permalink and title after sync. |
| 29 | +
|
| 30 | + Issue #452: Imported conversations show permalink: null in search results |
| 31 | + and title shows as filename instead of frontmatter title. |
| 32 | + """ |
| 33 | + base_path = project_config.home |
| 34 | + |
| 35 | + # Create parser and processor for importer |
| 36 | + parser = EntityParser(base_path) |
| 37 | + processor = MarkdownProcessor(parser) |
| 38 | + |
| 39 | + # Create importer |
| 40 | + importer = ClaudeConversationsImporter(base_path, processor) |
| 41 | + |
| 42 | + # Sample conversation data |
| 43 | + conversations = [{ |
| 44 | + 'uuid': 'test-123', |
| 45 | + 'name': 'My Test Conversation Title', |
| 46 | + 'created_at': '2025-01-15T10:00:00Z', |
| 47 | + 'updated_at': '2025-01-15T11:00:00Z', |
| 48 | + 'chat_messages': [ |
| 49 | + { |
| 50 | + 'uuid': 'msg-1', |
| 51 | + 'sender': 'human', |
| 52 | + 'created_at': '2025-01-15T10:00:00Z', |
| 53 | + 'text': 'Hello world', |
| 54 | + 'content': [{'type': 'text', 'text': 'Hello world'}], |
| 55 | + 'attachments': [] |
| 56 | + }, |
| 57 | + { |
| 58 | + 'uuid': 'msg-2', |
| 59 | + 'sender': 'assistant', |
| 60 | + 'created_at': '2025-01-15T10:01:00Z', |
| 61 | + 'text': 'Hello!', |
| 62 | + 'content': [{'type': 'text', 'text': 'Hello!'}], |
| 63 | + 'attachments': [] |
| 64 | + } |
| 65 | + ] |
| 66 | + }] |
| 67 | + |
| 68 | + # Run import |
| 69 | + result = await importer.import_data(conversations, 'conversations') |
| 70 | + assert result.success, f"Import failed: {result}" |
| 71 | + assert result.conversations == 1 |
| 72 | + |
| 73 | + # Verify the file was created with correct content |
| 74 | + conv_path = base_path / 'conversations' / '20250115-My_Test_Conversation_Title.md' |
| 75 | + assert conv_path.exists(), f"Expected file at {conv_path}" |
| 76 | + |
| 77 | + content = conv_path.read_text() |
| 78 | + assert '---' in content, "File should have frontmatter markers" |
| 79 | + assert 'title: My Test Conversation Title' in content, "File should have title in frontmatter" |
| 80 | + assert 'permalink: conversations/20250115-My_Test_Conversation_Title' in content, "File should have permalink in frontmatter" |
| 81 | + |
| 82 | + # Run sync to index the imported file |
| 83 | + await sync_service.sync(base_path, project_config.name) |
| 84 | + |
| 85 | + # Verify entity in database |
| 86 | + entities = await entity_repository.find_all() |
| 87 | + assert len(entities) == 1, f"Expected 1 entity, got {len(entities)}" |
| 88 | + |
| 89 | + entity = entities[0] |
| 90 | + |
| 91 | + # These are the key assertions for issue #452 |
| 92 | + assert entity.title == 'My Test Conversation Title', f"Title should be from frontmatter, got: {entity.title}" |
| 93 | + assert entity.permalink == 'conversations/20250115-My_Test_Conversation_Title', f"Permalink should be from frontmatter, got: {entity.permalink}" |
| 94 | + |
| 95 | + # Verify search index also has correct data |
| 96 | + results = await search_service.search(SearchQuery(text='Test Conversation')) |
| 97 | + assert len(results) >= 1, "Should find the conversation in search" |
| 98 | + |
| 99 | + # Find our entity in search results |
| 100 | + search_result = next((r for r in results if r.entity_id == entity.id), None) |
| 101 | + assert search_result is not None, "Entity should be in search results" |
| 102 | + assert search_result.title == 'My Test Conversation Title', f"Search title should be from frontmatter, got: {search_result.title}" |
| 103 | + assert search_result.permalink == 'conversations/20250115-My_Test_Conversation_Title', f"Search permalink should not be null, got: {search_result.permalink}" |
0 commit comments