import { startMockServer } from "./mock-server.js"; import http from "http"; let server: http.Server | null = null; export default async function setup() { const defaultUrl = process.env.API_URL || "http://localhost:3001"; let useLocalMock = false; try { // Send a health check request with a timeout const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 1000); const res = await fetch(`${defaultUrl}/health`, { signal: controller.signal }); clearTimeout(timeoutId); if (res.ok) { const data = await res.json() as any; // If port 3001 is running the Nova bot, or doesn't match coaxys panel body if (data && data.agent === "Nova") { useLocalMock = true; } } else { useLocalMock = true; } } catch { useLocalMock = true; } if (useLocalMock) { const mockPort = 3009; server = startMockServer(mockPort); process.env.API_URL = `http://localhost:${mockPort}`; console.log(`[E2E Setup] CoaxysPanel mock backend server started on port ${mockPort}`); } else { process.env.API_URL = defaultUrl; console.log(`[E2E Setup] Testing against existing API_URL: ${process.env.API_URL}`); } // Teardown hook executed by Vitest after all tests complete return async () => { if (server) { await new Promise((resolve, reject) => { server!.close((err) => { if (err) reject(err); else resolve(); }); }); console.log("[E2E Teardown] CoaxysPanel mock backend server stopped"); } }; }