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.

5.2 KB
Raw Blame
from unittest.mock import MagicMock

import pytest
from sqlalchemy.orm import Session

from models.agent import (
    AgentConfigDraft,
    AgentConfigDraftType,
    AgentConfigVersionKind,
    AgentDebugConversation,
    AgentWorkspaceBinding,
)
from models.agent_config_entities import AgentSoulConfig
from models.model import App, Conversation
from services.agent.runtime_config_service import AgentRuntimeConfigService


def _app() -> App:
    return App(id="app-1", tenant_id="tenant-1")


def _conversation(*, binding_id: str | None = None) -> Conversation:
    return Conversation(id="conversation-1", agent_workspace_binding_id=binding_id)


def _soul(prompt: str) -> AgentSoulConfig:
    return AgentSoulConfig.model_validate(
        {
            "prompt": {"system_prompt": prompt},
            "app_features": {"suggested_questions_after_answer": {"enabled": True}},
        }
    )


def _patch_published_soul(monkeypatch: pytest.MonkeyPatch, soul: AgentSoulConfig) -> MagicMock:
    roster_service = MagicMock()
    roster_service.return_value.get_published_agent_soul_for_app.return_value = soul
    monkeypatch.setattr("services.agent.roster_service.AgentRosterService", roster_service)
    return roster_service


def test_debug_without_mapping_falls_back_to_published_soul(
    monkeypatch: pytest.MonkeyPatch, sqlite_session: Session
) -> None:
    published = _soul("published")
    roster_service = _patch_published_soul(monkeypatch, published)

    result = AgentRuntimeConfigService(sqlite_session).resolve_conversation_soul(
        app_model=_app(),
        conversation=_conversation(),
        account_id="account-1",
        use_debug_draft=True,
    )

    assert result == published
    roster_service.return_value.get_published_agent_soul_for_app.assert_called_once_with(
        tenant_id="tenant-1",
        app_id="app-1",
    )


def test_debug_without_draft_falls_back_to_published_soul(
    monkeypatch: pytest.MonkeyPatch, sqlite_session: Session
) -> None:
    debug_conversation = AgentDebugConversation(
        tenant_id="tenant-1",
        app_id="app-1",
        account_id="account-1",
        conversation_id="conversation-1",
        agent_id="agent-1",
        draft_type=AgentConfigDraftType.DRAFT,
    )
    sqlite_session.add(debug_conversation)
    sqlite_session.flush()
    published = _soul("published")
    _patch_published_soul(monkeypatch, published)

    result = AgentRuntimeConfigService(sqlite_session).resolve_conversation_soul(
        app_model=_app(),
        conversation=_conversation(),
        account_id="account-1",
        use_debug_draft=True,
    )

    assert result == published


def test_missing_binding_falls_back_to_published_soul(monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None:
    published = _soul("published")
    _patch_published_soul(monkeypatch, published)

    result = AgentRuntimeConfigService(sqlite_session).resolve_conversation_soul(
        app_model=_app(),
        conversation=_conversation(binding_id="binding-1"),
        account_id=None,
        use_debug_draft=False,
    )

    assert result == published


@pytest.mark.parametrize("version_kind", [AgentConfigVersionKind.SNAPSHOT, AgentConfigVersionKind.DRAFT])
def test_missing_bound_version_falls_back_to_published_soul(
    version_kind: AgentConfigVersionKind,
    monkeypatch: pytest.MonkeyPatch,
    sqlite_session: Session,
) -> None:
    binding = AgentWorkspaceBinding(
        id="binding-1",
        tenant_id="tenant-1",
        app_id="app-1",
        workspace_id="workspace-1",
        backend_binding_ref="binding-ref-1",
        agent_id="agent-1",
        agent_config_version_id="version-1",
        agent_config_version_kind=version_kind,
    )
    sqlite_session.add(binding)
    sqlite_session.flush()
    published = _soul("published")
    _patch_published_soul(monkeypatch, published)

    result = AgentRuntimeConfigService(sqlite_session).resolve_conversation_soul(
        app_model=_app(),
        conversation=_conversation(binding_id="binding-1"),
        account_id=None,
        use_debug_draft=False,
    )

    assert result == published


def test_bound_draft_returns_its_soul(monkeypatch: pytest.MonkeyPatch, sqlite_session: Session) -> None:
    binding = AgentWorkspaceBinding(
        id="binding-1",
        tenant_id="tenant-1",
        app_id="app-1",
        workspace_id="workspace-1",
        backend_binding_ref="binding-ref-1",
        agent_id="agent-1",
        agent_config_version_id="draft-1",
        agent_config_version_kind=AgentConfigVersionKind.DRAFT,
    )
    bound = _soul("bound draft")
    draft = AgentConfigDraft(
        id="draft-1",
        tenant_id="tenant-1",
        agent_id="agent-1",
        draft_type=AgentConfigDraftType.DRAFT,
        config_snapshot=bound,
    )
    sqlite_session.add_all([binding, draft])
    sqlite_session.flush()
    sqlite_session.expire_all()
    roster_service = _patch_published_soul(monkeypatch, _soul("published"))

    result = AgentRuntimeConfigService(sqlite_session).resolve_conversation_soul(
        app_model=_app(),
        conversation=_conversation(binding_id="binding-1"),
        account_id=None,
        use_debug_draft=False,
    )

    assert result == bound
    roster_service.assert_not_called()