multi-agent-sdlc / tests /workflow /test_runner.py
Pedro
cover run persistence and runner
7206188
Raw History Blame Contribute Delete
5.99 kB
from dataclasses import replace
from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
from langchain_core.runnables import RunnableConfig
import multi_agent_sdlc.workflow.runner as runner_module
from multi_agent_sdlc.workflow.models import WorkflowOutcome, WorkflowTermination
from multi_agent_sdlc.workflow.run_repository import WorkflowRun, WorkflowRunStatus
@pytest.fixture
def workflow_run() -> WorkflowRun:
return WorkflowRun(
thread_id="test-thread",
status=WorkflowRunStatus.RUNNING,
request="Build an application.",
project_directory=None,
outcome=None,
terminal_reason=None,
terminal_node=None,
retryable=False,
error_type=None,
created_at="created",
updated_at="updated",
completed_at=None,
)
@pytest.fixture
def workflow_config() -> RunnableConfig:
return {"configurable": {"thread_id": "test-thread"}}
def test_invoke_graph_persists_unexpected_failure(
workflow_run: WorkflowRun,
workflow_config: RunnableConfig,
monkeypatch: pytest.MonkeyPatch,
) -> None:
graph = MagicMock()
graph.invoke.side_effect = RuntimeError("Planner service unavailable.")
graph.get_state.return_value = SimpleNamespace(next=("planner",))
fail_workflow_run = MagicMock()
monkeypatch.setattr(
runner_module,
"fail_workflow_run",
fail_workflow_run,
)
with pytest.raises(RuntimeError, match="Planner service unavailable"):
runner_module._invoke_graph(
graph,
{"request": "Build an application."},
workflow_config,
workflow_run,
)
fail_workflow_run.assert_called_once_with(
"test-thread",
reason="Planner service unavailable.",
node="planner",
error_type="RuntimeError",
)
@pytest.mark.parametrize(
"status",
[
WorkflowRunStatus.COMPLETED,
WorkflowRunStatus.FAILED,
],
)
def test_resume_workflow_allows_terminal_run_from_pending_checkpoint(
status: WorkflowRunStatus,
workflow_run: WorkflowRun,
monkeypatch: pytest.MonkeyPatch,
) -> None:
outcome = WorkflowOutcome.SUCCEEDED if status == WorkflowRunStatus.COMPLETED else WorkflowOutcome.INTERNAL_ERROR
terminal_workflow_run = replace(
workflow_run,
status=status,
outcome=outcome,
terminal_reason="Previous execution stopped.",
terminal_node="planner",
completed_at="completed",
)
get_workflow_run = MagicMock(return_value=terminal_workflow_run)
update_workflow_run_status = MagicMock()
graph = MagicMock()
graph.get_state.return_value = SimpleNamespace(
interrupts=(),
next=("planner",),
)
build_graph = MagicMock(return_value=graph)
checkpointer_context = MagicMock()
checkpointer_context.__enter__.return_value = MagicMock()
create_checkpointer = MagicMock(return_value=checkpointer_context)
invoke_graph = MagicMock(return_value={})
process_workflow_result = MagicMock()
monkeypatch.setattr(runner_module, "get_workflow_run", get_workflow_run)
monkeypatch.setattr(
runner_module,
"update_workflow_run_status",
update_workflow_run_status,
)
monkeypatch.setattr(runner_module, "create_checkpointer", create_checkpointer)
monkeypatch.setattr(runner_module, "build_graph", build_graph)
monkeypatch.setattr(runner_module, "_invoke_graph", invoke_graph)
monkeypatch.setattr(
runner_module,
"_process_workflow_result",
process_workflow_result,
)
runner_module.resume_workflow(
thread_id="test-thread",
checkpoint_id="pending-checkpoint",
configurable={},
)
get_workflow_run.assert_called_once_with("test-thread")
update_workflow_run_status.assert_called_once_with(
"test-thread",
WorkflowRunStatus.RUNNING,
)
invoke_graph.assert_called_once()
process_workflow_result.assert_called_once()
def test_process_workflow_result_persists_structured_termination(
workflow_run: WorkflowRun,
workflow_config: RunnableConfig,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
termination = WorkflowTermination(
outcome=WorkflowOutcome.SUCCEEDED,
reason="Deployment completed successfully.",
node="deployer",
)
graph = MagicMock()
graph.get_state.return_value = SimpleNamespace(
next=(),
values={"workflow_termination": termination},
)
finish_workflow_run = MagicMock()
monkeypatch.setattr(
runner_module,
"finish_workflow_run",
finish_workflow_run,
)
runner_module._process_workflow_result(
graph,
{},
workflow_run,
workflow_config,
)
finish_workflow_run.assert_called_once_with("test-thread", termination)
output = capsys.readouterr().out
assert "Outcome: succeeded" in output
assert "Terminal node: deployer" in output
assert "Thread ID: test-thread" in output
def test_process_workflow_result_rejects_missing_termination(
workflow_run: WorkflowRun,
workflow_config: RunnableConfig,
monkeypatch: pytest.MonkeyPatch,
) -> None:
graph = MagicMock()
graph.get_state.return_value = SimpleNamespace(next=(), values={})
fail_workflow_run = MagicMock()
monkeypatch.setattr(
runner_module,
"fail_workflow_run",
fail_workflow_run,
)
with pytest.raises(RuntimeError, match="without structured termination"):
runner_module._process_workflow_result(
graph,
{},
workflow_run,
workflow_config,
)
fail_workflow_run.assert_called_once_with(
"test-thread",
reason="Workflow reached END without structured termination information.",
node="finalize_workflow",
error_type="RuntimeError",
)