File size: 7,049 Bytes
2f4c4da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test the documents_tree integration with the API
"""

import sys
import os

# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from structured_outputs.api_models import (
    TreeNode, DocumentsTree, DocumentAnalysis, ChatRequest
)

def test_tree_node_creation():
    """Test creating a tree node"""
    print("🧪 Testing TreeNode creation...")
    
    file_analysis = DocumentAnalysis(
        summary="Test summary",
        actors="Actor 1, Actor 2",
        key_details="Key detail 1, Key detail 2"
    )
    
    file_node = TreeNode(
        name="test.pdf",
        type="file",
        file_path="user-id/test.pdf",
        file_size=100000,
        mime_type="application/pdf",
        created_at="2025-01-15T10:30:00Z",
        analysis=file_analysis
    )
    
    assert file_node.name == "test.pdf"
    assert file_node.type == "file"
    assert file_node.analysis.summary == "Test summary"
    print("✅ TreeNode creation works")

def test_documents_tree_creation():
    """Test creating a documents tree"""
    print("\n🧪 Testing DocumentsTree creation...")
    
    # Create file nodes with analyses
    file1_analysis = DocumentAnalysis(
        summary="Contract summary",
        actors="SCI Martin, SARL Dupont",
        key_details="Durée: 9 ans, Loyer: 3500€/mois"
    )
    
    file1 = TreeNode(
        name="bail-commercial.pdf",
        type="file",
        file_path="user-id/abc123-bail-commercial.pdf",
        file_size=245000,
        mime_type="application/pdf",
        created_at="2025-01-15T10:30:00Z",
        analysis=file1_analysis
    )
    
    file2_analysis = DocumentAnalysis(
        summary="Legal note summary",
        actors="Entreprise XYZ, CNIL",
        key_details="Base légale: intérêt légitime"
    )
    
    file2 = TreeNode(
        name="note-juridique.pdf",
        type="file",
        file_path="user-id/def456-note-juridique.pdf",
        file_size=120000,
        mime_type="application/pdf",
        created_at="2025-02-01T14:00:00Z",
        analysis=file2_analysis
    )
    
    # Create folder with files
    contracts_folder = TreeNode(
        name="Contracts",
        type="folder",
        children=[file1]
    )
    
    # Create root tree
    tree = DocumentsTree(
        name="root",
        type="folder",
        children=[contracts_folder, file2]
    )
    
    assert tree.name == "root"
    assert tree.type == "folder"
    assert len(tree.children) == 2
    assert tree.children[0].name == "Contracts"
    assert tree.children[0].type == "folder"
    assert tree.children[0].children[0].name == "bail-commercial.pdf"
    assert tree.children[1].name == "note-juridique.pdf"
    print("✅ DocumentsTree creation works")

def test_chat_request_with_tree():
    """Test creating a ChatRequest with documents_tree"""
    print("\n🧪 Testing ChatRequest with documents_tree...")
    
    # Create a simple tree
    file_analysis = DocumentAnalysis(
        summary="Test document",
        actors="Actor 1",
        key_details="Detail 1"
    )
    
    file_node = TreeNode(
        name="test.pdf",
        type="file",
        analysis=file_analysis
    )
    
    tree = DocumentsTree(
        children=[file_node]
    )
    
    # Create chat request
    request = ChatRequest(
        clientId="test-client",
        message="What are my documents?",
        userType="lawyer",
        jurisdiction="Romania",
        documents_tree=tree
    )
    
    assert request.clientId == "test-client"
    assert request.message == "What are my documents?"
    assert request.userType == "lawyer"
    assert request.documents_tree is not None
    assert request.documents_tree.children[0].name == "test.pdf"
    print("✅ ChatRequest with documents_tree works")

def test_format_documents_tree():
    """Test the _format_documents_tree method"""
    print("\n🧪 Testing _format_documents_tree...")
    
    # Import the API to access the method
    from agent_api import CyberLegalAPI
    
    # Create a tree
    file_analysis = DocumentAnalysis(
        summary="A very long summary that should be truncated at 100 characters and then show ellipsis",
        actors="Actor 1, Actor 2, Actor 3",
        key_details="Key detail"
    )
    
    file_node = TreeNode(
        name="test.pdf",
        type="file",
        analysis=file_analysis
    )
    
    tree = DocumentsTree(
        children=[file_node]
    )
    
    # Create API instance
    api = CyberLegalAPI()
    
    # Format the tree
    formatted = api._format_documents_tree(tree)
    
    print("📄 Formatted tree:")
    print(formatted)
    
    assert "test.pdf" in formatted
    assert "summary:" in formatted
    assert "actors:" in formatted
    assert "key_details:" in formatted
    print("✅ _format_documents_tree works")

def test_extract_flat_documents():
    """Test the _extract_flat_documents method"""
    print("\n🧪 Testing _extract_flat_documents...")
    
    # Import the API to access the method
    from agent_api import CyberLegalAPI
    
    # Create a tree with multiple files in folders
    file1_analysis = DocumentAnalysis(
        summary="File 1 summary",
        actors="Actor 1",
        key_details="Detail 1"
    )
    
    file1 = TreeNode(
        name="file1.pdf",
        type="file",
        analysis=file1_analysis
    )
    
    file2_analysis = DocumentAnalysis(
        summary="File 2 summary",
        actors="Actor 2",
        key_details="Detail 2"
    )
    
    file2 = TreeNode(
        name="file2.pdf",
        type="file",
        analysis=file2_analysis
    )
    
    folder = TreeNode(
        name="Folder",
        type="folder",
        children=[file1]
    )
    
    tree = DocumentsTree(
        children=[folder, file2]
    )
    
    # Create API instance
    api = CyberLegalAPI()
    
    # Extract flat documents
    flat_docs = api._extract_flat_documents(tree)
    
    print(f"📄 Extracted {len(flat_docs)} documents:")
    for doc in flat_docs:
        print(f"  - {doc['file_name']}: {doc['summary']}")
    
    assert len(flat_docs) == 2
    assert flat_docs[0]['file_name'] == 'file1.pdf'
    assert flat_docs[0]['summary'] == 'File 1 summary'
    assert flat_docs[1]['file_name'] == 'file2.pdf'
    assert flat_docs[1]['summary'] == 'File 2 summary'
    print("✅ _extract_flat_documents works")

if __name__ == "__main__":
    print("🚀 Running documents_tree integration tests\n")
    print("=" * 80)
    
    try:
        test_tree_node_creation()
        test_documents_tree_creation()
        test_chat_request_with_tree()
        test_format_documents_tree()
        test_extract_flat_documents()
        
        print("\n" + "=" * 80)
        print("✅ All tests passed!")
        print("=" * 80)
        
    except Exception as e:
        print("\n" + "=" * 80)
        print(f"❌ Test failed: {e}")
        print("=" * 80)
        import traceback
        traceback.print_exc()
        sys.exit(1)