-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_api_endpoint.py
More file actions
136 lines (106 loc) Β· 4.95 KB
/
test_api_endpoint.py
File metadata and controls
136 lines (106 loc) Β· 4.95 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
#!/usr/bin/env python3
"""
Test the actual API endpoint logic without running the server.
"""
import json
import sys
from pathlib import Path
from datetime import datetime
def simulate_list_jobs_api(limit: int = 50):
"""Simulate the list_jobs API endpoint logic."""
print(f"π Simulating list_jobs API call with limit={limit}")
try:
jobs_dir = Path("gap_analysis_jobs")
# Get all job files from disk
job_files = list(jobs_dir.glob("job_*.json"))
print(f"π Found {len(job_files)} job files on disk")
# Read job data and sort by creation time
jobs_data = []
for job_file in job_files:
try:
with open(job_file, 'r') as f:
job_data = json.load(f)
# CRITICAL FIX: Handle backwards compatibility for missing analysis_mode
if "request" in job_data and "analysis_mode" not in job_data["request"]:
job_data["request"]["analysis_mode"] = "deep" # Default for older jobs
print(f"π Added default analysis_mode to job {job_data.get('job_id', 'unknown')}")
# Add created_at as datetime for sorting
job_data['created_at_dt'] = datetime.fromisoformat(job_data['created_at'])
jobs_data.append(job_data)
except Exception as e:
print(f"β Failed to read job file {job_file}: {str(e)}")
print(f" Job file content: {job_file.read_text()[:500]}...")
continue
# Sort by creation time, most recent first
jobs_data.sort(key=lambda x: x['created_at_dt'], reverse=True)
# Convert to status info format and limit results
result = []
for job_data in jobs_data[:limit]:
job_id = job_data['job_id']
# Simulate get_job_status logic
status_info = {
"job_id": job_data["job_id"],
"status": job_data.get("status", "pending"),
"created_at": job_data["created_at"],
"progress_message": job_data.get("progress_message", "Job queued"),
"url": job_data["request"]["url"]
}
if job_data.get("started_at"):
status_info["started_at"] = job_data["started_at"]
if job_data.get("completed_at"):
status_info["completed_at"] = job_data["completed_at"]
# Calculate processing time
if job_data.get("started_at"):
started = datetime.fromisoformat(job_data["started_at"])
completed = datetime.fromisoformat(job_data["completed_at"])
status_info["processing_time_seconds"] = (completed - started).total_seconds()
if job_data.get("error_message"):
status_info["error"] = job_data["error_message"]
if job_data.get("result_file"):
status_info["result_file"] = job_data["result_file"]
result.append(status_info)
print(f"π Listed {len(result)} jobs from disk (out of {len(job_files)} total)")
return result
except Exception as e:
print(f"π₯ Failed to list jobs from disk: {str(e)}")
return []
def main():
print("π§ͺ Testing Gap Analysis Jobs API Endpoint Logic")
print("=" * 60)
# Test the list_jobs logic
jobs = simulate_list_jobs_api(20)
print(f"\nπ API Response Summary:")
print(f" Jobs returned: {len(jobs)}")
if len(jobs) == 0:
print(f" β EMPTY RESPONSE - This is the problem!")
return False
print(f" β
SUCCESSFUL RESPONSE")
print(f"\nπ Jobs Details:")
for i, job in enumerate(jobs[:3]):
print(f" {i+1}. Job ID: {job['job_id']}")
print(f" Status: {job['status']}")
print(f" URL: {job['url'][:60]}...")
print(f" Created: {job['created_at']}")
if len(jobs) > 3:
print(f" ... and {len(jobs) - 3} more jobs")
# Test JSON serialization
try:
json_output = json.dumps(jobs, indent=2, default=str)
print(f"\nβ
JSON serialization successful ({len(json_output)} chars)")
# Write to file for inspection
with open("test_api_output.json", "w") as f:
f.write(json_output)
print(f"π Output written to test_api_output.json")
except Exception as e:
print(f"β JSON serialization failed: {str(e)}")
return False
return True
if __name__ == "__main__":
success = main()
if success:
print(f"\nπ API ENDPOINT TEST PASSED!")
print(f"π The jobs endpoint should be returning data correctly")
sys.exit(0)
else:
print(f"\nπ₯ API ENDPOINT TEST FAILED!")
sys.exit(1)