11from pathlib import Path
2+ from types import SimpleNamespace
23from typing import Any
34
45import asyncio
5- import sys
6-
76import pytest
7+ import sys
88
99sys .path .insert (0 , str (Path (__file__ ).resolve ().parents [1 ]))
1010
@@ -37,38 +37,92 @@ def delete(self, record: Any) -> None:
3737 self .deleted .append (record )
3838
3939
40- def test_integrity_preserves_unknown_files_and_removes_known_orphans (monkeypatch : pytest .MonkeyPatch , tmp_path : Path ) -> None :
40+ def _build_emoji_record (
41+ record_id : int ,
42+ full_path : Path | str ,
43+ * ,
44+ image_hash : str = "emoji_hash" ,
45+ is_registered : bool = True ,
46+ is_banned : bool = False ,
47+ no_file_flag : bool = False ,
48+ ) -> SimpleNamespace :
49+ return SimpleNamespace (
50+ id = record_id ,
51+ image_type = emoji_module .ImageType .EMOJI ,
52+ image_hash = image_hash ,
53+ description = "开心" ,
54+ full_path = str (full_path ),
55+ query_count = 0 ,
56+ is_registered = is_registered ,
57+ is_banned = is_banned ,
58+ no_file_flag = no_file_flag ,
59+ register_time = None ,
60+ last_used_time = None ,
61+ )
62+
63+
64+ def test_load_removes_registered_records_when_file_missing (monkeypatch : pytest .MonkeyPatch , tmp_path : Path ) -> None :
65+ available_file = tmp_path / "available.png"
66+ missing_file = tmp_path / "missing.png"
67+ available_file .write_bytes (b"available" )
68+
69+ available_record = _build_emoji_record (1 , available_file , image_hash = "available_hash" )
70+ missing_record = _build_emoji_record (2 , missing_file , image_hash = "missing_hash" )
71+ session = _Session (records = [available_record , missing_record ])
72+ monkeypatch .setattr (emoji_module , "get_db_session" , lambda : session )
73+
74+ manager = emoji_module .EmojiManager ()
75+ try :
76+ manager .load_emojis_from_db ()
77+
78+ assert [emoji .file_hash for emoji in manager .emojis ] == ["available_hash" ]
79+ assert manager ._emoji_num == 1
80+ assert session .deleted == [missing_record ]
81+ finally :
82+ manager .shutdown ()
83+
84+
85+ def test_integrity_preserves_unknown_files_and_removes_missing_registered_records (
86+ monkeypatch : pytest .MonkeyPatch ,
87+ tmp_path : Path ,
88+ ) -> None :
4189 monkeypatch .setattr (emoji_module , "EMOJI_DIR" , tmp_path )
4290 new_file = tmp_path / "new.png"
43- known_orphan = tmp_path / "known .png"
91+ missing_file = tmp_path / "missing .png"
4492 new_file .write_bytes (b"new" )
45- known_orphan .write_bytes (b"known" )
4693
47- session = _Session (records = [])
94+ missing_record = _build_emoji_record (1 , missing_file , image_hash = "missing_hash" )
95+ session = _Session (records = [missing_record ])
4896 monkeypatch .setattr (emoji_module , "get_db_session" , lambda : session )
4997
5098 manager = emoji_module .EmojiManager ()
5199 try :
52- manager ._known_emoji_file_paths = {known_orphan .absolute ().resolve ()}
53-
54100 manager .check_emoji_file_integrity ()
55101
56102 assert new_file .exists ()
57- assert not known_orphan .exists ()
103+ assert session .deleted == [missing_record ]
104+ assert manager .emojis == []
58105 finally :
59106 manager .shutdown ()
60107
61108
62109@pytest .mark .asyncio
63- async def test_periodic_maintenance_scans_unknown_files_before_integrity (
110+ async def test_periodic_maintenance_scans_unregistered_records_before_integrity (
64111 monkeypatch : pytest .MonkeyPatch ,
65112 tmp_path : Path ,
66113) -> None :
67114 monkeypatch .setattr (emoji_module , "EMOJI_DIR" , tmp_path )
68- known_file = tmp_path / "known.png"
69- new_file = tmp_path / "new.png"
70- known_file .write_bytes (b"known" )
71- new_file .write_bytes (b"new" )
115+ registered_file = tmp_path / "registered.png"
116+ unregistered_file = tmp_path / "unregistered.png"
117+ registered_file .write_bytes (b"registered" )
118+ unregistered_file .write_bytes (b"unregistered" )
119+ session = _Session (
120+ records = [
121+ _build_emoji_record (1 , registered_file , image_hash = "registered_hash" ),
122+ _build_emoji_record (2 , unregistered_file , image_hash = "unregistered_hash" , is_registered = False ),
123+ ]
124+ )
125+ monkeypatch .setattr (emoji_module , "get_db_session" , lambda : session )
72126
73127 monkeypatch .setattr (emoji_module .global_config .emoji , "steal_emoji" , True )
74128 monkeypatch .setattr (emoji_module .global_config .emoji , "check_interval" , 0 )
@@ -79,7 +133,6 @@ async def test_periodic_maintenance_scans_unknown_files_before_integrity(
79133 first_check = asyncio .Event ()
80134
81135 manager = emoji_module .EmojiManager ()
82- manager ._known_emoji_file_paths = {known_file .absolute ().resolve ()}
83136
84137 async def _register_emoji_by_filename (path : Path | str ) -> emoji_module .EmojiRegisterStatus :
85138 emoji_path = Path (path )
@@ -105,6 +158,6 @@ def _check_emoji_file_integrity() -> None:
105158 await task
106159 manager .shutdown ()
107160
108- assert events [0 ] == ("scan" , "new .png" )
161+ assert events [0 ] == ("scan" , "unregistered .png" )
109162 assert events [1 ] == ("check" , "" )
110- assert ("scan" , "known .png" ) not in events
163+ assert ("scan" , "registered .png" ) not in events
0 commit comments