sync update from LAN repo (source ji @ 85078a31)
This commit is contained in:
1 parent
5d6627af50
commit
e602d88695
864 files changed
+99150
-6346
No files matched your search
@@ -0,0 +1,192 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
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"}
|
||||
|
||||
|
||||
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_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_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
|
||||
)
|
||||
Reference in new issue
Block a user