52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* 调用记录 API 可用性验证(部署后执行)
|
||
* 用法: node scripts/verify-call-records-api.js [BASE_URL]
|
||
* 默认: http://localhost:3000
|
||
* 成功: exit 0,失败: exit 1
|
||
*/
|
||
|
||
const baseUrl = process.argv[2] || 'http://localhost:3000';
|
||
const url = `${baseUrl.replace(/\/$/, '')}/api/admin/generation-tasks?page=1&pageSize=5`;
|
||
|
||
function main() {
|
||
const http = require(baseUrl.startsWith('https') ? 'https' : 'http');
|
||
const u = new URL(url);
|
||
const options = { hostname: u.hostname, port: u.port || (u.protocol === 'https:' ? 443 : 80), path: u.pathname + u.search, method: 'GET' };
|
||
|
||
const req = http.request(options, (res) => {
|
||
let body = '';
|
||
res.on('data', (ch) => { body += ch; });
|
||
res.on('end', () => {
|
||
if (res.statusCode !== 200) {
|
||
console.error('FAIL: status', res.statusCode, body.slice(0, 200));
|
||
process.exit(1);
|
||
}
|
||
try {
|
||
const data = JSON.parse(body);
|
||
if (!data.success || !Array.isArray(data.data?.list)) {
|
||
console.error('FAIL: response shape', JSON.stringify(data).slice(0, 300));
|
||
process.exit(1);
|
||
}
|
||
if (!data.data.pagination || typeof data.data.pagination.total !== 'number') {
|
||
console.error('FAIL: missing pagination');
|
||
process.exit(1);
|
||
}
|
||
console.log('OK: 调用记录 API 可用', data.data.list.length, '条本页, 共', data.data.pagination.total, '条');
|
||
process.exit(0);
|
||
} catch (e) {
|
||
console.error('FAIL: parse', e.message);
|
||
process.exit(1);
|
||
}
|
||
});
|
||
});
|
||
req.on('error', (e) => {
|
||
console.error('FAIL: request', e.message);
|
||
process.exit(1);
|
||
});
|
||
req.setTimeout(10000, () => { req.destroy(); process.exit(1); });
|
||
req.end();
|
||
}
|
||
|
||
main();
|