Files
deerflow-code/offline-backend-20260512/backend/packages/harness/tests/test_task_deeplink_threads.py
T

178 lines
6.6 KiB
Python

"""Dedicated taskId + goPath conversation-history coverage."""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
import pytest
from langgraph.store.memory import InMemoryStore
from sqlalchemy import select
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from deerflow.persistence.task_deeplink_threads.memory import (
MemoryTaskDeeplinkThreadStore,
)
from deerflow.persistence.task_deeplink_threads.model import TaskDeeplinkThreadRow
from deerflow.persistence.task_deeplink_threads.sql import TaskDeeplinkThreadRepository
from deerflow.persistence.thread_meta.memory import MemoryThreadMetaStore
from deerflow.persistence.thread_meta.model import ThreadMetaRow
from deerflow.persistence.thread_meta.sql import ThreadMetaRepository
@pytest.mark.asyncio
async def test_dedicated_history_is_partitioned_by_original_task_id_and_go_path():
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
session_factory = async_sessionmaker(engine, expire_on_commit=False)
try:
async with engine.begin() as connection:
await connection.run_sync(ThreadMetaRow.__table__.create)
await connection.run_sync(TaskDeeplinkThreadRow.__table__.create)
thread_repo = ThreadMetaRepository(session_factory)
history_repo = TaskDeeplinkThreadRepository(session_factory)
now = datetime.now(UTC)
await thread_repo.create(
"rwfx-oldest",
user_id="creator-a",
display_name="任务分析一",
metadata={
"taskId": "108",
"taskRecordId": "108",
"taskGoPath": "rwfx",
"thread_type": "task_deeplink",
"agent_id": "rwfx-agent",
},
)
await thread_repo.create(
"three-q",
user_id="creator-b",
metadata={
"taskId": "108",
"taskRecordId": "108",
"taskGoPath": "3qfx",
"thread_type": "task_deeplink",
"agent_id": "three-q-agent",
},
)
await thread_repo.create(
"xdfx-action",
user_id="creator-c",
metadata={
"taskId": "108",
"taskRecordId": "action-9",
"taskGoPath": "xdfx",
"thread_type": "task_deeplink",
"actionId": "action-9",
"agent_id": "xdfx-agent",
},
)
# A legacy task thread has no taskGoPath marker and must stay out of
# the new table: old data is not migrated implicitly.
await thread_repo.create(
"legacy-task-thread",
user_id="creator-d",
metadata={"taskId": "108", "agent_id": "rwfx-agent"},
)
# Listing orders by the canonical thread metadata timestamp/title,
# not stale duplicated presentation data in the dedicated index.
async with session_factory() as session:
row = await session.get(ThreadMetaRow, "rwfx-oldest")
assert row is not None
row.updated_at = now + timedelta(minutes=1)
await session.commit()
rwfx = await history_repo.list_threads("108", "rwfx")
three_q = await history_repo.list_threads("108", "3qfx")
xdfx = await history_repo.list_threads("action-9", "xdfx")
assert [row["thread_id"] for row in rwfx] == ["rwfx-oldest"]
assert rwfx[0]["values"] == {"title": "任务分析一"}
assert [row["thread_id"] for row in three_q] == ["three-q"]
assert [row["thread_id"] for row in xdfx] == ["xdfx-action"]
async with session_factory() as session:
indexed_ids = set((await session.scalars(select(TaskDeeplinkThreadRow.thread_id))).all())
assert indexed_ids == {"rwfx-oldest", "three-q", "xdfx-action"}
finally:
await engine.dispose()
@pytest.mark.asyncio
async def test_metadata_registration_and_thread_delete_keep_index_in_sync():
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
session_factory = async_sessionmaker(engine, expire_on_commit=False)
try:
async with engine.begin() as connection:
await connection.run_sync(ThreadMetaRow.__table__.create)
await connection.run_sync(TaskDeeplinkThreadRow.__table__.create)
thread_repo = ThreadMetaRepository(session_factory)
history_repo = TaskDeeplinkThreadRepository(session_factory)
await thread_repo.create(
"late-binding",
user_id="creator",
metadata={"taskId": "2055", "agent_id": "three-q-agent"},
)
assert await history_repo.list_threads("2055", "3qfx") == []
await thread_repo.update_metadata(
"late-binding",
{
"taskRecordId": "2055",
"taskGoPath": "3qfx",
"thread_type": "task_deeplink",
},
user_id=None,
)
assert [row["thread_id"] for row in await history_repo.list_threads("2055", "3qfx")] == ["late-binding"]
await thread_repo.delete("late-binding", user_id=None)
assert await history_repo.list_threads("2055", "3qfx") == []
finally:
await engine.dispose()
@pytest.mark.asyncio
async def test_memory_mode_uses_the_same_dedicated_partition_contract():
raw_store = InMemoryStore()
thread_store = MemoryThreadMetaStore(raw_store)
history_store = MemoryTaskDeeplinkThreadStore(raw_store, thread_store)
await thread_store.create(
"memory-rwfx",
user_id="creator-a",
display_name="内存任务会话",
metadata={
"taskId": "108",
"taskRecordId": "108",
"taskGoPath": "rwfx",
"thread_type": "task_deeplink",
},
)
await thread_store.create(
"memory-legacy",
user_id="creator-b",
metadata={"taskId": "108"},
)
await thread_store.create(
"memory-xdfx",
user_id="creator-c",
metadata={
"taskId": "108",
"taskRecordId": "action-9",
"taskGoPath": "xdfx",
"thread_type": "task_deeplink",
},
)
rwfx = await history_store.list_threads("108", "rwfx")
xdfx = await history_store.list_threads("action-9", "xdfx")
assert [row["thread_id"] for row in rwfx] == ["memory-rwfx"]
assert rwfx[0]["values"] == {"title": "内存任务会话"}
assert [row["thread_id"] for row in xdfx] == ["memory-xdfx"]
await thread_store.delete("memory-rwfx", user_id=None)
assert await history_store.list_threads("108", "rwfx") == []