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.

37.7 KB
Raw Blame
import dataclasses
import json
import logging
from collections.abc import Callable
from datetime import datetime, timedelta
from typing import cast
from unittest.mock import MagicMock

import pytest
from pydantic import JsonValue
from pytest_mock import MockerFixture
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker

import services.human_input_service as human_input_service_module
from core.app.app_config.entities import WorkflowUIBasedAppConfig
from core.app.entities.app_invoke_entities import InvokeFrom, UserFrom, WorkflowAppGenerateEntity
from core.app.layers.pause_state_persist_layer import WorkflowResumptionContext, _WorkflowGenerateEntityWrapper
from core.repositories.human_input_repository import (
    HumanInputFormRecord,
    HumanInputFormSubmissionRepository,
)
from core.workflow.human_input_adapter import DeliveryMethodType
from core.workflow.nodes.human_input.entities import (
    FileInputConfig,
    FileListInputConfig,
    FormDefinition,
    ParagraphInputConfig,
    SelectInputConfig,
    StringListSource,
    UserActionConfig,
)
from core.workflow.nodes.human_input.enums import HumanInputFormKind, HumanInputFormStatus, ValueSourceType
from graphon.file import File, FileTransferMethod, FileType
from graphon.runtime import RuntimeState, VariablePool
from libs.datetime_utils import naive_utc_now
from models.human_input import HumanInputDelivery, HumanInputForm, HumanInputFormRecipient, RecipientType
from models.model import App, AppMode
from models.workflow import WorkflowRun
from services.human_input_service import (
    Form,
    FormExpiredError,
    FormSubmittedError,
    HumanInputService,
    InvalidFormDataError,
)
from tests.helpers.unit.config_override import apply_config_overrides


def _make_app(mode: AppMode) -> App:
    return App(
        id="app-id",
        tenant_id="tenant-id",
        name="Test App",
        description="",
        mode=mode,
        workflow_id=None,
        enable_site=True,
        enable_api=True,
        max_active_requests=0,
    )


def _workflow_run() -> WorkflowRun:
    return WorkflowRun(id="workflow-run-id", app_id="app-id")


@pytest.fixture
def sample_form_record() -> HumanInputFormRecord:
    return HumanInputFormRecord(
        form_id="form-id",
        workflow_run_id="workflow-run-id",
        node_id="node-id",
        tenant_id="tenant-id",
        app_id="app-id",
        form_kind=HumanInputFormKind.RUNTIME,
        definition=FormDefinition(
            form_content="hello",
            inputs=[],
            user_actions=[UserActionConfig(id="submit", title="Submit")],
            rendered_content="<p>hello</p>",
            expiration_time=naive_utc_now() + timedelta(hours=1),
        ),
        rendered_content="<p>hello</p>",
        created_at=naive_utc_now(),
        expiration_time=naive_utc_now() + timedelta(hours=1),
        status=HumanInputFormStatus.WAITING,
        selected_action_id=None,
        submitted_data=None,
        submitted_at=None,
        submission_user_id=None,
        submission_end_user_id=None,
        completed_by_recipient_id=None,
        recipient_id="recipient-id",
        recipient_type=RecipientType.STANDALONE_WEB_APP,
        access_token="token",
    )


