Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

test: use real workflow state and command channels (#43173)

Asuka Minatoa0330a756f57
diff --git a/api/tests/unit_tests/core/app/layers/test_conversation_variable_persist_layer.py b/api/tests/unit_tests/core/app/layers/test_conversation_variable_persist_layer.py
index ba55e8f6950..cfbe27f191b 100644
--- a/api/tests/unit_tests/core/app/layers/test_conversation_variable_persist_layer.py
+++ b/api/tests/unit_tests/core/app/layers/test_conversation_variable_persist_layer.py
@@ -1,46 +1,26 @@
-from collections.abc import Sequence
from unittest.mock import Mock
from core.app.layers.conversation_variable_persist_layer import ConversationVariablePersistenceLayer
-from core.workflow.system_variables import SystemVariableKey
+from core.workflow.system_variables import build_system_variables
from core.workflow.variable_prefixes import CONVERSATION_VARIABLE_NODE_ID
from graphon.enums import BuiltinNodeTypes, WorkflowNodeExecutionStatus
-from graphon.graph_engine.command_channels import CommandChannel
+from graphon.graph_engine.command_channels import InMemoryChannel
from graphon.graph_events import NodeRunSucceededEvent, NodeRunVariableUpdatedEvent
from graphon.node_events import NodeRunResult
-from graphon.runtime import ReadOnlyGraphRuntimeState
+from graphon.runtime import GraphRuntimeState, ReadOnlyGraphRuntimeStateWrapper, VariablePool
from graphon.variables import StringVariable
-from graphon.variables.segments import Segment, StringSegment
from libs.datetime_utils import naive_utc_now
-class MockReadOnlyVariablePool:
- def __init__(self, variables: dict[tuple[str, str], Segment] | None = None) -> None:
- self._variables = variables or {}
-
- def get(self, selector: Sequence[str]) -> Segment | None:
- if len(selector) < 2:
- return None
- return self._variables.get((selector[0], selector[1]))
-
- def get_all_by_node(self, node_id: str) -> dict[str, object]:
- return {key: value for (nid, key), value in self._variables.items() if nid == node_id}
-
- def get_by_prefix(self, prefix: str) -> dict[str, object]:
- return {key: value for (nid, key), value in self._variables.items() if nid == prefix}
-
-
-def _build_graph_runtime_state(
- variable_pool: MockReadOnlyVariablePool,
- conversation_id: str | None = None,
-) -> ReadOnlyGraphRuntimeState:
- graph_runtime_state = Mock(spec=ReadOnlyGraphRuntimeState)
- if conversation_id is not None:
- variable_pool._variables[("sys", SystemVariableKey.CONVERSATION_ID.value)] = StringSegment(
- value=conversation_id
+def _build_graph_runtime_state(conversation_id: str) -> ReadOnlyGraphRuntimeStateWrapper:
+ return ReadOnlyGraphRuntimeStateWrapper(
+ GraphRuntimeState(
+ variable_pool=VariablePool.from_bootstrap(
+ system_variables=build_system_variables(conversation_id=conversation_id)
+ ),
+ start_at=0,
)
- graph_runtime_state.variable_pool = variable_pool
- return graph_runtime_state
+ )
def _build_node_run_succeeded_event() -> NodeRunSucceededEvent:
@@ -76,7 +56,7 @@ def test_persists_conversation_variables_from_variable_update_event():
)
updater = Mock()
layer = ConversationVariablePersistenceLayer(updater)
- layer.initialize(_build_graph_runtime_state(MockReadOnlyVariablePool(), conversation_id), Mock(spec=CommandChannel))
+ layer.initialize(_build_graph_runtime_state(conversation_id), InMemoryChannel())
event = _build_variable_updated_event(variable)
layer.on_event(event)
@@ -88,7 +68,7 @@ def test_skips_non_variable_update_events():
conversation_id = "conv-456"
updater = Mock()
layer = ConversationVariablePersistenceLayer(updater)
- layer.initialize(_build_graph_runtime_state(MockReadOnlyVariablePool(), conversation_id), Mock(spec=CommandChannel))
+ layer.initialize(_build_graph_runtime_state(conversation_id), InMemoryChannel())
event = _build_node_run_succeeded_event()
layer.on_event(event)
@@ -106,7 +86,7 @@ def test_skips_non_conversation_variables():
)
updater = Mock()
layer = ConversationVariablePersistenceLayer(updater)
- layer.initialize(_build_graph_runtime_state(MockReadOnlyVariablePool(), conversation_id), Mock(spec=CommandChannel))
+ layer.initialize(_build_graph_runtime_state(conversation_id), InMemoryChannel())
event = _build_variable_updated_event(non_conversation_variable)
layer.on_event(event)
diff --git a/api/tests/unit_tests/core/app/layers/test_pause_state_persist_layer.py b/api/tests/unit_tests/core/app/layers/test_pause_state_persist_layer.py
index b8bceb3a988..697cd531663 100644
--- a/api/tests/unit_tests/core/app/layers/test_pause_state_persist_layer.py
+++ b/api/tests/unit_tests/core/app/layers/test_pause_state_persist_layer.py
@@ -29,6 +29,7 @@ from graphon.graph_events import (
GraphRunSucceededEvent,
)
from graphon.runtime import ReadOnlyVariablePool
+from graphon.variables import build_segment
from graphon.variables.segments import Segment
from models.model import AppMode
from repositories.factory import DifyAPIRepositoryFactory
@@ -92,10 +93,7 @@ class MockReadOnlyVariablePool:
value = self._variables.get((selector[0], selector[1]))
if value is None:
return None
- mock_segment = Mock(spec=Segment)
- mock_segment.value = value
- mock_segment.text = value if isinstance(value, str) else None
- return mock_segment
+ return build_segment(value)
def get_all_by_node(self, node_id: str) -> dict[str, object]:
return {key: value for (nid, key), value in self._variables.items() if nid == node_id}
diff --git a/api/tests/unit_tests/core/workflow/test_workflow_entry_redis_channel.py b/api/tests/unit_tests/core/workflow/test_workflow_entry_redis_channel.py
index 80dc8927fa9..64852b6a12e 100644
--- a/api/tests/unit_tests/core/workflow/test_workflow_entry_redis_channel.py
+++ b/api/tests/unit_tests/core/workflow/test_workflow_entry_redis_channel.py
@@ -16,9 +16,8 @@ class TestWorkflowEntryRedisChannel:
# Mock dependencies
mock_graph = MagicMock()
mock_graph_config = {"nodes": [], "edges": []}
- mock_variable_pool = MagicMock(spec=VariablePool)
- mock_graph_runtime_state = MagicMock(spec=GraphRuntimeState)
- mock_graph_runtime_state.variable_pool = mock_variable_pool
+ variable_pool = VariablePool()
+ graph_runtime_state = GraphRuntimeState(variable_pool=variable_pool, start_at=0)
# Create a mock Redis channel
mock_redis_client = MagicMock()
@@ -37,8 +36,8 @@ class TestWorkflowEntryRedisChannel:
user_from=UserFrom.ACCOUNT,
invoke_from=InvokeFrom.DEBUGGER,
call_depth=0,
- variable_pool=mock_variable_pool,
- graph_runtime_state=mock_graph_runtime_state,
+ variable_pool=variable_pool,
+ graph_runtime_state=graph_runtime_state,
command_channel=redis_channel, # Provide Redis channel
)
@@ -53,9 +52,8 @@ class TestWorkflowEntryRedisChannel:
# Mock dependencies
mock_graph = MagicMock()
mock_graph_config = {"nodes": [], "edges": []}
- mock_variable_pool = MagicMock(spec=VariablePool)
- mock_graph_runtime_state = MagicMock(spec=GraphRuntimeState)
- mock_graph_runtime_state.variable_pool = mock_variable_pool
+ variable_pool = VariablePool()
+ graph_runtime_state = GraphRuntimeState(variable_pool=variable_pool, start_at=0)
# Patch GraphEngine and InMemoryChannel
with (
@@ -74,8 +72,8 @@ class TestWorkflowEntryRedisChannel:
user_from=UserFrom.ACCOUNT,
invoke_from=InvokeFrom.DEBUGGER,
call_depth=0,
- variable_pool=mock_variable_pool,
- graph_runtime_state=mock_graph_runtime_state,
+ variable_pool=variable_pool,
+ graph_runtime_state=graph_runtime_state,
command_channel=None, # No channel provided
)
@@ -93,9 +91,8 @@ class TestWorkflowEntryRedisChannel:
# Mock dependencies
mock_graph = MagicMock()
mock_graph_config = {"nodes": [], "edges": []}
- mock_variable_pool = MagicMock(spec=VariablePool)
- mock_graph_runtime_state = MagicMock(spec=GraphRuntimeState)
- mock_graph_runtime_state.variable_pool = mock_variable_pool
+ variable_pool = VariablePool()
+ graph_runtime_state = GraphRuntimeState(variable_pool=variable_pool, start_at=0)
# Create a mock Redis channel
mock_redis_client = MagicMock()
@@ -122,8 +119,8 @@ class TestWorkflowEntryRedisChannel:
user_from=UserFrom.ACCOUNT,
invoke_from=InvokeFrom.DEBUGGER,
call_depth=0,
- variable_pool=mock_variable_pool,
- graph_runtime_state=mock_graph_runtime_state,
+ variable_pool=variable_pool,
+ graph_runtime_state=graph_runtime_state,
command_channel=redis_channel,
)