Skip to content

Commit 1490365

Browse files
author
Murat Kaan Meral
committed
fix tests and styling
1 parent e3c2db6 commit 1490365

File tree

6 files changed

+46
-107
lines changed

6 files changed

+46
-107
lines changed

src/strands_agents_builder/strands.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import argparse
77
import os
8-
import sys
9-
from typing import Optional, Tuple
8+
from typing import Optional
109

1110
# Strands
1211
from strands import Agent
@@ -16,18 +15,13 @@
1615
from strands_agents_builder.tools import get_tools
1716
from strands_agents_builder.utils import model_utils
1817
from strands_agents_builder.utils.kb_utils import load_system_prompt, store_conversation_in_kb
19-
from strands_agents_builder.utils.welcome_utils import render_goodbye_message, render_welcome_message
2018
from strands_agents_builder.utils.session_utils import (
21-
create_session_manager,
22-
list_available_sessions,
23-
session_exists,
24-
get_session_info,
25-
list_sessions_command,
2619
display_agent_history,
27-
setup_session_management,
2820
handle_session_commands,
21+
list_sessions_command,
22+
setup_session_management,
2923
)
30-
24+
from strands_agents_builder.utils.welcome_utils import render_goodbye_message, render_welcome_message
3125

3226
os.environ["STRANDS_TOOL_CONSOLE_MODE"] = "enabled"
3327

