Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
194 commits
Select commit Hold shift + click to select a range
55bedaa
edit 1
Dec 25, 2024
619afa9
edit 2
Dec 25, 2024
38aaf58
Repository Governance
Dec 30, 2024
3fe801b
added stale-bot
Dec 30, 2024
7e7b72e
stalebot test
Dec 30, 2024
5a676da
chNGED TO 1 DAY
Dec 30, 2024
d20ffde
edit 3
Dec 30, 2024
9b55d1a
Merge branch 'main' of https://github.com/Harmanpreet-Microsoft/Multi…
Jan 1, 2025
5e568d8
Create test
Harmanpreet-Microsoft Jan 1, 2025
3eb8010
edit1
Jan 1, 2025
8b1298f
edit2
Jan 1, 2025
683c4a9
edit 4
Jan 1, 2025
8ebaa51
edit 5
Jan 1, 2025
801d6eb
edit 6
Jan 1, 2025
1c845fa
edit 6
Jan 1, 2025
82446b2
edit 7
Jan 1, 2025
a9f989f
edit 8
Jan 1, 2025
3e31574
edit 5
Jan 1, 2025
805a8ba
edit 10
Jan 1, 2025
ab4c4c5
edit 11
Jan 1, 2025
7a5dbb4
edit 12
Jan 1, 2025
a32fb8d
edit 13
Jan 1, 2025
bec99d7
Update stale-bot.yml
Harmanpreet-Microsoft Jan 1, 2025
df179d1
edit 14
Jan 2, 2025
d755232
Merge branch 'main' of https://github.com/Harmanpreet-Microsoft/Multi…
Jan 2, 2025
955eb5b
edit 15
Jan 2, 2025
bc48275
edit 16
Jan 2, 2025
e47008f
edit 17
Jan 2, 2025
6a56c07
Update stale-bot.yml
Harmanpreet-Microsoft Jan 3, 2025
5905679
Update CODEOWNER
Harmanpreet-Microsoft Jan 3, 2025
40dfc88
Create nwetest
Harmanpreet-Microsoft Jan 3, 2025
07e1d4e
added pylint
Jan 7, 2025
06f1c2f
Merge branch 'main' of https://github.com/Harmanpreet-Microsoft/Multi…
Jan 7, 2025
9222555
changed flak loc
Jan 7, 2025
bc4e843
edit 1
Jan 7, 2025
7ddb489
add backend test workflow
Jan 7, 2025
aceb522
added test file
Jan 8, 2025
47b3741
edit 2
Jan 8, 2025
950d330
added sample test file
Jan 8, 2025
962b38b
changed py version
Jan 8, 2025
f31bf82
made edits to workflow
Jan 8, 2025
0fcbff4
pass even if test filesa r not present
Jan 8, 2025
4569e80
removed test file
Jan 8, 2025
c0a4dae
added sample test file
Jan 8, 2025
182db35
Create docker-build-and-push.yml
Harmanpreet-Microsoft Jan 14, 2025
d6afd6f
Merge pull request #1 from Harmanpreet-Microsoft/stale_bot_tset
Harmanpreet-Microsoft Jan 14, 2025
0bf1920
Merge pull request #4 from microsoft/main
Harmanpreet-Microsoft Jan 15, 2025
c754e9c
Delete test
Harmanpreet-Microsoft Jan 15, 2025
3109279
Delete src/backend/middleware/test_health_check.py
Harmanpreet-Microsoft Jan 15, 2025
23cd01f
Delete .github/nwetest
Harmanpreet-Microsoft Jan 15, 2025
a720e12
Delete .github/workflows/CI.yml
Harmanpreet-Microsoft Jan 15, 2025
7791bd8
updated docker
Jan 15, 2025
4a1ece9
Merge branch 'main' of https://github.com/Harmanpreet-Microsoft/Multi…
Jan 15, 2025
e31db2d
Testcases
UtkarshMishra-Microsoft Jan 15, 2025
b8ef736
Testcases
UtkarshMishra-Microsoft Jan 16, 2025
945062f
Testcases
UtkarshMishra-Microsoft Jan 16, 2025
6d92760
Update test.yml
Harmanpreet-Microsoft Jan 17, 2025
701695a
Testcases
UtkarshMishra-Microsoft Jan 17, 2025
7c74a8c
Testcases
UtkarshMishra-Microsoft Jan 17, 2025
f5e648a
Merge branch 'hotfix' into UT-10888-backend
Roopan-Microsoft Jan 17, 2025
fc3e814
Merge branch 'testcases-branch' into UT-10888-backend
Harmanpreet-Microsoft Jan 17, 2025
e1dda51
Testcases
UtkarshMishra-Microsoft Jan 17, 2025
ab065a0
Testcases
UtkarshMishra-Microsoft Jan 17, 2025
352e88a
Testcases
UtkarshMishra-Microsoft Jan 17, 2025
f672d43
Testcases
UtkarshMishra-Microsoft Jan 17, 2025
3105a0e
Merge branch 'hotfix' into UT-10888-backend
Roopan-Microsoft Jan 20, 2025
5a683db
Pylint issues fixed
Roopan-Microsoft Jan 20, 2025
3769ccc
pylint issues fixed
Roopan-Microsoft Jan 20, 2025
472af31
Pylint issues fixed
Roopan-Microsoft Jan 20, 2025
87bb3bd
Pylint issues fixed
Roopan-Microsoft Jan 20, 2025
9f12529
pylint fixed for line
Roopan-Microsoft Jan 20, 2025
3b8a63f
fix for test cases failure
Jan 20, 2025
e34ecfc
added agentutils test
Jan 21, 2025
3415894
updated test utils file
Jan 21, 2025
4b91693
Testcases
UtkarshMishra-Microsoft Jan 21, 2025
7966c45
Testcases
UtkarshMishra-Microsoft Jan 21, 2025
0970f67
Testcases
UtkarshMishra-Microsoft Jan 21, 2025
1873fad
Testcases
UtkarshMishra-Microsoft Jan 21, 2025
7b99b4b
Testcases
UtkarshMishra-Microsoft Jan 21, 2025
624f7a6
added genric test file
Jan 22, 2025
eacc9ef
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
5297cfb
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
UtkarshMishra-Microsoft Jan 22, 2025
a1b6077
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
4c46ae3
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
ca3490b
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
8a8aa25
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
100b43a
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
3b229fe
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
a1d8a14
solving pylint errors
Jan 22, 2025
1e141d7
resolved pylint errors
Jan 22, 2025
4ef9083
edit 2
Jan 22, 2025
2c30ff4
resolving pylint
Jan 22, 2025
fda1e2f
edit 3
Jan 22, 2025
440ca84
resolved issue
Jan 22, 2025
89659b5
resolve edit 5
Jan 22, 2025
a61c0cb
resoleving pylint edit 6
Jan 22, 2025
f84ad1f
pylint error edit 7
Jan 22, 2025
f20b5f3
Update test.yml
UtkarshMishra-Microsoft Jan 22, 2025
f5940d4
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
2989411
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
UtkarshMishra-Microsoft Jan 22, 2025
33e9bc4
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
59f9794
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
88d27b3
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
a84a75c
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
e7b2375
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
e2a77bc
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
78738ce
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
af1681c
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
1e0531d
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
b1182d3
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
133e876
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
6694d2c
Testcases
UtkarshMishra-Microsoft Jan 22, 2025
8d774a2
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
39b1f53
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
ab92475
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
9eba704
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
9ec790d
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
6790879
Update test.yml
UtkarshMishra-Microsoft Jan 23, 2025
e44730e
Update test.yml
UtkarshMishra-Microsoft Jan 23, 2025
1b653dd
Update test.yml
UtkarshMishra-Microsoft Jan 23, 2025
fc14494
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
26a3cb8
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
UtkarshMishra-Microsoft Jan 23, 2025
7f901f4
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
510f3a0
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
5558053
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
9397322
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
a7badbe
Merge branch 'hotfix' into UT-10888-backend
UtkarshMishra-Microsoft Jan 23, 2025
3e41cfb
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
f4b4fb5
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
86df8c5
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
8652cff
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
f59954e
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
f7bd5fb
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
49934de
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
15231f7
Testcases
UtkarshMishra-Microsoft Jan 23, 2025
cf7aff8
Testing
UtkarshMishra-Microsoft Jan 24, 2025
a949dc2
Testing
UtkarshMishra-Microsoft Jan 24, 2025
bc879a5
Testcases
UtkarshMishra-Microsoft Jan 24, 2025
984a6d3
Testcases
UtkarshMishra-Microsoft Jan 24, 2025
4bd890b
Testcases
UtkarshMishra-Microsoft Jan 24, 2025
4918ff0
Testcases
UtkarshMishra-Microsoft Jan 24, 2025
8461a50
Testcases
UtkarshMishra-Microsoft Jan 27, 2025
a94e78b
Testcases
UtkarshMishra-Microsoft Jan 27, 2025
bceb635
added test_base_agent file
Jan 28, 2025
e624781
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
Jan 28, 2025
265a0a4
Merge branch 'hotfix' into UT-10888-backend
UtkarshMishra-Microsoft Jan 28, 2025
89a5430
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
70db478
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
a1ec196
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
626fc67
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
8d9dd35
added the test_agentutils and test_base_agent
Jan 28, 2025
c78ddcb
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
Jan 28, 2025
2feb981
deleted agentutils.py
Jan 28, 2025
b3c2ef4
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
82f27a4
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
e790043
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
5702440
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
8ba7f17
Testcases
UtkarshMishra-Microsoft Jan 28, 2025
4affc61
Testcases
UtkarshMishra-Microsoft Jan 29, 2025
c7ff11b
Testcases
UtkarshMishra-Microsoft Jan 29, 2025
40aef6b
edited flak
Jan 29, 2025
bd6dd94
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
Jan 29, 2025
6d84ffa
resolved pylint issues
Jan 29, 2025
b3f4305
Test case Group_chat_manager
Pradheep-Microsoft Jan 29, 2025
11d9f85
Test case Group_chat_manager
Pradheep-Microsoft Jan 29, 2025
c8379d9
updated test_agentutils.py
Jan 29, 2025
8da83f7
editv1
Jan 29, 2025
7152dfa
resolving pylint issue
Jan 29, 2025
1711b0e
edit 2
Jan 29, 2025
cdd1965
Testcases
UtkarshMishra-Microsoft Jan 30, 2025
e48cc18
Testcases
UtkarshMishra-Microsoft Jan 30, 2025
cbcad96
Testcases
UtkarshMishra-Microsoft Jan 30, 2025
4cdb383
Testcases
UtkarshMishra-Microsoft Jan 30, 2025
81aec05
Merge branch 'UT-10888-backend' of https://github.com/microsoft/Multi…
Pradheep-Microsoft Jan 30, 2025
cf7559b
Merge branch 'hotfix' into UT-10888-backend
Roopan-Microsoft Feb 3, 2025
f0c0590
docker changes reverted
Roopan-Microsoft Feb 3, 2025
0710856
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
0c34040
Merge branch 'hotfix' into UT-10888-backend
UtkarshMishra-Microsoft Feb 4, 2025
a589a0c
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
5bf8b4b
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
196b238
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
c27c863
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
5285912
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
f5944d5
Testcases
UtkarshMishra-Microsoft Feb 4, 2025
510e5b2
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
7cedc71
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
e88c66a
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
3431cd9
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
434bb25
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
6c5741d
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
4ad10d2
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
d1ccf12
Testcases
UtkarshMishra-Microsoft Feb 5, 2025
c88344f
TestCase agentutils
Pradheep-Microsoft Feb 5, 2025
1670bc0
TestCase agentutils
Pradheep-Microsoft Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 161 additions & 36 deletions src/backend/tests/agents/test_agentutils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# pylint: disable=import-error, wrong-import-position, missing-module-docstring
import os
import sys
from unittest.mock import MagicMock
import json
import pytest
from pydantic import ValidationError
from unittest.mock import MagicMock, patch
from pydantic import BaseModel

