Files
RAG/backend/tests/unit/test_seed_demo.py
YoVinchen 75592af33a
Some checks failed
verify / verify (push) Has been cancelled
Isolate cloud model access before enabling product RAG workflows
The API and ingestion tools now use a fixed internal model gateway while
governed profiles, embedding cache assignments, traceable citations, and
stable API errors establish the boundaries required by later workflows.

Constraint: The current Alibaba Cloud workspace rejects all three live model calls with authentication failures
Rejected: Give the API or seed tools the Bailian key and direct egress | combines database access, cloud credentials, and public network access
Rejected: Mix offline and Bailian vectors in one demo namespace | makes profile activation and retrieval ambiguous
Confidence: high
Scope-risk: moderate
Reversibility: clean
Directive: Keep Bailian credentials and egress exclusive to model-gateway and create a new immutable profile hash for any embedding identity change
Tested: make verify; 121 backend tests; 14 frontend tests; fresh and populated Alembic upgrade-downgrade-upgrade; two idempotent offline seeds; Docker health and HTTP retrieval; isolated provider smoke
Not-tested: Successful live Bailian responses because the supplied workspace credential currently fails authentication
2026-07-13 04:09:06 +08:00

96 lines
3.2 KiB
Python

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)