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.

2.7 KB
Raw Blame
import pytest
from pydantic import ValidationError
import dify_agent.layers.execution_context as execution_context_exports
from dify_agent.layers.execution_context import DifyExecutionContextLayerConfig


def test_execution_context_package_exports_client_safe_config_symbols_only() -> None:
    assert execution_context_exports.__all__ == [
        "ApplicationTracingContext",
        "DifyExecutionContextAgentConfigVersionKind",
        "DifyExecutionContextAgentMode",
        "DifyExecutionContextInvokeFrom",
        "DifyExecutionContextLayerConfig",
        "DifyExecutionContextUserFrom",
    ]
    assert not hasattr(execution_context_exports, "DifyExecutionContextLayer")


def test_execution_context_accepts_real_invoke_from_user_from_and_agent_mode() -> None:
    config = DifyExecutionContextLayerConfig(
        tenant_id="tenant-1", user_id="user-1", user_from="end-user", invoke_from="web-app", agent_mode="agent_app"
    )
    assert config.user_from == "end-user"
    assert config.invoke_from == "web-app"
    assert config.agent_mode == "agent_app"


def test_execution_context_rejects_legacy_agent_mode_in_invoke_from() -> None:
    with pytest.raises(ValidationError):
        _ = DifyExecutionContextLayerConfig.model_validate(
            {
                "tenant_id": "tenant-1",
                "user_from": "account",
                "agent_mode": "workflow_run",
                "invoke_from": "workflow_run",
            }
        )


def test_execution_context_layer_config_forbids_runtime_settings_and_unknown_fields() -> None:
    config = DifyExecutionContextLayerConfig(
        tenant_id="tenant-1",
        user_id="user-1",
        user_from="account",
        workflow_id="workflow-1",
        agent_mode="workflow_run",
        invoke_from="service-api",
    )
    assert config.tenant_id == "tenant-1"
    assert config.user_id == "user-1"
    assert config.user_from == "account"
    assert config.workflow_id == "workflow-1"
    assert config.agent_mode == "workflow_run"
    assert config.invoke_from == "service-api"
    with pytest.raises(ValidationError):
        _ = DifyExecutionContextLayerConfig.model_validate(
            {
                "tenant_id": "tenant-1",
                "user_from": "account",
                "agent_mode": "workflow_run",
                "invoke_from": "service-api",
                "daemon_url": "http://daemon",
            }
        )
    with pytest.raises(ValidationError):
        _ = DifyExecutionContextLayerConfig.model_validate(
            {
                "tenant_id": "tenant-1",
                "user_from": "account",
                "agent_mode": "workflow_run",
                "invoke_from": "service-api",
                "unknown": "value",
            }
        )