fix(file_watcher): reset stop event on restart (#233)

This commit is contained in:
Aqil Aziz 2026-05-14 13:37:17 +07:00 • committed by GitHub
parent d72f5fc581
commit ccadf1d3f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 27 deletions

View file

@ -76,12 +76,14 @@ class BaseFileWatcher:
if self._running:
return
self._stop_event = asyncio.Event()
self._running = True
async def _initialize_and_watch():
if self.rebuild_index_on_start:
await self.file_store.clear_all()
logger.info("Cleared all indexed data on start")
if self.file_store is not None:
await self.file_store.clear_all()
logger.info("Cleared all indexed data on start")
await self._scan_existing_files()
await self._watch_loop()

View file

@ -79,6 +79,15 @@ def temp_nested_dir(temp_dir: Path):
yield temp_dir
def make_mock_file_store():
"""Create an async-compatible mock file store."""
mock_file_store = MagicMock()
mock_file_store.clear_all = AsyncMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
return mock_file_store
# ==================== Test Existing Paths ====================
@ -147,6 +156,21 @@ class TestExistingPaths:
await watcher.close()
@pytest.mark.asyncio
async def test_restart_resets_stop_event(self, temp_dir: Path):
"""Test restarting watcher resets the previous stop signal."""
watcher = BaseFileWatcher(watch_paths=str(temp_dir))
await watcher.start()
await watcher.close()
assert watcher._stop_event.is_set() is True
await watcher.start()
assert watcher._stop_event.is_set() is False
await watcher.close()
@pytest.mark.asyncio
async def test_multiple_start_calls(self, temp_dir: Path):
"""Test that multiple start calls don't create multiple tasks."""
@ -381,9 +405,7 @@ class TestRebuildIndexOnStart:
callback_called.append(changes)
# Create mock file_store
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
watcher = BaseFileWatcher(
watch_paths=str(temp_dir),
@ -408,9 +430,7 @@ class TestRebuildIndexOnStart:
callback_called.append(changes)
# Create mock file_store
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
watcher = BaseFileWatcher(
watch_paths=str(temp_dir),
@ -441,9 +461,7 @@ class TestRebuildIndexOnStart:
async def callback(changes):
callback_called.append(changes)
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
watcher = BaseFileWatcher(
watch_paths=str(temp_dir),
@ -474,9 +492,7 @@ class TestRebuildIndexOnStart:
async def callback(changes):
callback_called.append(changes)
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
watcher = BaseFileWatcher(
watch_paths=str(temp_nested_dir),
@ -510,9 +526,7 @@ class TestRebuildIndexOnStart:
async def callback(changes):
callback_called.append(changes)
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
watcher = BaseFileWatcher(
watch_paths=str(temp_nested_dir),
@ -545,9 +559,7 @@ class TestRebuildIndexOnStart:
async def callback(changes):
callback_called.append(changes)
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
watcher = BaseFileWatcher(
watch_paths="/nonexistent/path",
@ -698,9 +710,7 @@ class TestEdgeCases:
"""Test watching a single file instead of directory."""
file_path = temp_files["txt_0"]
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
callback_called = []
@ -731,9 +741,7 @@ class TestEdgeCases:
empty_dir = temp_dir / "empty"
empty_dir.mkdir()
mock_file_store = MagicMock()
mock_file_store.list_files = AsyncMock(return_value=[])
mock_file_store.get_file_chunks = AsyncMock(return_value=[])
mock_file_store = make_mock_file_store()
callback_called = []
@ -774,7 +782,7 @@ class TestEdgeCases:
unicode_dir.mkdir()
file_path = unicode_dir / "文件.txt"
file_path.write_text("内容")
file_path.write_text("内容", encoding="utf-8")
watcher = BaseFileWatcher(
watch_paths=str(unicode_dir),