|
| 1 | +"""Test UTF-8 character handling in file operations.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from basic_memory import file_utils |
| 6 | +from basic_memory.file_utils import write_file_atomic, compute_checksum |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_write_utf8_characters(tmp_path): |
| 11 | + """Test writing files with UTF-8 characters.""" |
| 12 | + # Create a test file with various UTF-8 characters |
| 13 | + test_path = tmp_path / "utf8_test.md" |
| 14 | + |
| 15 | + # Include characters from various scripts |
| 16 | + utf8_content = """# UTF-8 Test Document |
| 17 | + |
| 18 | +## Cyrillic |
| 19 | +Привет мир! (Hello world in Russian) |
| 20 | +
|
| 21 | +## Greek |
| 22 | +Γειά σου Κόσμε! (Hello world in Greek) |
| 23 | +
|
| 24 | +## Chinese |
| 25 | +你好世界! (Hello world in Chinese) |
| 26 | +
|
| 27 | +## Japanese |
| 28 | +こんにちは世界! (Hello world in Japanese) |
| 29 | +
|
| 30 | +## Arabic |
| 31 | +مرحبا بالعالم! (Hello world in Arabic) |
| 32 | +
|
| 33 | +## Emoji |
| 34 | +🌍 🌎 🌏 🌐 🗺️ 🧭 |
| 35 | +""" |
| 36 | + |
| 37 | + # Test the atomic write function with UTF-8 content |
| 38 | + await write_file_atomic(test_path, utf8_content) |
| 39 | + |
| 40 | + # Verify the file was written correctly |
| 41 | + assert test_path.exists() |
| 42 | + |
| 43 | + # Read the file back and verify content |
| 44 | + content = test_path.read_text(encoding="utf-8") |
| 45 | + assert "Привет мир!" in content |
| 46 | + assert "Γειά σου Κόσμε!" in content |
| 47 | + assert "你好世界" in content |
| 48 | + assert "こんにちは世界" in content |
| 49 | + assert "مرحبا بالعالم" in content |
| 50 | + assert "🌍 🌎 🌏" in content |
| 51 | + |
| 52 | + # Verify checksums match |
| 53 | + checksum1 = await compute_checksum(utf8_content) |
| 54 | + checksum2 = await compute_checksum(content) |
| 55 | + assert checksum1 == checksum2 |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_frontmatter_with_utf8(tmp_path): |
| 60 | + """Test handling of frontmatter with UTF-8 characters.""" |
| 61 | + # Create a test file with frontmatter containing UTF-8 |
| 62 | + test_path = tmp_path / "frontmatter_utf8.md" |
| 63 | + |
| 64 | + # Create content with UTF-8 in frontmatter |
| 65 | + content = """--- |
| 66 | +title: UTF-8 测试文件 (Test File) |
| 67 | +author: José García |
| 68 | +keywords: |
| 69 | + - тестирование |
| 70 | + - 테스트 |
| 71 | + - 测试 |
| 72 | +--- |
| 73 | +
|
| 74 | +# UTF-8 Support Test |
| 75 | +
|
| 76 | +This file has UTF-8 characters in the frontmatter. |
| 77 | +""" |
| 78 | + |
| 79 | + # Write the file |
| 80 | + await write_file_atomic(test_path, content) |
| 81 | + |
| 82 | + # Update frontmatter with more UTF-8 |
| 83 | + await file_utils.update_frontmatter( |
| 84 | + test_path, |
| 85 | + { |
| 86 | + "description": "这是一个测试文件 (This is a test file)", |
| 87 | + "tags": ["国际化", "юникод", "🔤"], |
| 88 | + }, |
| 89 | + ) |
| 90 | + |
| 91 | + # Read back and check |
| 92 | + updated_content = test_path.read_text(encoding="utf-8") |
| 93 | + |
| 94 | + # Original values should be preserved |
| 95 | + assert "title: UTF-8 测试文件" in updated_content |
| 96 | + assert "author: José García" in updated_content |
| 97 | + |
| 98 | + # New values should be added |
| 99 | + assert "description: 这是一个测试文件" in updated_content |
| 100 | + assert "国际化" in updated_content |
| 101 | + assert "юникод" in updated_content |
| 102 | + assert "🔤" in updated_content |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_utf8_in_entity_sync(sync_service, test_config): |
| 107 | + """Test syncing an entity with UTF-8 content.""" |
| 108 | + project_dir = test_config.home |
| 109 | + |
| 110 | + # Create a test file with UTF-8 characters |
| 111 | + test_file = project_dir / "utf8_entity.md" |
| 112 | + utf8_content = """--- |
| 113 | +type: knowledge |
| 114 | +permalink: i18n/utf8-document |
| 115 | +--- |
| 116 | +# UTF-8 测试文档 |
| 117 | +
|
| 118 | +## Observations |
| 119 | +- [language] العربية (Arabic) is written right-to-left |
| 120 | +- [language] 日本語 (Japanese) uses three writing systems |
| 121 | +- [encoding] UTF-8 可以编码所有 Unicode 字符 |
| 122 | +
|
| 123 | +## Relations |
| 124 | +- relates_to [[i18n/Ελληνικά]] |
| 125 | +- relates_to [[i18n/Русский]] |
| 126 | +""" |
| 127 | + |
| 128 | + # Write the file |
| 129 | + test_file.parent.mkdir(parents=True, exist_ok=True) |
| 130 | + test_file.write_text(utf8_content, encoding="utf-8") |
| 131 | + |
| 132 | + # Sync the file |
| 133 | + await sync_service.sync(test_config.home, show_progress=False) |
| 134 | + |
| 135 | + # Verify entity was created |
| 136 | + entity = await sync_service.entity_service.get_by_permalink("i18n/utf8-document") |
| 137 | + assert entity is not None |
| 138 | + |
| 139 | + # Verify observations were created with UTF-8 content |
| 140 | + assert len(entity.observations) == 3 |
| 141 | + |
| 142 | + # Check observation content |
| 143 | + obs_categories = [o.category for o in entity.observations] |
| 144 | + obs_content = [o.content for o in entity.observations] |
| 145 | + |
| 146 | + assert "language" in obs_categories |
| 147 | + assert "encoding" in obs_categories |
| 148 | + assert any("العربية" in c for c in obs_content) |
| 149 | + assert any("日本語" in c for c in obs_content) |
| 150 | + assert any("Unicode" in c for c in obs_content) |
| 151 | + |
| 152 | + # Check relations |
| 153 | + assert len(entity.relations) == 2 |
| 154 | + relation_names = [r.to_name for r in entity.relations] |
| 155 | + assert "i18n/Ελληνικά" in relation_names |
| 156 | + assert "i18n/Русский" in relation_names |
| 157 | + |
| 158 | + |
| 159 | +@pytest.mark.asyncio |
| 160 | +async def test_write_file_service_utf8(sync_service, test_config): |
| 161 | + """Test FileService handling of UTF-8 content.""" |
| 162 | + file_service = sync_service.file_service |
| 163 | + |
| 164 | + # Test writing UTF-8 content through the file service |
| 165 | + test_path = "utf8_service_test.md" |
| 166 | + utf8_content = """--- |
| 167 | +title: FileService UTF-8 Test |
| 168 | +--- |
| 169 | +# UTF-8 通过 FileService 测试 |
| 170 | +
|
| 171 | +Testing FileService with UTF-8: |
| 172 | +* Português: Olá mundo! |
| 173 | +* Thai: สวัสดีชาวโลก |
| 174 | +* Korean: 안녕하세요 세계 |
| 175 | +* Hindi: नमस्ते दुनिया |
| 176 | +""" |
| 177 | + |
| 178 | + # Use the file service to write the file |
| 179 | + checksum = await file_service.write_file(test_path, utf8_content) |
| 180 | + |
| 181 | + # Verify file exists |
| 182 | + full_path = file_service.base_path / test_path |
| 183 | + assert full_path.exists() |
| 184 | + |
| 185 | + # Read back content and verify |
| 186 | + content, read_checksum = await file_service.read_file(test_path) |
| 187 | + |
| 188 | + assert "通过" in content |
| 189 | + assert "Português" in content |
| 190 | + assert "สวัสดีชาวโลก" in content |
| 191 | + assert "안녕하세요" in content |
| 192 | + assert "नमस्ते" in content |
| 193 | + |
| 194 | + # Checksums should match |
| 195 | + assert checksum == read_checksum |
0 commit comments