sync update from LAN repo (source dhk-1 @ 3e87be55)
This commit is contained in:
1 parent
ab6068a7c1
commit
42f2a26fa8
1482 files changed
+227759
-9584
No files matched your search
@@ -5,10 +5,10 @@ from io import BytesIO
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI, UploadFile
|
||||
from fastapi import FastAPI, HTTPException, UploadFile
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.routers import wiki_packages
|
||||
from app.gateway.routers import assistant_knowledge, wiki_packages
|
||||
from deerflow.assistant_knowledge.archive import PackageArchive
|
||||
from deerflow.assistant_knowledge.package_io import write_package
|
||||
from deerflow.config.llmwiki_config import LocalWikiEmbeddingConfig
|
||||
@@ -22,6 +22,69 @@ from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.llmwiki_index.memory import MemoryLlmWikiIndexStore
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_existing_wiki_vectors_can_be_exported_without_runtime_encoder(monkeypatch):
|
||||
request = SimpleNamespace(
|
||||
app=SimpleNamespace(
|
||||
state=SimpleNamespace(llmwiki_sync_service=None),
|
||||
),
|
||||
)
|
||||
|
||||
async def has_vectors(_request, mapping_id):
|
||||
assert mapping_id == "mapping-1"
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(assistant_knowledge, "_has_local_wiki_vectors_for_export", has_vectors)
|
||||
|
||||
await assistant_knowledge._ensure_local_wiki_vectors_for_export(
|
||||
request,
|
||||
{"id": "mapping-1", "weknora_id": "remote-1"},
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_restore_rejects_target_with_raw_documents_before_upload(
|
||||
monkeypatch,
|
||||
tmp_path,
|
||||
):
|
||||
class Mappings:
|
||||
async def get_authorized(self, *_args, **_kwargs):
|
||||
return {"id": "target", "weknora_id": "remote-target"}
|
||||
|
||||
class Remote:
|
||||
async def get_knowledge_base(self, _knowledge_base_id):
|
||||
return {"indexing_strategy": {"wiki_enabled": True}}
|
||||
|
||||
async def list_wiki_pages(self, _knowledge_base_id, *, page_size):
|
||||
assert page_size == 1
|
||||
return {"total": 0, "pages": []}
|
||||
|
||||
async def list_documents(self, _knowledge_base_id, *, page_size):
|
||||
assert page_size == 1
|
||||
return {"total": 1, "items": [{"id": "already-uploaded"}]}
|
||||
|
||||
async def actor(_request, *, admin):
|
||||
assert admin is True
|
||||
return "admin"
|
||||
|
||||
app = FastAPI()
|
||||
app.state.config = None
|
||||
app.state.llmwiki_store = Mappings()
|
||||
request = SimpleNamespace(app=app)
|
||||
upload = UploadFile(filename="wiki-vector.jsonl", file=BytesIO(b"unused"))
|
||||
monkeypatch.setattr(wiki_packages, "_actor", actor)
|
||||
monkeypatch.setattr(wiki_packages, "_package_root", lambda: tmp_path)
|
||||
monkeypatch.setattr(wiki_packages, "get_resolved_llmwiki_runtime", lambda _config: None)
|
||||
monkeypatch.setattr(wiki_packages, "build_weknora_client", lambda _runtime: Remote())
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
await wiki_packages.upload_restore(request, "target", upload)
|
||||
|
||||
assert exc_info.value.status_code == 409
|
||||
assert "没有原始资料和 Wiki 页面" in str(exc_info.value.detail)
|
||||
assert not list(tmp_path.iterdir())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_metadata_vectors_and_links_survive_roundtrip_without_encoding(tmp_path, monkeypatch):
|
||||
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
|
||||
@@ -102,6 +165,9 @@ async def test_metadata_vectors_and_links_survive_roundtrip_without_encoding(tmp
|
||||
async def rebuild_wiki_links(self, kb):
|
||||
pass
|
||||
|
||||
async def upload_document(self, *_args, **_kwargs):
|
||||
raise AssertionError("Package restore must not upload raw documents")
|
||||
|
||||
class Embedding:
|
||||
dimensions = 3
|
||||
fingerprint = "test-fp"
|
||||
@@ -375,8 +441,10 @@ async def test_admin_can_upload_and_activate_offline_encoder(monkeypatch, tmp_pa
|
||||
class Embedding:
|
||||
def __init__(self):
|
||||
self.model_path = ""
|
||||
self.reset_calls = 0
|
||||
|
||||
async def reset_local_encoder(self, *, model_path=None):
|
||||
self.reset_calls += 1
|
||||
if model_path is not None:
|
||||
self.model_path = model_path
|
||||
|
||||
@@ -395,6 +463,17 @@ async def test_admin_can_upload_and_activate_offline_encoder(monkeypatch, tmp_pa
|
||||
monkeypatch.setattr(llmwiki_index, "load_system_settings", SystemSettings)
|
||||
monkeypatch.setattr(llmwiki_index, "save_system_settings", lambda value: saved.append(value) or value)
|
||||
embedding = Embedding()
|
||||
|
||||
async def rebuild(_app, *, force=False):
|
||||
assert force is True
|
||||
embedding.model_path = saved[-1].local_wiki_encoder.model_path
|
||||
|
||||
monkeypatch.setattr(llmwiki_index, "rebuild_local_wiki_index_runtime", rebuild)
|
||||
monkeypatch.setattr(
|
||||
llmwiki_index,
|
||||
"local_encoder_runtime_status",
|
||||
lambda _app: embedding.local_encoder_status(),
|
||||
)
|
||||
app = FastAPI()
|
||||
app.state.config = SimpleNamespace(
|
||||
llmwiki=SimpleNamespace(
|
||||
@@ -414,11 +493,15 @@ async def test_admin_can_upload_and_activate_offline_encoder(monkeypatch, tmp_pa
|
||||
paths=["onnx/model.onnx", "config.json"],
|
||||
)
|
||||
|
||||
active = tmp_path / "models" / "bge-m3"
|
||||
revision = saved[-1].local_wiki_encoder.model_revision
|
||||
active = tmp_path / "models" / "bge-m3-revisions" / revision
|
||||
assert (active / "onnx" / "model.onnx").read_bytes() == b"onnx"
|
||||
assert result["uploaded_files"] == 2
|
||||
assert saved[-1].local_wiki_encoder.model_path == str(active.resolve())
|
||||
assert saved[-1].local_wiki_encoder.mode == "local"
|
||||
assert revision
|
||||
assert embedding.model_path == str(active.resolve())
|
||||
assert embedding.reset_calls == 0
|
||||
|
||||
|
||||
def test_encoder_upload_rejects_parent_traversal():
|
||||
|
||||
Reference in new issue
Block a user