# Environment and module setup
sys.modules["azure.monitor.events.extension"] = MagicMock()
# Adjust sys.path so that the project root is found.
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../")))

# Set required environment variables.
os.environ["COSMOSDB_ENDPOINT"] = "https://mock-endpoint"
os.environ["COSMOSDB_KEY"] = "mock-key"
os.environ["COSMOSDB_DATABASE"] = "mock-database"
Expand All @@ -16,39 +17,163 @@
os.environ["AZURE_OPENAI_API_VERSION"] = "2023-01-01"
os.environ["AZURE_OPENAI_ENDPOINT"] = "https://mock-openai-endpoint"

from src.backend.agents.agentutils import extract_and_update_transition_states # noqa: F401, C0413
from src.backend.models.messages import Step # noqa: F401, C0413
# Patch missing azure module so that event_utils imports without error.
sys.modules["azure.monitor.events.extension"] = MagicMock()

# --- Import the function and constant under test ---
from src.backend.agents.agentutils import (
extract_and_update_transition_states,
common_agent_system_message,
)
from src.backend.models.messages import Step
from autogen_core.components.models import AzureOpenAIChatCompletionClient

# Configure the Step model to allow extra attributes.
Step.model_config["extra"] = "allow"


# Dummy Cosmos class that records update calls.
class DummyCosmosRecorder:
def __init__(self):
self.update_called = False

