-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-api.js
More file actions
219 lines (180 loc) Β· 6.99 KB
/
test-api.js
File metadata and controls
219 lines (180 loc) Β· 6.99 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
/**
* API Endpoint Tester
*
* Run this in the browser console while logged in to test all endpoints.
* It will use your current session token automatically.
*/
const API_BASE = 'http://localhost:8080/api';
// Get auth token from supabase session
async function getAuthToken() {
try {
// Try to get from local storage first (faster)
const authData = localStorage.getItem('sb-nvkdepbkmptnqwxupfrr-auth-token');
if (authData) {
const parsed = JSON.parse(authData);
if (parsed?.access_token) {
return parsed.access_token;
}
}
// Fallback: try window.__supabase or any global supabase instance
if (typeof window !== 'undefined') {
// Check for supabase in window
const supabase = window.__supabase ||
window.supabase ||
(window.App && window.App.supabase);
if (supabase?.auth?.getSession) {
const { data: { session } } = await supabase.auth.getSession();
return session?.access_token || null;
}
}
return null;
} catch (error) {
console.warn('Could not get auth token:', error.message);
return null;
}
}
// Helper to make API calls
async function testEndpoint(method, path, description, body = null) {
const token = await getAuthToken();
console.log(`\nπ§ͺ Testing: ${description}`);
console.log(` ${method} ${path}`);
const options = {
method,
headers: {
'Content-Type': 'application/json',
...(token ? { 'Authorization': `Bearer ${token}` } : {})
}
};
if (body && (method === 'POST' || method === 'PUT')) {
options.body = JSON.stringify(body);
}
try {
const response = await fetch(`${API_BASE}${path}`, options);
const data = await response.json();
if (response.ok) {
console.log(` β
Success (${response.status})`);
console.log(' Response:', data);
} else {
console.error(` β Failed (${response.status})`);
console.error(' Error:', data);
}
return { success: response.ok, status: response.status, data };
} catch (error) {
console.error(` β Error:`, error);
return { success: false, error: error.message };
}
}
async function runAllTests() {
console.clear();
console.log('π Starting API Endpoint Tests...\n');
console.log('=' .repeat(60));
const results = {
passed: 0,
failed: 0,
total: 0
};
// Track results
const trackResult = (result) => {
results.total++;
if (result.success) {
results.passed++;
} else {
results.failed++;
}
};
// 1. Public Endpoints
console.log('\nπ Section 1: Public Endpoints');
console.log('=' .repeat(60));
trackResult(await testEndpoint('GET', '/health', 'Health Check'));
trackResult(await testEndpoint('GET', '/plans', 'Get Plans'));
trackResult(await testEndpoint('GET', '/jobs/meta/filters', 'Get Job Filters'));
// 2. Jobs
console.log('\nπ Section 2: Jobs Endpoints');
console.log('=' .repeat(60));
trackResult(await testEndpoint('GET', '/jobs?limit=5', 'List Jobs (5 items)'));
trackResult(await testEndpoint('GET', '/jobs?search=software&limit=3', 'Search Jobs'));
// Get a job ID from the list for detail test
const jobsResult = await testEndpoint('GET', '/jobs?limit=1', 'Get First Job');
if (jobsResult.success && jobsResult.data?.items?.[0]?.id) {
const jobId = jobsResult.data.items[0].id;
trackResult(await testEndpoint('GET', `/jobs/${jobId}`, 'Get Job Detail'));
}
// 3. Profile (requires auth)
console.log('\nπ Section 3: Profile Endpoints (Auth Required)');
console.log('=' .repeat(60));
const token = await getAuthToken();
if (!token) {
console.warn('β οΈ No auth token found. Please login first.');
console.log(' Skipping authenticated endpoints...');
} else {
trackResult(await testEndpoint('GET', '/profile', 'Get Profile'));
trackResult(await testEndpoint('PUT', '/profile', 'Update Profile', {
name: 'Test User',
skills: ['JavaScript', 'TypeScript']
}));
}
// 4. Knowledge Sources (requires auth)
if (token) {
console.log('\nπ Section 4: Knowledge Sources');
console.log('=' .repeat(60));
trackResult(await testEndpoint('GET', '/knowledge-sources', 'List Knowledge Sources'));
// Try to add a text source
const addSourceResult = await testEndpoint('POST', '/knowledge-sources/text', 'Add Text Source', {
content: 'I am a software engineer with 5 years of experience in React, Node.js, and TypeScript. I have worked on large-scale web applications.'
});
trackResult(addSourceResult);
// If successful, try to delete it
if (addSourceResult.success && addSourceResult.data?.source?.id) {
trackResult(await testEndpoint('DELETE', `/knowledge-sources/${addSourceResult.data.source.id}`, 'Delete Test Source'));
}
}
// 5. Preferences (requires auth and knowledge sources)
if (token) {
console.log('\nπ Section 5: Preferences');
console.log('=' .repeat(60));
trackResult(await testEndpoint('GET', '/preferences', 'Get Preferences'));
// Don't auto-run predict to avoid API costs
console.log('\nβοΈ Skipping POST /preferences/predict (LLM API call)');
console.log(' Run manually: testEndpoint("POST", "/preferences/predict", "Predict Preferences")');
}
// 6. Applications (requires auth)
if (token) {
console.log('\nπ Section 6: Applications');
console.log('=' .repeat(60));
trackResult(await testEndpoint('GET', '/applications', 'List Applications'));
}
// 7. Resume Analysis (requires auth)
if (token) {
console.log('\nπ Section 7: Resume Analysis');
console.log('=' .repeat(60));
trackResult(await testEndpoint('GET', '/resume/analyses', 'List Resume Analyses'));
}
// 8. Materials Generation (requires auth) - Skip to avoid API costs
console.log('\nπ Section 8: Material Generation');
console.log('=' .repeat(60));
console.log('βοΈ Skipped (requires job IDs and makes LLM API calls)');
// 9. HR Search (requires auth) - Skip to avoid API costs
console.log('\nπ Section 9: HR Search');
console.log('=' .repeat(60));
console.log('βοΈ Skipped (makes external API calls)');
// Summary
console.log('\n' + '=' .repeat(60));
console.log('π Test Summary');
console.log('=' .repeat(60));
console.log(`Total Tests: ${results.total}`);
console.log(`β
Passed: ${results.passed}`);
console.log(`β Failed: ${results.failed}`);
console.log(`Success Rate: ${((results.passed / results.total) * 100).toFixed(1)}%`);
console.log('=' .repeat(60));
return results;
}
// Auto-run if in browser
if (typeof window !== 'undefined') {
console.log('π‘ API Tester loaded!');
console.log(' Run: runAllTests()');
console.log(' Or test individual endpoints: testEndpoint("GET", "/health", "Health Check")');
}
// Export for module use
if (typeof module !== 'undefined' && module.exports) {
module.exports = { testEndpoint, runAllTests, getAuthToken };
}