from pathlib import Path import pytest from app.adapters.fake import FakeEmbeddingProvider from app.core.config import Settings from app.tools.seed_demo import ( BAILIAN_NAMESPACE, OFFLINE_NAMESPACE, embed_in_batches, embedding_profile_hash, load_documents, namespace_for_mode, prepare_chunks, ) PROJECT_ROOT = Path(__file__).resolve().parents[3] DOCUMENTS_PATH = PROJECT_ROOT / "data" / "samples" / "public" / "demo_documents.jsonl" @pytest.mark.asyncio async def test_seed_preparation_batches_and_hash_binds_twenty_documents() -> None: documents = load_documents(DOCUMENTS_PATH) settings = Settings() provider = FakeEmbeddingProvider() texts = [ f"标题:{item.title}\n地区:{item.region}\n矿种:{item.mineral}\n正文:{item.content}" for item in documents ] vectors, model = await embed_in_batches(provider, texts) chunks = prepare_chunks( documents, vectors, profile_hash=embedding_profile_hash(settings, "fake"), embedding_model=model, ) assert len(chunks) == 20 assert all(len(item.vector) == 1024 for item in chunks) assert all(item.embedding_text == item.embedding_prefix + item.cloud_text for item in chunks) assert len({item.chunk_id for item in chunks}) == 20 assert all(len(item.outbound_manifest_sha256) == 64 for item in chunks) assert all(item.embedding_elapsed_ms >= 0 for item in chunks) @pytest.mark.asyncio async def test_live_and_offline_seed_namespaces_cannot_share_document_identity() -> None: documents = load_documents(DOCUMENTS_PATH) settings = Settings() vectors, model = await embed_in_batches( FakeEmbeddingProvider(), [f"标题:{item.title}\n正文:{item.content}" for item in documents], ) profile_hash = embedding_profile_hash(settings, "fake") offline = prepare_chunks( documents, vectors, profile_hash=profile_hash, embedding_model=model, namespace=OFFLINE_NAMESPACE, ) live = prepare_chunks( documents, vectors, profile_hash=profile_hash, embedding_model=model, namespace=BAILIAN_NAMESPACE, ) assert OFFLINE_NAMESPACE.knowledge_base_id != BAILIAN_NAMESPACE.knowledge_base_id assert OFFLINE_NAMESPACE.access_scope_id != BAILIAN_NAMESPACE.access_scope_id assert {item.document_id for item in offline}.isdisjoint({item.document_id for item in live}) assert namespace_for_mode("fake") is OFFLINE_NAMESPACE assert namespace_for_mode("bailian") is BAILIAN_NAMESPACE def test_seed_rejects_unknown_provider_namespace() -> None: with pytest.raises(ValueError, match="invalid_provider_mode"): namespace_for_mode("unknown") def test_seed_rejects_fixture_not_explicitly_marked_synthetic(tmp_path: Path) -> None: path = tmp_path / "documents.jsonl" path.write_text( '{"doc_id":"x","title":"x","content":"x","region":"x",' '"mineral":"x","page_no":1,"source_type":"unknown",' '"review_state":"LOCAL_PARSED_PENDING_CLOUD_REVIEW",' '"cloud_policy_id":"synthetic-demo-v1"}\n', encoding="utf-8", ) with pytest.raises(ValueError, match="non_synthetic_document"): load_documents(path)