Spaces:
Running
Running
| #!/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) |