Files
ExecuBoard/scripts/test-api.js
2025-08-01 00:55:05 +08:00

101 lines
2.7 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// API 測試腳本
const http = require('http')
async function testAPI() {
console.log('🧪 開始測試 API 端點...\n')
// 測試資料庫連接
console.log('1⃣ 測試資料庫連接...')
try {
const dbResponse = await makeRequest('/api/database/test')
console.log(' ✅ 資料庫連接正常')
console.log(` 📊 回應: ${JSON.stringify(dbResponse, null, 2)}`)
} catch (error) {
console.log(' ❌ 資料庫連接失敗:', error.message)
}
// 測試 KPI API
console.log('\n2⃣ 測試 KPI API...')
try {
const kpiResponse = await makeRequest('/api/kpi?userId=user_001')
console.log(' ✅ KPI API 正常')
console.log(` 📊 找到 ${kpiResponse.length} 個 KPI`)
kpiResponse.forEach(kpi => {
console.log(` - ${kpi.title}: ${kpi.current_value}/${kpi.target_value} ${kpi.unit}`)
})
} catch (error) {
console.log(' ❌ KPI API 失敗:', error.message)
}
// 測試用戶 API
console.log('\n3⃣ 測試用戶 API...')
try {
const usersResponse = await makeRequest('/api/users')
console.log(' ✅ 用戶 API 正常')
console.log(` 📊 找到 ${usersResponse.length} 個用戶`)
usersResponse.slice(0, 3).forEach(user => {
console.log(` - ${user.name} (${user.role})`)
})
} catch (error) {
console.log(' ❌ 用戶 API 失敗:', error.message)
}
// 測試評審 API
console.log('\n4⃣ 測試評審 API...')
try {
const reviewsResponse = await makeRequest('/api/reviews')
console.log(' ✅ 評審 API 正常')
console.log(` 📊 找到 ${reviewsResponse.length} 個評審`)
reviewsResponse.forEach(review => {
console.log(` - ${review.review_type} 評審 (${review.status})`)
})
} catch (error) {
console.log(' ❌ 評審 API 失敗:', error.message)
}
console.log('\n🎉 API 測試完成!')
}
function makeRequest(path) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'localhost',
port: 3006,
path: path,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}
const req = http.request(options, (res) => {
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
try {
const jsonData = JSON.parse(data)
resolve(jsonData)
} catch (error) {
reject(new Error(`解析回應失敗: ${error.message}`))
}
})
})
req.on('error', (error) => {
reject(new Error(`請求失敗: ${error.message}`))
})
req.setTimeout(5000, () => {
req.destroy()
reject(new Error('請求超時'))
})
req.end()
})
}
testAPI().catch(console.error)