Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tests #227

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/controlflow/orchestration/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def get_agent_tasks(self, agent: BaseAgent, ready_tasks: list[Task]) -> list[Tas
f'Task "{task.friendly_name()}" has exceeded max iterations and will be marked failed'
)
task.mark_failed(
message="Task was not completed before exceeding its maximum number of iterations."
reason="Task was not completed before exceeding its maximum number of iterations."
)
continue

Expand Down
2 changes: 1 addition & 1 deletion src/controlflow/tui/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def run():
result="this is my result\n\n and here is more and here is more and here is more and here is more and here is more and here is more\n\n and here is more and here is more and here is more"
)
await asyncio.sleep(1)
t0.mark_failed(message="this is my result")
t0.mark_failed(reason="this is my result")
app.update_message(AIMessage(content="hello there"))
await asyncio.sleep(1)
app.update_message(AIMessage(content="hello there"))
Expand Down
10 changes: 5 additions & 5 deletions tests/agents/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_agent_loads_instructions_at_creation(self):
class TestDefaultAgent:
def test_default_agent(self):
assert controlflow.defaults.agent.name == "Marvin"
assert Task("task").get_agents()[0] is controlflow.defaults.agent
assert Task("task").get_agent() is controlflow.defaults.agent

def test_default_agent_has_no_tools(self):
assert controlflow.defaults.agent.tools == []
Expand All @@ -56,8 +56,8 @@ def test_default_agent_can_be_assigned(self):
controlflow.defaults.agent = new_default_agent

assert controlflow.defaults.agent.name == "New Agent"
assert Task("task").get_agents()[0] is new_default_agent
assert [a.name for a in Task("task").get_agents()] == ["New Agent"]
assert Task("task").get_agent() is new_default_agent
assert Task("task").get_agent().name == "New Agent"

def test_updating_the_default_model_updates_the_default_agent_model(self):
new_model = ChatOpenAI(model="gpt-3.5-turbo")
Expand All @@ -68,8 +68,8 @@ def test_updating_the_default_model_updates_the_default_agent_model(self):
assert new_agent.get_model() is new_model

task = Task("task")
assert task.get_agents()[0].model is None
assert task.get_agents()[0].get_model() is new_model
assert task.get_agent().model is None
assert task.get_agent().get_model() is new_model


class TestAgentPrompt:
Expand Down
17 changes: 7 additions & 10 deletions tests/flows/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_flow_initialization(self):
flow = Flow()
assert flow.thread_id is not None
assert len(flow.tools) == 0
assert len(flow.agents) == 0
assert len(flow.context) == 0
assert flow.agent is None
assert flow.context == {}

def test_flow_with_custom_tools(self):
def tool1():
Expand Down Expand Up @@ -145,20 +145,17 @@ def test_child_flow_messages_dont_go_to_parent(self):
class TestFlowCreatesDefaults:
def test_flow_with_custom_agents(self):
agent1 = Agent(name="Agent 1")
agent2 = Agent(name="Agent 2")
flow = Flow(agents=[agent1, agent2])
assert len(flow.agents) == 2
assert agent1 in flow.agents
assert agent2 in flow.agents
flow = Flow(agent=agent1)
assert flow.agent == agent1

def test_flow_agent_becomes_task_default(self):
agent = Agent()
t1 = Task("t1")
assert t1.agents != [agent]
assert t1.agent is not agent

with Flow(agents=[agent]):
with Flow(agent=agent):
t2 = Task("t2")
assert t2.get_agents() == [agent]
assert t2.get_agent() == agent


class TestFlowPrompt:
Expand Down
30 changes: 15 additions & 15 deletions tests/tasks/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def test_status_coverage():
assert INCOMPLETE_STATUSES + COMPLETE_STATUSES == set(TaskStatus)
assert INCOMPLETE_STATUSES | COMPLETE_STATUSES == set(TaskStatus)


def test_context_open_and_close():
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_task_mark_successful_and_mark_failed():
task = SimpleTask()
task.mark_successful(result=None)
assert task.status == TaskStatus.SUCCESSFUL
task.mark_failed(message="test error")
task.mark_failed(reason="test error")
assert task.status == TaskStatus.FAILED


Expand Down Expand Up @@ -103,54 +103,54 @@ def test_task_parent_context():

def test_task_agent_assignment():
agent = Agent(name="Test Agent")
task = SimpleTask(agents=[agent])
assert agent in task.agents
task = SimpleTask(agent=agent)
assert task.agent is agent


def test_task_bad_agent_assignment():
with pytest.raises(ValueError):
SimpleTask(agents=[])
SimpleTask(agent=5)


def test_task_loads_agent_from_parent():
agent = Agent(name="Test Agent")
with SimpleTask(agents=[agent]):
with SimpleTask(agent=agent):
child = SimpleTask()

assert child.agents is None
assert child.get_agents() == [agent]
assert child.get_agent() == agent


def test_task_loads_agent_from_flow():
def_agent = controlflow.defaults.agent
agent = Agent(name="Test Agent")
with Flow(agents=[agent]):
with Flow(agent=agent):
task = SimpleTask()

assert task.agents is None
assert task.get_agents() == [agent]
assert task.get_agent() == agent

# outside the flow context, pick up the default agent
assert task.get_agents() == [def_agent]
assert task.get_agent() == def_agent


def test_task_loads_agent_from_default_if_none_otherwise():
agent = controlflow.defaults.agent
task = SimpleTask()

assert task.agents is None
assert task.get_agents() == [agent]
assert task.get_agent() == agent


def test_task_loads_agent_from_parent_before_flow():
agent1 = Agent(name="Test Agent 1")
agent2 = Agent(name="Test Agent 2")
with Flow(agents=[agent1]):
with SimpleTask(agents=[agent2]):
with Flow(agent=agent1):
with SimpleTask(agent=agent2):
child = SimpleTask()

assert child.agents is None
assert child.get_agents() == [agent2]
assert child.agent is None
assert child.get_agent() == agent2


class TestFlowRegistration:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_default_agent_failed_validation():
def test_set_default_agent():
agent = controlflow.Agent(name="Marvin")
controlflow.defaults.agent = agent
assert controlflow.Task("").get_agents()[0] is agent
assert controlflow.Task("").get_agent() is agent


def test_default_history_failed_validation():
Expand Down
11 changes: 5 additions & 6 deletions tests/utilities/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ def test_record_task_events(default_fake_llm):
with record_events() as events:
task.run()

assert events[0].event == "select-agent"
assert events[1].event == "agent-message"
assert response == events[1].ai_message
assert events[0].event == "agent-message"
assert response == events[0].ai_message

assert events[5].event == "tool-result"
assert events[5].tool_call == {
assert events[2].event == "tool-result"
assert events[2].tool_call == {
"name": "mark_task_12345_successful",
"args": {"result": "Hello!"},
"id": "call_ZEPdV8mCgeBe5UHjKzm6e3pe",
}
assert events[5].tool_result.model_dump() == dict(
assert events[2].tool_result.model_dump() == dict(
tool_call_id="call_ZEPdV8mCgeBe5UHjKzm6e3pe",
str_result='Task 12345 ("say hello") marked successful.',
is_error=False,
Expand Down