src/strands_agents_builder/utils/session_utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
"""
55

66
import datetime
7-
import os
87
import time
98
import uuid
109
from pathlib import Path
1110
from typing import Optional, Tuple
1211

1312
from colorama import Fore, Style
14-
from rich.console import Console
15-
from rich.panel import Panel
1613
from rich.align import Align
1714
from rich.box import ROUNDED
15+
from rich.console import Console
16+
from rich.panel import Panel
1817
from strands.session.file_session_manager import FileSessionManager
1918

2019
# Create console for rich formatting
@@ -126,7 +125,8 @@ def list_sessions_command(session_base_path: Optional[str]) -> None:
126125
"""Handle the --list-sessions command."""
127126
if not session_base_path:
128127
console.print(
129-
"[red]Error: Session management not enabled. Use --session-path or set STRANDS_SESSION_PATH environment variable.[/red]"
128+
"[red]Error: Session management not enabled. Use --session-path or "
129+
"set STRANDS_SESSION_PATH environment variable.[/red]"
130130
)
131131
return
132132

@@ -192,7 +192,7 @@ def display_agent_history(agent, session_id: str) -> None:
192192
print(f"{Fore.WHITE}{content}{Style.RESET_ALL}")
193193
print() # Empty line after assistant message
194194

195-
except Exception as e:
195+
except Exception:
196196
# If we can't load history, just continue silently
197197
pass
198198

tests/test_strands.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55

66
import os
77
import sys
8-
import tempfile
9-
from pathlib import Path
108
from unittest import mock
119

1210
import pytest
1311

1412
from strands_agents_builder import strands
1513
from strands_agents_builder.utils.session_utils import (
14+
handle_session_commands,
1615
list_sessions_command,
1716
setup_session_management,
18-
handle_session_commands,
1917
)
2018

2119

@@ -440,9 +438,9 @@ def test_welcome_message_failure(
440438
class TestSessionManagement:
441439
"""Test cases for session management functionality"""
442440

443-
@mock.patch("builtins.print")
441+
@mock.patch("strands_agents_builder.utils.session_utils.console.print")
444442
@mock.patch("strands_agents_builder.utils.session_utils.list_available_sessions")
445-
def test_list_sessions_command_no_sessions(self, mock_list_sessions, mock_print):
443+
def test_list_sessions_command_no_sessions(self, mock_list_sessions, mock_console_print):
446444
"""Test list-sessions command when no sessions exist"""
447445
# Setup mocks
448446
mock_list_sessions.return_value = []
@@ -452,12 +450,12 @@ def test_list_sessions_command_no_sessions(self, mock_list_sessions, mock_print)
452450
strands.main()
453451

454452
# Verify appropriate message was called
455-
mock_print.assert_any_call("No sessions found.")
453+
mock_console_print.assert_any_call("[yellow]No sessions found.[/yellow]")
456454

457-
@mock.patch("builtins.print")
455+
@mock.patch("strands_agents_builder.utils.session_utils.console.print")
458456
@mock.patch("strands_agents_builder.utils.session_utils.get_session_info")
459457
@mock.patch("strands_agents_builder.utils.session_utils.list_available_sessions")
460-
def test_list_sessions_command_with_sessions(self, mock_list_sessions, mock_get_info, mock_print):
458+
def test_list_sessions_command_with_sessions(self, mock_list_sessions, mock_get_info, mock_console_print):
461459
"""Test list-sessions command when sessions exist"""
462460
# Setup mocks
463461
mock_list_sessions.return_value = ["session1", "session2"]
@@ -471,21 +469,22 @@ def test_list_sessions_command_with_sessions(self, mock_list_sessions, mock_get_
471469
strands.main()
472470

473471
# Verify sessions were listed
474-
mock_print.assert_any_call("Available sessions:")
472+
mock_console_print.assert_any_call("[bold cyan]Available sessions:[/bold cyan]")
475473
# Check that session info was called for each session
476474
mock_get_info.assert_any_call("session1", "/tmp/sessions")
477475
mock_get_info.assert_any_call("session2", "/tmp/sessions")
478476

479-
@mock.patch("builtins.print")
480-
def test_list_sessions_command_no_base_path(self, mock_print):
477+
@mock.patch("strands_agents_builder.utils.session_utils.console.print")
478+
def test_list_sessions_command_no_base_path(self, mock_console_print):
481479
"""Test list-sessions command when no session path is configured"""
482480
# Mock sys.argv without session path
483481
with mock.patch.object(sys, "argv", ["strands", "--list-sessions"]):
484482
strands.main()
485483

486484
# Verify appropriate error message was called
487-
mock_print.assert_called_with(
488-
"Error: Session management not enabled. Use --session-path or set STRANDS_SESSION_PATH environment variable."
485+
mock_console_print.assert_called_with(
486+
"[red]Error: Session management not enabled. Use --session-path or "
487+
"set STRANDS_SESSION_PATH environment variable.[/red]"
489488
)
490489

491490
@mock.patch("strands_agents_builder.utils.session_utils.create_session_manager")

tests/tools/test_strand.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,11 @@ def test_strand_env_system_prompt(self):
105105
def test_strand_file_system_prompt(self):
106106
"""Test loading system prompt from file"""
107107
with (
108-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getenv", return_value=None),
109-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getcwd", return_value="/test/dir"),
110-
mock.patch("src.strands_agents_builder.utils.kb_utils.Path") as mock_path_class,
108+
mock.patch("pathlib.Path.exists", return_value=True),
109+
mock.patch("pathlib.Path.is_file", return_value=True),
110+
mock.patch("pathlib.Path.read_text", return_value="Prompt from file\n"),
111111
mock.patch("tools.strand.Agent") as mock_agent_class,
112112
):
113-
# Setup mock path instances
114-
mock_cwd_path = mock.MagicMock()
115-
mock_prompt_file = mock.MagicMock()
116-
117-
# Mock Path constructor to return mock_cwd_path
118-
mock_path_class.return_value = mock_cwd_path
119-
120-
# Mock the / operator to return mock_prompt_file
121-
mock_cwd_path.__truediv__.return_value = mock_prompt_file
122-
123-
# Setup mock_prompt_file behavior
124-
mock_prompt_file.exists.return_value = True
125-
mock_prompt_file.is_file.return_value = True
126-
mock_prompt_file.read_text.return_value = "Prompt from file\n"
127113
# Setup mock agent
128114
mock_agent_instance = mock.MagicMock()
129115
mock_agent_class.return_value = mock_agent_instance
@@ -183,26 +169,12 @@ def test_strand_tool_with_file_prompt(self):
183169

184170
# Create a temporary prompt file
185171
with (
186-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getenv", return_value=None),
187-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getcwd", return_value="/test/dir"),
188-
mock.patch("src.strands_agents_builder.utils.kb_utils.Path") as mock_path_class,
172+
mock.patch("pathlib.Path.exists", return_value=True),
173+
mock.patch("pathlib.Path.is_file", return_value=True),
174+
mock.patch("pathlib.Path.read_text", return_value="Test prompt from file"),
189175
mock.patch("sys.stdout", new_callable=StringIO),
190176
mock.patch("tools.strand.Agent") as mock_agent_class,
191177
):
192-
# Setup mock path instances
193-
mock_cwd_path = mock.MagicMock()
194-
mock_prompt_file = mock.MagicMock()
195-
196-
# Mock Path constructor to return mock_cwd_path
197-
mock_path_class.return_value = mock_cwd_path
198-
199-
# Mock the / operator to return mock_prompt_file
200-
mock_cwd_path.__truediv__.return_value = mock_prompt_file
201-
202-
# Setup mock_prompt_file behavior
203-
mock_prompt_file.exists.return_value = True
204-
mock_prompt_file.is_file.return_value = True
205-
mock_prompt_file.read_text.return_value = "Test prompt from file"
206178
# Mock the agent instance
207179
mock_agent_instance = mock.MagicMock()
208180
mock_agent_class.return_value = mock_agent_instance

tests/utils/test_kb_utils.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,14 @@ def test_load_system_prompt_from_env(temp_env):
1919
def test_load_system_prompt_from_file():
2020
"""Test loading system prompt from .prompt file"""
2121
with (
22-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getenv", return_value=None),
23-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getcwd", return_value="/test/dir"),
24-
mock.patch("src.strands_agents_builder.utils.kb_utils.Path") as mock_path_class,
22+
mock.patch("pathlib.Path.exists") as mock_exists,
23+
mock.patch("pathlib.Path.is_file") as mock_is_file,
24+
mock.patch("pathlib.Path.read_text") as mock_read_text,
2525
):
26-
# Setup mock path instances
27-
mock_cwd_path = mock.MagicMock()
28-
mock_prompt_file = mock.MagicMock()
29-
30-
# Mock Path constructor to return mock_cwd_path
31-
mock_path_class.return_value = mock_cwd_path
32-
33-
# Mock the / operator to return mock_prompt_file
34-
mock_cwd_path.__truediv__.return_value = mock_prompt_file
35-
36-
# Setup mock_prompt_file behavior
37-
mock_prompt_file.exists.return_value = True
38-
mock_prompt_file.is_file.return_value = True
39-
mock_prompt_file.read_text.return_value = "Test prompt from file\n"
26+
# Setup mocks
27+
mock_exists.return_value = True
28+
mock_is_file.return_value = True
29+
mock_read_text.return_value = "Test prompt from file\n"
4030

4131
# Load prompt
4232
prompt = load_system_prompt()
@@ -47,23 +37,9 @@ def test_load_system_prompt_from_file():
4737

4838
def test_load_default_system_prompt():
4939
"""Test loading default system prompt when env and file are not available"""
50-
with (
51-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getenv", return_value=None),
52-
mock.patch("src.strands_agents_builder.utils.kb_utils.os.getcwd", return_value="/test/dir"),
53-
mock.patch("src.strands_agents_builder.utils.kb_utils.Path") as mock_path_class,
54-
):
55-
# Setup mock path instances
56-
mock_cwd_path = mock.MagicMock()
57-
mock_prompt_file = mock.MagicMock()
58-
59-
# Mock Path constructor to return mock_cwd_path
60-
mock_path_class.return_value = mock_cwd_path
61-
62-
# Mock the / operator to return mock_prompt_file
63-
mock_cwd_path.__truediv__.return_value = mock_prompt_file
64-
65-
# Setup mock_prompt_file behavior - file doesn't exist
66-
mock_prompt_file.exists.return_value = False
40+
with mock.patch("pathlib.Path.exists") as mock_exists:
41+
# Setup mock
42+
mock_exists.return_value = False
6743

6844
# Load prompt
6945
prompt = load_system_prompt()

tests/utils/test_session_utils.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
Unit tests for the session_utils module using pytest
44
"""
55

