deer-flow/backend/tests/test_deer_flow_context.py
greatmengqi 5e1c309bd7 Merge release/2.0-rc into refactor/config-deerflow-context
Brings release/2.0-rc up to PR base. Three structural conflicts resolved:

- gateway/deps.py: kept both the new release-side persistence imports
  (FeedbackRepository, RunEventStore, RunStore, StreamBridge, TypeVar T)
  and the PR's get_config FastAPI dependency. Imports are independent;
  T is still used by _require() helper added on the release side.

- agents/middlewares/thread_data_middleware.py: release added HumanMessage
  stamping that records run_id on user-input messages. Adapted to the PR's
  typed DeerFlowContext: dropped 'from langgraph.config import get_config'
  (no longer needed) and changed runtime.context.get("run_id") to
  runtime.context.run_id (typed dataclass access).

- runtime/runs/worker.py: kept the PR's RunContext.app_config field and
  DeerFlowContext construction; passes run_id explicitly into the new
  context so the middleware above can read it.

To bridge the new HumanMessage stamping on release with PR's frozen
DeerFlowContext, this merge also adds a run_id: str | None field to
DeerFlowContext (with matching test + CLAUDE.md doc update). The field
fits the per-invocation-immutable-value-object semantics: run_id is
known at run start and never changes during the run.

Verification: backend test suite — 2372 passed, 4 skipped. One flaky
test (test_client_e2e::test_tool_call_produces_events) passes in
isolation and when its file runs alone; only fails under specific
full-suite ordering. Tracked separately; not introduced by this merge.
2026-04-26 12:30:20 +08:00

68 lines
2.5 KiB
Python

"""Tests for DeerFlowContext and resolve_context()."""
from dataclasses import FrozenInstanceError
from unittest.mock import MagicMock, patch
import pytest
from deerflow.config.app_config import AppConfig
from deerflow.config.deer_flow_context import DeerFlowContext, resolve_context
from deerflow.config.sandbox_config import SandboxConfig
def _make_config(**overrides) -> AppConfig:
defaults = {"sandbox": SandboxConfig(use="test")}
defaults.update(overrides)
return AppConfig(**defaults)
class TestDeerFlowContext:
def test_frozen(self):
ctx = DeerFlowContext(app_config=_make_config(), thread_id="t1")
with pytest.raises(FrozenInstanceError):
ctx.app_config = _make_config()
def test_fields(self):
config = _make_config()
ctx = DeerFlowContext(app_config=config, thread_id="t1", run_id="r1", agent_name="test-agent")
assert ctx.thread_id == "t1"
assert ctx.run_id == "r1"
assert ctx.agent_name == "test-agent"
assert ctx.app_config is config
def test_agent_name_default(self):
ctx = DeerFlowContext(app_config=_make_config(), thread_id="t1")
assert ctx.agent_name is None
def test_run_id_default(self):
ctx = DeerFlowContext(app_config=_make_config(), thread_id="t1")
assert ctx.run_id is None
def test_thread_id_required(self):
with pytest.raises(TypeError):
DeerFlowContext(app_config=_make_config()) # type: ignore[call-arg]
class TestResolveContext:
def test_returns_typed_context_directly(self):
"""Gateway/Client path: runtime.context is DeerFlowContext → return as-is."""
config = _make_config()
ctx = DeerFlowContext(app_config=config, thread_id="t1")
runtime = MagicMock()
runtime.context = ctx
assert resolve_context(runtime) is ctx
def test_raises_on_none_context(self):
"""Without a typed DeerFlowContext, resolve_context refuses to guess."""
runtime = MagicMock()
runtime.context = None
with pytest.raises(RuntimeError, match="resolve_context: runtime.context is not a DeerFlowContext"):
resolve_context(runtime)
def test_raises_on_dict_context(self):
"""Legacy dict shape is no longer supported — we raise instead of lazily loading AppConfig."""
runtime = MagicMock()
runtime.context = {"thread_id": "old-dict", "agent_name": "from-dict"}
with pytest.raises(RuntimeError, match="resolve_context: runtime.context is not a DeerFlowContext"):
resolve_context(runtime)