async def update_step(self, step):
# To allow setting extra attributes, ensure __pydantic_extra__ is initialized.
if step.__pydantic_extra__ is None:
step.__pydantic_extra__ = {}
step.__pydantic_extra__["updated_field"] = True
self.update_called = True


# Dummy model client classes to simulate LLM responses.

class DummyModelClient(AzureOpenAIChatCompletionClient):
def __init__(self, **kwargs):
# Bypass parent's __init__.
pass

async def create(self, messages, extra_create_args=None):
# Simulate a valid response that matches the expected FSMStateAndTransition schema.
response_dict = {
"identifiedTargetState": "State1",
"identifiedTargetTransition": "Transition1"
}
dummy_resp = MagicMock()
dummy_resp.content = json.dumps(response_dict)
return dummy_resp


class DummyModelClientError(AzureOpenAIChatCompletionClient):
def __init__(self, **kwargs):
pass

async def create(self, messages, extra_create_args=None):
raise Exception("LLM error")


class DummyModelClientInvalidJSON(AzureOpenAIChatCompletionClient):
def __init__(self, **kwargs):
pass

def test_step_initialization():
"""Test Step initialization with valid data."""
async def create(self, messages, extra_create_args=None):
dummy_resp = MagicMock()
dummy_resp.content = "invalid json"
return dummy_resp


