-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-api.cjs
More file actions
143 lines (120 loc) · 4.88 KB
/
debug-api.cjs
File metadata and controls
143 lines (120 loc) · 4.88 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
const http = require('http');
console.log('='.repeat(60));
console.log('BACKEND CONNECTION DEBUG TEST');
console.log('='.repeat(60));
const tests = [
{ name: 'Backend Root', path: '/', expectedStatus: [200, 404, 301, 302] },
{ name: 'Health Check', path: '/api/health', expectedStatus: [200] },
{ name: 'Workers List (no auth)', path: '/api/workers/list', expectedStatus: [401] },
{ name: 'Jobs Create (no auth)', path: '/api/jobs/create', expectedStatus: [401, 405] },
];
async function testEndpoint(name, path, expectedStatus) {
return new Promise((resolve) => {
console.log(`\n[${name}] GET http://localhost:3000${path}`);
const req = http.get(`http://localhost:3000${path}`, (res) => {
const status = res.statusCode;
const expected = expectedStatus.includes(status);
const icon = expected ? '✅' : '⚠️';
console.log(`${icon} Status: ${status} ${res.statusMessage}${expected ? '' : ` (expected ${expectedStatus.join(' or ')})`}`);
console.log(` Content-Type: ${res.headers['content-type'] || 'none'}`);
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
if (data && data.length < 500) {
try {
const json = JSON.parse(data);
console.log(` Response:`, JSON.stringify(json, null, 2));
} catch {
console.log(` Response: ${data.substring(0, 200)}`);
}
} else if (data) {
console.log(` Response: ${data.length} bytes`);
}
resolve({ ok: true, status, expected });
});
});
req.on('error', (err) => {
console.log(`❌ Error: ${err.message}`);
console.log(` Code: ${err.code}`);
if (err.code === 'ECONNREFUSED') {
console.log(` → Backend not running on port 3000`);
}
resolve({ ok: false, error: err.message });
});
req.setTimeout(5000, () => {
console.log(`❌ Timeout after 5 seconds`);
req.destroy();
resolve({ ok: false, error: 'Timeout' });
});
});
}
async function testWorkerRegister() {
return new Promise((resolve) => {
console.log(`\n[Worker Registration] POST http://localhost:3000/api/workers/register`);
const postData = JSON.stringify({
workerId: `debug-test-${Date.now()}`,
hostname: 'debug-host',
os: 'Windows_NT',
cpuCount: 4,
ramTotalMb: 8192
});
const options = {
hostname: 'localhost',
port: 3000,
path: '/api/workers/register',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'x-worker-token': 'test-token-will-fail'
}
};
const req = http.request(options, (res) => {
console.log(` Status: ${res.statusCode} ${res.statusMessage}`);
console.log(` (Expected 401 without valid token)`);
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
if (data) {
try {
console.log(` Response:`, JSON.parse(data));
} catch {
console.log(` Response:`, data);
}
}
resolve({ ok: true, status: res.statusCode });
});
});
req.on('error', (err) => {
console.log(`❌ Error: ${err.message}`);
resolve({ ok: false, error: err.message });
});
req.write(postData);
req.end();
});
}
(async () => {
const results = {};
for (const test of tests) {
results[test.name] = await testEndpoint(test.name, test.path, test.expectedStatus);
}
results['Worker Register'] = await testWorkerRegister();
console.log('\n' + '='.repeat(60));
console.log('SUMMARY');
console.log('='.repeat(60));
Object.entries(results).forEach(([name, result]) => {
const icon = result.ok ? (result.expected !== false ? '✅' : '⚠️') : '❌';
const status = result.status ? `[${result.status}]` : '';
const error = result.error ? ` - ${result.error}` : '';
console.log(`${icon} ${name.padEnd(30)} ${status}${error}`);
});
console.log('='.repeat(60));
const backendUp = Object.values(results).some(r => r.ok);
if (backendUp) {
console.log('✅ Backend is running and responding');
} else {
console.log('❌ Backend appears to be down');
console.log(' Run: npm run dev');
}
console.log('='.repeat(60));
})();