Spaces:
Running
Running
File size: 5,137 Bytes
8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 6dd54b4 8cc8e89 | 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 | #!/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()) |