# Fixture: a dummy Step for testing.
@pytest.fixture
def dummy_step():
step = Step(
data_type="step",
plan_id="test_plan",
action="test_action",
agent="HumanAgent",
session_id="test_session",
user_id="test_user",
agent_reply="test_reply",
id="step1",
plan_id="plan1",
action="Test Action",
agent="HumanAgent", # Using string for simplicity.
status="planned",
session_id="sess1",
user_id="user1",
human_approval_status="requested",
)
# Provide a value for agent_reply.
step.agent_reply = "Test reply"
# Ensure __pydantic_extra__ is initialized for extra fields.
step.__pydantic_extra__ = {}
return step


# Tests for extract_and_update_transition_states
@pytest.mark.asyncio
async def test_extract_and_update_transition_states_success(dummy_step):
"""
Test that extract_and_update_transition_states correctly parses the LLM response,
updates the step with the expected target state and transition, and calls cosmos.update_step.
"""
model_client = DummyModelClient()
dummy_cosmos = DummyCosmosRecorder()
with patch("src.backend.agents.agentutils.CosmosBufferedChatCompletionContext", return_value=dummy_cosmos):
updated_step = await extract_and_update_transition_states(dummy_step, "sess1", "user1", "anything", model_client)
assert updated_step.identified_target_state == "State1"
assert updated_step.identified_target_transition == "Transition1"
assert dummy_cosmos.update_called is True
# Check that our extra field was set.
assert updated_step.__pydantic_extra__.get("updated_field") is True


