Files

33 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 後端冒煙測試:載入 index.js(自動監聽 3000-> 測 REST 路由 -> 優雅關閉。
// 使用方式: node scripts/smoke_test_server.js (需在專案根目錄執行)
const { app, io } = require("../server/index.js");
async function run() {
let exitCode = 0;
try {
const listRes = await fetch("http://localhost:3000/api/rooms");
const listBody = await listRes.json();
console.log("[OK] GET /api/rooms ->", listRes.status, JSON.stringify(listBody));
const createRes = await fetch("http://localhost:3000/api/rooms", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ hostId: "test-host-1" }),
});
const room = await createRes.json();
console.log("[OK] POST /api/rooms ->", createRes.status,
"roomId=", room.id, "players=", room.players.length, "status=", room.status);
} catch (e) {
console.error("[FAIL]", e.message);
exitCode = 1;
}
io.close(() => {
console.log(exitCode === 0 ? "[OK] 後端可正常啟動與回應 REST API" : "[FAIL] 後端測試失敗");
process.exit(exitCode);
});
// 保險:2 秒後強制退出,避免卡住
setTimeout(() => process.exit(exitCode), 2500);
}
setTimeout(run, 1500);