from __future__ import annotations import asyncio from datetime import UTC, datetime, timedelta from types import SimpleNamespace from typing import Any import pytest import app.gateway.llmwiki_index_scheduler as scheduler from app.gateway.llmwiki_index_scheduler import ( _mapping_is_due, schedule_local_wiki_mutation_sync, ) class _RemoteClient: def __init__(self, processing: list[Any]) -> None: self.processing = list(processing) self.status_reads = 0 async def get_knowledge_base(self, knowledge_base_id: str) -> dict[str, Any]: assert knowledge_base_id == "remote-kb" self.status_reads += 1 current = self.processing.pop(0) if self.processing else False return {"id": knowledge_base_id, "is_processing": current} class _SyncService: def __init__( self, processing: list[bool], results: list[dict[str, Any]] | None = None, ) -> None: self.client = _RemoteClient(processing) self.results = list(results or []) self.sync_calls = 0 self.seen_mappings: list[dict[str, Any]] = [] async def sync_mapping( self, mapping: dict[str, Any], *, force: bool, ) -> dict[str, Any]: assert mapping["id"] == "mapping-kb" assert force is False self.sync_calls += 1 self.seen_mappings.append(dict(mapping)) if self.results: return self.results.pop(0) return {"status": "completed"} class _BlockingBackoffSyncService(_SyncService): def __init__(self) -> None: super().__init__([False, False]) self.started = asyncio.Event() self.release = asyncio.Event() async def sync_mapping( self, mapping: dict[str, Any], *, force: bool, ) -> dict[str, Any]: assert force is False self.sync_calls += 1 self.seen_mappings.append(dict(mapping)) if self.sync_calls == 1: self.started.set() await self.release.wait() return { "status": "provider_backoff", "failure_scope": "embedding_provider", "retry_after_seconds": 60, } def _app(service: _SyncService) -> SimpleNamespace: return SimpleNamespace(state=SimpleNamespace(llmwiki_sync_service=service)) @pytest.mark.asyncio async def test_mutation_sync_reserves_scan_until_remote_processing_finishes() -> None: service = _SyncService([True, True, False, False, False, False]) task = schedule_local_wiki_mutation_sync( _app(service), {"id": "mapping-kb", "weknora_id": "remote-kb"}, delays=(0.001,), ) assert task is not None await asyncio.wait_for(task, timeout=1) # One scan immediately after processing clears plus one short quiet # confirmation protects against providers that publish the Wiki directory # a few seconds after flipping the processing flag. assert service.client.status_reads == 6 assert service.sync_calls == 2 @pytest.mark.asyncio async def test_mutation_sync_timeout_starts_from_latest_processing_observation( monkeypatch: pytest.MonkeyPatch, ) -> None: service = _SyncService([True, False, False, False]) clock = [0.0, 9.0, 11.0, 15.0, 16.0, 17.0] monkeypatch.setattr(scheduler, "_MUTATION_SYNC_MAX_SECONDS", 10.0) monkeypatch.setattr( scheduler, "monotonic", lambda: clock.pop(0) if clock else 17.0, ) task = schedule_local_wiki_mutation_sync( _app(service), {"id": "mapping-kb", "weknora_id": "remote-kb"}, delays=(0.0,), ) assert task is not None await asyncio.wait_for(task, timeout=1) # The second poll happens after the original upload-based deadline. A # confirmed in-progress observation must refresh that deadline so the # completed snapshot is still synchronized. assert service.sync_calls == 2 @pytest.mark.asyncio async def test_mutation_sync_stops_after_a_quiet_scan_observes_delta() -> None: service = _SyncService( [False, False], results=[{"status": "completed", "added_pages": 1}], ) task = schedule_local_wiki_mutation_sync( _app(service), {"id": "mapping-kb", "weknora_id": "remote-kb"}, delays=(0.001, 0.001, 0.001), ) assert task is not None await asyncio.wait_for(task, timeout=1) assert service.sync_calls == 1 @pytest.mark.asyncio async def test_mutation_sync_retries_quiet_old_snapshot_with_bounded_delays() -> None: service = _SyncService([False] * 6) task = schedule_local_wiki_mutation_sync( _app(service), {"id": "mapping-kb", "weknora_id": "remote-kb"}, delays=(0.001, 0.001, 0.001), ) assert task is not None await asyncio.wait_for(task, timeout=1) assert service.sync_calls == 3 @pytest.mark.asyncio async def test_mutation_sync_retries_service_processing_fence_then_confirms() -> None: service = _SyncService( [False] * 6, results=[ { "status": "partial", "added_pages": 1, "deferred_deletion": True, }, {"status": "completed"}, {"status": "completed"}, ], ) task = schedule_local_wiki_mutation_sync( _app(service), {"id": "mapping-kb", "weknora_id": "remote-kb"}, delays=(0.001, 0.001, 0.001), ) assert task is not None await asyncio.wait_for(task, timeout=1) assert service.sync_calls == 3 @pytest.mark.asyncio async def test_mutation_sync_stops_when_embedding_provider_breaker_is_open() -> None: service = _SyncService( [False], results=[ { "status": "provider_backoff", "failure_scope": "embedding_provider", "retry_after_seconds": 60, } ], ) task = schedule_local_wiki_mutation_sync( _app(service), {"id": "mapping-kb", "weknora_id": "remote-kb"}, delays=(0.001, 0.001, 0.001), ) assert task is not None await asyncio.wait_for(task, timeout=1) assert service.sync_calls == 1 @pytest.mark.asyncio async def test_new_mutation_during_provider_backoff_is_consumed_and_cleaned_up() -> None: service = _BlockingBackoffSyncService() app = _app(service) first = schedule_local_wiki_mutation_sync( app, {"id": "mapping-kb", "weknora_id": "remote-kb", "name": "old"}, delays=(0,), ) assert first is not None await asyncio.wait_for(service.started.wait(), timeout=1) second = schedule_local_wiki_mutation_sync( app, {"id": "mapping-kb", "weknora_id": "remote-kb", "name": "new"}, delays=(0,), ) assert second is first service.release.set() await asyncio.wait_for(first, timeout=1) assert service.sync_calls == 2 assert [mapping["name"] for mapping in service.seen_mappings] == ["old", "new"] assert app.state.llmwiki_mutation_sync_tasks == {} assert app.state.llmwiki_mutation_sync_plans == {} @pytest.mark.asyncio async def test_new_generation_wakes_backoff_and_uses_latest_mapping() -> None: service = _SyncService( [False, False], results=[{"status": "completed", "added_pages": 1}], ) app = _app(service) first = schedule_local_wiki_mutation_sync( app, {"id": "mapping-kb", "weknora_id": "remote-kb", "name": "old"}, delays=(0.2,), ) assert first is not None await asyncio.sleep(0.05) second = schedule_local_wiki_mutation_sync( app, {"id": "mapping-kb", "weknora_id": "remote-kb", "name": "new"}, delays=(0.2,), ) assert second is first # The original deadline has passed, but the newer mutation reset the # debounce window; a lost wakeup would already have launched the scan. await asyncio.sleep(0.17) assert service.sync_calls == 0 await asyncio.wait_for(first, timeout=1) assert service.seen_mappings[0]["name"] == "new" def test_mapping_due_treats_string_false_as_not_processing() -> None: now = datetime.now(UTC) state = { "index_revision": 1, "state": "idle", "last_success_at": now - timedelta(minutes=1), } remote = { "updated_at": now, "isProcessing": "false", "processingCount": "0", } assert _mapping_is_due({}, state, remote, force_scan=False) is True assert ( _mapping_is_due( {}, state, {**remote, "isProcessing": "true"}, force_scan=False, ) is False ) assert ( _mapping_is_due( {}, state, {**remote, "isProcessing": "true"}, force_scan=True, ) is False )