@pytest.mark.asyncio
async def test_extract_and_update_transition_states_model_client_error(dummy_step):
"""
Test that if the model client raises an exception, it propagates.
"""
model_client = DummyModelClientError()
with patch("src.backend.agents.agentutils.CosmosBufferedChatCompletionContext", return_value=DummyCosmosRecorder()):
with pytest.raises(Exception, match="LLM error"):
await extract_and_update_transition_states(dummy_step, "sess1", "user1", "anything", model_client)


@pytest.mark.asyncio
async def test_extract_and_update_transition_states_invalid_json(dummy_step):
"""
Test that an invalid JSON response from the model client causes an exception.
"""
model_client = DummyModelClientInvalidJSON()
with patch("src.backend.agents.agentutils.CosmosBufferedChatCompletionContext", return_value=DummyCosmosRecorder()):
with pytest.raises(Exception):
await extract_and_update_transition_states(dummy_step, "sess1", "user1", "anything", model_client)


def test_common_agent_system_message_contains_delivery_address():
"""
Test that the common_agent_system_message constant contains instructions regarding the delivery address.
"""
assert "delivery address" in common_agent_system_message


@pytest.mark.asyncio
async def test_extract_and_update_transition_states_no_agent_reply(dummy_step):
"""
Test the behavior when step.agent_reply is empty.
"""
dummy_step.agent_reply = ""
# Ensure extra dict is initialized.
dummy_step.__pydantic_extra__ = {}
model_client = DummyModelClient()
with patch("src.backend.agents.agentutils.CosmosBufferedChatCompletionContext", return_value=DummyCosmosRecorder()):
updated_step = await extract_and_update_transition_states(dummy_step, "sess1", "user1", "anything", model_client)
# Even with an empty agent_reply, our dummy client returns the same valid JSON.
assert updated_step.identified_target_state == "State1"
assert updated_step.identified_target_transition == "Transition1"


def test_dummy_json_parsing():
"""
Test that the JSON parsing in extract_and_update_transition_states works for valid JSON.
"""
json_str = '{"identifiedTargetState": "TestState", "identifiedTargetTransition": "TestTransition"}'
data = json.loads(json_str)

assert step.data_type == "step"
assert step.plan_id == "test_plan"
assert step.action == "test_action"
assert step.agent == "HumanAgent"
assert step.session_id == "test_session"
assert step.user_id == "test_user"
assert step.agent_reply == "test_reply"
assert step.status == "planned"
assert step.human_approval_status == "requested"


def test_step_missing_required_fields():
"""Test Step initialization with missing required fields."""
with pytest.raises(ValidationError):
Step(
data_type="step",
action="test_action",
agent="test_agent",
session_id="test_session",
)
class DummySchema(BaseModel):
identifiedTargetState: str
identifiedTargetTransition: str
schema = DummySchema(**data)
assert schema.identifiedTargetState == "TestState"
assert schema.identifiedTargetTransition == "TestTransition"
Loading
Loading