Jaiwincr7 commited on
Commit
05f4426
·
1 Parent(s): 83fb250

FEAT: Added logging to test_case function to debug missing PDF

Browse files
Files changed (1) hide show
  1. main.py +44 -18
main.py CHANGED
@@ -51,30 +51,56 @@ test_prompt = ChatPromptTemplate.from_messages(
51
  )
52
 
53
  def test_case(code):
54
- # ... (Prompt setup) ...
55
-
56
- test_chain = test_prompt | llm | StrOutputParser()
57
- test_cases = test_chain.invoke({"code": code})
58
-
59
- print("\n[LOG] 1. LLM Raw Output Length:", len(test_cases)) # <-- NEW LOG
60
 
61
- # Aggressive cleaning...
62
- # ... (cleaning code) ...
 
 
 
63
 
 
64
  if not test_cases:
65
- test_cases = "Error: Test case generation failed or returned empty content."
66
-
67
- print("\n[LOG] 2. Cleaned Text:", test_cases) # <-- NEW LOG
68
-
69
  safe_text = test_cases
70
-
71
  pdf = FPDF()
72
- # ... (FPDF setup with DejaVuSans) ...
73
 
74
- pdf.multi_cell(0, 10, txt=safe_text)
75
 
76
- file = pdf.output(dest='S').encode('latin-1')
 
77
 
78
- print("\n[LOG] 3. PDF Bytes Length:", len(file)) # <-- NEW LOG
79
 
80
- return file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  )
52
 
53
  def test_case(code):
54
+ # ... (LLM generation and cleaning remains the same) ...
 
 
 
 
 
55
 
56
+ # -----------------------------------------------------------
57
+ # (Existing cleaning and check)
58
+ test_cases = re.sub(r"```.*?```", "", test_cases, flags=re.DOTALL)
59
+ test_cases = re.sub(r"```", "", test_cases)
60
+ test_cases = test_cases.strip()
61
 
62
+ # If the LLM returns nothing, force a known output
63
  if not test_cases:
64
+ test_cases = "Error: Test case generation failed or returned empty content. Check LLM output in logs."
65
+ # -----------------------------------------------------------
66
+
 
67
  safe_text = test_cases
 
68
  pdf = FPDF()
69
+ pdf.add_page()
70
 
71
+ # ... (FPDF Unicode font setup remains the same) ...
72
 
73
+ pdf.multi_cell(0, 10, txt="--- Generated Test Cases ---", align='C')
74
+ pdf.multi_cell(0, 10, txt=safe_text)
75
 
 
76
 
77
+ # -----------------------------------------------------------
78
+ # CRITICAL CHANGE: Get bytes object directly from FPDF2
79
+ try:
80
+ # dest='S' returns a string in FPDF2. We need to encode this string.
81
+ pdf_string = pdf.output(dest='S')
82
+ pdf_bytes = pdf_string.encode('latin-1')
83
+
84
+ # Check to ensure the file is not empty before returning
85
+ if len(pdf_bytes) < 100:
86
+ print("[CRITICAL LOG] FPDF generated very small file, likely failed.")
87
+ # Return a non-empty, guaranteed failure PDF to force the button to display
88
+ # This is a temporary diagnostic tool.
89
+
90
+ # Recreate PDF with ONLY the error message
91
+ error_pdf = FPDF()
92
+ error_pdf.add_page()
93
+ error_pdf.set_font("Arial", size=12)
94
+ error_pdf.multi_cell(0, 10, txt="ERROR: PDF generation failed to create content. Check logs.")
95
+ return error_pdf.output(dest='S').encode('latin-1')
96
+
97
+ return pdf_bytes
98
+
99
+ except Exception as e:
100
+ print(f"[FATAL LOG] PDF output failed with error: {e}")
101
+ # Return a guaranteed failure PDF to force button display
102
+ error_pdf = FPDF()
103
+ error_pdf.add_page()
104
+ error_pdf.set_font("Arial", size=12)
105
+ error_pdf.multi_cell(0, 10, txt=f"FATAL ERROR: PDF generation crashed. Reason: {e}")
106
+ return error_pdf.output(dest='S').encode('latin-1')