Skip to content

Commit 13bbafe

Browse files
moonbox3Copilot
andcommitted
Add integration test exercising full activity code path (microsoft#4500)
Address PR review comment: add test_executor_activity_detects_nested_state_mutations that captures the actual executor_activity function from _setup_executor_activity and verifies it detects in-place nested mutations. This test would fail if _app.py line 314 regressed from _create_state_snapshot() back to dict(). Co-authored-by: Copilot <[email protected]>
1 parent f9e9790 commit 13bbafe

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

‎python/packages/azurefunctions/tests/test_app.py‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,79 @@ def test_create_state_snapshot_isolates_nested_objects(self) -> None:
16361636
assert snapshot["nested_dict"]["a"] == 1
16371637
assert snapshot["nested_list"] == [1, 2, 3]
16381638

1639+
def test_executor_activity_detects_nested_state_mutations(self) -> None:
1640+
"""Integration test: the full activity wrapper detects nested mutations.
1641+
1642+
This exercises the actual executor_activity function registered by
1643+
_setup_executor_activity to verify the production code path uses
1644+
_create_state_snapshot (deep copy) rather than dict() (shallow copy).
1645+
If line 314 of _app.py regressed to ``dict(deserialized_state)``,
1646+
this test would fail because in-place mutations would leak into the
1647+
snapshot and produce an empty diff.
1648+
"""
1649+
mock_executor = Mock()
1650+
mock_executor.id = "test-exec"
1651+
1652+
async def mutate_nested_state(
1653+
message: Any,
1654+
source_executor_ids: Any,
1655+
state: Any,
1656+
runner_context: Any,
1657+
) -> None:
1658+
config = state.get("Local.config")
1659+
config["code"] = "MUTATED"
1660+
config["enabled"] = True
1661+
state.commit()
1662+
1663+
mock_executor.execute = AsyncMock(side_effect=mutate_nested_state)
1664+
1665+
mock_workflow = Mock()
1666+
mock_workflow.executors = {"test-exec": mock_executor}
1667+
1668+
# Capture the activity function by making decorators pass-through
1669+
captured_activity: dict[str, Any] = {}
1670+
1671+
def passthrough_function_name(name: str) -> Callable[[FuncT], FuncT]:
1672+
def decorator(fn: FuncT) -> FuncT:
1673+
captured_activity["fn"] = fn
1674+
return fn
1675+
1676+
return decorator
1677+
1678+
def passthrough_activity_trigger(input_name: str) -> Callable[[FuncT], FuncT]:
1679+
def decorator(fn: FuncT) -> FuncT:
1680+
return fn
1681+
1682+
return decorator
1683+
1684+
with (
1685+
patch.object(AgentFunctionApp, "function_name", side_effect=passthrough_function_name),
1686+
patch.object(AgentFunctionApp, "activity_trigger", side_effect=passthrough_activity_trigger),
1687+
patch.object(AgentFunctionApp, "_setup_workflow_orchestration"),
1688+
):
1689+
AgentFunctionApp(workflow=mock_workflow)
1690+
1691+
assert "fn" in captured_activity, "activity function was not captured"
1692+
1693+
# Call the activity with nested state that the executor will mutate
1694+
input_data = json.dumps({
1695+
"message": "test",
1696+
"shared_state_snapshot": {
1697+
"Local.config": {"code": "", "enabled": False},
1698+
},
1699+
"source_executor_ids": ["orchestrator"],
1700+
})
1701+
1702+
result = json.loads(captured_activity["fn"](input_data))
1703+
1704+
# The deep copy snapshot must detect the in-place nested mutations
1705+
assert "Local.config" in result["shared_state_updates"], (
1706+
"nested mutation not detected — snapshot may be using shallow copy"
1707+
)
1708+
updated_config = result["shared_state_updates"]["Local.config"]
1709+
assert updated_config["code"] == "MUTATED"
1710+
assert updated_config["enabled"] is True
1711+
16391712

16401713
if __name__ == "__main__":
16411714
pytest.main([__file__, "-v", "--tb=short"])

0 commit comments

Comments
 (0)