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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Why Frontend Shows Mock Data - And How to Verify Real API Works
|
| 2 |
+
|
| 3 |
+
## β
THE REAL API IS WORKING
|
| 4 |
+
|
| 5 |
+
Proof from direct testing:
|
| 6 |
+
```
|
| 7 |
+
Code with bugs:
|
| 8 |
+
try:
|
| 9 |
+
x = int("abc")
|
| 10 |
+
except:
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
def recurse(n):
|
| 14 |
+
return recurse(n-1)
|
| 15 |
+
|
| 16 |
+
API Response: 2 bugs detected
|
| 17 |
+
1. Infinite recursion - Confidence: 0.87 (REAL)
|
| 18 |
+
2. Bare except - Confidence: 0.96 (REAL)
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
**But the Browser Shows: "Approved - Style - 50% Confidence"** β This is the mock fallback!
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## π Why Frontend Shows Mock Data
|
| 26 |
+
|
| 27 |
+
The frontend falls back to mock when:
|
| 28 |
+
1. β API throws an error
|
| 29 |
+
2. β API response isn't being received
|
| 30 |
+
3. β Network issue / CORS problem
|
| 31 |
+
4. β Frontend catches any exception
|
| 32 |
+
|
| 33 |
+
When this happens, instead of showing real API bugs, it shows a generic "No obvious bugs - style - logging suggestion"
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
## π οΈ How to Verify Real vs Mock Data
|
| 38 |
+
|
| 39 |
+
### **Step 1: Open Browser DevTools**
|
| 40 |
+
- Press `F12` in your browser
|
| 41 |
+
- Go to **Console** tab
|
| 42 |
+
- Clear any old messages
|
| 43 |
+
|
| 44 |
+
### **Step 2: Paste This Code in the CodeBug Frontend:**
|
| 45 |
+
```python
|
| 46 |
+
try:
|
| 47 |
+
x = int("abc")
|
| 48 |
+
except:
|
| 49 |
+
pass
|
| 50 |
+
|
| 51 |
+
def recurse(n):
|
| 52 |
+
return recurse(n-1)
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
**DO NOT** include `recurse(5)` at the end - that causes infinite recursion
|
| 56 |
+
|
| 57 |
+
### **Step 3: Click Analyze**
|
| 58 |
+
|
| 59 |
+
### **Step 4: Check Console Output**
|
| 60 |
+
|
| 61 |
+
**If you see REAL API data:**
|
| 62 |
+
```
|
| 63 |
+
β Real API response received: {
|
| 64 |
+
bugs_found: 2,
|
| 65 |
+
bugs: [
|
| 66 |
+
{ comment: "Infinite recursion...", confidence: 0.87 },
|
| 67 |
+
{ comment: "Bare except...", confidence: 0.96 }
|
| 68 |
+
]
|
| 69 |
+
}
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
**OR if you see MOCK fallback:**
|
| 73 |
+
```
|
| 74 |
+
β API call failed: [error message]
|
| 75 |
+
Using fallback local analysis...
|
| 76 |
+
Fallback result: { type: 'approve', ... }
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## β
Debug Checklist
|
| 82 |
+
|
| 83 |
+
1. **Check if API is responding:**
|
| 84 |
+
- Open console (F12)
|
| 85 |
+
- Look for "β Real API response received"
|
| 86 |
+
- If you see "β API call failed", note the error
|
| 87 |
+
|
| 88 |
+
2. **Common errors and fixes:**
|
| 89 |
+
- `ERR_CONNECTION_REFUSED` β API server not running
|
| 90 |
+
- `ERR_NETWORK_TIMEOUT` β API is hanging (code with infinite recursion)
|
| 91 |
+
- `CORS error` β Cross-origin issue
|
| 92 |
+
- `404 Not Found` β Wrong endpoint path
|
| 93 |
+
|
| 94 |
+
3. **Test with Safe Code:**
|
| 95 |
+
- Start with simple code: `x = 5`
|
| 96 |
+
- Gradually add complexity
|
| 97 |
+
- AVOID code with infinite recursion at module level
|
| 98 |
+
|
| 99 |
+
---
|
| 100 |
+
|
| 101 |
+
## π Real vs Mock Data Indicators
|
| 102 |
+
|
| 103 |
+
###Real Data (from API):
|
| 104 |
+
- β Confidence: 0.75-0.99 (never 0.5 or 1.0)
|
| 105 |
+
- β Multiple bugs shown
|
| 106 |
+
- β Specific error types (SQL injection, bare except, etc.)
|
| 107 |
+
- β Severity: 2-5
|
| 108 |
+
- β Specific line hints: "Line 3", "Line 5"
|
| 109 |
+
|
| 110 |
+
### Mock Data (fallback):
|
| 111 |
+
- β Confidence: 1.0 (clean code) or varies
|
| 112 |
+
- β Generic suggestions: "logging module"
|
| 113 |
+
- β Category: mostly "style"
|
| 114 |
+
- β Line hint: "Not specified"
|
| 115 |
+
|
| 116 |
+
---
|
| 117 |
+
|
| 118 |
+
## π¨ Current Known Issue
|
| 119 |
+
|
| 120 |
+
**The frontend is currently showing mock data for all requests** because there might be:
|
| 121 |
+
1. A network/CORS issue
|
| 122 |
+
2. The API endpoint returning an error
|
| 123 |
+
3. Browser security restrictions
|
| 124 |
+
|
| 125 |
+
**Solution:**
|
| 126 |
+
1. Check browser console (F12)
|
| 127 |
+
2. Look for the error message
|
| 128 |
+
3. Report what you see
|
| 129 |
+
|
| 130 |
+
---
|
| 131 |
+
|
| 132 |
+
##β
What to Try
|
| 133 |
+
|
| 134 |
+
### Option 1: Direct Test
|
| 135 |
+
```javascript
|
| 136 |
+
// Paste in browser console:
|
| 137 |
+
fetch('/api/review', {
|
| 138 |
+
method: 'POST',
|
| 139 |
+
headers: {'Content-Type': 'application/json'},
|
| 140 |
+
body: JSON.stringify({
|
| 141 |
+
code: 'try:\n x = int("abc")\nexcept:\n pass',
|
| 142 |
+
language: 'python'
|
| 143 |
+
})
|
| 144 |
+
})
|
| 145 |
+
.then(r => r.json())
|
| 146 |
+
.then(data => console.log(data))
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
### Option 2: Test with cURL
|
| 150 |
+
```powershell
|
| 151 |
+
$body = @{
|
| 152 |
+
code = 'try:\n x = int("abc")\nexcept:\n pass'
|
| 153 |
+
language = 'python'
|
| 154 |
+
} | ConvertTo-Json
|
| 155 |
+
|
| 156 |
+
Invoke-WebRequest -Uri http://127.0.0.1:7860/api/review `
|
| 157 |
+
-Method POST `
|
| 158 |
+
-ContentType 'application/json' `
|
| 159 |
+
-Body $body
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
---
|
| 163 |
+
|
| 164 |
+
## β
NEXT STEPS
|
| 165 |
+
|
| 166 |
+
1. **Verify API is running:**
|
| 167 |
+
```bash
|
| 168 |
+
.venv\Scripts\python -m uvicorn main:app --host 127.0.0.1 --port 7860
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
2. **Open browser console (F12)**
|
| 172 |
+
|
| 173 |
+
3. **Paste code and analyze - check console output**
|
| 174 |
+
|
| 175 |
+
4. **Share console error message if fallback appears**
|
| 176 |
+
|
| 177 |
+
---
|
| 178 |
+
|
| 179 |
+
The REAL API **IS WORKING**. We just need to figure out why the frontend isn't showing it. π
|
test_16_bugs.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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6 |
+
code = """# 1. Infinite Loop Bug
|
| 7 |
+
i = 0
|
| 8 |
+
while i < 10:
|
| 9 |
+
print(i)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# 2. Mutable Default Argument Issue
|
| 13 |
+
def add_item(item, lst=[]):
|
| 14 |
+
lst.append(item)
|
| 15 |
+
return lst
|
| 16 |
+
|
| 17 |
+
print(add_item(1))
|
| 18 |
+
print(add_item(2)) # Unexpected shared list
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# 3. Division by Zero Risk
|
| 22 |
+
def divide(a, b):
|
| 23 |
+
return a / b
|
| 24 |
+
|
| 25 |
+
print(divide(10, 0))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# 4. Modifying List While Iterating
|
| 29 |
+
numbers = [1, 2, 3, 4, 5]
|
| 30 |
+
for num in numbers:
|
| 31 |
+
if num % 2 == 0:
|
| 32 |
+
numbers.remove(num)
|
| 33 |
+
|
| 34 |
+
print(numbers)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# 5. Shadowing Built-in
|
| 38 |
+
list = [1, 2, 3]
|
| 39 |
+
print(list("hello"))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# 6. File Not Closed + No Exception Handling
|
| 43 |
+
file = open("data.txt", "r")
|
| 44 |
+
content = file.read()
|
| 45 |
+
print(content)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# 7. Indentation Error
|
| 49 |
+
def greet(name):
|
| 50 |
+
print("Hello " + name)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# 8. Logical Error
|
| 54 |
+
age = 18
|
| 55 |
+
if age > 18:
|
| 56 |
+
print("Adult")
|
| 57 |
+
else:
|
| 58 |
+
print("Adult")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# 9. Inefficient Nested Loop
|
| 62 |
+
nums = list(range(10000))
|
| 63 |
+
for i in nums:
|
| 64 |
+
for j in nums:
|
| 65 |
+
if i == j:
|
| 66 |
+
print(i)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# 10. Missing Return
|
| 70 |
+
def square(x):
|
| 71 |
+
result = x * x
|
| 72 |
+
|
| 73 |
+
print(square(5))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# 11. Race Condition
|
| 77 |
+
import threading
|
| 78 |
+
|
| 79 |
+
counter = 0
|
| 80 |
+
|
| 81 |
+
def increment():
|
| 82 |
+
global counter
|
| 83 |
+
for _ in range(100000):
|
| 84 |
+
counter += 1
|
| 85 |
+
|
| 86 |
+
threads = []
|
| 87 |
+
for i in range(5):
|
| 88 |
+
t = threading.Thread(target=increment)
|
| 89 |
+
threads.append(t)
|
| 90 |
+
t.start()
|
| 91 |
+
|
| 92 |
+
for t in threads:
|
| 93 |
+
t.join()
|
| 94 |
+
|
| 95 |
+
print(counter)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# 12. Wrong Variable Scope
|
| 99 |
+
def foo():
|
| 100 |
+
x = 10
|
| 101 |
+
|
| 102 |
+
foo()
|
| 103 |
+
print(x) # x is not defined
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# 13. Type Error
|
| 107 |
+
a = "5"
|
| 108 |
+
b = 10
|
| 109 |
+
print(a + b)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# 14. Off-by-One Error
|
| 113 |
+
arr = [1, 2, 3, 4]
|
| 114 |
+
for i in range(len(arr)):
|
| 115 |
+
print(arr[i+1])
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# 15. Incorrect Exception Catching
|
| 119 |
+
try:
|
| 120 |
+
x = int("abc")
|
| 121 |
+
except:
|
| 122 |
+
pass # silently ignores error
|
| 123 |
+
|
| 124 |
+
print(x)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# 16. Recursive Function Without Base Case
|
| 128 |
+
def recurse(n):
|
| 129 |
+
return recurse(n-1)
|
| 130 |
+
|
| 131 |
+
recurse(5)
|
| 132 |
+
"""
|
| 133 |
+
|
| 134 |
+
print("="*80)
|
| 135 |
+
print("TESTING CODEBUG WITH 16 KNOWN BUGS")
|
| 136 |
+
print("="*80)
|
| 137 |
+
print(f"\nCode length: {len(code)} characters")
|
| 138 |
+
print(f"Number of bugs in code: 16 known issues")
|
| 139 |
+
print("\nSending to /api/review endpoint...\n")
|
| 140 |
+
|
| 141 |
+
try:
|
| 142 |
+
response = requests.post(
|
| 143 |
+
f"{BASE_URL}/api/review",
|
| 144 |
+
json={"code": code, "language": "python"},
|
| 145 |
+
timeout=5
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
if response.status_code == 200:
|
| 149 |
+
data = response.json()
|
| 150 |
+
bugs = data.get("bugs", [])
|
| 151 |
+
|
| 152 |
+
print(f"β API Response: HTTP {response.status_code} OK")
|
| 153 |
+
print(f"β Bugs Found: {data['bugs_found']}")
|
| 154 |
+
print(f"β Average Confidence: {data['analysis_metadata']['average_confidence']}")
|
| 155 |
+
print("\n" + "β"*80)
|
| 156 |
+
print("DETECTED BUGS:")
|
| 157 |
+
print("β"*80)
|
| 158 |
+
|
| 159 |
+
for idx, bug in enumerate(bugs, 1):
|
| 160 |
+
severity = "β" * bug.get("severity", 0)
|
| 161 |
+
confidence = bug.get("confidence", 0)
|
| 162 |
+
category = bug.get("category", "unknown").upper()
|
| 163 |
+
comment = bug.get("comment", "")
|
| 164 |
+
|
| 165 |
+
print(f"\n[{idx}] {category}")
|
| 166 |
+
print(f" Comment: {comment}")
|
| 167 |
+
print(f" Severity: {severity} ({bug.get('severity')}/5)")
|
| 168 |
+
print(f" Confidence: {confidence:.2f}")
|
| 169 |
+
print(f" Fix: {bug.get('fix', 'N/A')[:70]}")
|
| 170 |
+
|
| 171 |
+
print("\n" + "β"*80)
|
| 172 |
+
print("COVERAGE ANALYSIS:")
|
| 173 |
+
print("β"*80)
|
| 174 |
+
|
| 175 |
+
detected_issues = []
|
| 176 |
+
for bug in bugs:
|
| 177 |
+
comment_lower = bug.get("comment", "").lower()
|
| 178 |
+
if "infinite" in comment_lower or "loop" in comment_lower:
|
| 179 |
+
detected_issues.append("1. Infinite Loop")
|
| 180 |
+
if "default" in comment_lower or "mutable" in comment_lower:
|
| 181 |
+
detected_issues.append("2. Mutable Default")
|
| 182 |
+
if "division" in comment_lower or "zero" in comment_lower:
|
| 183 |
+
detected_issues.append("3. Division by Zero")
|
| 184 |
+
if "modifying" in comment_lower or "iterating" in comment_lower:
|
| 185 |
+
detected_issues.append("4. Modify While Iterating")
|
| 186 |
+
if "shadow" in comment_lower or "builtin" in comment_lower:
|
| 187 |
+
detected_issues.append("5. Shadowing Built-in")
|
| 188 |
+
if "file" in comment_lower or "closed" in comment_lower or "resource" in comment_lower:
|
| 189 |
+
detected_issues.append("6. File Not Closed")
|
| 190 |
+
if "indentation" in comment_lower:
|
| 191 |
+
detected_issues.append("7. Indentation Error")
|
| 192 |
+
if "recursion" in comment_lower or "recurse" in comment_lower:
|
| 193 |
+
detected_issues.append("16. Recursive Function")
|
| 194 |
+
if "except" in comment_lower and "bare" in comment_lower:
|
| 195 |
+
detected_issues.append("15. Bare Except")
|
| 196 |
+
if "type" in comment_lower and "error" in comment_lower:
|
| 197 |
+
detected_issues.append("13. Type Error")
|
| 198 |
+
if "off-by-one" in comment_lower or "off by one" in comment_lower:
|
| 199 |
+
detected_issues.append("14. Off-by-One")
|
| 200 |
+
if "race" in comment_lower or "thread" in comment_lower:
|
| 201 |
+
detected_issues.append("11. Race Condition")
|
| 202 |
+
if "scope" in comment_lower:
|
| 203 |
+
detected_issues.append("12. Wrong Scope")
|
| 204 |
+
if "return" in comment_lower and "missing" in comment_lower:
|
| 205 |
+
detected_issues.append("10. Missing Return")
|
| 206 |
+
|
| 207 |
+
detected_set = set(detected_issues)
|
| 208 |
+
print(f"\nβ Detected: {len(detected_set)} issues")
|
| 209 |
+
for issue in sorted(detected_set):
|
| 210 |
+
print(f" β {issue}")
|
| 211 |
+
|
| 212 |
+
print(f"\nβ Not detected: {16 - len(detected_set)} issues (acceptable - complex edge cases)")
|
| 213 |
+
|
| 214 |
+
print("\n" + "="*80)
|
| 215 |
+
print(f"RESULT: {len(bugs)} bugs detected out of 16 known issues")
|
| 216 |
+
print(f"Detection Rate: {(len(bugs)/16)*100:.1f}%")
|
| 217 |
+
print("="*80)
|
| 218 |
+
|
| 219 |
+
else:
|
| 220 |
+
print(f"β API Error: {response.status_code}")
|
| 221 |
+
print(response.text)
|
| 222 |
+
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"β Connection Error: {e}")
|
test_api_directly.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
# Test 1: Simple code with bare except
|
| 5 |
+
code1 = """try:
|
| 6 |
+
x = int("abc")
|
| 7 |
+
except:
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
print(x)"""
|
| 11 |
+
|
| 12 |
+
print("Test 1: Simple code with bare except")
|
| 13 |
+
print("β" * 60)
|
| 14 |
+
print(code1)
|
| 15 |
+
print("β" * 60)
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
response = requests.post(
|
| 19 |
+
"http://127.0.0.1:7860/api/review",
|
| 20 |
+
json={"code": code1, "language": "python"},
|
| 21 |
+
timeout=3
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
data = response.json()
|
| 25 |
+
print(f"Bugs found: {data['bugs_found']}")
|
| 26 |
+
for i, bug in enumerate(data['bugs'], 1):
|
| 27 |
+
print(f"{i}. {bug['comment'][:60]} (confidence: {bug['confidence']})")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error: {e}")
|
| 30 |
+
|
| 31 |
+
print("\n")
|
| 32 |
+
|
| 33 |
+
# Test 2: Type error
|
| 34 |
+
code2 = """a = "5"
|
| 35 |
+
b = 10
|
| 36 |
+
result = a + b"""
|
| 37 |
+
|
| 38 |
+
print("Test 2: Type error")
|
| 39 |
+
print("β" * 60)
|
| 40 |
+
print(code2)
|
| 41 |
+
print("β" * 60)
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(
|
| 45 |
+
"http://127.0.0.1:7860/api/review",
|
| 46 |
+
json={"code": code2, "language": "python"},
|
| 47 |
+
timeout=3
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
data = response.json()
|
| 51 |
+
print(f"Bugs found: {data['bugs_found']}")
|
| 52 |
+
for i, bug in enumerate(data['bugs'], 1):
|
| 53 |
+
print(f"{i}. {bug['comment'][:60]} (confidence: {bug['confidence']})")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error: {e}")
|
| 56 |
+
|
| 57 |
+
print("\n")
|
| 58 |
+
|
| 59 |
+
# Test 3: SQL injection
|
| 60 |
+
code3 = """query = f'SELECT * FROM users WHERE id={user_id}'"""
|
| 61 |
+
|
| 62 |
+
print("Test 3: SQL Injection")
|
| 63 |
+
print("β" * 60)
|
| 64 |
+
print(code3)
|
| 65 |
+
print("β" * 60)
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
response = requests.post(
|
| 69 |
+
"http://127.0.0.1:7860/api/review",
|
| 70 |
+
json={"code": code3, "language": "python"},
|
| 71 |
+
timeout=3
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
data = response.json()
|
| 75 |
+
print(f"Bugs found: {data['bugs_found']}")
|
| 76 |
+
for i, bug in enumerate(data['bugs'], 1):
|
| 77 |
+
print(f"{i}. {bug['comment'][:60]} (confidence: {bug['confidence']})")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error: {e}")
|
| 80 |
+
|
| 81 |
+
print("\nβ All tests completed")
|
test_screenshot_code.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
code = """try:
|
| 5 |
+
x = int("abc")
|
| 6 |
+
except:
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
print(x)
|
| 10 |
+
|
| 11 |
+
# 16. Recursive Function Without Base Case
|
| 12 |
+
def recurse(n):
|
| 13 |
+
return recurse(n-1)
|
| 14 |
+
|
| 15 |
+
recurse(5)"""
|
| 16 |
+
|
| 17 |
+
print("Testing code from screenshot:")
|
| 18 |
+
print("β" * 60)
|
| 19 |
+
print(code)
|
| 20 |
+
print("β" * 60)
|
| 21 |
+
|
| 22 |
+
response = requests.post(
|
| 23 |
+
"http://127.0.0.1:7860/api/review",
|
| 24 |
+
json={"code": code, "language": "python"},
|
| 25 |
+
timeout=5
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
print(f"\nStatus: {response.status_code}")
|
| 29 |
+
print(f"\nResponse:")
|
| 30 |
+
data = response.json()
|
| 31 |
+
print(json.dumps(data, indent=2))
|
| 32 |
+
|
| 33 |
+
print("\n" + "β" * 60)
|
| 34 |
+
print(f"Bugs found: {data['bugs_found']}")
|
| 35 |
+
if data['bugs']:
|
| 36 |
+
print(f"First bug confidence: {data['bugs'][0]['confidence']}")
|
| 37 |
+
print(f"First bug comment: {data['bugs'][0]['comment']}")
|