-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_agents.py
More file actions
281 lines (239 loc) · 9.59 KB
/
test_agents.py
File metadata and controls
281 lines (239 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Test Script for Instagram Automation Tool Agents
-----------------------------------------------
This script tests the functionality of each agent and verifies
that they can communicate with each other properly.
"""
import logging
import time
from datetime import datetime, timedelta
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Import the Instagram Automation Tool
from instagram_automation_tool import InstagramAutomationTool
def test_account_manager_agent():
"""Test the AccountManagerAgent functionality."""
logger.info("Testing AccountManagerAgent...")
# Initialize the tool
tool = InstagramAutomationTool()
# Test account creation
account = tool.create_account(
username="test_user",
password="TestPassword123!",
email="test@example.com"
)
logger.info(f"Account created with ID: {account['id']}")
assert account['username'] == "test_user", "Account username doesn't match"
# Test profile setup
profile_result = tool.setup_profile(
account_id=account["id"],
bio="Test bio",
profile_pic="test_pic.jpg",
external_link="https://example.com"
)
logger.info("Profile setup completed")
assert profile_result['status'] == "success", "Profile setup failed"
# Test account health check
health_result = tool.agency.get_agent("AccountManagerAgent").check_account_health(
account_id=account["id"]
)
logger.info(f"Account health status: {health_result['health']['status']}")
assert 'health' in health_result, "Health check failed"
logger.info("AccountManagerAgent tests passed")
return account
def test_messaging_agent(account_id):
"""Test the MessagingAgent functionality."""
logger.info("Testing MessagingAgent...")
# Initialize the tool
tool = InstagramAutomationTool()
# Test campaign creation
campaign = tool.create_messaging_campaign(
account_ids=[account_id],
target_source="follower_list",
target_details={
"account_to_scrape": "test_account",
"name": "Test Campaign"
},
message_templates=[
{
"id": "template_1",
"content": "Hi there! This is a test message.",
"sequence_position": 1
}
],
use_cupidbot=False
)
logger.info(f"Campaign created with ID: {campaign['id']}")
assert campaign['account_ids'] == [account_id], "Campaign account IDs don't match"
# Test campaign start
start_result = tool.agency.get_agent("MessagingAgent").start_campaign(
campaign_id=campaign["id"]
)
logger.info(f"Campaign started: {start_result['status']}")
assert start_result['status'] == "active", "Campaign not active"
# Test message sending
message_result = tool.agency.get_agent("MessagingAgent").send_message(
account_id=account_id,
target_username="test_target",
message_template={"content": "Hi there! This is a test message."},
use_cupidbot=False
)
logger.info(f"Message sent: {message_result['message_sent']}")
assert message_result['message_sent'] == True, "Message not sent"
logger.info("MessagingAgent tests passed")
return campaign
def test_content_poster_agent(account_id):
"""Test the ContentPosterAgent functionality."""
logger.info("Testing ContentPosterAgent...")
# Initialize the tool
tool = InstagramAutomationTool()
# Test post creation
post = tool.create_post(
account_id=account_id,
media_paths=["test_image.jpg"],
caption="Test caption",
hashtags=["test", "example"],
scheduled_time=None # Post immediately
)
logger.info(f"Post created with ID: {post['id']}")
assert post['account_id'] == account_id, "Post account ID doesn't match"
# Test post performance tracking
performance = tool.agency.get_agent("ContentPosterAgent").track_post_performance(
post_id=post["id"]
)
logger.info(f"Post performance tracked: {performance['post_id']}")
assert performance['post_id'] == post["id"], "Post ID doesn't match"
# Test batch post creation
batch_result = tool.agency.get_agent("ContentPosterAgent").create_batch_posts(
account_id=account_id,
posts_data=[
{
"media_paths": ["test_image1.jpg"],
"caption": "Test caption 1",
"hashtags": ["test1"]
},
{
"media_paths": ["test_image2.jpg"],
"caption": "Test caption 2",
"hashtags": ["test2"]
}
]
)
logger.info(f"Batch posts created: {batch_result['posts_created']}")
assert batch_result['posts_created'] == 2, "Incorrect number of posts created"
logger.info("ContentPosterAgent tests passed")
return post
def test_browser_manager_agent():
"""Test the BrowserManagerAgent functionality."""
logger.info("Testing BrowserManagerAgent...")
# Initialize the tool
tool = InstagramAutomationTool()
browser_manager = tool.agency.get_agent("BrowserManagerAgent")
# Test browser profile creation
profile = browser_manager.create_browser_profile(
profile_name="test_profile"
)
logger.info(f"Browser profile created: {profile['profile_id']}")
assert profile['status'] == "created", "Browser profile not created"
# Test browser opening
browser_session = browser_manager.open_browser(
profile_id=profile["profile_id"]
)
logger.info(f"Browser opened: {browser_session['status']}")
assert browser_session['status'] == "running", "Browser not running"
# Test navigation
navigation_result = browser_manager.navigate(
profile_id=profile["profile_id"],
url="https://www.instagram.com"
)
logger.info(f"Navigation result: {navigation_result['status']}")
assert navigation_result['status'] == "success", "Navigation failed"
# Test action performance
action_result = browser_manager.perform_action(
profile_id=profile["profile_id"],
action_type="click",
action_params={"selector": ".login-button"}
)
logger.info(f"Action performed: {action_result['status']}")
assert action_result['status'] == "success", "Action failed"
# Test browser closing
close_result = browser_manager.close_browser(
profile_id=profile["profile_id"]
)
logger.info(f"Browser closed: {close_result['status']}")
assert close_result['status'] == "stopped", "Browser not stopped"
logger.info("BrowserManagerAgent tests passed")
return profile
def test_agent_communication():
"""Test communication between agents."""
logger.info("Testing agent communication...")
# Initialize the tool
tool = InstagramAutomationTool()
# Test AccountManagerAgent -> BrowserManagerAgent communication
logger.info("Testing AccountManagerAgent -> BrowserManagerAgent communication...")
account = tool.create_account(
username="comm_test_user",
password="TestPassword123!",
email="comm_test@example.com"
)
logger.info(f"Account created with ID: {account['id']}")
# Test MessagingAgent -> BrowserManagerAgent communication
logger.info("Testing MessagingAgent -> BrowserManagerAgent communication...")
campaign = tool.create_messaging_campaign(
account_ids=[account['id']],
target_source="follower_list",
target_details={
"account_to_scrape": "comm_test_account",
"name": "Communication Test Campaign"
},
message_templates=[
{
"id": "template_1",
"content": "Hi there! This is a communication test message.",
"sequence_position": 1
}
],
use_cupidbot=True
)
# Test CupidBot activation (MessagingAgent -> BrowserManagerAgent)
message_result = tool.agency.get_agent("MessagingAgent").send_message(
account_id=account['id'],
target_username="comm_test_target",
message_template={"content": "Hi there! This is a communication test message."},
use_cupidbot=True
)
logger.info(f"Message sent with CupidBot: {message_result['cupidbot_activated']}")
assert message_result['cupidbot_activated'] == True, "CupidBot not activated"
# Test ContentPosterAgent -> BrowserManagerAgent communication
logger.info("Testing ContentPosterAgent -> BrowserManagerAgent communication...")
post = tool.create_post(
account_id=account['id'],
media_paths=["comm_test_image.jpg"],
caption="Communication test caption",
hashtags=["comm_test"],
scheduled_time=None # Post immediately
)
logger.info(f"Post created with ID: {post['id']}")
logger.info("Agent communication tests passed")
def main():
"""Run all agent tests."""
logger.info("Starting agent tests...")
try:
# Test individual agents
account = test_account_manager_agent()
campaign = test_messaging_agent(account['id'])
post = test_content_poster_agent(account['id'])
profile = test_browser_manager_agent()
# Test agent communication
test_agent_communication()
logger.info("All agent tests passed!")
except AssertionError as e:
logger.error(f"Test failed: {e}")
except Exception as e:
logger.error(f"Unexpected error: {e}")
if __name__ == "__main__":
main()