@pytest.fixture
def form_repository(
    sqlite_session_factory: sessionmaker[Session],
    monkeypatch: pytest.MonkeyPatch,
    mocker: MockerFixture,
) -> Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository]:
    monkeypatch.setattr(
        "core.repositories.human_input_repository.session_factory.create_session", sqlite_session_factory
    )

    def build(record: HumanInputFormRecord | None) -> HumanInputFormSubmissionRepository:
        """Persist the form and recipient; None leaves the database empty for missing-token tests."""
        if record is not None:
            assert record.recipient_id is not None
            assert record.recipient_type is not None
            assert record.access_token is not None
            form = HumanInputForm(
                id=record.form_id,
                tenant_id=record.tenant_id,
                app_id=record.app_id,
                workflow_run_id=record.workflow_run_id,
                conversation_id=record.conversation_id,
                node_id=record.node_id,
                form_kind=record.form_kind,
                form_definition=record.definition.model_dump_json(),
                rendered_content=record.rendered_content,
                created_at=record.created_at,
                expiration_time=record.expiration_time,
                status=record.status,
                selected_action_id=record.selected_action_id,
                submitted_data=json.dumps(record.submitted_data) if record.submitted_data is not None else None,
                submitted_at=record.submitted_at,
                submission_user_id=record.submission_user_id,
                submission_end_user_id=record.submission_end_user_id,
                completed_by_recipient_id=record.completed_by_recipient_id,
            )
            delivery = HumanInputDelivery(
                id="delivery-id",
                form_id=record.form_id,
                delivery_method_type=DeliveryMethodType.WEBAPP,
                channel_payload="{}",
            )
            recipient = HumanInputFormRecipient(
                id=record.recipient_id,
                form_id=record.form_id,
                delivery_id=delivery.id,
                recipient_type=record.recipient_type,
                recipient_payload=json.dumps({"TYPE": record.recipient_type}),
                access_token=record.access_token,
            )
            with sqlite_session_factory.begin() as session:
                session.add_all([form, delivery, recipient])
        repository = HumanInputFormSubmissionRepository()
        mocker.spy(repository, "get_by_token")
        mocker.spy(repository, "mark_submitted")
        return repository

    return build


