CyberLegalAIendpoint / tests /test_tools_ainvoke.py
Charles Grandjean
solve tests
8cc8e89
#!/usr/bin/env python3
"""
Simple test to verify tools work with .ainvoke()
"""
import asyncio
import sys
import os
# Add parent directory to path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from utils.editor_tools import replace_html, add_html, delete_html, inspect_document, attempt_completion
async def test_tool_ainvoke():
"""Test that all tools work with .ainvoke() method."""
print("=" * 80)
print("Testing LangChain Tools with .ainvoke()")
print("=" * 80)
# Simple HTML document for testing
doc_html = """
<html>
<head><title>Test</title></head>
<body>
<h1>Hello World</h1>
<p>This is a test paragraph.</p>
</body>
</html>
"""
# Test 1: replace_html
print("\n1️⃣ Testing replace_html.ainvoke()...")
try:
result = await replace_html.ainvoke({
"doc_text": doc_html,
"search": "Hello World",
"replace": "Bonjour Monde",
"expected_matches": 1
})
if result['ok']:
print(f"βœ… replace_html works! Found {result['matches']} matches")
doc_html = result['doc_text']
else:
print(f"❌ replace_html failed: {result['error']}")
return False
except Exception as e:
print(f"❌ replace_html error: {e}")
return False
# Test 2: add_html
print("\n2️⃣ Testing add_html.ainvoke()...")
try:
result = await add_html.ainvoke({
"doc_text": doc_html,
"anchor_search": "<h1>Bonjour Monde</h1>",
"insert": "<p>Added paragraph</p>",
"position": "after",
"expected_matches": 1
})
if result['ok']:
print(f"βœ… add_html works! Found {result['matches']} matches")
doc_html = result['doc_text']
else:
print(f"❌ add_html failed: {result['error']}")
return False
except Exception as e:
print(f"❌ add_html error: {e}")
return False
# Test 3: delete_html
print("\n3️⃣ Testing delete_html.ainvoke()...")
try:
result = await delete_html.ainvoke({
"doc_text": doc_html,
"search": "<p>This is a test paragraph.</p>",
"expected_matches": 1
})
if result['ok']:
print(f"βœ… delete_html works! Removed {result['matches']} elements")
doc_html = result['doc_text']
else:
print(f"❌ delete_html failed: {result['error']}")
return False
except Exception as e:
print(f"❌ delete_html error: {e}")
return False
# Test 4: inspect_document
print("\n4️⃣ Testing inspect_document.ainvoke()...")
try:
result = await inspect_document.ainvoke({
"doc_text": doc_html
})
if result['ok']:
print(f"βœ… inspect_document works! Document size: {len(result['content'])} bytes")
else:
print(f"❌ inspect_document failed")
return False
except Exception as e:
print(f"❌ inspect_document error: {e}")
return False
# Test 5: attempt_completion
print("\n5️⃣ Testing attempt_completion.ainvoke()...")
try:
result = await attempt_completion.ainvoke({
"message": "Successfully tested all tools"
})
if result['ok']:
print(f"βœ… attempt_completion works! Message: {result['message']}")
else:
print(f"❌ attempt_completion failed")
return False
except Exception as e:
print(f"❌ attempt_completion error: {e}")
return False
print("\n" + "=" * 80)
print("βœ… ALL TESTS PASSED!")
print("=" * 80)
return True
if __name__ == "__main__":
success = asyncio.run(test_tool_ainvoke())
sys.exit(0 if success else 1)