generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 197
Add end to end test for read, write, edit tools #100
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
Merged
JackYPCOnline
merged 5 commits into
strands-agents:main
from
JackYPCOnline:test_read_write_edit
Jun 27, 2025
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5436f4d
test(memory): add an end-to-end integration test
JackYPCOnline d671040
test: refactor code to be more readable
JackYPCOnline d6ba072
test(read_write_edit): add end to end tests for these tools
JackYPCOnline 0f9d142
Merge branch 'strands-agents:main' into test_read_write_edit
JackYPCOnline 115033f
test(read_write_edit): refactor minor code style
JackYPCOnline File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import os | ||
| import re | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from strands import Agent | ||
| from strands_tools import editor, file_read, file_write | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def agent(): | ||
| """Agent with file read, write, and editor tools.""" | ||
| return Agent(tools=[file_write, file_read, editor]) | ||
|
|
||
|
|
||
| def extract_file_content(response): | ||
| """Helper function to extract code block content from LLM output.""" | ||
| match = re.search(r"```(?:[a-zA-Z]*\n)?(.*?)```", str(response), re.DOTALL) | ||
| return match.group(1) if match else str(response) | ||
|
|
||
|
|
||
| @patch.dict(os.environ, {"BYPASS_TOOL_CONSENT": "true"}) | ||
JackYPCOnline marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def test_semantic_write_read_edit_workflow(agent, tmp_path): | ||
| """Test complete semantic workflow: write -> read -> edit -> verify.""" | ||
| file_path = tmp_path / "semantic_test.txt" | ||
| initial_content = "Hello world from integration test!" | ||
|
|
||
| # 1. Write file | ||
| write_response = agent(f"Write '{initial_content}' to file `{file_path}`") | ||
| assert "success" in str(write_response).lower() or "written" in str(write_response).lower() | ||
|
|
||
| # 2. Read file back | ||
| read_response = agent(f"Read the contents of file `{file_path}`") | ||
zastrowm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| content = extract_file_content(read_response) | ||
| assert initial_content in content | ||
|
|
||
| # 3. Replace text | ||
| edit_response = agent(f"In file `{file_path}`, replace 'Hello' with 'Hi'") | ||
| assert "success" in str(edit_response).lower() or "replaced" in str(edit_response).lower() | ||
|
|
||
| # 4. Verify | ||
| verify_response = agent(f"Show me the contents of `{file_path}`") | ||
| final_content = extract_file_content(verify_response) | ||
| assert "Hi world" in final_content | ||
JackYPCOnline marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert "Hello" not in final_content | ||
|
|
||
|
|
||
| @patch.dict(os.environ, {"BYPASS_TOOL_CONSENT": "true"}) | ||
| def test_semantic_python_file_creation(agent, tmp_path): | ||
| """Test creating and modifying Python code semantically.""" | ||
| file_path = tmp_path / "test_script.py" | ||
|
|
||
| # 1. Create Python file | ||
| create_response = agent(f"Create a Python file at `{file_path}` with a function that prints 'Hello World'") | ||
| assert "success" in str(create_response).lower() or "created" in str(create_response).lower() | ||
|
|
||
| # 2. Read and verify | ||
| read_response = agent(f"Show me the Python code in `{file_path}`") | ||
| content = str(read_response) | ||
| assert "def" in content and "print" in content and "Hello World" in content | ||
|
|
||
| # 3. Modify the function | ||
| modify_response = agent(f"In `{file_path}`, change the print statement to say 'Hi there!' instead") | ||
| semantic_success = any( | ||
| phrase in str(modify_response).lower() | ||
| for phrase in [ | ||
| "file has been updated", | ||
| "now prints 'hi there!'", | ||
| "updated successfully", | ||
| "replacement was successful", | ||
| "print statement to say 'hi there!'", | ||
| ] | ||
| ) | ||
| assert semantic_success, str(modify_response) | ||
|
|
||
| # 4. Verify modification | ||
| final_response = agent(f"Read `{file_path}` and show me the code") | ||
| final_content = str(final_response) | ||
| assert "Hi there!" in final_content | ||
|
|
||
|
|
||
| @patch.dict(os.environ, {"BYPASS_TOOL_CONSENT": "true"}) | ||
| def test_semantic_search_and_replace(agent, tmp_path): | ||
| """Test semantic search and replace operations.""" | ||
| file_path = tmp_path / "config.txt" | ||
|
|
||
| # 1. Create config file | ||
| agent(f"Create a config file at `{file_path}` with settings: debug=true, port=8080, host=localhost") | ||
|
|
||
| # 2. Change a specific setting | ||
| agent(f"In `{file_path}`, change the port from 8080 to 3000") | ||
|
|
||
| # 3. Verify the change | ||
| verify_response = agent(f"What is the port setting in `{file_path}`?") | ||
| assert "3000" in str(verify_response) | ||
|
|
||
| # 4. Final check | ||
| final_response = agent(f"Show me all settings in `{file_path}`") | ||
| final_content = str(final_response) | ||
| assert "3000" in final_content | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.