def test_enqueue_resume_dispatches_task_for_workflow(
    mocker: MockerFixture,
    sqlite_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(sqlite_session_factory)

    workflow_run = _workflow_run()

    workflow_run_repo = MagicMock()
    workflow_run_repo.get_workflow_run_by_id_without_tenant.return_value = workflow_run
    mocker.patch(
        "services.human_input_service.DifyAPIRepositoryFactory.create_api_workflow_run_repository",
        return_value=workflow_run_repo,
    )

    with sqlite_session_factory.begin() as arrange_session:
        arrange_session.add(_make_app(AppMode.WORKFLOW))

    resume_task = mocker.patch("services.human_input_service.resume_app_execution")

    service.enqueue_resume("workflow-run-id")

    resume_task.apply_async.assert_called_once()
    call_kwargs = resume_task.apply_async.call_args.kwargs
    assert call_kwargs["kwargs"]["payload"]["workflow_run_id"] == "workflow-run-id"


def test_ensure_form_active_respects_global_timeout(
    monkeypatch: pytest.MonkeyPatch,
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(unbound_session_factory)
    expired_record = dataclasses.replace(
        sample_form_record,
        created_at=naive_utc_now() - timedelta(hours=2),
        expiration_time=naive_utc_now() + timedelta(hours=2),
    )
    apply_config_overrides(monkeypatch, HUMAN_INPUT_GLOBAL_TIMEOUT_SECONDS=3600)

    with pytest.raises(FormExpiredError):
        service.ensure_form_active(Form(expired_record))


def test_enqueue_resume_dispatches_task_for_advanced_chat(
    mocker: MockerFixture,
    sqlite_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(sqlite_session_factory)

    workflow_run = _workflow_run()

    workflow_run_repo = MagicMock()
    workflow_run_repo.get_workflow_run_by_id_without_tenant.return_value = workflow_run
    mocker.patch(
        "services.human_input_service.DifyAPIRepositoryFactory.create_api_workflow_run_repository",
        return_value=workflow_run_repo,
    )

    with sqlite_session_factory.begin() as arrange_session:
        arrange_session.add(_make_app(AppMode.ADVANCED_CHAT))

    resume_task = mocker.patch("services.human_input_service.resume_app_execution")

    service.enqueue_resume("workflow-run-id")

    resume_task.apply_async.assert_called_once()
    call_kwargs = resume_task.apply_async.call_args.kwargs
    assert call_kwargs["kwargs"]["payload"]["workflow_run_id"] == "workflow-run-id"


def test_enqueue_resume_skips_unsupported_app_mode(
    mocker: MockerFixture,
    sqlite_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(sqlite_session_factory)

    workflow_run = _workflow_run()

    workflow_run_repo = MagicMock()
    workflow_run_repo.get_workflow_run_by_id_without_tenant.return_value = workflow_run
    mocker.patch(
        "services.human_input_service.DifyAPIRepositoryFactory.create_api_workflow_run_repository",
        return_value=workflow_run_repo,
    )

    with sqlite_session_factory.begin() as arrange_session:
        arrange_session.add(_make_app(AppMode.COMPLETION))

    resume_task = mocker.patch("services.human_input_service.resume_app_execution")

    service.enqueue_resume("workflow-run-id")

    resume_task.apply_async.assert_not_called()


def test_get_form_definition_by_token_for_console_uses_repository(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    console_record = dataclasses.replace(sample_form_record, recipient_type=RecipientType.CONSOLE)
    repo = form_repository(console_record)

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    form = service.get_form_definition_by_token_for_console("token")

    cast(MagicMock, repo.get_by_token).assert_called_once_with("token")
    assert form is not None
    assert form.get_definition() == console_record.definition


def _build_resumption_context_state(*, options: list[str], workflow_run_id: str) -> bytes:
    app_config = WorkflowUIBasedAppConfig(
        tenant_id="tenant-id",
        app_id="app-id",
        app_mode=AppMode.WORKFLOW,
        workflow_id="workflow-id",
    )
    generate_entity = WorkflowAppGenerateEntity(
        user_from=UserFrom.ACCOUNT,
        task_id="task-id",
        app_config=app_config,
        inputs={},
        files=[],
        user_id="user-id",
        stream=True,
        invoke_from=InvokeFrom.EXPLORE,
        call_depth=0,
        workflow_execution_id=workflow_run_id,
    )
    runtime_state = RuntimeState(workflow_id="test-workflow", variable_pool=VariablePool(), start_at=0.0)
    runtime_state.variable_pool.add(("start", "options"), options)
    context = WorkflowResumptionContext(
        generate_entity=_WorkflowGenerateEntityWrapper(entity=generate_entity),
        serialized_graph_runtime_state=runtime_state.dumps(),
    )
    return context.dumps().encode()


def test_resolve_form_inputs_uses_runtime_select_options(
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    configured_input = SelectInputConfig(
        output_variable_name="decision",
        option_source=StringListSource(
            type=ValueSourceType.VARIABLE,
            selector=["start", "options"],
            value=["configured"],
        ),
    )
    record = dataclasses.replace(
        sample_form_record,
        definition=sample_form_record.definition.model_copy(update={"inputs": [configured_input]}),
    )
    pause = MagicMock()
    pause.resumed_at = None
    pause.get_state.return_value = _build_resumption_context_state(
        options=["approve", "reject"],
        workflow_run_id=record.workflow_run_id or "",
    )
    workflow_run_repo = MagicMock()
    workflow_run_repo.get_workflow_pause.return_value = pause
    mocker.patch(
        "services.human_input_service.DifyAPIRepositoryFactory.create_api_workflow_run_repository",
        return_value=workflow_run_repo,
    )
    service = HumanInputService(unbound_session_factory)

    resolved_inputs = service.resolve_form_inputs(Form(record))

    assert len(resolved_inputs) == 1
    resolved_input = resolved_inputs[0]
    assert isinstance(resolved_input, SelectInputConfig)
    assert resolved_input.option_source.type == ValueSourceType.CONSTANT
    assert resolved_input.option_source.selector == ()
    assert resolved_input.option_source.value == ["approve", "reject"]
    workflow_run_repo.get_workflow_pause.assert_called_once_with(record.workflow_run_id)


def test_constant_select_rejects_unlisted_submission(
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    configured_input = SelectInputConfig(
        output_variable_name="decision",
        option_source=StringListSource(
            type=ValueSourceType.CONSTANT,
            value=["approve", "reject"],
        ),
    )
    record = dataclasses.replace(
        sample_form_record,
        definition=sample_form_record.definition.model_copy(update={"inputs": [configured_input]}),
    )
    service = HumanInputService(unbound_session_factory)

    with pytest.raises(InvalidFormDataError, match="Invalid value for select input 'decision': forged"):
        service._validate_submission(Form(record), selected_action_id="submit", form_data={"decision": "forged"})


def test_submit_form_by_token_calls_repository_and_enqueue(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    repo = form_repository(sample_form_record)
    service = HumanInputService(unbound_session_factory, form_repository=repo)
    enqueue_spy = mocker.patch.object(service, "enqueue_resume")

    service.submit_form_by_token(
        recipient_type=RecipientType.STANDALONE_WEB_APP,
        form_token="token",
        selected_action_id="submit",
        form_data={"field": "value"},
        submission_end_user_id="end-user-id",
    )

    cast(MagicMock, repo.get_by_token).assert_called_once_with("token")
    cast(MagicMock, repo.mark_submitted).assert_called_once()
    call_kwargs = cast(MagicMock, repo.mark_submitted).call_args.kwargs
    assert call_kwargs["form_id"] == sample_form_record.form_id
    assert call_kwargs["recipient_id"] == sample_form_record.recipient_id
    assert call_kwargs["selected_action_id"] == "submit"
    assert call_kwargs["form_data"] == {"field": "value"}
    assert call_kwargs["submission_end_user_id"] == "end-user-id"
    persisted = repo.get_by_form_id(sample_form_record.form_id)
    assert persisted is not None
    assert persisted.status == HumanInputFormStatus.SUBMITTED
    assert persisted.submitted_data == {"field": "value"}
    assert persisted.submission_end_user_id == "end-user-id"
    enqueue_spy.assert_called_once_with(sample_form_record.workflow_run_id)


def test_submit_form_by_token_skips_enqueue_for_delivery_test(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    test_record = dataclasses.replace(
        sample_form_record,
        form_kind=HumanInputFormKind.DELIVERY_TEST,
        workflow_run_id=None,
    )
    repo = form_repository(test_record)
    service = HumanInputService(unbound_session_factory, form_repository=repo)
    enqueue_spy = mocker.patch.object(service, "enqueue_resume")

    service.submit_form_by_token(
        recipient_type=RecipientType.STANDALONE_WEB_APP,
        form_token="token",
        selected_action_id="submit",
        form_data={"field": "value"},
    )

    enqueue_spy.assert_not_called()


def test_submit_form_by_token_passes_submission_user_id(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    repo = form_repository(sample_form_record)
    service = HumanInputService(unbound_session_factory, form_repository=repo)
    enqueue_spy = mocker.patch.object(service, "enqueue_resume")

    service.submit_form_by_token(
        recipient_type=RecipientType.STANDALONE_WEB_APP,
        form_token="token",
        selected_action_id="submit",
        form_data={"field": "value"},
        submission_user_id="account-id",
    )

    call_kwargs = cast(MagicMock, repo.mark_submitted).call_args.kwargs
    assert call_kwargs["submission_user_id"] == "account-id"
    assert call_kwargs["submission_end_user_id"] is None
    enqueue_spy.assert_called_once_with(sample_form_record.workflow_run_id)


def test_submit_form_by_token_invalid_action(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    repo = form_repository(dataclasses.replace(sample_form_record))
    service = HumanInputService(unbound_session_factory, form_repository=repo)

    with pytest.raises(InvalidFormDataError) as exc_info:
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="invalid",
            form_data={},
        )

    assert "Invalid action" in str(exc_info.value)
    cast(MagicMock, repo.mark_submitted).assert_not_called()


def test_submit_form_by_token_missing_inputs(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    definition_with_input = FormDefinition(
        form_content="hello",
        inputs=[ParagraphInputConfig(output_variable_name="content")],
        user_actions=sample_form_record.definition.user_actions,
        rendered_content="<p>hello</p>",
        expiration_time=sample_form_record.expiration_time,
    )
    form_with_input = dataclasses.replace(sample_form_record, definition=definition_with_input)
    repo = form_repository(form_with_input)
    service = HumanInputService(unbound_session_factory, form_repository=repo)

    with pytest.raises(InvalidFormDataError) as exc_info:
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="submit",
            form_data={},
        )

    assert "Missing required inputs" in str(exc_info.value)
    cast(MagicMock, repo.mark_submitted).assert_not_called()


@pytest.mark.parametrize(
    ("input_definition", "submitted_value", "expected_message"),
    [
        *[
            ({"type": "paragraph", "output_variable_name": "note"}, value, "note")
            for value in (12, True, None, ["text"], {"text": "text"})
        ],
        (
            {
                "type": "select",
                "output_variable_name": "decision",
                "option_source": {
                    "type": "constant",
                    "value": ["approve", "reject"],
                },
            },
            "unknown",
            "decision",
        ),
        (
            {
                "type": "file",
                "output_variable_name": "attachment",
                "allowed_file_types": ["document"],
                "allowed_file_upload_methods": ["remote_url"],
            },
            "not-a-file",
            "attachment",
        ),
        (
            {
                "type": "file-list",
                "output_variable_name": "attachments",
                "allowed_file_types": ["document"],
                "allowed_file_upload_methods": ["remote_url"],
                "number_limits": 2,
            },
            [
                {
                    "type": "document",
                    "transfer_method": "remote_url",
                    "remote_url": "https://example.com/ok.txt",
                    "filename": "ok.txt",
                    "extension": ".txt",
                    "mime_type": "text/plain",
                },
                "not-a-file",
            ],
            "attachments",
        ),
    ],
)
def test_validate_human_input_submission_rejects_invalid_input_payloads(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    input_definition: dict[str, JsonValue],
    submitted_value: JsonValue,
    expected_message: str,
) -> None:
    definition = FormDefinition.model_validate(
        {
            "form_content": "Validate form data",
            "inputs": [input_definition],
            "user_actions": [{"id": "submit", "title": "Submit"}],
            "rendered_content": "<p>Validate form data</p>",
            "expiration_time": naive_utc_now() + timedelta(hours=1),
        }
    )
    repo = form_repository(dataclasses.replace(sample_form_record, definition=definition))
    service = HumanInputService(unbound_session_factory, form_repository=repo)

    with pytest.raises(InvalidFormDataError) as exc_info:
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="submit",
            form_data={definition.inputs[0].output_variable_name: submitted_value},
        )

    assert expected_message in str(exc_info.value)
    cast(MagicMock, repo.mark_submitted).assert_not_called()


def test_form_properties(sample_form_record: HumanInputFormRecord) -> None:
    form = Form(sample_form_record)
    assert form.id == "form-id"
    assert form.workflow_run_id == "workflow-run-id"
    assert form.tenant_id == "tenant-id"
    assert form.app_id == "app-id"
    assert form.recipient_id == "recipient-id"
    assert form.recipient_type == RecipientType.STANDALONE_WEB_APP
    assert form.status == HumanInputFormStatus.WAITING
    assert form.form_kind == HumanInputFormKind.RUNTIME
    assert isinstance(form.created_at, datetime)
    assert isinstance(form.expiration_time, datetime)


def test_form_submitted_error_init() -> None:
    error = FormSubmittedError(form_id="test-form")
    assert error.description == "This form has already been submitted by another user, form_id=test-form"
    assert error.code == 412


def test_human_input_service_init_with_engine(sqlite_engine: Engine) -> None:
    service = HumanInputService(session_factory=sqlite_engine)

    assert isinstance(service._session_factory, sessionmaker)
    assert service._session_factory.kw["bind"] is sqlite_engine


def test_get_form_by_token_none(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    unbound_session_factory: sessionmaker[Session],
) -> None:
    repo = form_repository(None)

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    assert service.get_form_by_token("invalid") is None


def test_get_form_definition_by_token_mismatch(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    repo = form_repository(sample_form_record)

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    # RecipientType mismatch
    assert service.get_form_definition_by_token(RecipientType.CONSOLE, "token") is None


def test_get_form_definition_by_token_success(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    repo = form_repository(sample_form_record)

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    form = service.get_form_definition_by_token(RecipientType.STANDALONE_WEB_APP, "token")
    assert form is not None
    assert form.id == sample_form_record.form_id


def test_get_form_definition_by_token_for_console_mismatch(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    repo = form_repository(sample_form_record)  # is STANDALONE_WEB_APP

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    assert service.get_form_definition_by_token_for_console("token") is None


def test_submit_form_by_token_delivery_not_enabled(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    unbound_session_factory: sessionmaker[Session],
) -> None:
    repo = form_repository(None)

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    with pytest.raises(human_input_service_module.WebAppDeliveryNotEnabledError):
        service.submit_form_by_token(RecipientType.STANDALONE_WEB_APP, "token", "action", {})


def test_submit_form_by_token_no_workflow_run_id(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    # Preserve the legacy ownerless-record case: submission must not enqueue a workflow.
    repo = form_repository(dataclasses.replace(sample_form_record, workflow_run_id=None))

    service = HumanInputService(unbound_session_factory, form_repository=repo)
    enqueue_spy = mocker.patch.object(service, "enqueue_resume")

    service.submit_form_by_token(RecipientType.STANDALONE_WEB_APP, "token", "submit", {})
    enqueue_spy.assert_not_called()


def test_ensure_form_active_errors(
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(unbound_session_factory)

    # Submitted
    submitted_record = dataclasses.replace(sample_form_record, submitted_at=naive_utc_now())
    with pytest.raises(human_input_service_module.FormSubmittedError):
        service.ensure_form_active(Form(submitted_record))

    # Timeout status
    timeout_record = dataclasses.replace(sample_form_record, status=HumanInputFormStatus.TIMEOUT)
    with pytest.raises(FormExpiredError):
        service.ensure_form_active(Form(timeout_record))

    # Expired time
    expired_time_record = dataclasses.replace(
        sample_form_record, expiration_time=naive_utc_now() - timedelta(minutes=1)
    )
    with pytest.raises(FormExpiredError):
        service.ensure_form_active(Form(expired_time_record))


def test_ensure_not_submitted_raises(
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(unbound_session_factory)
    submitted_record = dataclasses.replace(sample_form_record, submitted_at=naive_utc_now())

    with pytest.raises(human_input_service_module.FormSubmittedError):
        service._ensure_not_submitted(Form(submitted_record))


def test_enqueue_resume_workflow_not_found(
    mocker: MockerFixture,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(unbound_session_factory)

    workflow_run_repo = MagicMock()
    workflow_run_repo.get_workflow_run_by_id_without_tenant.return_value = None
    mocker.patch(
        "services.human_input_service.DifyAPIRepositoryFactory.create_api_workflow_run_repository",
        return_value=workflow_run_repo,
    )

    with pytest.raises(AssertionError) as excinfo:
        service.enqueue_resume("workflow-run-id")
    assert "WorkflowRun not found" in str(excinfo.value)


def test_enqueue_resume_app_not_found(
    mocker: MockerFixture,
    sqlite_session_factory: sessionmaker[Session],
    caplog: pytest.LogCaptureFixture,
) -> None:
    service = HumanInputService(sqlite_session_factory)

    workflow_run = _workflow_run()

    workflow_run_repo = MagicMock()
    workflow_run_repo.get_workflow_run_by_id_without_tenant.return_value = workflow_run
    mocker.patch(
        "services.human_input_service.DifyAPIRepositoryFactory.create_api_workflow_run_repository",
        return_value=workflow_run_repo,
    )
    resume_task = mocker.patch("services.human_input_service.resume_app_execution")

    with caplog.at_level(logging.ERROR, logger="services.human_input_service"):
        service.enqueue_resume("workflow-run-id")

    assert (
        "services.human_input_service",
        logging.ERROR,
        "App not found for WorkflowRun, workflow_run_id=workflow-run-id, app_id=app-id",
    ) in caplog.record_tuples
    resume_task.apply_async.assert_not_called()


def test_is_globally_expired_zero_timeout(
    monkeypatch: pytest.MonkeyPatch,
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    service = HumanInputService(unbound_session_factory)

    apply_config_overrides(monkeypatch, HUMAN_INPUT_GLOBAL_TIMEOUT_SECONDS=0)
    assert service._is_globally_expired(Form(sample_form_record)) is False


def test_submit_form_by_token_normalizes_select_and_files(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    definition = FormDefinition(
        form_content="hello",
        inputs=[
            SelectInputConfig(
                output_variable_name="decision",
                option_source=StringListSource(type=ValueSourceType.CONSTANT, value=["approve", "reject"]),
            ),
            FileInputConfig(output_variable_name="attachment"),
            FileListInputConfig(output_variable_name="attachments", number_limits=3),
        ],
        user_actions=[UserActionConfig(id="submit", title="Submit")],
        rendered_content="<p>hello</p>",
        expiration_time=sample_form_record.expiration_time,
    )
    form_with_inputs = dataclasses.replace(sample_form_record, definition=definition)
    repo = form_repository(form_with_inputs)
    service = HumanInputService(unbound_session_factory, form_repository=repo)

    single_file = File(
        file_id="file-1",
        file_type=FileType.DOCUMENT,
        transfer_method=FileTransferMethod.LOCAL_FILE,
        related_id="upload-1",
        filename="resume.pdf",
        extension=".pdf",
        mime_type="application/pdf",
        size=128,
    )
    list_files = [
        File(
            file_id="file-2",
            file_type=FileType.DOCUMENT,
            transfer_method=FileTransferMethod.LOCAL_FILE,
            related_id="upload-2",
            filename="a.pdf",
            extension=".pdf",
            mime_type="application/pdf",
            size=64,
        ),
        File(
            file_id="file-3",
            file_type=FileType.DOCUMENT,
            transfer_method=FileTransferMethod.REMOTE_URL,
            remote_url="https://example.com/b.pdf",
            filename="b.pdf",
            extension=".pdf",
            mime_type="application/pdf",
            size=96,
        ),
    ]
    mocker.patch("services.human_input_service.build_from_mapping", return_value=single_file)
    mocker.patch("services.human_input_service.build_from_mappings", return_value=list_files)
    enqueue_spy = mocker.patch.object(service, "enqueue_resume")

    service.submit_form_by_token(
        recipient_type=RecipientType.STANDALONE_WEB_APP,
        form_token="token",
        selected_action_id="submit",
        form_data={
            "decision": "approve",
            "attachment": {"transfer_method": "local_file", "upload_file_id": "upload-1", "type": "document"},
            "attachments": [
                {"transfer_method": "local_file", "upload_file_id": "upload-2", "type": "document"},
                {"transfer_method": "remote_url", "url": "https://example.com/b.pdf", "type": "document"},
            ],
        },
    )

    submitted_data = cast(MagicMock, repo.mark_submitted).call_args.kwargs["form_data"]
    assert submitted_data["decision"] == "approve"
    assert submitted_data["attachment"]["filename"] == "resume.pdf"
    assert submitted_data["attachment"]["transfer_method"] == "local_file"
    assert submitted_data["attachments"][0]["filename"] == "a.pdf"
    assert submitted_data["attachments"][1]["filename"] == "b.pdf"
    enqueue_spy.assert_called_once_with(sample_form_record.workflow_run_id)


def test_submit_form_by_token_invalid_select_value(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    definition = FormDefinition(
        form_content="hello",
        inputs=[
            SelectInputConfig(
                output_variable_name="decision",
                option_source=StringListSource(type=ValueSourceType.CONSTANT, value=["approve", "reject"]),
            )
        ],
        user_actions=[UserActionConfig(id="submit", title="Submit")],
        rendered_content="<p>hello</p>",
        expiration_time=sample_form_record.expiration_time,
    )
    repo = form_repository(dataclasses.replace(sample_form_record, definition=definition))
    service = HumanInputService(unbound_session_factory, form_repository=repo)

    with pytest.raises(InvalidFormDataError, match="Invalid value for select input 'decision'"):
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="submit",
            form_data={"decision": "hold"},
        )


def test_submit_form_by_token_invalid_file_list_item(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
) -> None:
    definition = FormDefinition(
        form_content="hello",
        inputs=[FileListInputConfig(output_variable_name="attachments", number_limits=2)],
        user_actions=[UserActionConfig(id="submit", title="Submit")],
        rendered_content="<p>hello</p>",
        expiration_time=sample_form_record.expiration_time,
    )
    repo = form_repository(dataclasses.replace(sample_form_record, definition=definition))
    service = HumanInputService(unbound_session_factory, form_repository=repo)

    with pytest.raises(
        InvalidFormDataError,
        match="Invalid value for file list input 'attachments'",
    ):
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="submit",
            form_data={"attachments": ["not-a-file"]},
        )


def test_submit_form_by_token_rejects_cross_tenant_file(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    definition = FormDefinition(
        form_content="hello",
        inputs=[FileInputConfig(output_variable_name="attachment")],
        user_actions=[UserActionConfig(id="submit", title="Submit")],
        rendered_content="<p>hello</p>",
        expiration_time=sample_form_record.expiration_time,
    )
    repo = form_repository(dataclasses.replace(sample_form_record, definition=definition))
    service = HumanInputService(unbound_session_factory, form_repository=repo)
    mocker.patch("services.human_input_service.build_from_mapping", side_effect=ValueError("Invalid upload file"))

    with pytest.raises(InvalidFormDataError, match="Invalid value for file input 'attachment'"):
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="submit",
            form_data={
                "attachment": {
                    "transfer_method": "local_file",
                    "upload_file_id": "4e0d1b87-52f2-49f6-b8c6-95cd9c954b3e",
                    "type": "document",
                }
            },
        )

    cast(MagicMock, repo.mark_submitted).assert_not_called()


def test_submit_form_by_token_rejects_cross_tenant_file_list(
    form_repository: Callable[[HumanInputFormRecord | None], HumanInputFormSubmissionRepository],
    sample_form_record: HumanInputFormRecord,
    unbound_session_factory: sessionmaker[Session],
    mocker: MockerFixture,
) -> None:
    definition = FormDefinition(
        form_content="hello",
        inputs=[FileListInputConfig(output_variable_name="attachments", number_limits=2)],
        user_actions=[UserActionConfig(id="submit", title="Submit")],
        rendered_content="<p>hello</p>",
        expiration_time=sample_form_record.expiration_time,
    )
    repo = form_repository(dataclasses.replace(sample_form_record, definition=definition))
    service = HumanInputService(unbound_session_factory, form_repository=repo)
    mocker.patch("services.human_input_service.build_from_mappings", side_effect=ValueError("Invalid upload file"))

    with pytest.raises(
        InvalidFormDataError,
        match="Invalid value for file list input 'attachments'",
    ):
        service.submit_form_by_token(
            recipient_type=RecipientType.STANDALONE_WEB_APP,
            form_token="token",
            selected_action_id="submit",
            form_data={
                "attachments": [
                    {
                        "transfer_method": "local_file",
                        "upload_file_id": "4e0d1b87-52f2-49f6-b8c6-95cd9c954b3e",
                        "type": "document",
                    }
                ]
            },
        )

    cast(MagicMock, repo.mark_submitted).assert_not_called()