-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_data_structure.py
More file actions
executable file
·340 lines (270 loc) · 10.9 KB
/
test_data_structure.py
File metadata and controls
executable file
·340 lines (270 loc) · 10.9 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python3
"""
Test script to validate data structure integrity.
This script checks that:
1. All conversation/project JSON files are valid
2. Required fields are present
3. UUIDs are unique
4. File naming conventions are followed
5. The sync scripts' validation functions work correctly
"""
import json
import sys
from pathlib import Path
from typing import Dict, List, Set, Tuple
from paths import LLM_DATA_SUBDIR
# Color codes for output
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
RESET = "\033[0m"
def print_success(msg: str) -> None:
"""Print success message in green."""
print(f"{GREEN}✓{RESET} {msg}")
def print_error(msg: str) -> None:
"""Print error message in red."""
print(f"{RED}✗{RESET} {msg}")
def print_warning(msg: str) -> None:
"""Print warning message in yellow."""
print(f"{YELLOW}⚠{RESET} {msg}")
def validate_json_file(filepath: Path) -> Tuple[bool, str]:
"""
Validate that a file contains valid JSON.
Returns (success, error_message).
"""
try:
with open(filepath, "r", encoding="utf-8") as f:
json.load(f)
return True, ""
except json.JSONDecodeError as e:
return False, f"Invalid JSON: {e}"
except Exception as e:
return False, f"Error reading file: {e}"
def validate_conversation_structure(data: dict, filepath: Path) -> List[str]:
"""
Validate conversation structure.
Returns list of error messages (empty if valid).
"""
errors = []
# Required top-level fields
required_fields = ["uuid", "name", "created_at", "updated_at"]
for field in required_fields:
if field not in data:
errors.append(f"Missing required field: {field}")
# Check account structure (for provider conversations)
if "account" in data:
if not isinstance(data["account"], dict):
errors.append("'account' should be a dictionary")
elif "uuid" not in data["account"]:
errors.append("'account' missing 'uuid' field")
# Check chat_messages structure
if "chat_messages" in data:
if not isinstance(data["chat_messages"], list):
errors.append("'chat_messages' should be a list")
else:
for i, msg in enumerate(data["chat_messages"]):
if not isinstance(msg, dict):
errors.append(f"Message {i} is not a dictionary")
continue
# Check message fields
if "uuid" not in msg:
errors.append(f"Message {i} missing 'uuid'")
if "sender" not in msg:
errors.append(f"Message {i} missing 'sender'")
elif msg["sender"] not in ["human", "assistant"]:
errors.append(f"Message {i} has invalid sender: {msg['sender']}")
return errors
def validate_project_structure(data: dict, filepath: Path) -> List[str]:
"""
Validate project structure.
Returns list of error messages (empty if valid).
"""
errors = []
# Required top-level fields
required_fields = ["uuid", "name", "created_at"]
for field in required_fields:
if field not in data:
errors.append(f"Missing required field: {field}")
# Check creator structure
if "creator" in data:
if not isinstance(data["creator"], dict):
errors.append("'creator' should be a dictionary")
elif "uuid" not in data["creator"]:
errors.append("'creator' missing 'uuid' field")
# Check docs structure
if "docs" in data:
if not isinstance(data["docs"], list):
errors.append("'docs' should be a list")
return errors
def check_uuid_uniqueness(data_dir: Path) -> Tuple[bool, List[str]]:
"""
Check that all conversation/project UUIDs are unique.
Returns (success, error_messages).
"""
uuid_to_files: Dict[str, List[Path]] = {}
errors = []
# Scan provider directories (claude/, chatgpt/, etc.)
for provider in ["claude", "chatgpt", "gemini"]:
provider_dir = data_dir / provider
if not provider_dir.exists():
continue
for user_dir in provider_dir.iterdir():
if not user_dir.is_dir():
continue
# Check conversations
conversations_dir = user_dir / "conversations"
if conversations_dir.exists():
for conv_file in conversations_dir.glob("*.json"):
try:
with open(conv_file, "r", encoding="utf-8") as f:
data = json.load(f)
uuid = data.get("uuid")
if uuid:
if uuid not in uuid_to_files:
uuid_to_files[uuid] = []
uuid_to_files[uuid].append(conv_file)
except Exception:
pass # Will be caught by other validation
# Check projects
projects_dir = user_dir / "projects"
if projects_dir.exists():
for proj_file in projects_dir.glob("*.json"):
try:
with open(proj_file, "r", encoding="utf-8") as f:
data = json.load(f)
uuid = data.get("uuid")
if uuid:
if uuid not in uuid_to_files:
uuid_to_files[uuid] = []
uuid_to_files[uuid].append(proj_file)
except Exception:
pass # Will be caught by other validation
# Find duplicates
for uuid, files in uuid_to_files.items():
if len(files) > 1:
file_list = ", ".join(str(f.relative_to(data_dir)) for f in files)
errors.append(f"UUID {uuid} found in multiple files: {file_list}")
return len(errors) == 0, errors
def test_format_validators() -> Tuple[bool, List[str]]:
"""
Test the sync scripts' validation functions with test data.
Returns (success, error_messages).
"""
errors = []
script_dir = Path(__file__).parent
test_data_dir = script_dir / "tests/test-data"
if not test_data_dir.exists():
return False, ["tests/test-data directory not found"]
# Test Claude validator
try:
from sync_local_chats_archive import validate_claude_export_format
with open(test_data_dir / "users.json") as f:
users = json.load(f)
with open(test_data_dir / "conversations.json") as f:
convs = json.load(f)
with open(test_data_dir / "projects.json") as f:
projs = json.load(f)
# This should not raise an exception
validate_claude_export_format(users, convs, projs)
print_success("Claude format validator passed with test data")
except SystemExit:
errors.append("Claude format validator failed with test data")
except Exception as e:
errors.append(f"Error testing Claude validator: {e}")
return len(errors) == 0, errors
def main():
"""Main entry point."""
script_dir = Path(__file__).parent
data_dir = script_dir / LLM_DATA_SUBDIR
print("\n=== Data Structure Validation ===\n")
if not data_dir.exists():
print_warning(f"Data directory not found: {data_dir}")
print("This is normal if you haven't synced any archives yet.")
print("\nTesting format validators only...\n")
success, errors = test_format_validators()
if not success:
for error in errors:
print_error(error)
sys.exit(1)
print(f"\n{GREEN}All validation tests passed!{RESET}")
sys.exit(0)
total_errors = 0
total_files = 0
# Validate all provider directories
for provider in ["claude", "chatgpt", "gemini"]:
provider_dir = data_dir / provider
if not provider_dir.exists():
continue
print(f"\nScanning {provider}/ directory...")
for user_dir in provider_dir.iterdir():
if not user_dir.is_dir():
continue
email = user_dir.name
print(f"\nValidating: {provider}/{email}")
# Check conversations
conversations_dir = user_dir / "conversations"
if conversations_dir.exists():
for conv_file in sorted(conversations_dir.glob("*.json")):
total_files += 1
# Check JSON validity
success, error = validate_json_file(conv_file)
if not success:
print_error(f"{conv_file.name}: {error}")
total_errors += 1
continue
# Check structure
with open(conv_file, "r", encoding="utf-8") as f:
data = json.load(f)
errors = validate_conversation_structure(data, conv_file)
if errors:
print_error(f"{conv_file.name}:")
for error in errors:
print(f" - {error}")
total_errors += len(errors)
# Check projects
projects_dir = user_dir / "projects"
if projects_dir.exists():
for proj_file in sorted(projects_dir.glob("*.json")):
total_files += 1
# Check JSON validity
success, error = validate_json_file(proj_file)
if not success:
print_error(f"{proj_file.name}: {error}")
total_errors += 1
continue
# Check structure
with open(proj_file, "r", encoding="utf-8") as f:
data = json.load(f)
errors = validate_project_structure(data, proj_file)
if errors:
print_error(f"{proj_file.name}:")
for error in errors:
print(f" - {error}")
total_errors += len(errors)
# Check UUID uniqueness
print("\nChecking UUID uniqueness...")
success, errors = check_uuid_uniqueness(data_dir)
if not success:
for error in errors:
print_error(error)
total_errors += len(errors)
else:
print_success("All UUIDs are unique")
# Test format validators
print("\nTesting format validators...")
success, errors = test_format_validators()
if not success:
for error in errors:
print_error(error)
total_errors += len(errors)
# Summary
print("\n=== Summary ===")
print(f"Files checked: {total_files}")
if total_errors == 0:
print(f"{GREEN}All validation tests passed!{RESET}")
sys.exit(0)
else:
print(f"{RED}Found {total_errors} error(s){RESET}")
sys.exit(1)
if __name__ == "__main__":
main()