6-
import os
76
import tempfile
87
from pathlib import Path
98
from unittest import mock
109

11-
import pytest
12-
1310
from strands_agents_builder.utils.session_utils import (
14-
get_sessions_directory,
15-
list_available_sessions,
1611
create_session_manager,
12+
display_agent_history,
1713
generate_session_id,
18-
session_exists,
1914
get_session_info,
15+
get_sessions_directory,
16+
handle_session_commands,
17+
list_available_sessions,
2018
list_sessions_command,
21-
display_agent_history,
19+
session_exists,
2220
setup_session_management,
23-
handle_session_commands,
2421
)
2522

2623

@@ -156,7 +153,7 @@ def test_generates_session_id_when_none_provided(self, mock_file_session_manager
156153
mock_file_session_manager.return_value = mock_manager_instance
157154

158155
with tempfile.TemporaryDirectory() as temp_dir:
159-
result = create_session_manager(base_path=temp_dir)
156+
create_session_manager(base_path=temp_dir)
160157

161158
# Verify FileSessionManager was called with a generated session ID
162159
mock_file_session_manager.assert_called_once()
@@ -288,7 +285,7 @@ def test_full_session_workflow(self):
288285

289286
# Test create_session_manager creates directory and manager
290287
with mock.patch("strands_agents_builder.utils.session_utils.FileSessionManager") as mock_fsm:
291-
manager = create_session_manager(session_id="test-session", base_path=base_path)
288+
create_session_manager(session_id="test-session", base_path=base_path)
292289
mock_fsm.assert_called_once()
293290

294291
# Create some test session directories
@@ -369,7 +366,8 @@ def test_list_sessions_command_no_base_path(self, mock_console_print):
369366

370367
# Verify appropriate error message was called
371368
mock_console_print.assert_called_with(
372-
"[red]Error: Session management not enabled. Use --session-path or set STRANDS_SESSION_PATH environment variable.[/red]"
369+
"[red]Error: Session management not enabled. Use --session-path or "
370+
"set STRANDS_SESSION_PATH environment variable.[/red]"
373371
)
374372

375373
@mock.patch("strands_agents_builder.utils.session_utils.console.print")

0 commit comments

Comments
 (0)