2026-04-30 14:11:46 +08:00
|
|
|
"""MemoryIngestService 将 lineage_json 传入 create_source。"""
|
2026-04-08 15:37:09 +08:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from types import SimpleNamespace
|
|
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
import pytest
|
2026-04-08 15:37:09 +08:00
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
from app.features.memory.ingest_service import MemoryIngestService
|
2026-04-08 15:37:09 +08:00
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_memory_ingest_passes_lineage(monkeypatch) -> None:
|
2026-04-08 15:37:09 +08:00
|
|
|
captured: dict = {}
|
|
|
|
|
|
|
|
|
|
class FakeSession:
|
|
|
|
|
commit_calls = 0
|
|
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
async def commit(self) -> None:
|
2026-04-08 15:37:09 +08:00
|
|
|
self.commit_calls += 1
|
|
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
async def flush(self) -> None:
|
2026-04-08 15:37:09 +08:00
|
|
|
pass
|
|
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
class FakeScheduler:
|
|
|
|
|
def schedule(self, request):
|
|
|
|
|
captured["scheduled"] = request
|
|
|
|
|
return "task-1"
|
2026-04-08 15:37:09 +08:00
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
async def fake_create_source(session, **kwargs):
|
2026-04-08 15:37:09 +08:00
|
|
|
captured.update(kwargs)
|
|
|
|
|
return SimpleNamespace(id="src-1")
|
|
|
|
|
|
2026-04-30 14:11:46 +08:00
|
|
|
async def fake_create_chunk(*_args, **kwargs):
|
|
|
|
|
return SimpleNamespace(id=f"ch-{kwargs.get('chunk_index')}")
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
monkeypatch.setattr(
|
2026-04-30 14:11:46 +08:00
|
|
|
"app.features.memory.ingest_service.create_source",
|
2026-04-08 15:37:09 +08:00
|
|
|
fake_create_source,
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setattr(
|
2026-04-30 14:11:46 +08:00
|
|
|
"app.features.memory.ingest_service.create_chunk",
|
|
|
|
|
fake_create_chunk,
|
2026-04-08 15:37:09 +08:00
|
|
|
)
|
|
|
|
|
monkeypatch.setattr("app.core.config.settings.memory_enrichment_enabled", False)
|
|
|
|
|
|
|
|
|
|
lineage = {
|
|
|
|
|
"schema_version": 1,
|
|
|
|
|
"conversation_id": "c9",
|
|
|
|
|
"turns": [
|
|
|
|
|
{"user_message_id": "um-1", "assistant_message_id": "as-1"},
|
|
|
|
|
],
|
|
|
|
|
"primary_user_message_id": "um-1",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fake_session = FakeSession()
|
2026-04-30 14:11:46 +08:00
|
|
|
service = MemoryIngestService(
|
|
|
|
|
fake_session, # type: ignore[arg-type]
|
|
|
|
|
embedding_provider=None,
|
|
|
|
|
enrichment_scheduler=FakeScheduler(), # type: ignore[arg-type]
|
|
|
|
|
)
|
|
|
|
|
sid = await service.ingest_transcript(
|
2026-04-08 15:37:09 +08:00
|
|
|
"u1",
|
|
|
|
|
"c9",
|
|
|
|
|
"hello there",
|
|
|
|
|
lineage_json=lineage,
|
|
|
|
|
)
|
|
|
|
|
assert sid == "src-1"
|
|
|
|
|
assert captured.get("lineage_json") == lineage
|
|
|
|
|
assert captured.get("primary_user_message_id") == "um-1"
|
2026-04-30 14:11:46 +08:00
|
|
|
assert captured["scheduled"].source_id == "src-1"
|