CyberLegalAIendpoint / tests /test_create_draft_document.py
Charles Grandjean
add the doc_editor to chat agent
6dd54b4
#!/usr/bin/env python3
"""
Test script for create_draft_document tool
"""
import asyncio
import os
import sys
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from utils.tools import create_draft_document, _create_draft_document
async def test_tool_facade():
"""Test the facade version of the tool"""
print("🧪 Testing create_draft_document (facade)...")
# This should return immediately (facade just returns)
result = await create_draft_document.ainvoke({
"path_with_filename": "Test/Test_Document.pdf",
"plan_to_build_document": "1. Create title\n2. Add content",
"instruction": "Create a test document"
})
print(f"✅ Facade test passed: {result}")
return result
async def test_tool_real():
"""Test the real implementation of the tool"""
print("\n🧪 Testing _create_draft_document (real implementation)...")
# Mock user_id
test_user_id = os.getenv("TEST_USER_ID", "test-user-uuid-123")
# Test with various paths and plans
test_cases = [
{
"path_with_filename": "Contracts/Contract_de_bail.pdf",
"plan_to_build_document": """1. Create header with title "Contrat de bail"
2. Add section for parties (bailleur and preneur)
3. Add main clauses for the lease
4. Add signature section""",
"instruction": "Create a commercial lease contract",
"expected_path": "Contracts/Contract_de_bail.pdf"
},
{
"path_with_filename": "Mon_document.pdf",
"plan_to_build_document": """1. Add document title
2. Add introductory paragraph
3. Add main content sections""",
"instruction": "Create a simple document",
"expected_path": "Mon_document.pdf"
},
{
"path_with_filename": "Drafts/Legal/Note_juridique.pdf",
"plan_to_build_document": """1. Create title "Note juridique"
2. Add legal context
3. Add analysis section
4. Add conclusions""",
"instruction": "Create a legal note",
"expected_path": "Drafts/Legal/Note_juridique.pdf"
}
]
for i, test_case in enumerate(test_cases, 1):
print(f"\n Test case {i}: {test_case['path_with_filename']}")
print(f" Instruction: {test_case['instruction']}")
print(f" Expected: {test_case['expected_path']}")
result = await _create_draft_document.ainvoke({
"user_id": test_user_id,
"path_with_filename": test_case["path_with_filename"],
"plan_to_build_document": test_case["plan_to_build_document"],
"instruction": test_case["instruction"]
})
print(f" Result: {result}")
# Check if the path is correct in the result
if test_case["expected_path"] in result:
print(f" ✅ Path is correct")
else:
print(f" ⚠️ Expected path not found in result")
print("\n✅ Real implementation test completed")
return True
async def test_path_parsing():
"""Test path parsing logic"""
print("\n🧪 Testing path parsing...")
test_cases = [
("Contracts/Test.pdf", "Contracts", "Test.pdf", "Test"),
("Test.pdf", "", "Test.pdf", "Test"),
("Drafts/Legal/Note.pdf", "Drafts/Legal", "Note.pdf", "Note"),
("./Contracts/Test.pdf", "./Contracts", "Test.pdf", "Test"),
]
for path_with_filename, expected_path, expected_filename, expected_title in test_cases:
# Simulate the path parsing logic
parts = path_with_filename.split('/')
if len(parts) > 1:
path = '/'.join(parts[:-1])
filename = parts[-1]
else:
path = ''
filename = parts[0]
# Remove .pdf extension
title = filename.replace('.pdf', '')
print(f" Input: '{path_with_filename}'")
print(f" → Path: '{path}'")
print(f" → Filename: '{filename}'")
print(f" → Title: '{title}'")
if path == expected_path and filename == expected_filename and title == expected_title:
print(f" ✅ Correct")
else:
print(f" ⚠️ Mismatch - Expected: path='{expected_path}', filename='{expected_filename}', title='{expected_title}'")
print("\n✅ Path parsing test completed")
return True
async def main():
"""Run all tests"""
print("=" * 80)
print("🚀 Testing create_draft_document tool")
print("=" * 80)
try:
# Test facade
await test_tool_facade()
# Test real implementation
await test_tool_real()
# Test path parsing
await test_path_parsing()
print("\n" + "=" * 80)
print("✅ All tests completed successfully!")
print("=" * 80)
except Exception as e:
print(f"\n❌ Test failed with error: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(main())