feat: Auto-detect local server in test.js — falls back to live API

This commit is contained in:
Shinei Nouzen
2026-07-29 07:22:14 +05:30
parent 159c9ca8aa
commit 78b149983d
+20 -2
View File
@@ -13,7 +13,24 @@
* ======= • ======= • ======= • ======= • =======• =======
*/
const BASE = process.env.API_URL || "https://anikototvapi.vercel.app/api";
const LIVE_API = "https://anikototvapi.vercel.app/api";
const LOCAL_API = "http://localhost:4444/api";
let BASE = process.env.API_URL || LIVE_API;
async function detectBase() {
if (process.env.API_URL) return;
try {
const res = await fetch(`${LOCAL_API}/health`, { signal: AbortSignal.timeout(2000) });
if (res.ok) {
BASE = LOCAL_API;
console.log("📡 Detected local server — using localhost:4444\n");
return;
}
} catch (_) {}
BASE = LIVE_API;
console.log("📡 No local server found — using live API\n");
}
const tests = [
// Core endpoints
@@ -147,7 +164,8 @@ async function fetchJson(url) {
}
async function runAll() {
console.log(`\n🧪 Running ${tests.length} tests...\n`);
await detectBase();
console.log(`🧪 Running ${tests.length} tests...\n`);
console.log(`📡 API: ${BASE}\n`);
// ---- FEATURE: Resolve dynamic test dependencies ----