gateway/tests/functional/test_repair_debug.py
2025-11-30 22:55:03 +01:00

58 lines
2.3 KiB
Python

"""Debug what repairBrokenJson returns"""
import json
import sys
import os
# Add gateway directory to path
_gateway_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if _gateway_path not in sys.path:
sys.path.insert(0, _gateway_path)
from modules.shared.jsonUtils import extractJsonString, repairBrokenJson
# Load actual incomplete JSON response
json_file = os.path.join(
os.path.dirname(__file__),
"..", "..", "..", "local", "debug", "prompts",
"20251130-211706-078-document_generation_response.txt"
)
with open(json_file, 'r', encoding='utf-8') as f:
content = f.read()
extracted = extractJsonString(content)
print(f"Extracted JSON length: {len(extracted)} chars")
print(f"Last 200 chars: {extracted[-200:]}")
repaired = repairBrokenJson(extracted)
if repaired:
print(f"\nRepaired JSON structure:")
print(f" Has 'documents': {'documents' in repaired}")
if 'documents' in repaired and isinstance(repaired['documents'], list) and len(repaired['documents']) > 0:
doc = repaired['documents'][0]
print(f" Has 'sections': {'sections' in doc}")
if 'sections' in doc and isinstance(doc['sections'], list) and len(doc['sections']) > 0:
section = doc['sections'][0]
print(f" Has 'elements': {'elements' in section}")
if 'elements' in section and isinstance(section['elements'], list) and len(section['elements']) > 0:
element = section['elements'][0]
print(f" Has 'rows': {'rows' in element}")
if 'rows' in element:
rows = element['rows']
print(f" Rows type: {type(rows)}")
if isinstance(rows, list):
print(f" Rows count: {len(rows)}")
if len(rows) > 0:
print(f" First row: {rows[0]}")
print(f" Last row: {rows[-1]}")
else:
print(f" Rows value: {rows}")
# Save to file for inspection
output_file = os.path.join(os.path.dirname(__file__), "repaired_debug.json")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(repaired, f, indent=2, ensure_ascii=False)
print(f"\nSaved repaired JSON to: {output_file}")
else:
print("Repair failed")