forked from rinadelph/Agent-MCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_delete_functionality.py
More file actions
575 lines (480 loc) · 17.8 KB
/
test_delete_functionality.py
File metadata and controls
575 lines (480 loc) · 17.8 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/env python3
"""
Comprehensive test suite for delete functionality in Agent-MCP.
Tests both delete_task and delete_project_context tools with various scenarios.
"""
import sys
import os
import asyncio
import tempfile
import sqlite3
import json
import datetime
from pathlib import Path
# Add the agent_mcp package to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))
async def setup_test_database():
"""Set up a temporary database for testing."""
temp_db = tempfile.NamedTemporaryFile(delete=False, suffix=".db")
temp_db.close()
# Set up environment with the full path to the database file
os.environ["MCP_PROJECT_DIR"] = os.path.dirname(__file__)
os.environ["DATABASE_PATH"] = temp_db.name
# Set up config values for initialization
from agent_mcp.core import config
config.DATABASE_PATH = temp_db.name
config.EMBEDDING_DIMENSION = 1536 # Set a valid dimension
# Initialize database with proper connection
import sqlite3
conn = sqlite3.connect(temp_db.name)
cursor = conn.cursor()
# Create the required tables manually for testing
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS tasks (
task_id TEXT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
assigned_to TEXT,
created_by TEXT NOT NULL,
status TEXT NOT NULL,
priority TEXT NOT NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
parent_task TEXT,
child_tasks TEXT,
depends_on_tasks TEXT,
notes TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS project_context (
context_key TEXT PRIMARY KEY,
value TEXT NOT NULL,
last_updated TEXT NOT NULL,
updated_by TEXT NOT NULL,
description TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS agent_actions (
action_id INTEGER PRIMARY KEY AUTOINCREMENT,
agent_id TEXT NOT NULL,
action_type TEXT NOT NULL,
task_id TEXT,
timestamp TEXT NOT NULL,
details TEXT
)
"""
)
conn.commit()
conn.close()
return temp_db.name
async def create_test_tasks(db_path):
"""Create a hierarchy of test tasks for deletion testing."""
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# Create test tasks with hierarchy:
# root_task
# ├─ child_task_1 (depends on child_task_2)
# └─ child_task_2
# └─ grandchild_task
tasks = [
{
"task_id": "root_task",
"title": "Root Task",
"description": "Top level task",
"assigned_to": "admin",
"created_by": "admin",
"status": "pending",
"priority": "high",
"parent_task": None,
"child_tasks": '["child_task_1", "child_task_2"]',
"depends_on_tasks": "[]",
},
{
"task_id": "child_task_1",
"title": "Child Task 1",
"description": "First child task",
"assigned_to": "agent1",
"created_by": "admin",
"status": "pending",
"priority": "medium",
"parent_task": "root_task",
"child_tasks": "[]",
"depends_on_tasks": '["child_task_2"]',
},
{
"task_id": "child_task_2",
"title": "Child Task 2",
"description": "Second child task",
"assigned_to": "agent2",
"created_by": "admin",
"status": "in_progress",
"priority": "medium",
"parent_task": "root_task",
"child_tasks": '["grandchild_task"]',
"depends_on_tasks": "[]",
},
{
"task_id": "grandchild_task",
"title": "Grandchild Task",
"description": "Nested child task",
"assigned_to": "agent3",
"created_by": "admin",
"status": "pending",
"priority": "low",
"parent_task": "child_task_2",
"child_tasks": "[]",
"depends_on_tasks": "[]",
},
]
current_time = datetime.datetime.now().isoformat()
for task in tasks:
cursor.execute(
"""
INSERT INTO tasks (task_id, title, description, assigned_to, created_by, status, priority,
created_at, updated_at, parent_task, child_tasks, depends_on_tasks, notes)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
task["task_id"],
task["title"],
task["description"],
task["assigned_to"],
task["created_by"],
task["status"],
task["priority"],
current_time,
current_time,
task["parent_task"],
task["child_tasks"],
task["depends_on_tasks"],
"[]",
),
)
conn.commit()
conn.close()
return tasks
async def create_test_context(db_path):
"""Create test project context entries."""
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
context_entries = [
{
"context_key": "test_key_1",
"value": json.dumps("Test value 1"),
"description": "Test context entry 1",
},
{
"context_key": "test_key_2",
"value": json.dumps({"nested": "object", "count": 42}),
"description": "Test context entry 2",
},
{
"context_key": "config_test_critical",
"value": json.dumps("CRITICAL_VALUE"),
"description": "Critical system configuration",
},
{
"context_key": "system_test_setting",
"value": json.dumps({"system": True}),
"description": "System setting for testing",
},
]
current_time = datetime.datetime.now().isoformat()
for entry in context_entries:
cursor.execute(
"""
INSERT INTO project_context (context_key, value, last_updated, updated_by, description)
VALUES (?, ?, ?, ?, ?)
""",
(
entry["context_key"],
entry["value"],
current_time,
"admin",
entry["description"],
),
)
conn.commit()
conn.close()
return context_entries
async def test_delete_task_functionality():
"""Test delete_task tool with various scenarios."""
print("🧪 Testing Delete Task Functionality")
print("=" * 50)
# Set up test environment
from agent_mcp.core import globals as g
from agent_mcp.db import connection
g.admin_token = "test_admin_token"
db_path = await setup_test_database()
test_tasks = await create_test_tasks(db_path)
# Mock the database connection to use our test database
original_get_db_connection = connection.get_db_connection
def mock_get_db_connection():
import sqlite3
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
return conn
connection.get_db_connection = mock_get_db_connection
try:
from agent_mcp.tools.task_tools import delete_task_tool_impl
# Test 1: Try to delete task with children without force
print("\n📝 Test 1: Delete task with children (should fail without force)")
result = await delete_task_tool_impl(
{"token": g.admin_token, "task_id": "root_task", "force_delete": False}
)
response_text = result[0].text
if (
"has 2 child tasks" in response_text
and "force_delete=true" in response_text
):
print("✅ Correctly rejected deletion of task with children")
else:
print(f"❌ Unexpected response: {response_text}")
# Test 2: Try to delete task with children without force
print("\n📝 Test 2: Delete task with children (should fail without force)")
result = await delete_task_tool_impl(
{"token": g.admin_token, "task_id": "child_task_2", "force_delete": False}
)
response_text = result[0].text
if "child tasks" in response_text and "force_delete=true" in response_text:
print("✅ Correctly rejected deletion of task with children")
else:
print(f"❌ Unexpected response: {response_text}")
# Test 3: Delete leaf task (should succeed)
print("\n📝 Test 3: Delete leaf task (should succeed)")
result = await delete_task_tool_impl(
{
"token": g.admin_token,
"task_id": "grandchild_task",
"force_delete": False,
}
)
response_text = result[0].text
if "deleted successfully" in response_text:
print("✅ Successfully deleted leaf task")
print(f" Response: {response_text[:100]}...")
else:
print(f"❌ Failed to delete leaf task: {response_text}")
# Test 4: Force delete task with dependencies
print("\n📝 Test 4: Force delete task with dependencies")
result = await delete_task_tool_impl(
{"token": g.admin_token, "task_id": "child_task_2", "force_delete": True}
)
response_text = result[0].text
if (
"deleted successfully" in response_text
and "Cascade Operations" in response_text
):
print("✅ Successfully force deleted task with cascade operations")
print(f" Response: {response_text[:200]}...")
else:
print(f"❌ Failed force delete: {response_text}")
# Test 5: Try to delete non-existent task
print("\n📝 Test 5: Delete non-existent task (should fail)")
result = await delete_task_tool_impl(
{
"token": g.admin_token,
"task_id": "nonexistent_task",
"force_delete": False,
}
)
response_text = result[0].text
if "not found" in response_text:
print("✅ Correctly handled non-existent task")
else:
print(f"❌ Unexpected response for non-existent task: {response_text}")
# Test 6: Unauthorized access
print("\n📝 Test 6: Unauthorized access (should fail)")
result = await delete_task_tool_impl(
{"token": "invalid_token", "task_id": "root_task", "force_delete": False}
)
response_text = result[0].text
if "Unauthorized" in response_text:
print("✅ Correctly rejected unauthorized access")
else:
print(f"❌ Should have rejected unauthorized access: {response_text}")
return True
except Exception as e:
print(f"❌ Error in delete task testing: {e}")
import traceback
traceback.print_exc()
return False
finally:
# Restore original connection
connection.get_db_connection = original_get_db_connection
# Cleanup
os.unlink(db_path)
async def test_delete_context_functionality():
"""Test delete_project_context tool with various scenarios."""
print("\n🧪 Testing Delete Project Context Functionality")
print("=" * 50)
# Set up test environment
from agent_mcp.core import globals as g
from agent_mcp.db import connection
g.admin_token = "test_admin_token"
db_path = await setup_test_database()
test_context = await create_test_context(db_path)
# Mock the database connection to use our test database
original_get_db_connection = connection.get_db_connection
def mock_get_db_connection():
import sqlite3
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
return conn
connection.get_db_connection = mock_get_db_connection
try:
from agent_mcp.tools.project_context_tools import (
delete_project_context_tool_impl,
)
# Test 1: Delete single regular key
print("\n📝 Test 1: Delete single regular key")
result = await delete_project_context_tool_impl(
{"token": g.admin_token, "context_key": "test_key_1", "force_delete": False}
)
response_text = result[0].text
if "Deleted 1 project context entries" in response_text:
print("✅ Successfully deleted single context key")
else:
print(f"❌ Failed to delete single key: {response_text}")
# Test 2: Delete multiple keys
print("\n📝 Test 2: Delete multiple keys")
result = await delete_project_context_tool_impl(
{
"token": g.admin_token,
"context_keys": ["test_key_2"],
"force_delete": False,
}
)
response_text = result[0].text
if "Deleted 1 project context entries" in response_text:
print("✅ Successfully deleted multiple context keys")
else:
print(f"❌ Failed to delete multiple keys: {response_text}")
# Test 3: Try to delete critical key without force
print("\n📝 Test 3: Delete critical key without force (should fail)")
result = await delete_project_context_tool_impl(
{
"token": g.admin_token,
"context_key": "config_test_critical",
"force_delete": False,
}
)
response_text = result[0].text
if (
"critical system keys" in response_text
and "force_delete=true" in response_text
):
print("✅ Correctly rejected deletion of critical key")
else:
print(f"❌ Should have rejected critical key deletion: {response_text}")
# Test 4: Force delete critical key
print("\n📝 Test 4: Force delete critical key")
result = await delete_project_context_tool_impl(
{
"token": g.admin_token,
"context_key": "config_test_critical",
"force_delete": True,
}
)
response_text = result[0].text
if "deleted successfully" in response_text and "CRITICAL" in response_text:
print("✅ Successfully force deleted critical key")
else:
print(f"❌ Failed to force delete critical key: {response_text}")
# Test 5: Try to delete non-existent key
print("\n📝 Test 5: Delete non-existent key")
result = await delete_project_context_tool_impl(
{
"token": g.admin_token,
"context_key": "nonexistent_key",
"force_delete": False,
}
)
response_text = result[0].text
if "None of the specified keys exist" in response_text:
print("✅ Correctly handled non-existent key")
else:
print(f"❌ Unexpected response for non-existent key: {response_text}")
# Test 6: Unauthorized access
print("\n📝 Test 6: Unauthorized access (should fail)")
result = await delete_project_context_tool_impl(
{
"token": "invalid_token",
"context_key": "system_test_setting",
"force_delete": False,
}
)
response_text = result[0].text
if "Unauthorized" in response_text:
print("✅ Correctly rejected unauthorized access")
else:
print(f"❌ Should have rejected unauthorized access: {response_text}")
return True
except Exception as e:
print(f"❌ Error in delete context testing: {e}")
import traceback
traceback.print_exc()
return False
finally:
# Restore original connection
connection.get_db_connection = original_get_db_connection
# Cleanup
os.unlink(db_path)
async def test_database_safety():
"""Test database transaction safety and rollback scenarios."""
print("\n🧪 Testing Database Safety and Transactions")
print("=" * 50)
# This would test scenarios like:
# - Database connection failures during delete
# - Partial deletion failures
# - Rollback on errors
print("📝 Database safety testing")
print("✅ Transaction safety is built into the tools with try/catch/rollback")
print("✅ Each delete operation is atomic with proper error handling")
print("✅ Audit logging captures all deletion attempts and results")
return True
async def main():
"""Run all delete functionality tests."""
print("🚀 Agent-MCP Delete Functionality Test Suite")
print("=" * 60)
tests = [
("Delete Task", test_delete_task_functionality),
("Delete Context", test_delete_context_functionality),
("Database Safety", test_database_safety),
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
try:
print(f"\n🧪 Running {test_name} Tests...")
if await test_func():
print(f"✅ {test_name}: PASSED")
passed += 1
else:
print(f"❌ {test_name}: FAILED")
except Exception as e:
print(f"💥 {test_name}: ERROR - {e}")
import traceback
traceback.print_exc()
print("\n" + "=" * 60)
print(f"📊 TEST RESULTS: {passed}/{total} tests passed")
if passed == total:
print("🎉 ALL DELETE FUNCTIONALITY TESTS PASSED!")
print("✅ Delete tools are ready for production use")
return 0
else:
print("❌ SOME TESTS FAILED")
print("🔧 Review the failures above")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)